update-user.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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: role || [],
  83. mobile,
  84. email,
  85. tags: tags || [],
  86. status
  87. }
  88. // 更新用户名时验证用户名是否重新
  89. if (username) {
  90. const userMatched = await findUser({
  91. userQuery: {
  92. username
  93. },
  94. authorizedApp
  95. })
  96. if (userMatched.filter(user => user._id !== uid).length) {
  97. throw {
  98. errCode: ERROR.ACCOUNT_EXISTS
  99. }
  100. }
  101. }
  102. if (password) {
  103. const passwordUtils = new PasswordUtils({
  104. passwordSecret: this.config.passwordSecret
  105. })
  106. const {
  107. passwordHash,
  108. version
  109. } = passwordUtils.generatePasswordHash({
  110. password
  111. })
  112. data.password = passwordHash
  113. data.password_secret_version = version
  114. }
  115. await userCollection.doc(uid).update(data)
  116. return {
  117. errCode: 0
  118. }
  119. }