总结代码,之前写的 以后方便看而已
登录
appLoginWx() {
var that = this
// #ifdef MP-WEIXIN
uni.getProvider({
service: 'oauth',
success: function(res) {
if (~res.provider.indexOf('weixin')) {
uni.login({
provider: 'weixin',
success: (res2) => {
console.log(res2)
that.code = res2.code
},
fail: () => {
uni.showToast({
title: "微信登录授权失败",
icon: "none"
});
}
})
} else {
uni.showToast({
title: '请先安装微信或升级版本',
icon: "none"
});
}
}
});
//#endif
},
login(data) {
console.log(data)
var that = this
if (data.detail.encryptedData && data.detail.iv) {
this.$http
.post(
that.url.wechatuserauth, {
authcode: that.code
})
.then(res => {
if (res.data.openid) {
this.$http
.post(
that.url.wechatuserinfo, {
openid: res.data.openid,
referrer: 0,
encrypted_data: data.detail.encryptedData,
iv: data.detail.iv
})
.then(res2 => {
that.userInfo = res2.data
that.setStorage(res2)
that.$emit('setUserInfo', res2.data)
uni.showToast({
title: res2.msg,
icon: "none"
});
})
.catch(e => {
uni.showToast({
title: '授权登录失败',
icon: "none"
});
});
} else {
that.userInfo = res.data
that.setStorage(res)
that.$emit('setUserInfo', res.data)
uni.showToast({
title: res.msg,
icon: "none"
});
}
})
.catch(e => {
});
}
},
setStorage(res) {
uni.setStorageSync('userInfo', JSON.stringify(res.data));
uni.setStorageSync('token', res.data.token);
},
微信支付
this.$http
.post(
url, {
ids: ids,
payment_id: '1' //固定为微信付款
}
).then(res => {
//测试 增加带付款
let result = res.data.data
uni.showLoading({}) //拉起支付加载提示
uni.hideLoading()
uni.requestPayment({ //下面参数为必传
appId: result.appId, //小程序Appid
timeStamp: result.timeStamp, //创建订单时间戳
nonceStr: result.nonceStr,
package: result.package, // 订单包
signType: result.signType, // 加密方式统一'MD5'
paySign: result.paySign, // 后台支付签名返回
success(res) {
uni.showToast({
title: '支付成功',
icon: "none"
});
setTimeout(() => { //支付成功跳转
uni.navigateTo({
})
}, 500)
},
fail(err) {
uni.showToast({
title: '支付失败',
icon: "none"
});
setTimeout(() => { //支付失败跳转
uni.navigateTo({
})
// callback()
})
}
})
}).catch(res => {
})
图片上传,参数根据后台需求传
imgList.forEach(el=>{
console.log(el)
uni.uploadFile({
url:url, // 后端api接口
filePath: el, // uni.chooseImage函数调用后获取的本地文件路劲
name:'', //后端通过'file'获取上传的文件对象
formData: {
action: 'uploadimage',
path_type: that.oderObj.editor_path_type,
},
// header:{"Content-Type": "multipart/form-data"},
success:(res) => {
var resData = JSON.parse(res.data)
console.log(resData)
if(resData.code == 0){
this.upList.push(resData.data.url)
}
if( this.upList.length == this.imgList.length){ //图片全部上传成功
}
}
})
})