forked from bayetech/wechat_mall_applet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
151 lines (143 loc) · 4.26 KB
/
app.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const jsonApi = require('utils/jsonapi-datastore/dist/jsonapi-datastore.js')
require('utils/polyfill.js')
App({
onLaunch: function () {
var that = this
this.store = new(jsonApi.JsonApiDataStore)
this.jsonModel = jsonApi.JsonApiDataStoreModel
this.globalData.code = wx.getStorageSync('code')
this.getUserInfo(function() {
that.postEncryptedData(function(res){
that.globalData.wechatUserType = res.data.wechat_user_type
})
})
this.request({
url: `${that.globalData.API_URL}/manage_features`,
success: function(res) { that.globalData.featureManager = res.data }
})
},
getUserInfo: function (cb) {
var that = this
if(this.globalData.userInfo){
typeof cb == "function" && cb(this.globalData.userInfo)
}else{
wx.login({
success: function (res) {
if (res.code) {
that.globalData.code = res.code
wx.setStorageSync('code', res.code)
wx.getUserInfo({
success: function (res) {
that.globalData.encrypted = {encryptedData: res.encryptedData, iv: res.iv}
that.globalData.userInfo = res.userInfo
typeof cb == "function" && cb(that.globalData.userInfo)
}
})
} else {
console.log('获取用户登录态失败!' + res.errMsg)
}
}
})
}
},
request: function(obj) {
var header = obj.header || {}
if (!header['Content-Type']) {
header['Content-Type'] = 'application/json'
}
if (!header['Authorization']) {
header['Authorization'] = this.globalData.token
}
// This must be wx.request !
wx.request({
url: obj.url,
data: obj.data || {},
method: obj.method || 'GET',
header: header,
success: function(res) {
typeof obj.success == "function" && obj.success(res)
},
fail: obj.fail || function() {},
complete: obj.complete || function() {}
})
},
authRequest: function(obj) {
var that = this
if (!that.globalData.token) {
var token = wx.getStorageSync('userToken')
if (!token) {
wx.hideToast()
wx.showModal({
title: '未登录',
content: '请前往 “我的” 页面绑定手机号',
showCancel: false,
success: function(res) {
// 清除没用的token
wx.removeStorage({key: 'userToken'})
that.globalData.token = undefined
if (getCurrentPages().length > 1) {
wx.navigateBack()
}
}
})
return
}
that.globalData.token = token
that.request({
url: `${that.globalData.API_URL}/sessions/login`,
method: 'POST',
data: {code: that.globalData.code},
success: function(res) {
if (!res.data.token) {
wx.hideToast()
wx.showModal({
title: '未登录',
content: '请前往 “我的” 页面绑定手机号',
showCancel: false,
success: function(res) {
// 清除没用的token
wx.removeStorage({key: 'userToken'})
that.globalData.token = undefined
if (getCurrentPages().length > 1) {
wx.navigateBack()
}
}
})
} else {
that.globalData.currentCustomer = res.data.customer
that.globalData.token = res.data.token
wx.setStorage({
key: 'userToken',
data: res.data.token
})
that.request(obj)
}
},
fail: function(res) {}
})
} else {
that.request(obj)
}
},
postEncryptedData: function (resolve) {
this.request({
method: 'POST',
url: `${this.globalData.API_URL}/sessions/wechat_user_type`,
data: {
code: this.globalData.code,
encrypted: this.globalData.encrypted,
userInfo: this.globalData.userInfo
},
success: resolve,
fail: function(res) {}
})
},
globalData:{
wechatUserType: 'normal',
featureManager: {},
userInfo: null,
currentCustomer: null,
// API_URL: 'http://localhost:3000',
API_URL: 'https://rapi.bayekeji.com'
}
})