index.obj.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. const uniIdCommon = require('uni-id-common')
  2. const uniCaptcha = require('uni-captcha')
  3. const {
  4. getType
  5. } = require('./common/utils')
  6. const {
  7. checkClientInfo,
  8. Validator
  9. } = require('./common/validator')
  10. const ConfigUtils = require('./lib/utils/config')
  11. const {
  12. isUniIdError
  13. } = require('./common/error')
  14. const middleware = require('./middleware/index')
  15. const {
  16. registerAdmin,
  17. registerUser,
  18. registerUserByEmail
  19. } = require('./module/register/index')
  20. const {
  21. addUser,
  22. updateUser
  23. } = require('./module/admin/index')
  24. const {
  25. login,
  26. loginBySms,
  27. loginByUniverify,
  28. loginByWeixin,
  29. loginByAlipay,
  30. loginByQQ,
  31. loginByApple
  32. } = require('./module/login/index')
  33. const {
  34. logout
  35. } = require('./module/logout/index')
  36. const {
  37. bindMobileBySms,
  38. bindMobileByUniverify,
  39. bindMobileByMpWeixin,
  40. bindAlipay,
  41. bindApple,
  42. bindQQ,
  43. bindWeixin
  44. } = require('./module/relate/index')
  45. const {
  46. updatePwd,
  47. resetPwdBySms,
  48. resetPwdByEmail,
  49. closeAccount,
  50. getAccountInfo
  51. } = require('./module/account/index')
  52. const {
  53. createCaptcha,
  54. refreshCaptcha,
  55. sendSmsCode,
  56. sendEmailCode
  57. } = require('./module/verify/index')
  58. const {
  59. refreshToken,
  60. setPushCid
  61. } = require('./module/utils/index')
  62. const {
  63. getInvitedUser,
  64. acceptInvite
  65. } = require('./module/fission')
  66. const {
  67. authorizeAppLogin,
  68. removeAuthorizedApp,
  69. setAuthorizedApp
  70. } = require('./module/multi-end')
  71. const {
  72. getSupportedLoginType
  73. } = require('./module/dev/index')
  74. module.exports = {
  75. async _before() {
  76. const clientInfo = this.getClientInfo()
  77. /**
  78. * 检查clientInfo,无appId和uniPlatform时本云对象无法正常运行
  79. * 此外需要保证用到的clientInfo字段均经过类型检查
  80. * clientInfo由客户端上传并非完全可信,clientInfo内除clientIP、userAgent、source外均为客户端上传参数
  81. * 否则可能会出现一些意料外的情况
  82. */
  83. checkClientInfo(clientInfo)
  84. let clientPlatform = clientInfo.uniPlatform
  85. // 统一platform名称
  86. switch (clientPlatform) {
  87. case 'app':
  88. case 'app-plus':
  89. clientPlatform = 'app'
  90. break
  91. case 'web':
  92. case 'h5':
  93. clientPlatform = 'web'
  94. break
  95. default:
  96. break
  97. }
  98. this.clientPlatform = clientPlatform
  99. // 挂载uni-id实例到this上,方便后续调用
  100. this.uniIdCommon = uniIdCommon.createInstance({
  101. clientInfo
  102. })
  103. // 包含uni-id配置合并等功能的工具集
  104. this.configUtils = new ConfigUtils({
  105. context: this
  106. })
  107. this.config = this.configUtils.getPlatformConfig()
  108. this.hooks = this.configUtils.getHooks()
  109. this.validator = new Validator({
  110. passwordStrength: this.config.passwordStrength
  111. })
  112. /**
  113. * 示例:覆盖密码验证规则
  114. */
  115. // this.validator.mixin('password', function (password) {
  116. // if (typeof password !== 'string' || password.length < 10) {
  117. // // 调整为密码长度不能小于10
  118. // return {
  119. // errCode: ERROR.INVALID_PASSWORD
  120. // }
  121. // }
  122. // })
  123. /**
  124. * 示例:新增验证规则
  125. */
  126. // this.validator.mixin('timestamp', function (timestamp) {
  127. // if (typeof timestamp !== 'number' || timestamp > Date.now()) {
  128. // return {
  129. // errCode: ERROR.INVALID_PARAM
  130. // }
  131. // }
  132. // })
  133. // // 新增规则同样可以在数组验证规则中使用
  134. // this.validator.valdate({
  135. // timestamp: 123456789
  136. // }, {
  137. // timestamp: 'timestamp'
  138. // })
  139. // this.validator.valdate({
  140. // timestampList: [123456789, 123123123123]
  141. // }, {
  142. // timestampList: 'array<timestamp>'
  143. // })
  144. // // 甚至更复杂的写法
  145. // this.validator.valdate({
  146. // timestamp: [123456789, 123123123123]
  147. // }, {
  148. // timestamp: 'timestamp|array<timestamp>'
  149. // })
  150. // 挂载uni-captcha到this上,方便后续调用
  151. this.uniCaptcha = uniCaptcha
  152. Object.defineProperty(this, 'uniOpenBridge', {
  153. get() {
  154. return require('uni-open-bridge-common')
  155. }
  156. })
  157. // 挂载中间件
  158. this.middleware = {}
  159. for (const mwName in middleware) {
  160. this.middleware[mwName] = middleware[mwName].bind(this)
  161. }
  162. // 国际化
  163. const i18n = uniCloud.initI18n({
  164. locale: clientInfo.locale,
  165. fallbackLocale: 'zh-Hans',
  166. messages: require('./lang/index')
  167. })
  168. this.t = i18n.t.bind(i18n)
  169. this.response = {}
  170. // 通用权限校验模块
  171. await this.middleware.accessControl()
  172. },
  173. _after(error, result) {
  174. if (error) {
  175. // 处理中间件内抛出的标准响应对象
  176. if (error.errCode && getType(error) === 'object') {
  177. const errCode = error.errCode
  178. if (!isUniIdError(errCode)) {
  179. return error
  180. }
  181. return {
  182. errCode,
  183. errMsg: error.errMsg || this.t(errCode, error.errMsgValue)
  184. }
  185. }
  186. throw error
  187. }
  188. return Object.assign(this.response, result)
  189. },
  190. /**
  191. * 注册管理员
  192. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#register-admin
  193. * @param {Object} params
  194. * @param {String} params.username 用户名
  195. * @param {String} params.password 密码
  196. * @param {String} params.nickname 昵称
  197. * @returns
  198. */
  199. registerAdmin,
  200. /**
  201. * 新增用户
  202. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#add-user
  203. * @param {Object} params
  204. * @param {String} params.username 用户名
  205. * @param {String} params.password 密码
  206. * @param {String} params.nickname 昵称
  207. * @param {Array} params.authorizedApp 允许登录的AppID列表
  208. * @param {Array} params.role 用户角色列表
  209. * @returns
  210. */
  211. addUser,
  212. /**
  213. * 修改用户
  214. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#update-user
  215. * @param {Object} params
  216. * @param {String} params.id 要更新的用户id
  217. * @param {String} params.username 用户名
  218. * @param {String} params.password 密码
  219. * @param {String} params.nickname 昵称
  220. * @param {Array} params.authorizedApp 允许登录的AppID列表
  221. * @param {Array} params.role 用户角色列表
  222. * @param {String} params.mobile 手机号
  223. * @param {String} params.email 邮箱
  224. * @param {Array} params.tags 用户标签
  225. * @param {Number} params.status 用户状态
  226. * @returns
  227. */
  228. updateUser,
  229. /**
  230. * 授权用户登录应用
  231. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#authorize-app-login
  232. * @param {Object} params
  233. * @param {String} params.uid 用户id
  234. * @param {String} params.appId 授权的应用的AppId
  235. * @returns
  236. */
  237. authorizeAppLogin,
  238. /**
  239. * 移除用户登录授权
  240. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#remove-authorized-app
  241. * @param {Object} params
  242. * @param {String} params.uid 用户id
  243. * @param {String} params.appId 取消授权的应用的AppId
  244. * @returns
  245. */
  246. removeAuthorizedApp,
  247. /**
  248. * 设置用户允许登录的应用列表
  249. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#set-authorized-app
  250. * @param {Object} params
  251. * @param {String} params.uid 用户id
  252. * @param {Array} params.appIdList 允许登录的应用AppId列表
  253. * @returns
  254. */
  255. setAuthorizedApp,
  256. /**
  257. * 注册普通用户
  258. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#register-user
  259. * @param {Object} params
  260. * @param {String} params.username 用户名
  261. * @param {String} params.password 密码
  262. * @param {String} params.captcha 图形验证码
  263. * @param {String} params.nickname 昵称
  264. * @param {String} params.inviteCode 邀请码
  265. * @returns
  266. */
  267. registerUser,
  268. /**
  269. * 通过邮箱+验证码注册用户
  270. * @param {Object} params
  271. * @param {String} params.email 邮箱
  272. * @param {String} params.password 密码
  273. * @param {String} params.nickname 昵称
  274. * @param {String} params.code 邮箱验证码
  275. * @param {String} params.inviteCode 邀请码
  276. * @returns
  277. */
  278. registerUserByEmail,
  279. /**
  280. * 用户名密码登录
  281. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#login
  282. * @param {Object} params
  283. * @param {String} params.username 用户名
  284. * @param {String} params.mobile 手机号
  285. * @param {String} params.email 邮箱
  286. * @param {String} params.password 密码
  287. * @param {String} params.captcha 图形验证码
  288. * @returns
  289. */
  290. login,
  291. /**
  292. * 短信验证码登录
  293. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#login-by-sms
  294. * @param {Object} params
  295. * @param {String} params.mobile 手机号
  296. * @param {String} params.code 短信验证码
  297. * @param {String} params.captcha 图形验证码
  298. * @param {String} params.inviteCode 邀请码
  299. * @returns
  300. */
  301. loginBySms,
  302. /**
  303. * App端一键登录
  304. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#login-by-univerify
  305. * @param {Object} params
  306. * @param {String} params.access_token APP端一键登录返回的access_token
  307. * @param {String} params.openid APP端一键登录返回的openid
  308. * @param {String} params.inviteCode 邀请码
  309. * @returns
  310. */
  311. loginByUniverify,
  312. /**
  313. * 微信登录
  314. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#login-by-weixin
  315. * @param {Object} params
  316. * @param {String} params.code 微信登录返回的code
  317. * @param {String} params.inviteCode 邀请码
  318. * @returns
  319. */
  320. loginByWeixin,
  321. /**
  322. * 支付宝登录
  323. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#login-by-alipay
  324. * @param {Object} params
  325. * @param {String} params.code 支付宝小程序客户端登录返回的code
  326. * @param {String} params.inviteCode 邀请码
  327. * @returns
  328. */
  329. loginByAlipay,
  330. /**
  331. * QQ登录
  332. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#login-by-qq
  333. * @param {Object} params
  334. * @param {String} params.code QQ小程序登录返回的code参数
  335. * @param {String} params.accessToken App端QQ登录返回的accessToken参数
  336. * @param {String} params.accessTokenExpired accessToken过期时间,由App端QQ登录返回的expires_in参数计算而来,单位:毫秒
  337. * @param {String} params.inviteCode 邀请码
  338. * @returns
  339. */
  340. loginByQQ,
  341. /**
  342. * 苹果登录
  343. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#login-by-apple
  344. * @param {Object} params
  345. * @param {String} params.identityToken 苹果登录返回的identityToken
  346. * @param {String} params.nickname 用户昵称
  347. * @param {String} params.inviteCode 邀请码
  348. * @returns
  349. */
  350. loginByApple,
  351. /**
  352. * 用户退出登录
  353. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#logout
  354. * @returns
  355. */
  356. logout,
  357. /**
  358. * 通过短信验证码绑定手机号
  359. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-mobile-by-sms
  360. * @param {Object} params
  361. * @param {String} params.mobile 手机号
  362. * @param {String} params.code 短信验证码
  363. * @param {String} params.captcha 图形验证码
  364. * @returns
  365. */
  366. bindMobileBySms,
  367. /**
  368. * 通过一键登录绑定手机号
  369. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-mobile-by-univerify
  370. * @param {Object} params
  371. * @param {String} params.openid APP端一键登录返回的openid
  372. * @param {String} params.access_token APP端一键登录返回的access_token
  373. * @returns
  374. */
  375. bindMobileByUniverify,
  376. /**
  377. * 通过微信绑定手机号
  378. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-mobile-by-mp-weixin
  379. * @param {Object} params
  380. * @param {String} params.encryptedData 微信获取手机号返回的加密信息
  381. * @param {String} params.iv 微信获取手机号返回的初始向量
  382. * @returns
  383. */
  384. bindMobileByMpWeixin,
  385. /**
  386. * 绑定微信
  387. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-weixin
  388. * @param {Object} params
  389. * @param {String} params.code 微信登录返回的code
  390. * @returns
  391. */
  392. bindWeixin,
  393. /**
  394. * 绑定QQ
  395. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-qq
  396. * @param {Object} params
  397. * @param {String} params.code 小程序端QQ登录返回的code
  398. * @param {String} params.accessToken APP端QQ登录返回的accessToken
  399. * @param {String} params.accessTokenExpired accessToken过期时间,由App端QQ登录返回的expires_in参数计算而来,单位:毫秒
  400. * @returns
  401. */
  402. bindQQ,
  403. /**
  404. * 绑定支付宝账号
  405. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-alipay
  406. * @param {Object} params
  407. * @param {String} params.code 支付宝小程序登录返回的code参数
  408. * @returns
  409. */
  410. bindAlipay,
  411. /**
  412. * 绑定苹果账号
  413. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-apple
  414. * @param {Object} params
  415. * @param {String} params.identityToken 苹果登录返回identityToken
  416. * @returns
  417. */
  418. bindApple,
  419. /**
  420. * 更新密码
  421. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#update-pwd
  422. * @param {object} params
  423. * @param {string} params.oldPassword 旧密码
  424. * @param {string} params.newPassword 新密码
  425. * @returns {object}
  426. */
  427. updatePwd,
  428. /**
  429. * 通过短信验证码重置密码
  430. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#reset-pwd-by-sms
  431. * @param {object} params
  432. * @param {string} params.mobile 手机号
  433. * @param {string} params.mobile 短信验证码
  434. * @param {string} params.password 密码
  435. * @param {string} params.captcha 图形验证码
  436. * @returns {object}
  437. */
  438. resetPwdBySms,
  439. /**
  440. * 通过邮箱验证码重置密码
  441. * @param {object} params
  442. * @param {string} params.email 邮箱
  443. * @param {string} params.code 邮箱验证码
  444. * @param {string} params.password 密码
  445. * @param {string} params.captcha 图形验证码
  446. * @returns {object}
  447. */
  448. resetPwdByEmail,
  449. /**
  450. * 注销账户
  451. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#close-account
  452. * @returns
  453. */
  454. closeAccount,
  455. /**
  456. * 获取账户账户简略信息
  457. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#get-account-info
  458. */
  459. getAccountInfo,
  460. /**
  461. * 创建图形验证码
  462. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#create-captcha
  463. * @param {Object} params
  464. * @param {String} params.scene 图形验证码使用场景
  465. * @returns
  466. */
  467. createCaptcha,
  468. /**
  469. * 刷新图形验证码
  470. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#refresh-captcha
  471. * @param {Object} params
  472. * @param {String} params.scene 图形验证码使用场景
  473. * @returns
  474. */
  475. refreshCaptcha,
  476. /**
  477. * 发送短信验证码
  478. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#send-sms-code
  479. * @param {Object} params
  480. * @param {String} params.mobile 手机号
  481. * @param {String} params.captcha 图形验证码
  482. * @param {String} params.scene 短信验证码使用场景
  483. * @returns
  484. */
  485. sendSmsCode,
  486. /**
  487. * 发送邮箱验证码
  488. * @tutorial 需自行实现功能
  489. * @param {Object} params
  490. * @param {String} params.email 邮箱
  491. * @param {String} params.captcha 图形验证码
  492. * @param {String} params.scene 短信验证码使用场景
  493. * @returns
  494. */
  495. sendEmailCode,
  496. /**
  497. * 刷新token
  498. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#refresh-token
  499. */
  500. refreshToken,
  501. /**
  502. * 接受邀请
  503. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#accept-invite
  504. * @param {Object} params
  505. * @param {String} params.inviteCode 邀请码
  506. * @returns
  507. */
  508. acceptInvite,
  509. /**
  510. * 获取受邀用户
  511. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#get-invited-user
  512. * @param {Object} params
  513. * @param {Number} params.level 获取受邀用户的级数,1表示直接邀请的用户
  514. * @param {Number} params.limit 返回数据大小
  515. * @param {Number} params.offset 返回数据偏移
  516. * @param {Boolean} params.needTotal 是否需要返回总数
  517. * @returns
  518. */
  519. getInvitedUser,
  520. /**
  521. * 更新device表的push_clien_id
  522. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#set-push-cid
  523. * @param {object} params
  524. * @param {string} params.pushClientId 客户端pushClientId
  525. * @returns
  526. */
  527. setPushCid,
  528. /**
  529. * 获取支持的登录方式
  530. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#get-supported-login-type
  531. * @returns
  532. */
  533. getSupportedLoginType
  534. }