Fork me on GitHub

[微信小程序]

2018年初微信小游戏跳一跳的火爆带热了一批左小程序的热潮,我们在小程序上有一些项目实战经验,也在实现业务逻辑的过程中遇到过一些问题,这里对于遇到的问题进行一个汇总和记录式的填坑,需要沟通的小伙伴可以加微信 xjw005 沟通

图片单张上传



html设置:

<button bindtap="upimg" class='jia_img' >上传</button> 
<image src=""></image>
js设置:
data{ 
tempFilePaths:[]; 
}, 
 upimg: function () { 
 wx.chooseImage({ 
 success: function (res) { 
 var data = { 
 program_id: app.jtappid 
 } 
 var tempFilePaths = res.tempFilePaths //图片 
 wx.uploadFile({ 
 url: 'aaa.cn', //仅为示例,非真实的接口地址 
 filePath: tempFilePaths[0], 
 name: 'add_image', //文件对应的参数名字(key) 
 formData: data, //其它的表单信息 
 success: function (res) { 
 } 
 }) 
 } 
 }) 
 },

图片多张上传

html设置:
<view class="big-logos"> 
 <image bindtap="upimg" src='../../../image/s.png'></image> 
 <block wx:for="" wx:key="index"> 
 <view class='logoinfo'> 
 <text class='xxx' bindtap='xxx' data-index=''>x</text> 
 <image src=''></image> 
 </view> 
 </block> 
</view> 
<button class='top_20 btn' bindtap="upconfirm">确定</button>
js设置:
upconfirm: function () { 
 this.up(); 
}, 
up: function () { 
 var that = this; 
 data = { 
 openid: app.openid, 
 program_id: app.program_id, 
 only_num: only_num 
 } 
 wx.uploadFile({ 
 url: 'pg.php/Aishen/upload_photo', 
 filePath: that.data.img_arr[i], 
 name: 'image', //文件对应的参数名字(key) 
 formData: data, //其它的表单信息 
 success: function (res) { 
 }, complete: function (complete) { 
 console.log(complete) 
 i++ 
 if (i == that.data.img_arr.length) { 
 util.request('https://sz800800.cn/pg.php/Aishen/uploade_photo_r', 'post', { 'only_num': only_num }, '正在加载数据', function (res) { 
 console.log(res) 
 if (res.data.state == 1) { 
 wx.showModal({ 
 title: '提示', 
 content: '提交成功!', 
 success: function (res) { 
 that.onLoad() 
 wx.navigateBack({ 
 delta: 1 
 }) 
 } 
 }) 
 } else { 
 wx.showModal({ 
 title: '提示', 
 content: '提交失败,请重新提交!', 
 }) 
 } 
 }) 
 } else if (i < that.data.img_arr.length) {//若图片还没有传完,则继续调用函数 
 that.up() 
 } 
 } 
 }) 
}, 

// 上传 
upimg: function () { 
 var that = this; 
 if (this.data.img_arr.length < 12) { 
 wx.chooseImage({ 
 sizeType: ['original', 'compressed'], 
 success: function (res) { 
 that.setData({ 
 img_arr: that.data.img_arr.concat(res.tempFilePaths) 
 }) 
 num = that.data.img_arr.length 
 } 
 }) 
 } else { 
 wx.showToast({ 
 title: '最多上传12张图片', 
 icon: 'loading', 
 duration: 3000 
 }); 
 } 
},

ES6对两个数组的去重操作

key => !arr.includes(key)

改变数组的某一个键值

var iset = "markers[" + i + "].alpha";
 this.setData({
 [iset]: 1
 })
支持作者