// pages/upload/upload.js const app = getApp() var host = app.globalData.host; Page({ /** * 页面的初始数据 */ data: { open:true, stock_date:'', stock:[{name:'',fund:''}], stockList:[], today_stock_img:[], value:[-1], is_markt:0, today_fund:'', change:0, freshen:true, disable:false }, /** * 生命周期函数--监听页面加载 */ onShow(){ setTimeout(()=>{ if (!this.data.userInfo.token || !this.data.freshen) { return; } wx.request({ url: host + '/api/wx/player/currecord', header: { 'Authorization': this.data.userInfo.token }, success: res => { console.log(res) if (res.data.data) { let stock = res.data.data.today_stock this.setData({ stock: stock.length > 0 ? stock : [{ name: '', fund: '' }], today_stock_img: res.data.data.today_stock_img, today_fund: res.data.data.today_fund, is_markt: res.data.data.is_markt, change:1 }) } } }) this.getuser() },500) }, onLoad: function (options) { var y=new Date().getFullYear() var m=Number(new Date().getMonth() + 1) var d = new Date().getDate() m=m>9?m:'0'+m; d=d>9?d:'0'+d this.setData({ stock_date:y+'-'+m+'-'+d }) wx.getStorage({ key: 'userInfo', success: res => { if (res.data) { this.setData({ userInfo: res.data }) this.getData(); } else { wx.switchTab({ url: '../user/user', }) } }, fail:error=>{ wx.switchTab({ url: '../user/user', }) } }) }, getuser() { wx.request({ url: host + '/api/wx/authinfo', header: { 'Authorization': this.data.userInfo.token }, success: res => { if (res.data.data.need_fill) { wx.switchTab({ url: '../user/user', }) } } }) }, getData() { wx.request({ url: host + '/api/wx/stock/search', header: { 'Authorization': this.data.userInfo.token }, success: res => { this.setData({ stockList:res.data.data }) }, fail: error => { //失败重新登陆 wx.switchTab({ url: '../user/user', }) } }) }, /**添加持股 */ add(){ var stock = this.data.stock; var value=this.data.value; stock.push({ name: '', fund: '' }) value.push(-1) this.setData({ stock: stock, value: value }) }, del(e){ var i = e.target.dataset.id; var stock = this.data.stock; var value = this.data.value; stock.splice(i,1) value.splice(i,1) this.setData({ stock: stock, value: value }) }, bindPickerChange(e){ var i=e.target.dataset.id; var v = e.detail.value var stock=this.data.stock; var list=this.data.stockList; var value=this.data.value; value[i] =v; stock[i]={ name: list[v].name, code:list[v].code, label: list[v].label } this.setData({ stock: stock, value: value }) }, nameChange(e){ var i = e.target.dataset.id; var v = e.detail.value var stock = this.data.stock; stock[i].name = v; this.setData({ stock: stock }) }, fundChange(e){ var i = e.target.dataset.id; var v = e.detail.value var stock = this.data.stock; stock[i].fund = v; this.setData({ stock: stock }) }, fundChange1(e) { var v = e.detail.value this.setData({ today_fund: v }) }, switch1Change(e){ this.setData({ is_markt: Number(e.detail.value) }) }, upload(){ this.setData({ freshen:false }) var today_stock_img = this.data.today_stock_img; wx.chooseImage({ success: res=> { console.log(res) const tempFilePaths = res.tempFiles for (let i = 0; i < tempFilePaths.length;i++){ // if(tempFilePaths[i].size > 1024){ // wx.showToast({ // icon: 'none', // title: '图片大小不能超过1M' // }) // }else{ wx.uploadFile({ url: host + '/api/wx/uploadfile', filePath: tempFilePaths[i].path, name: 'file', header: { 'Authorization': this.data.userInfo.token }, success: res => { const data = JSON.parse(res.data) today_stock_img.push(data.data.url) this.setData({ today_stock_img: today_stock_img }) } }) // } } } }) }, del1(e) { var i = e.target.dataset.id; var today_stock_img = this.data.today_stock_img; today_stock_img.splice(i, 1) this.setData({ today_stock_img: today_stock_img }) }, prview(e){ var src = e.target.dataset.src; wx.previewImage({ current: src, // 当前显示图片的http链接 urls: [src] // 需要预览的图片http链接列表 }) }, post(){ var parm={ stock_date: this.data.stock_date, today_stock:this.data.stock, is_markt: this.data.is_markt, today_stock_img: this.data.today_stock_img, today_fund: Number(this.data.today_fund) } if (!parm.today_fund){ wx.showToast({ icon: 'none', title: '请输入今日净资产' }) return } if (!parm.is_markt){ // for (let i = 0; i < parm.today_stock.length;i++){ // if (!parm.today_stock[i].name || !parm.today_stock[i].fund) { // wx.showToast({ // icon: 'none', // title: '请选择股票并输入资金额' // }) // return; // break; // } // } }else{ parm.today_stock = [] } // if (parm.today_stock_img.length<=0) { // wx.showToast({ // icon: 'none', // title: '请上传截图' // }) // return // } wx.showLoading({ title: '正在提交', }) this.setData({ disable:true }) wx.request({ url: host + '/api/wx/player/record', data:parm, header: { 'Authorization': this.data.userInfo.token }, method:'POST', success: res => { wx.hideLoading() if (res.data.code != 0) { wx.showToast({ icon: 'none', title: res.data.message, }) return; } wx.showToast({ title: '提交成功', }) this.setData({ freshen: true, }) setTimeout(()=>{ this.setData({ stock: [{ label: '', name: '', code: '', fund: '' }], stockList: [], today_stock_img: [], value: [-1], is_markt: 0, today_fund: '', disable:false }) wx.switchTab({ url: '../index/index', }) },200) }, fail: error => { wx.hideLoading() } }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })