自娱自乐
封装公共请求
导出
module.exports={
onShareAppMessage:function(url,customtitle,imgSrc){},
baocun(imageUrl){},
.....
}
引入
const core = require('../../utils/core.js');
登录
//登录授权
bindgetuserinfo(e) {
return new Promise((resolve, reject) => {
console.log("login")
let $this = this;
let channel = wx.getStorageSync('apppath')
wx.login({
success: function (ret) {
console.log(ret)
if (ret.code) {
wx.getUserInfo({
success(res2) {
http('post', 'wxLogin', {
code: ret.code,
encryptedData: res2.encryptedData,
iv: res2.iv,
userInfos: JSON.stringify(res2.userInfo),
signature: res2.signature,
channel,
}, function (login_res) {
console.log(login_res)
if (login_res.code == 200) {
let data = login_res.result
console.log(data)
wx.setStorageSync('userinfo', data.userInfo);
wx.setStorageSync('phone', data.userInfo.mobile);
wx.setStorageSync('userName', data.userInfo.nickname);
wx.setStorageSync('userId', data.userId);
// 设置统计代码的openid
// console.log($this)
// $this.matomo.setUserId(data.userInfo.openid)
resolve(true);
} else {
wx.showToast({
title: '获取用户登录态失败:' + login_res.message,
icon: "none"
})
}
})
}
})
}
},
fail: function (rett) {
wx.showToast({
title: '获取用户登录态失败:' + rett,
icon: "none"
})
reject(false);
}
})
})
}
封装http
function http(method, url, data={}){
return new Promise((resolve, reject) => {
let uid = wx.getStorageSync('userinfo').id
let activeId = wx.getStorageSync('activeId')
if (!method) {
method = "POST"
}
data.memberId = uid ? uid : ""
data.activeId = activeId ? activeId : ''
wx.request({
url: basePath + url,
method,
data,
header: {
'Content-Type': 'application/x-www-form-urlencoded',
'memberId': uid ? uid : null
},
success: function (res) {
resolve(res.data)
// var statusCode = res.statusCode.toString();
// if (statusCode.startsWith("2")) {
// params.success(res.data);
// } else {
// var errorCode = res.data.code;
// _show_error(errorCode);
// }
},
fail: err => {
reject(err)
// _show_error(1);
}
})
})
}
使用
http("post", 'selectStore', {
...data
}).then(res=>{
console.log(res)
})