1.封装接口
module.exports=(url,method,data,token=true)=>{
return new Promise((resolve,reject)=>{
wx.request({
url,url,
method:method,
data:Object.assign({},data),
header:token?{"Content-Type": "application/text"}:{"content-type":"application/x-www-form-urlencoded"},
success(res){
resolve(res)
},
fail(err){
reject(err)
}
})
})
}
2.如下操作
function logines(code){
return fetch.http('URL','post',{code: code,type: 2},false)
}
function delet(token){
return fetch.http(`URL?token=${token}`)
}
//必须设置open-type,bindgetuserinfo,这些是固定的,不能改
<van-button type="default" size='small' bindgetuserinfo="getUserInfo" open-type="getUserInfo">立即登录</van-button>
let app = getApp()
Page({
data: {
show: true,
list: []
},
qin(){
wx.clearStorage()
wx.reLaunch({
url: '/pages/wd/wd',
})
},
getUserInfo(e) {
if(!e.detail.userInfo){
wx.showToast({
title: '已取消',
icon:'nane'
})
return false
}
let _that=this
wx.login({
success(req) {
app.http.logines(req.code).then(res => {
wx.setStorageSync('token', res.data.data.token)
_that.login()
})
}
})
},
login(){
app.http.delet(wx.getStorageSync('token')).then(res => {
if(res.data.code==2000){
this.setData({
show:true
})
}else{
this.setData({
list:res.data.data.base,
show:false
})
}
})
},
onLoad: function () {
if(wx.getStorageSync('token')){
this.login()
this.setData({
show:false
})
}else{
this.setData({
show:true
})
}
},
tuei(){
app.http.onlogin(wx.getStorageSync('token')).then(res=>{
if(res.code==0){
thiswx.removeStorageSync('token')
this.setData({
show:true
})
}
})
wx.reLaunch({
url: '/pages/wd/wd',
})
}
})