mw-common-components 0.0.1-security → 99.99.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of mw-common-components might be problematic. Click here for more details.
- package/README.md +3 -3
- package/config/dev.env.js +37 -0
- package/config/hap.amend.js +29 -0
- package/config/index.js +8 -0
- package/config/prod.env.js +28 -0
- package/index.d.ts +130 -0
- package/index.js +1 -0
- package/package.json +44 -3
- package/src/index.html +12 -0
- package/src/lib/app.js +103 -0
- package/src/lib/axios/CHANGELOG.md +245 -0
- package/src/lib/axios/LICENSE +19 -0
- package/src/lib/axios/README.md +625 -0
- package/src/lib/axios/UPGRADE_GUIDE.md +162 -0
- package/src/lib/axios/dist/axios.js +1603 -0
- package/src/lib/axios/dist/axios.map +1 -0
- package/src/lib/axios/dist/axios.min.js +9 -0
- package/src/lib/axios/dist/axios.min.map +1 -0
- package/src/lib/axios/index.d.ts +131 -0
- package/src/lib/axios/index.js +1 -0
- package/src/lib/axios/lib/adapters/README.md +37 -0
- package/src/lib/axios/lib/adapters/http.js +237 -0
- package/src/lib/axios/lib/adapters/xhr.js +180 -0
- package/src/lib/axios/lib/axios.js +52 -0
- package/src/lib/axios/lib/cancel/Cancel.js +19 -0
- package/src/lib/axios/lib/cancel/CancelToken.js +57 -0
- package/src/lib/axios/lib/cancel/isCancel.js +5 -0
- package/src/lib/axios/lib/core/Axios.js +79 -0
- package/src/lib/axios/lib/core/InterceptorManager.js +52 -0
- package/src/lib/axios/lib/core/README.md +7 -0
- package/src/lib/axios/lib/core/createError.js +18 -0
- package/src/lib/axios/lib/core/dispatchRequest.js +86 -0
- package/src/lib/axios/lib/core/enhanceError.js +21 -0
- package/src/lib/axios/lib/core/settle.js +26 -0
- package/src/lib/axios/lib/core/transformData.js +20 -0
- package/src/lib/axios/lib/defaults.js +96 -0
- package/src/lib/axios/lib/helpers/README.md +7 -0
- package/src/lib/axios/lib/helpers/bind.js +11 -0
- package/src/lib/axios/lib/helpers/btoa.js +36 -0
- package/src/lib/axios/lib/helpers/buildURL.js +66 -0
- package/src/lib/axios/lib/helpers/combineURLs.js +14 -0
- package/src/lib/axios/lib/helpers/cookies.js +53 -0
- package/src/lib/axios/lib/helpers/deprecatedMethod.js +24 -0
- package/src/lib/axios/lib/helpers/isAbsoluteURL.js +14 -0
- package/src/lib/axios/lib/helpers/isURLSameOrigin.js +68 -0
- package/src/lib/axios/lib/helpers/normalizeHeaderName.js +12 -0
- package/src/lib/axios/lib/helpers/parseHeaders.js +53 -0
- package/src/lib/axios/lib/helpers/spread.js +27 -0
- package/src/lib/axios/lib/utils.js +303 -0
- package/src/lib/axios/modules/debug/CHANGELOG.md +395 -0
- package/src/lib/axios/modules/debug/LICENSE +19 -0
- package/src/lib/axios/modules/debug/README.md +437 -0
- package/src/lib/axios/modules/debug/dist/debug.js +886 -0
- package/src/lib/axios/modules/debug/node.js +1 -0
- package/src/lib/axios/modules/debug/package.json +96 -0
- package/src/lib/axios/modules/debug/src/browser.js +180 -0
- package/src/lib/axios/modules/debug/src/common.js +249 -0
- package/src/lib/axios/modules/debug/src/index.js +12 -0
- package/src/lib/axios/modules/debug/src/node.js +174 -0
- package/src/lib/axios/modules/follow-redirects/LICENSE +18 -0
- package/src/lib/axios/modules/follow-redirects/README.md +145 -0
- package/src/lib/axios/modules/follow-redirects/http.js +1 -0
- package/src/lib/axios/modules/follow-redirects/https.js +1 -0
- package/src/lib/axios/modules/follow-redirects/index.js +452 -0
- package/src/lib/axios/modules/follow-redirects/package.json +92 -0
- package/src/lib/axios/modules/is-buffer/LICENSE +21 -0
- package/src/lib/axios/modules/is-buffer/README.md +53 -0
- package/src/lib/axios/modules/is-buffer/index.js +21 -0
- package/src/lib/axios/modules/is-buffer/package.json +90 -0
- package/src/lib/axios/modules/is-buffer/test/basic.js +24 -0
- package/src/lib/axios/modules/ms/index.js +162 -0
- package/src/lib/axios/modules/ms/license.md +21 -0
- package/src/lib/axios/modules/ms/package.json +72 -0
- package/src/lib/axios/modules/ms/readme.md +60 -0
- package/src/lib/axios/package-lock.json +10437 -0
- package/src/lib/axios/package.json +111 -0
- package/src/lib/axios.min.js +10 -0
- package/src/lib/axiosRequest.js +84 -0
- package/src/lib/bmob.js +36 -0
- package/src/lib/common.js +227 -0
- package/src/lib/config.dev.js +62 -0
- package/src/lib/config.js +69 -0
- package/src/lib/dataType.js +17 -0
- package/src/lib/error.js +32 -0
- package/src/lib/file.js +250 -0
- package/src/lib/hapRequest.js +68 -0
- package/src/lib/hapStorage.js +62 -0
- package/src/lib/init.js +0 -0
- package/src/lib/nodestorage.js +14 -0
- package/src/lib/pay.js +19 -0
- package/src/lib/pointer.js +18 -0
- package/src/lib/query.js +689 -0
- package/src/lib/relation.js +45 -0
- package/src/lib/request.js +21 -0
- package/src/lib/sms.js +40 -0
- package/src/lib/socket.js +292 -0
- package/src/lib/storage.js +21 -0
- package/src/lib/user.js +225 -0
- package/src/lib/utf8md5.js +205 -0
- package/src/lib/utils.js +61 -0
- package/src/lib/webstorage.js +32 -0
- package/src/lib/wxRequest.js +88 -0
- package/src/lib/wxstorage.js +27 -0
- package/src/main.js +27 -0
- package/test/index.html +15 -0
- package/test/index.js +12 -0
- package/tsconfig.json +100 -0
@@ -0,0 +1,21 @@
|
|
1
|
+
// const Bmob = require('./bmob')
|
2
|
+
const utils = require('./utils')
|
3
|
+
|
4
|
+
let request
|
5
|
+
// 获取当前应用类型
|
6
|
+
const type = utils.getAppType()
|
7
|
+
// h5
|
8
|
+
if (type === 'h5') {
|
9
|
+
request = require('./axiosRequest')
|
10
|
+
} else if (type === 'wx') {
|
11
|
+
// 小程序
|
12
|
+
request = require('./wxRequest')
|
13
|
+
} else if (type === 'hap') {
|
14
|
+
// 快应用功能
|
15
|
+
request = require('./hapRequest')
|
16
|
+
} else if (type === 'nodejs') {
|
17
|
+
// 快应用功能
|
18
|
+
request = require('./axiosRequest')
|
19
|
+
}
|
20
|
+
|
21
|
+
module.exports = request
|
package/src/lib/sms.js
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
|
2
|
+
const request = require('./request')
|
3
|
+
const Bmob = require('./bmob')
|
4
|
+
const Error = require('./error')
|
5
|
+
const { isObject, isString } = require('./dataType')
|
6
|
+
|
7
|
+
// /**
|
8
|
+
// * 请求短信验证码
|
9
|
+
// * @param {Object} 相应的参数
|
10
|
+
// * @param {Object} Backbone-style options 对象。 options.success, 如果设置了,将会处理云端代码调用成功的情况。 options.error 如果设置了,将会处理云端代码调用失败的情况。 两个函数都是可选的。两个函数都只有一个参数。
|
11
|
+
// * @return {Bmob.Promise}
|
12
|
+
// */
|
13
|
+
|
14
|
+
const requestSmsCode = (data, options) => {
|
15
|
+
if (!isObject(data)) {
|
16
|
+
throw new Error(415)
|
17
|
+
}
|
18
|
+
let route = Bmob._config.parameters.REQUESTSMSCODE
|
19
|
+
return request(route, 'post', data)
|
20
|
+
}
|
21
|
+
// /**
|
22
|
+
// * 验证短信验证码
|
23
|
+
// * @param {Object} 相应的参数
|
24
|
+
// * @param {Object} Backbone-style options 对象。 options.success, 如果设置了,将会处理云端代码调用成功的情况。 options.error 如果设置了,将会处理云端代码调用失败的情况。 两个函数都是可选的。两个函数都只有一个参数。
|
25
|
+
// * @return {Bmob.Promise}
|
26
|
+
// */
|
27
|
+
const verifySmsCode = (smscode, data, options) => {
|
28
|
+
if (!isString(smscode)) {
|
29
|
+
// 参数异常
|
30
|
+
throw new Error(415)
|
31
|
+
}
|
32
|
+
if (!isObject(data)) {
|
33
|
+
// 参数异常
|
34
|
+
throw new Error(415)
|
35
|
+
}
|
36
|
+
let route = `${Bmob._config.parameters.VERIFYSMSCODE}/${smscode}`
|
37
|
+
return request(route, 'post', data)
|
38
|
+
}
|
39
|
+
|
40
|
+
module.exports = { requestSmsCode, verifySmsCode }
|
@@ -0,0 +1,292 @@
|
|
1
|
+
// const Bmob = require('./bmob')
|
2
|
+
const Error = require('./error')
|
3
|
+
const Emitter = {
|
4
|
+
setup (target) {
|
5
|
+
let listeners = []
|
6
|
+
|
7
|
+
Object.assign(target, {
|
8
|
+
on (type, handle) {
|
9
|
+
if (typeof handle === 'function') {
|
10
|
+
listeners.push([type, handle])
|
11
|
+
}
|
12
|
+
},
|
13
|
+
emit (type, ...params) {
|
14
|
+
listeners.forEach(
|
15
|
+
([listenType, handle]) => type === listenType && handle(...params)
|
16
|
+
)
|
17
|
+
},
|
18
|
+
removeAllListeners () {
|
19
|
+
listeners = []
|
20
|
+
}
|
21
|
+
})
|
22
|
+
}
|
23
|
+
}
|
24
|
+
|
25
|
+
/**
|
26
|
+
* 基于小程序 WebSocket 接口封装信道
|
27
|
+
*/
|
28
|
+
module.exports = class socket {
|
29
|
+
constructor (id = '') {
|
30
|
+
if (id === '') {
|
31
|
+
throw new Error(415)
|
32
|
+
}
|
33
|
+
this.config = {
|
34
|
+
host: 'wss.bmobcloud.com'
|
35
|
+
}
|
36
|
+
Emitter.setup((this.emitter = {}))
|
37
|
+
this.applicationId = id
|
38
|
+
this.initialize()
|
39
|
+
}
|
40
|
+
handshake () {
|
41
|
+
function complete (data) {
|
42
|
+
if (data instanceof Error) {
|
43
|
+
self.connecting = false
|
44
|
+
self.onError(data.message)
|
45
|
+
} else {
|
46
|
+
return data.split(':')[0]
|
47
|
+
}
|
48
|
+
}
|
49
|
+
|
50
|
+
let url = 'https://' + this.config.host + '/socket.io/1/?t=' + new Date().getTime()
|
51
|
+
let dataObject = {}
|
52
|
+
let data = JSON.stringify(dataObject)
|
53
|
+
|
54
|
+
let method = 'GET'
|
55
|
+
|
56
|
+
return new Promise((resolve, reject) => {
|
57
|
+
wx.request({
|
58
|
+
method: method,
|
59
|
+
url: url,
|
60
|
+
data: data,
|
61
|
+
header: {
|
62
|
+
'content-type': 'text/plain'
|
63
|
+
},
|
64
|
+
success: function (res) {
|
65
|
+
if (res.data && res.data.statusCode) {
|
66
|
+
return resolve('request error', e)
|
67
|
+
} else if (res.statusCode !== 200) {
|
68
|
+
return resolve('request error', e)
|
69
|
+
} else {
|
70
|
+
return resolve(complete(res.data))
|
71
|
+
}
|
72
|
+
},
|
73
|
+
fail: function (e) {
|
74
|
+
return resolve('request error', e)
|
75
|
+
}
|
76
|
+
})
|
77
|
+
})
|
78
|
+
}
|
79
|
+
initialize () {
|
80
|
+
this.emitter.removeAllListeners()
|
81
|
+
this.handshake().then(protocol => {
|
82
|
+
try {
|
83
|
+
let connectObj = this.connect(
|
84
|
+
`wss://${this.config.host}/socket.io/1/websocket/` + protocol,
|
85
|
+
{}
|
86
|
+
)
|
87
|
+
console.log(connectObj, 'connectObj')
|
88
|
+
connectObj.then(res => {
|
89
|
+
console.log(res, 'res-res')
|
90
|
+
})
|
91
|
+
} catch (connectError) {
|
92
|
+
console.error({ connectError })
|
93
|
+
throw connectError
|
94
|
+
}
|
95
|
+
})
|
96
|
+
this.on('close', () => {
|
97
|
+
console.log('连接已中断')
|
98
|
+
setTimeout(() => this.initialize(), 5000)
|
99
|
+
})
|
100
|
+
|
101
|
+
return new Promise((resolve, reject) => {
|
102
|
+
this.on('server_pub', data => {
|
103
|
+
switch (data.action) {
|
104
|
+
case 'updateTable':
|
105
|
+
this.onUpdateTable(data.tableName, data.data)
|
106
|
+
break
|
107
|
+
case 'updateRow':
|
108
|
+
this.onUpdateRow(data.tableName, data.objectId, data.data)
|
109
|
+
break
|
110
|
+
case 'deleteTable':
|
111
|
+
this.onDeleteTable(data.tableName, data.data)
|
112
|
+
break
|
113
|
+
case 'deleteRow':
|
114
|
+
this.onDeleteRow(data.tableName, data.objectId, data.data)
|
115
|
+
break
|
116
|
+
}
|
117
|
+
})
|
118
|
+
|
119
|
+
// 连接上socket.io服务器后触发的事件
|
120
|
+
this.on('client_send_data', resp => {
|
121
|
+
this.onInitListen()
|
122
|
+
})
|
123
|
+
})
|
124
|
+
}
|
125
|
+
|
126
|
+
onInitListen () { }
|
127
|
+
|
128
|
+
connect (url, header) {
|
129
|
+
// 小程序 wx.connectSocket() API header 参数无效,把会话信息附加在 URL 上
|
130
|
+
const query = Object.keys(header)
|
131
|
+
.map(key => `${key}=${encodeURIComponent(header[key])}`)
|
132
|
+
.join('&')
|
133
|
+
const seperator = url.indexOf('?') > -1 ? '&' : '?'
|
134
|
+
url = [url, query].join(seperator)
|
135
|
+
|
136
|
+
return new Promise((resolve, reject) => {
|
137
|
+
wx.onSocketOpen(resolve)
|
138
|
+
wx.onSocketError(reject)
|
139
|
+
wx.onSocketMessage(packet => {
|
140
|
+
try {
|
141
|
+
let filter = function (str) {
|
142
|
+
const { name, args } = JSON.parse(str)
|
143
|
+
return { name, args }
|
144
|
+
}
|
145
|
+
let str = packet.data
|
146
|
+
let startStr = str.slice(0, 4)
|
147
|
+
// 检测心跳
|
148
|
+
if (startStr === '2:::') {
|
149
|
+
this.emit(false, true)
|
150
|
+
}
|
151
|
+
str = str.slice(4)
|
152
|
+
|
153
|
+
// 截取后不能为空
|
154
|
+
if (str === null || str === '') {
|
155
|
+
return
|
156
|
+
}
|
157
|
+
const { name, args } = filter(str)
|
158
|
+
let data = args == null ? '' : JSON.parse(args[0])
|
159
|
+
this.emitter.emit(name, data)
|
160
|
+
} catch (e) {
|
161
|
+
console.log('Handle packet failed: ' + packet.data, e)
|
162
|
+
}
|
163
|
+
})
|
164
|
+
wx.onSocketClose(() => this.emitter.emit('close'))
|
165
|
+
wx.connectSocket({ url, header })
|
166
|
+
})
|
167
|
+
}
|
168
|
+
|
169
|
+
on (message, handle) {
|
170
|
+
this.emitter.on(message, handle)
|
171
|
+
}
|
172
|
+
|
173
|
+
emit (message, data) {
|
174
|
+
data = data === undefined ? '5:::' : '2:::'
|
175
|
+
message = message ? JSON.stringify(message) : ''
|
176
|
+
wx.sendSocketMessage({
|
177
|
+
data: data + message
|
178
|
+
})
|
179
|
+
}
|
180
|
+
|
181
|
+
emitData (name, data) {
|
182
|
+
data = JSON.stringify(data)
|
183
|
+
return { name: name, args: [data] }
|
184
|
+
}
|
185
|
+
|
186
|
+
updateTable (tablename) {
|
187
|
+
let data = {
|
188
|
+
appKey: this.applicationId,
|
189
|
+
tableName: tablename,
|
190
|
+
objectId: '',
|
191
|
+
action: 'updateTable'
|
192
|
+
}
|
193
|
+
data = this.emitData('client_sub', data)
|
194
|
+
this.emit(data)
|
195
|
+
}
|
196
|
+
|
197
|
+
// 取消订阅更新数据表的数据
|
198
|
+
unsubUpdateTable (tablename) {
|
199
|
+
let data = {
|
200
|
+
appKey: this.applicationId,
|
201
|
+
tableName: tablename,
|
202
|
+
objectId: '',
|
203
|
+
action: 'unsub_updateTable'
|
204
|
+
}
|
205
|
+
data = this.emitData('client_unsub', data)
|
206
|
+
this.emit(data)
|
207
|
+
}
|
208
|
+
|
209
|
+
// 订阅行更新的数据
|
210
|
+
updateRow (tablename, objectId) {
|
211
|
+
let data = {
|
212
|
+
appKey: this.applicationId,
|
213
|
+
tableName: tablename,
|
214
|
+
objectId: objectId,
|
215
|
+
action: 'updateRow'
|
216
|
+
}
|
217
|
+
data = this.emitData('client_sub', data)
|
218
|
+
this.emit(data)
|
219
|
+
}
|
220
|
+
|
221
|
+
// 取消订阅行更新的数据
|
222
|
+
unsubUpdateRow (tablename, objectId) {
|
223
|
+
let data = {
|
224
|
+
appKey: this.applicationId,
|
225
|
+
tableName: tablename,
|
226
|
+
objectId: objectId,
|
227
|
+
action: 'unsub_updateRow'
|
228
|
+
}
|
229
|
+
data = this.emitData('client_unsub', data)
|
230
|
+
this.emit(data)
|
231
|
+
}
|
232
|
+
|
233
|
+
// 订阅表删除的数据
|
234
|
+
deleteTable (tablename) {
|
235
|
+
let data = {
|
236
|
+
appKey: this.applicationId,
|
237
|
+
tableName: tablename,
|
238
|
+
objectId: '',
|
239
|
+
action: 'deleteTable'
|
240
|
+
}
|
241
|
+
data = this.emitData('client_sub', data)
|
242
|
+
this.emit(data)
|
243
|
+
}
|
244
|
+
|
245
|
+
// 取消订阅表删除的数据
|
246
|
+
unsubDeleteTable (tablename) {
|
247
|
+
let data = {
|
248
|
+
appKey: this.applicationId,
|
249
|
+
tableName: tablename,
|
250
|
+
objectId: '',
|
251
|
+
action: 'unsub_deleteTable'
|
252
|
+
}
|
253
|
+
data = this.emitData('client_unsub', data)
|
254
|
+
this.emit(data)
|
255
|
+
}
|
256
|
+
|
257
|
+
// 订阅更新数据表的数据
|
258
|
+
deleteRow (tablename, objectId) {
|
259
|
+
let data = {
|
260
|
+
appKey: this.applicationId,
|
261
|
+
tableName: tablename,
|
262
|
+
objectId: objectId,
|
263
|
+
action: 'deleteRow'
|
264
|
+
}
|
265
|
+
data = this.emitData('client_sub', data)
|
266
|
+
this.emit(data)
|
267
|
+
}
|
268
|
+
|
269
|
+
// 订阅更新数据表的数据
|
270
|
+
unsubDeleteRow (tablename, objectId) {
|
271
|
+
let data = {
|
272
|
+
appKey: this.applicationId,
|
273
|
+
tableName: tablename,
|
274
|
+
objectId: objectId,
|
275
|
+
action: 'unsub_deleteRow'
|
276
|
+
}
|
277
|
+
data = this.emitData('client_unsub', data)
|
278
|
+
this.emit(data)
|
279
|
+
}
|
280
|
+
|
281
|
+
// 监听服务器返回的更新数据表的数据,需要用户重写
|
282
|
+
onUpdateTable (tablename, data) { }
|
283
|
+
|
284
|
+
// 监听服务器返回的更新数据表的数据,需要用户重写
|
285
|
+
onUpdateRow (tablename, objectId, data) { }
|
286
|
+
|
287
|
+
// 监听服务器返回的更新数据表的数据,需要用户重写
|
288
|
+
onDeleteTable (tablename, data) { }
|
289
|
+
|
290
|
+
// 监听服务器返回的更新数据表的数据,需要用户重写
|
291
|
+
onDeleteRow (tablename, objectId, data) { }
|
292
|
+
}
|
@@ -0,0 +1,21 @@
|
|
1
|
+
// const Bmob = require('./bmob')
|
2
|
+
const utils = require('./utils')
|
3
|
+
|
4
|
+
let storage
|
5
|
+
// //获取当前应用类型
|
6
|
+
const type = utils.getAppType()
|
7
|
+
// h5
|
8
|
+
if (type === 'h5') {
|
9
|
+
storage = require('./webstorage')
|
10
|
+
} else if (type === 'wx') {
|
11
|
+
// 小程序
|
12
|
+
storage = require('./wxstorage')
|
13
|
+
} else if (type === 'hap') {
|
14
|
+
storage = require('./hapStorage')
|
15
|
+
// 快应用功能
|
16
|
+
} else if (type === 'nodejs') {
|
17
|
+
// 快应用功能
|
18
|
+
storage = require('./nodestorage')
|
19
|
+
}
|
20
|
+
|
21
|
+
module.exports = storage
|
package/src/lib/user.js
ADDED
@@ -0,0 +1,225 @@
|
|
1
|
+
const request = require("./request");
|
2
|
+
const storage = require("./storage");
|
3
|
+
const query = require("./query");
|
4
|
+
const Bmob = require("./bmob");
|
5
|
+
const Error = require("./error");
|
6
|
+
const { isObject, isString, isNumber } = require("./dataType");
|
7
|
+
|
8
|
+
const user = class user extends query {
|
9
|
+
constructor() {
|
10
|
+
const tableName = "_User";
|
11
|
+
super(tableName);
|
12
|
+
}
|
13
|
+
set(key, val = "") {
|
14
|
+
if (isString(key)) {
|
15
|
+
this.setData[key] = val;
|
16
|
+
}
|
17
|
+
}
|
18
|
+
requestEmailVerify(email) {
|
19
|
+
if (!isString(email)) {
|
20
|
+
// 异常
|
21
|
+
throw new Error(415);
|
22
|
+
}
|
23
|
+
|
24
|
+
this.setData = Object.assign({}, { email });
|
25
|
+
console.log(this.setData);
|
26
|
+
const route = Bmob._config.parameters.REQUEST_EMAIL_VERIFY;
|
27
|
+
return request(route, "post", this.setData);
|
28
|
+
}
|
29
|
+
register(parma) {
|
30
|
+
if (!isObject(parma)) {
|
31
|
+
// 异常
|
32
|
+
throw new Error(415);
|
33
|
+
}
|
34
|
+
this.setData = Object.assign({}, parma);
|
35
|
+
const route = Bmob._config.parameters.REGISTER;
|
36
|
+
return request(route, "post", this.setData);
|
37
|
+
}
|
38
|
+
|
39
|
+
login(username, password) {
|
40
|
+
if (!isString(username) || !isString(password)) {
|
41
|
+
// 异常
|
42
|
+
throw new Error(415);
|
43
|
+
}
|
44
|
+
this.setData = Object.assign({}, { username, password });
|
45
|
+
const route = Bmob._config.parameters.LOGIN;
|
46
|
+
return new Promise((resolve, reject) => {
|
47
|
+
request(route, "get", this.setData)
|
48
|
+
.then((res) => {
|
49
|
+
storage.save("bmob", res);
|
50
|
+
resolve(res);
|
51
|
+
})
|
52
|
+
.catch((err) => {
|
53
|
+
reject(err);
|
54
|
+
});
|
55
|
+
});
|
56
|
+
}
|
57
|
+
logout() {
|
58
|
+
storage.clear();
|
59
|
+
}
|
60
|
+
users() {
|
61
|
+
const route = Bmob._config.parameters.USERS;
|
62
|
+
return request(route, "get");
|
63
|
+
}
|
64
|
+
decryption(e) {
|
65
|
+
let self = this;
|
66
|
+
return new Promise((resolve, reject) => {
|
67
|
+
const i = e.iv ? e.iv : e.detail.iv;
|
68
|
+
const d = e.encryptedData ? e.encryptedData : e.detail.encryptedData;
|
69
|
+
|
70
|
+
// 调用云函数解密
|
71
|
+
const current = self.current();
|
72
|
+
let s;
|
73
|
+
if (typeof tt !== "undefined") {
|
74
|
+
s = current.authData.toutiao.session_key;
|
75
|
+
} else if (typeof qq !== "undefined") {
|
76
|
+
s = current.authData.qqapp.session_key;
|
77
|
+
} else {
|
78
|
+
s = current.authData.weapp.sk;
|
79
|
+
}
|
80
|
+
const data = {
|
81
|
+
sk: s,
|
82
|
+
encryptedData: d,
|
83
|
+
iv: i,
|
84
|
+
};
|
85
|
+
const route = Bmob._config.parameters.DECRYPTION;
|
86
|
+
request(route, "POST", data)
|
87
|
+
.then((res) => {
|
88
|
+
resolve(res);
|
89
|
+
})
|
90
|
+
.catch((err) => {
|
91
|
+
reject(err);
|
92
|
+
});
|
93
|
+
});
|
94
|
+
}
|
95
|
+
signOrLoginByMobilePhone(mobilePhoneNumber, smsCode) {
|
96
|
+
// 手机号登陆
|
97
|
+
if (!isNumber(mobilePhoneNumber) || !isNumber(smsCode)) {
|
98
|
+
// 异常
|
99
|
+
throw new Error(415);
|
100
|
+
}
|
101
|
+
this.setData = Object.assign({}, { mobilePhoneNumber, smsCode });
|
102
|
+
const route = Bmob._config.parameters.LOGIN;
|
103
|
+
return request(route, "get", this.setData);
|
104
|
+
}
|
105
|
+
requestOpenId(code, a = "") {
|
106
|
+
const route = Bmob._config.parameters.WECHAT_APP;
|
107
|
+
return request(route + code, "POST", { anonymous_code: a });
|
108
|
+
}
|
109
|
+
linkWith(data) {
|
110
|
+
// 第三方登陆
|
111
|
+
let authData = { authData: data };
|
112
|
+
const route = Bmob._config.parameters.USERSV1;
|
113
|
+
return request(route, "POST", authData);
|
114
|
+
}
|
115
|
+
loginWithWeapp(code, a = "", str) {
|
116
|
+
return new Promise((resolve, reject) => {
|
117
|
+
this.requestOpenId(code, a)
|
118
|
+
.then((res) => {
|
119
|
+
let w = { weapp: res };
|
120
|
+
if (typeof tt !== "undefined") {
|
121
|
+
delete res.error;
|
122
|
+
w = { toutiao: res };
|
123
|
+
}
|
124
|
+
if (typeof qq !== "undefined") {
|
125
|
+
delete res.errcode;
|
126
|
+
delete res.errmsg;
|
127
|
+
w = { qqapp: res };
|
128
|
+
}
|
129
|
+
if (str === "openid") {
|
130
|
+
resolve(res);
|
131
|
+
} else {
|
132
|
+
const result = this.linkWith(w);
|
133
|
+
resolve(result);
|
134
|
+
}
|
135
|
+
})
|
136
|
+
.catch((err) => {
|
137
|
+
reject(err);
|
138
|
+
});
|
139
|
+
});
|
140
|
+
}
|
141
|
+
|
142
|
+
upInfo(userInfo) {
|
143
|
+
if (!isObject(userInfo)) {
|
144
|
+
throw new Error(415);
|
145
|
+
}
|
146
|
+
return new Promise((resolve, reject) => {
|
147
|
+
let nickName = userInfo.nickName;
|
148
|
+
let avatarUrl = userInfo.avatarUrl;
|
149
|
+
|
150
|
+
let currentUser = this.current();
|
151
|
+
if (!currentUser) {
|
152
|
+
throw new Error(415);
|
153
|
+
}
|
154
|
+
let openid = storage.fetch("openid");
|
155
|
+
this.get(currentUser.objectId)
|
156
|
+
.then((res) => {
|
157
|
+
res.set("nickName", nickName);
|
158
|
+
res.set("userPic", avatarUrl);
|
159
|
+
res.set("openid", openid);
|
160
|
+
res
|
161
|
+
.save()
|
162
|
+
.then((result) => {
|
163
|
+
resolve(result);
|
164
|
+
})
|
165
|
+
.catch((err) => {
|
166
|
+
console.log(err);
|
167
|
+
reject(err);
|
168
|
+
});
|
169
|
+
})
|
170
|
+
.catch((err) => {
|
171
|
+
console.log(err);
|
172
|
+
reject(err);
|
173
|
+
});
|
174
|
+
});
|
175
|
+
}
|
176
|
+
openId() {
|
177
|
+
this.auth("openid");
|
178
|
+
}
|
179
|
+
auth(str = "") {
|
180
|
+
let that = this;
|
181
|
+
return new Promise((resolve, reject) => {
|
182
|
+
const login = () => {
|
183
|
+
wx.login({
|
184
|
+
success: (res) => {
|
185
|
+
let anonymousCode = "";
|
186
|
+
if (typeof tt !== "undefined") {
|
187
|
+
anonymousCode = res.anonymousCode;
|
188
|
+
}
|
189
|
+
that.loginWithWeapp(res.code, anonymousCode, str).then(
|
190
|
+
(user) => {
|
191
|
+
if (user.error) {
|
192
|
+
throw new Error(415);
|
193
|
+
}
|
194
|
+
let openid;
|
195
|
+
if (typeof tt !== "undefined") {
|
196
|
+
openid = user.openid!=undefined?user.openid:user.authData.toutiao.openid;
|
197
|
+
} else if (typeof qq !== "undefined") {
|
198
|
+
openid = user.authData.qqapp.openid;
|
199
|
+
} else {
|
200
|
+
openid = user.authData.weapp.openid;
|
201
|
+
}
|
202
|
+
storage.save("openid", openid);
|
203
|
+
storage.save("bmob", user);
|
204
|
+
// 保存用户其他信息到用户表
|
205
|
+
resolve(user);
|
206
|
+
},
|
207
|
+
function (err) {
|
208
|
+
reject(err);
|
209
|
+
}
|
210
|
+
);
|
211
|
+
},
|
212
|
+
});
|
213
|
+
};
|
214
|
+
|
215
|
+
let c = that.current();
|
216
|
+
if (c === null) {
|
217
|
+
login()
|
218
|
+
}else{
|
219
|
+
resolve(c);
|
220
|
+
}
|
221
|
+
});
|
222
|
+
}
|
223
|
+
};
|
224
|
+
|
225
|
+
module.exports = user;
|