# 1. 封装接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//在根目录下创建http文件,里面新建一个js文件,CV进去
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. 如下操作

1
2
3
4
5
6
7
//举例
function logines(code){
return fetch.http('URL','post',{code: code,type: 2},false)
}
function delet(token){
return fetch.http(`URL?token=${token}`)
}
1
2
//必须设置open-type,bindgetuserinfo,这些是固定的,不能改
<van-button type="default" size='small' bindgetuserinfo="getUserInfo" open-type="getUserInfo">立即登录</van-button>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// pages/user/user.js
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',
})
}
})
更新于

请我喝[茶]~( ̄▽ ̄)~*

高祥 微信支付

微信支付

高祥 支付宝

支付宝

高祥 贝宝

贝宝