upload.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // pages/upload/upload.js
  2. const app = getApp()
  3. var host = app.globalData.host;
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. open:true,
  10. stock_date:'',
  11. stock:[{name:'',fund:''}],
  12. stockList:[],
  13. today_stock_img:[],
  14. value:[-1],
  15. is_markt:0,
  16. today_fund:'',
  17. change:0,
  18. freshen:true,
  19. disable:false
  20. },
  21. /**
  22. * 生命周期函数--监听页面加载
  23. */
  24. onShow(){
  25. setTimeout(()=>{
  26. if (!this.data.userInfo.token || !this.data.freshen) {
  27. return;
  28. }
  29. wx.request({
  30. url: host + '/api/wx/player/currecord',
  31. header: {
  32. 'Authorization': this.data.userInfo.token
  33. },
  34. success: res => {
  35. console.log(res)
  36. if (res.data.data) {
  37. let stock = res.data.data.today_stock
  38. this.setData({
  39. stock: stock.length > 0 ? stock : [{ name: '', fund: '' }],
  40. today_stock_img: res.data.data.today_stock_img,
  41. today_fund: res.data.data.today_fund,
  42. is_markt: res.data.data.is_markt,
  43. change:1
  44. })
  45. }
  46. }
  47. })
  48. this.getuser()
  49. },500)
  50. },
  51. onLoad: function (options) {
  52. var y=new Date().getFullYear()
  53. var m=Number(new Date().getMonth() + 1)
  54. var d = new Date().getDate()
  55. m=m>9?m:'0'+m;
  56. d=d>9?d:'0'+d
  57. this.setData({
  58. stock_date:y+'-'+m+'-'+d
  59. })
  60. wx.getStorage({
  61. key: 'userInfo',
  62. success: res => {
  63. if (res.data) {
  64. this.setData({
  65. userInfo: res.data
  66. })
  67. this.getData();
  68. } else {
  69. wx.switchTab({
  70. url: '../user/user',
  71. })
  72. }
  73. },
  74. fail:error=>{
  75. wx.switchTab({
  76. url: '../user/user',
  77. })
  78. }
  79. })
  80. },
  81. getuser() {
  82. wx.request({
  83. url: host + '/api/wx/authinfo',
  84. header: {
  85. 'Authorization': this.data.userInfo.token
  86. },
  87. success: res => {
  88. if (res.data.data.need_fill) {
  89. wx.switchTab({
  90. url: '../user/user',
  91. })
  92. }
  93. }
  94. })
  95. },
  96. getData() {
  97. wx.request({
  98. url: host + '/api/wx/stock/search',
  99. header: {
  100. 'Authorization': this.data.userInfo.token
  101. },
  102. success: res => {
  103. this.setData({
  104. stockList:res.data.data
  105. })
  106. },
  107. fail: error => {
  108. //失败重新登陆
  109. wx.switchTab({
  110. url: '../user/user',
  111. })
  112. }
  113. })
  114. },
  115. /**添加持股 */
  116. add(){
  117. var stock = this.data.stock;
  118. var value=this.data.value;
  119. stock.push({ name: '', fund: '' })
  120. value.push(-1)
  121. this.setData({
  122. stock: stock,
  123. value: value
  124. })
  125. },
  126. del(e){
  127. var i = e.target.dataset.id;
  128. var stock = this.data.stock;
  129. var value = this.data.value;
  130. stock.splice(i,1)
  131. value.splice(i,1)
  132. this.setData({
  133. stock: stock,
  134. value: value
  135. })
  136. },
  137. bindPickerChange(e){
  138. var i=e.target.dataset.id;
  139. var v = e.detail.value
  140. var stock=this.data.stock;
  141. var list=this.data.stockList;
  142. var value=this.data.value;
  143. value[i] =v;
  144. stock[i]={
  145. name: list[v].name,
  146. code:list[v].code,
  147. label: list[v].label
  148. }
  149. this.setData({
  150. stock: stock,
  151. value: value
  152. })
  153. },
  154. nameChange(e){
  155. var i = e.target.dataset.id;
  156. var v = e.detail.value
  157. var stock = this.data.stock;
  158. stock[i].name = v;
  159. this.setData({
  160. stock: stock
  161. })
  162. },
  163. fundChange(e){
  164. var i = e.target.dataset.id;
  165. var v = e.detail.value
  166. var stock = this.data.stock;
  167. stock[i].fund = v;
  168. this.setData({
  169. stock: stock
  170. })
  171. },
  172. fundChange1(e) {
  173. var v = e.detail.value
  174. this.setData({
  175. today_fund: v
  176. })
  177. },
  178. switch1Change(e){
  179. this.setData({
  180. is_markt: Number(e.detail.value)
  181. })
  182. },
  183. upload(){
  184. this.setData({
  185. freshen:false
  186. })
  187. var today_stock_img = this.data.today_stock_img;
  188. wx.chooseImage({
  189. success: res=> {
  190. console.log(res)
  191. const tempFilePaths = res.tempFiles
  192. for (let i = 0; i < tempFilePaths.length;i++){
  193. // if(tempFilePaths[i].size > 1024){
  194. // wx.showToast({
  195. // icon: 'none',
  196. // title: '图片大小不能超过1M'
  197. // })
  198. // }else{
  199. wx.uploadFile({
  200. url: host + '/api/wx/uploadfile',
  201. filePath: tempFilePaths[i].path,
  202. name: 'file',
  203. header: {
  204. 'Authorization': this.data.userInfo.token
  205. },
  206. success: res => {
  207. const data = JSON.parse(res.data)
  208. today_stock_img.push(data.data.url)
  209. this.setData({
  210. today_stock_img: today_stock_img
  211. })
  212. }
  213. })
  214. // }
  215. }
  216. }
  217. })
  218. },
  219. del1(e) {
  220. var i = e.target.dataset.id;
  221. var today_stock_img = this.data.today_stock_img;
  222. today_stock_img.splice(i, 1)
  223. this.setData({
  224. today_stock_img: today_stock_img
  225. })
  226. },
  227. prview(e){
  228. var src = e.target.dataset.src;
  229. wx.previewImage({
  230. current: src, // 当前显示图片的http链接
  231. urls: [src] // 需要预览的图片http链接列表
  232. })
  233. },
  234. post(){
  235. var parm={
  236. stock_date: this.data.stock_date,
  237. today_stock:this.data.stock,
  238. is_markt: this.data.is_markt,
  239. today_stock_img: this.data.today_stock_img,
  240. today_fund: Number(this.data.today_fund)
  241. }
  242. if (!parm.today_fund){
  243. wx.showToast({
  244. icon: 'none',
  245. title: '请输入今日净资产'
  246. })
  247. return
  248. }
  249. if (!parm.is_markt){
  250. // for (let i = 0; i < parm.today_stock.length;i++){
  251. // if (!parm.today_stock[i].name || !parm.today_stock[i].fund) {
  252. // wx.showToast({
  253. // icon: 'none',
  254. // title: '请选择股票并输入资金额'
  255. // })
  256. // return;
  257. // break;
  258. // }
  259. // }
  260. }else{
  261. parm.today_stock = []
  262. }
  263. // if (parm.today_stock_img.length<=0) {
  264. // wx.showToast({
  265. // icon: 'none',
  266. // title: '请上传截图'
  267. // })
  268. // return
  269. // }
  270. wx.showLoading({
  271. title: '正在提交',
  272. })
  273. this.setData({
  274. disable:true
  275. })
  276. wx.request({
  277. url: host + '/api/wx/player/record',
  278. data:parm,
  279. header: {
  280. 'Authorization': this.data.userInfo.token
  281. },
  282. method:'POST',
  283. success: res => {
  284. wx.hideLoading()
  285. if (res.data.code != 0) {
  286. wx.showToast({
  287. icon: 'none',
  288. title: res.data.message,
  289. })
  290. return;
  291. }
  292. wx.showToast({
  293. title: '提交成功',
  294. })
  295. this.setData({
  296. freshen: true,
  297. })
  298. setTimeout(()=>{
  299. this.setData({
  300. stock: [{ label: '', name: '', code: '', fund: '' }],
  301. stockList: [],
  302. today_stock_img: [],
  303. value: [-1],
  304. is_markt: 0,
  305. today_fund: '',
  306. disable:false
  307. })
  308. wx.switchTab({
  309. url: '../index/index',
  310. })
  311. },200)
  312. },
  313. fail: error => {
  314. wx.hideLoading()
  315. }
  316. })
  317. },
  318. /**
  319. * 生命周期函数--监听页面初次渲染完成
  320. */
  321. onReady: function () {
  322. },
  323. /**
  324. * 生命周期函数--监听页面隐藏
  325. */
  326. onHide: function () {
  327. },
  328. /**
  329. * 生命周期函数--监听页面卸载
  330. */
  331. onUnload: function () {
  332. },
  333. /**
  334. * 页面相关事件处理函数--监听用户下拉动作
  335. */
  336. onPullDownRefresh: function () {
  337. },
  338. /**
  339. * 页面上拉触底事件的处理函数
  340. */
  341. onReachBottom: function () {
  342. },
  343. /**
  344. * 用户点击右上角分享
  345. */
  346. onShareAppMessage: function () {
  347. }
  348. })