update-user.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. const {
  2. findUser
  3. } = require('../../lib/utils/account')
  4. const {
  5. ERROR
  6. } = require('../../common/error')
  7. const {
  8. userCollection
  9. } = require('../../common/constants')
  10. const PasswordUtils = require('../../lib/utils/password')
  11. /**
  12. * 修改用户
  13. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#update-user
  14. * @param {Object} params
  15. * @param {String} params.uid 要更新的用户id
  16. * @param {String} params.username 用户名
  17. * @param {String} params.password 密码
  18. * @param {String} params.nickname 昵称
  19. * @param {Array} params.authorizedApp 允许登录的AppID列表
  20. * @param {Array} params.role 用户角色列表
  21. * @param {String} params.mobile 手机号
  22. * @param {String} params.email 邮箱
  23. * @param {Array} params.tags 用户标签
  24. * @param {Number} params.status 用户状态
  25. * @returns
  26. */
  27. module.exports = async function (params = {}) {
  28. const schema = {
  29. uid: 'string',
  30. username: 'username',
  31. password: {
  32. required: false,
  33. type: 'password'
  34. },
  35. authorizedApp: {
  36. required: false,
  37. type: 'array<string>'
  38. }, // 指定允许登录的app,传空数组或不传时表示可以不可以在任何端登录
  39. nickname: {
  40. required: false,
  41. type: 'nickname'
  42. },
  43. role: {
  44. require: false,
  45. type: 'array<string>'
  46. },
  47. mobile: {
  48. required: false,
  49. type: 'mobile'
  50. },
  51. email: {
  52. required: false,
  53. type: 'email'
  54. },
  55. tags: {
  56. required: false,
  57. type: 'array<string>'
  58. },
  59. status: {
  60. required: false,
  61. type: 'number'
  62. }
  63. }
  64. this.middleware.validate(params, schema)
  65. const {
  66. uid,
  67. username,
  68. password,
  69. authorizedApp,
  70. nickname,
  71. role,
  72. mobile,
  73. email,
  74. tags,
  75. status
  76. } = params
  77. // 更新的用户数据字段
  78. const data = {
  79. username,
  80. dcloud_appid: authorizedApp,
  81. nickname,
  82. role,
  83. mobile,
  84. email,
  85. tags,
  86. status
  87. }
  88. const realData = Object.keys(data).reduce((res, key) => {
  89. const item = data[key]
  90. if (item !== undefined) {
  91. res[key] = item
  92. }
  93. return res
  94. }, {})
  95. // 更新用户名时验证用户名是否重新
  96. if (username) {
  97. const {
  98. userMatched
  99. } = await findUser({
  100. userQuery: {
  101. username
  102. },
  103. authorizedApp
  104. })
  105. if (userMatched.filter(user => user._id !== uid).length) {
  106. throw {
  107. errCode: ERROR.ACCOUNT_EXISTS
  108. }
  109. }
  110. }
  111. if (password) {
  112. const passwordUtils = new PasswordUtils({
  113. clientInfo: this.getUniversalClientInfo(),
  114. passwordSecret: this.config.passwordSecret
  115. })
  116. const {
  117. passwordHash,
  118. version
  119. } = passwordUtils.generatePasswordHash({
  120. password
  121. })
  122. realData.password = passwordHash
  123. realData.password_secret_version = version
  124. }
  125. await userCollection.doc(uid).update(realData)
  126. return {
  127. errCode: 0
  128. }
  129. }