微信获取openid get请求方式封装
util.js:var header //请求头
// 封装post请求
const post = (url, data, isUrl) => {
header = {
'content-type': isUrl ? 'application/x-www-form-urlencoded' : 'application/json ',
'Cookie': wx.getStorageSync("cookieKey") //读取cookie
'toekn':'123456'
};
var promise = new Promise((resolve, reject) => {
//网络请求
wx.request({
url: req + url,
data: data,
method: 'POST',
header: header,
success: function (res) { //服务器返回数据
if (res.statusCode == 200) {
if (res.data.code == '301') {
//身份验证过期
wx.hideLoading()
wx.showModal({
showCancel: false,
content: res.data.msg,
success(res) {
if (res.confirm) {
console.log('用户点击确定')
wx.clearStorage()
wx.navigateBack()
} else if (res.cancel) {
console.log('用户点击取消')
}
}
})
} else {
resolve(res);//返回成功信息
}
} else { //返回错误提示信息
reject(res.data);
}
},
error: function (e) {
reject('网络出错');
}
})
});
return promise;
}
// 封装get请求
const get = (url, data, isUrl) => {
header = {
'content-type': !isUrl ? 'application/x-www-form-urlencoded' : 'application/json ',
'Cookie': wx.getStorageSync("cookieKey") //读取cookie
'toekn':'123456'
}
var promise = new Promise((resolve, reject) => {
//网络请求
wx.request({
url: req + url,
data: data,
header: header,
success: function (res) { //服务器返回数据
if (res.statusCode == 200) {
console.log();
if (res.data.code == '301') {
//身份验证过期
wx.hideLoading()
wx.showModal({
showCancel: false,
content: res.data.msg,
success(res) {
if (res.confirm) {
console.log('用户点击确定')
wx.clearStorage()
wx.navigateBack()
} else if (res.cancel) {
console.log('用户点击取消')
}
}
})
} else {
resolve(res);//返回成功信息
}
} else { //返回错误提示信息
reject(res.data);
}
},
error: function (e) {
reject('网络出错');
}
})
});
return promise;
}
function json2Form(json) {
var str = [];
for (var p in json) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]));
}
return str.join("&");
}
//图片地址转换
function addX(b) {
var d
var c = []
b.map(item => {
c.push('\\"' + item + '\\"');
})
if (c[2]) {
d = "[" + c[0] + ',' + c[1] + ',' + c[2] + "]"
} else if (c[1]) {
d = "[" + c[0] + ',' + c[1] + "]"
} else if (c[0]) {
d = "[" + c[0] + "]"
}
return d
}
module.exports = {
post,
get,
reqImg,
json2Form,
addX //图片地址转换
}
使用:
//获取应用实例
const app = getApp()
var req = require('../../utils/util')
Page({
data: {
isLogin: false, //是否登录
openid: wx.getStorageSync('openid'),//获取本地openid
shop: '',
capy: false,
mony: 0,
isModelOne: false,
isModel: false,
isUse: false,
isUsek: false
},
clearAll() {
wx.clearStorage() //清除本地缓存
},
onShow() {
//get请求
if (!wx.getStorageSync('tokenQi'))
req.get('/qiniu/token/get', {
type: 1,
version: '1.0'
}, 1)
.then(res => {
var tokenQi = res.data.msg
wx.setStorageSync('tokenQi', tokenQi)
})
},
onLoad() {
},
//post请求
getPhoneNumber: function (e) {
var that = this
let data = e.detail
let iv = data.iv
let encryptedData = data.encryptedData
req.post('/merchant/phoneNumber', {
encryptedData: encryptedData,
iv: iv,
openId: that.data.openid
})
.then(res => {
if (res.data.code == 0) {
if (res.data.data == null) {
wx.showToast({
title: '没有获取到手机号',
icon: "none"
})
} else {
that.setData({
phoneNumber: res.data.data.phoneNumber
})
let phoneNumber = res.data.data.phoneNumber
that.loginM(phoneNumber)
}
} else {
wx.showToast({
title: res.data.msg,
icon: 'none',
duration: 2000
})
}
})
}
})