cshenfu
2023-06-14 de93aa62c83d6afb9f70cd5a22838375db0a8cbc
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
const uniIdCommon = require('uni-id-common')
const uniCaptcha = require('uni-captcha')
const {
  getType
} = require('./common/utils')
const {
  checkClientInfo,
  Validator
} = require('./common/validator')
const ConfigUtils = require('./lib/utils/config')
const {
  isUniIdError
} = require('./common/error')
const middleware = require('./middleware/index')
const universal = require('./common/universal')
 
const {
  registerAdmin,
  registerUser,
  registerUserByEmail
} = require('./module/register/index')
const {
  addUser,
  updateUser
} = require('./module/admin/index')
const {
  login,
  loginBySms,
  loginByUniverify,
  loginByWeixin,
  loginByAlipay,
  loginByQQ,
  loginByApple,
  loginByWeixinMobile
} = require('./module/login/index')
const {
  logout
} = require('./module/logout/index')
const {
  bindMobileBySms,
  bindMobileByUniverify,
  bindMobileByMpWeixin,
  bindAlipay,
  bindApple,
  bindQQ,
  bindWeixin,
  unbindWeixin,
  unbindAlipay,
  unbindQQ,
  unbindApple
} = require('./module/relate/index')
const {
  setPwd,
  updatePwd,
  resetPwdBySms,
  resetPwdByEmail,
  closeAccount,
  getAccountInfo
} = require('./module/account/index')
const {
  createCaptcha,
  refreshCaptcha,
  sendSmsCode,
  sendEmailCode
} = require('./module/verify/index')
const {
  refreshToken,
  setPushCid,
  secureNetworkHandshakeByWeixin
} = require('./module/utils/index')
const {
  getInvitedUser,
  acceptInvite
} = require('./module/fission')
const {
  authorizeAppLogin,
  removeAuthorizedApp,
  setAuthorizedApp
} = require('./module/multi-end')
const {
  getSupportedLoginType
} = require('./module/dev/index')
 
const {
  externalRegister,
  externalLogin,
  updateUserInfoByExternal
} = require('./module/external')
 
module.exports = {
  async _before () {
    // 支持 callFunction 与 URL化
    universal.call(this)
 
    const clientInfo = this.getUniversalClientInfo()
    /**
     * 检查clientInfo,无appId和uniPlatform时本云对象无法正常运行
     * 此外需要保证用到的clientInfo字段均经过类型检查
     * clientInfo由客户端上传并非完全可信,clientInfo内除clientIP、userAgent、source外均为客户端上传参数
     * 否则可能会出现一些意料外的情况
     */
    checkClientInfo(clientInfo)
    let clientPlatform = clientInfo.uniPlatform
    // 统一platform名称
    switch (clientPlatform) {
      case 'app':
      case 'app-plus':
        clientPlatform = 'app'
        break
      case 'web':
      case 'h5':
        clientPlatform = 'web'
        break
      default:
        break
    }
 
    this.clientPlatform = clientPlatform
 
    // 挂载uni-id实例到this上,方便后续调用
    this.uniIdCommon = uniIdCommon.createInstance({
      clientInfo
    })
 
    // 包含uni-id配置合并等功能的工具集
    this.configUtils = new ConfigUtils({
      context: this
    })
    this.config = this.configUtils.getPlatformConfig()
    this.hooks = this.configUtils.getHooks()
 
    this.validator = new Validator({
      passwordStrength: this.config.passwordStrength
    })
    /**
     * 示例:覆盖密码验证规则
     */
    // this.validator.mixin('password', function (password) {
    //   if (typeof password !== 'string' || password.length < 10) {
    //     // 调整为密码长度不能小于10
    //     return {
    //       errCode: ERROR.INVALID_PASSWORD
    //     }
    //   }
    // })
    /**
     * 示例:新增验证规则
     */
    // this.validator.mixin('timestamp', function (timestamp) {
    //   if (typeof timestamp !== 'number' || timestamp > Date.now()) {
    //     return {
    //       errCode: ERROR.INVALID_PARAM
    //     }
    //   }
    // })
    // // 新增规则同样可以在数组验证规则中使用
    // this.validator.valdate({
    //   timestamp: 123456789
    // }, {
    //   timestamp: 'timestamp'
    // })
    // this.validator.valdate({
    //   timestampList: [123456789, 123123123123]
    // }, {
    //   timestampList: 'array<timestamp>'
    // })
    // // 甚至更复杂的写法
    // this.validator.valdate({
    //   timestamp: [123456789, 123123123123]
    // }, {
    //   timestamp: 'timestamp|array<timestamp>'
    // })
 
    // 挂载uni-captcha到this上,方便后续调用
    this.uniCaptcha = uniCaptcha
    Object.defineProperty(this, 'uniOpenBridge', {
      get () {
        return require('uni-open-bridge-common')
      }
    })
 
    // 挂载中间件
    this.middleware = {}
    for (const mwName in middleware) {
      this.middleware[mwName] = middleware[mwName].bind(this)
    }
 
    // 国际化
    const i18n = uniCloud.initI18n({
      locale: clientInfo.locale,
      fallbackLocale: 'zh-Hans',
      messages: require('./lang/index')
    })
    this.t = i18n.t.bind(i18n)
 
    this.response = {}
 
    // 请求鉴权验证
    await this.middleware.verifyRequestSign()
 
    // 通用权限校验模块
    await this.middleware.accessControl()
  },
  _after (error, result) {
    if (error) {
      // 处理中间件内抛出的标准响应对象
      if (error.errCode && getType(error) === 'object') {
        const errCode = error.errCode
        if (!isUniIdError(errCode)) {
          return error
        }
        return {
          errCode,
          errMsg: error.errMsg || this.t(errCode, error.errMsgValue)
        }
      }
      throw error
    }
    return Object.assign(this.response, result)
  },
  /**
   * 注册管理员
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#register-admin
   * @param {Object} params
   * @param {String} params.username   用户名
   * @param {String} params.password   密码
   * @param {String} params.nickname   昵称
   * @returns
   */
  registerAdmin,
  /**
   * 新增用户
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#add-user
   * @param {Object}  params
   * @param {String}  params.username       用户名
   * @param {String}  params.password       密码
   * @param {String}  params.nickname       昵称
   * @param {Array}   params.authorizedApp  允许登录的AppID列表
   * @param {Array}   params.role           用户角色列表
   * @param {String}  params.mobile         手机号
   * @param {String}  params.email          邮箱
   * @param {Array}   params.tags           用户标签
   * @param {Number}  params.status         用户状态
   * @returns
   */
  addUser,
  /**
   * 修改用户
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#update-user
   * @param {Object}  params
   * @param {String} params.id              要更新的用户id
   * @param {String}  params.username       用户名
   * @param {String}  params.password       密码
   * @param {String}  params.nickname       昵称
   * @param {Array}   params.authorizedApp  允许登录的AppID列表
   * @param {Array}   params.role           用户角色列表
   * @param {String} params.mobile          手机号
   * @param {String} params.email           邮箱
   * @param {Array}  params.tags            用户标签
   * @param {Number} params.status          用户状态
   * @returns
   */
  updateUser,
  /**
   * 授权用户登录应用
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#authorize-app-login
   * @param {Object} params
   * @param {String} params.uid   用户id
   * @param {String} params.appId 授权的应用的AppId
   * @returns
   */
  authorizeAppLogin,
  /**
   * 移除用户登录授权
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#remove-authorized-app
   * @param {Object} params
   * @param {String} params.uid   用户id
   * @param {String} params.appId 取消授权的应用的AppId
   * @returns
   */
  removeAuthorizedApp,
  /**
   * 设置用户允许登录的应用列表
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#set-authorized-app
   * @param {Object} params
   * @param {String} params.uid       用户id
   * @param {Array} params.appIdList 允许登录的应用AppId列表
   * @returns
   */
  setAuthorizedApp,
  /**
   * 注册普通用户
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#register-user
   * @param {Object} params
   * @param {String} params.username    用户名
   * @param {String} params.password    密码
   * @param {String} params.captcha     图形验证码
   * @param {String} params.nickname    昵称
   * @param {String} params.inviteCode  邀请码
   * @returns
   */
  registerUser,
  /**
   * 通过邮箱+验证码注册用户
   * @param {Object} params
   * @param {String} params.email    邮箱
   * @param {String} params.password      密码
   * @param {String} params.nickname    昵称
   * @param {String} params.code  邮箱验证码
   * @param {String} params.inviteCode  邀请码
   * @returns
   */
  registerUserByEmail,
  /**
   * 用户名密码登录
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#login
   * @param {Object} params
   * @param {String} params.username  用户名
   * @param {String} params.mobile    手机号
   * @param {String} params.email     邮箱
   * @param {String} params.password  密码
   * @param {String} params.captcha   图形验证码
   * @returns
   */
  login,
  /**
   * 短信验证码登录
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#login-by-sms
   * @param {Object} params
   * @param {String} params.mobile      手机号
   * @param {String} params.code        短信验证码
   * @param {String} params.captcha     图形验证码
   * @param {String} params.inviteCode  邀请码
   * @returns
   */
  loginBySms,
  /**
   * App端一键登录
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#login-by-univerify
   * @param {Object} params
   * @param {String} params.access_token  APP端一键登录返回的access_token
   * @param {String} params.openid        APP端一键登录返回的openid
   * @param {String} params.inviteCode    邀请码
   * @returns
   */
  loginByUniverify,
  /**
   * 微信登录
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#login-by-weixin
   * @param {Object} params
   * @param {String} params.code          微信登录返回的code
   * @param {String} params.inviteCode    邀请码
   * @returns
   */
  loginByWeixin,
  /**
   * 支付宝登录
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#login-by-alipay
   * @param {Object} params
   * @param {String} params.code        支付宝小程序客户端登录返回的code
   * @param {String} params.inviteCode  邀请码
   * @returns
   */
  loginByAlipay,
  /**
   * QQ登录
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#login-by-qq
   * @param {Object} params
   * @param {String} params.code                  QQ小程序登录返回的code参数
   * @param {String} params.accessToken           App端QQ登录返回的accessToken参数
   * @param {String} params.accessTokenExpired    accessToken过期时间,由App端QQ登录返回的expires_in参数计算而来,单位:毫秒
   * @param {String} params.inviteCode            邀请码
   * @returns
   */
  loginByQQ,
  /**
   * 苹果登录
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#login-by-apple
   * @param {Object} params
   * @param {String} params.identityToken 苹果登录返回的identityToken
   * @param {String} params.nickname      用户昵称
   * @param {String} params.inviteCode    邀请码
   * @returns
   */
  loginByApple,
  loginByWeixinMobile,
  /**
   * 用户退出登录
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#logout
   * @returns
   */
  logout,
  /**
   * 通过短信验证码绑定手机号
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-mobile-by-sms
   * @param {Object} params
   * @param {String} params.mobile    手机号
   * @param {String} params.code      短信验证码
   * @param {String} params.captcha   图形验证码
   * @returns
   */
  bindMobileBySms,
  /**
   * 通过一键登录绑定手机号
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-mobile-by-univerify
   * @param {Object} params
   * @param {String} params.openid        APP端一键登录返回的openid
   * @param {String} params.access_token  APP端一键登录返回的access_token
   * @returns
   */
  bindMobileByUniverify,
  /**
   * 通过微信绑定手机号
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-mobile-by-mp-weixin
   * @param {Object} params
   * @param {String} params.encryptedData   微信获取手机号返回的加密信息
   * @param {String} params.iv              微信获取手机号返回的初始向量
   * @returns
   */
  bindMobileByMpWeixin,
  /**
   * 绑定微信
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-weixin
   * @param {Object} params
   * @param {String} params.code  微信登录返回的code
   * @returns
   */
  bindWeixin,
  /**
   * 绑定QQ
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-qq
   * @param {Object} params
   * @param {String} params.code          小程序端QQ登录返回的code
   * @param {String} params.accessToken   APP端QQ登录返回的accessToken
   * @param {String} params.accessTokenExpired    accessToken过期时间,由App端QQ登录返回的expires_in参数计算而来,单位:毫秒
   * @returns
   */
  bindQQ,
  /**
   * 绑定支付宝账号
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-alipay
   * @param {Object} params
   * @param {String} params.code  支付宝小程序登录返回的code参数
   * @returns
   */
  bindAlipay,
  /**
   * 绑定苹果账号
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-apple
   * @param {Object} params
   * @param {String} params.identityToken 苹果登录返回identityToken
   * @returns
   */
  bindApple,
  /**
   * 更新密码
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#update-pwd
   * @param {object} params
   * @param {string} params.oldPassword 旧密码
   * @param {string} params.newPassword 新密码
   * @returns {object}
   */
  updatePwd,
  /**
   * 通过短信验证码重置密码
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#reset-pwd-by-sms
   * @param {object} params
   * @param {string} params.mobile   手机号
   * @param {string} params.mobile   短信验证码
   * @param {string} params.password 密码
   * @param {string} params.captcha  图形验证码
   * @returns {object}
   */
  resetPwdBySms,
  /**
   * 通过邮箱验证码重置密码
   * @param {object} params
   * @param {string} params.email   邮箱
   * @param {string} params.code   邮箱验证码
   * @param {string} params.password 密码
   * @param {string} params.captcha  图形验证码
   * @returns {object}
   */
  resetPwdByEmail,
  /**
   * 注销账户
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#close-account
   * @returns
   */
  closeAccount,
  /**
   * 获取账户账户简略信息
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#get-account-info
   */
  getAccountInfo,
  /**
   * 创建图形验证码
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#create-captcha
   * @param {Object} params
   * @param {String} params.scene   图形验证码使用场景
   * @returns
   */
  createCaptcha,
  /**
   * 刷新图形验证码
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#refresh-captcha
   * @param {Object} params
   * @param {String} params.scene   图形验证码使用场景
   * @returns
   */
  refreshCaptcha,
  /**
   * 发送短信验证码
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#send-sms-code
   * @param {Object} params
   * @param {String} params.mobile    手机号
   * @param {String} params.captcha   图形验证码
   * @param {String} params.scene     短信验证码使用场景
   * @returns
   */
  sendSmsCode,
  /**
   * 发送邮箱验证码
   * @tutorial 需自行实现功能
   * @param {Object} params
   * @param {String} params.email    邮箱
   * @param {String} params.captcha   图形验证码
   * @param {String} params.scene     短信验证码使用场景
   * @returns
   */
  sendEmailCode,
  /**
   * 刷新token
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#refresh-token
   */
  refreshToken,
  /**
   * 接受邀请
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#accept-invite
   * @param {Object} params
   * @param {String} params.inviteCode  邀请码
   * @returns
   */
  acceptInvite,
  /**
   * 获取受邀用户
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#get-invited-user
   * @param {Object} params
   * @param {Number} params.level       获取受邀用户的级数,1表示直接邀请的用户
   * @param {Number} params.limit       返回数据大小
   * @param {Number} params.offset      返回数据偏移
   * @param {Boolean} params.needTotal  是否需要返回总数
   * @returns
   */
  getInvitedUser,
  /**
   * 更新device表的push_clien_id
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#set-push-cid
   * @param {object} params
   * @param {string} params.pushClientId  客户端pushClientId
   * @returns
   */
  setPushCid,
  /**
   * 获取支持的登录方式
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#get-supported-login-type
   * @returns
   */
  getSupportedLoginType,
 
  /**
   * 解绑微信
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#unbind-weixin
   * @returns
   */
  unbindWeixin,
  /**
   * 解绑支付宝
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#unbind-alipay
   * @returns
   */
  unbindAlipay,
  /**
   * 解绑QQ
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#unbind-qq
   * @returns
   */
  unbindQQ,
  /**
   * 解绑Apple
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#unbind-apple
   * @returns
   */
  unbindApple,
  /**
   * 安全网络握手,目前仅处理微信小程序安全网络握手
   */
  secureNetworkHandshakeByWeixin,
  /**
   * 设置密码
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#set-pwd
   * @returns
   */
  setPwd,
  /**
   * 外部注册用户
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#external-register
   * @param {object} params
   * @param {string} params.externalUid   业务系统的用户id
   * @param {string} params.nickname  昵称
   * @param {string} params.gender  性别
   * @param {string} params.avatar  头像
   * @returns {object}
   */
  externalRegister,
  /**
   * 外部用户登录
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#external-login
   * @param {object} params
   * @param {string} params.userId  uni-id体系用户id
   * @param {string} params.externalUid   业务系统的用户id
   * @returns {object}
   */
  externalLogin,
  /**
   * 使用 userId 或 externalUid 获取用户信息
   * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#external-update-userinfo
   * @param {object} params
   * @param {string} params.userId   uni-id体系的用户id
   * @param {string} params.externalUid   业务系统的用户id
   * @param {string} params.nickname  昵称
   * @param {string} params.gender  性别
   * @param {string} params.avatar  头像
   * @returns {object}
   */
  updateUserInfoByExternal
}