update-user.js 3.1 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
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
const {
  findUser
} = require('../../lib/utils/account')
const {
  ERROR
} = require('../../common/error')
const {
  userCollection
} = require('../../common/constants')
const PasswordUtils = require('../../lib/utils/password')

/**
 * 修改用户
 * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#update-user
 * @param {Object}  params
 * @param {String}  params.uid            要更新的用户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
 */
module.exports = async function (params = {}) {
  const schema = {
    uid: 'string',
    username: 'username',
    password: {
      required: false,
      type: 'password'
    },
    authorizedApp: {
      required: false,
      type: 'array<string>'
    }, // 指定允许登录的app,传空数组或不传时表示可以不可以在任何端登录
    nickname: {
      required: false,
      type: 'nickname'
    },
    role: {
      require: false,
      type: 'array<string>'
    },
    mobile: {
      required: false,
      type: 'mobile'
    },
    email: {
      required: false,
      type: 'email'
    },
    tags: {
      required: false,
      type: 'array<string>'
    },
    status: {
      required: false,
      type: 'number'
    }
  }

  this.middleware.validate(params, schema)

  const {
    uid,
    username,
    password,
    authorizedApp,
    nickname,
    role,
    mobile,
    email,
    tags,
    status
  } = params

  // 更新的用户数据字段
  const data = {
    username,
84
    dcloud_appid: authorizedApp,
DCloud_JSON's avatar
DCloud_JSON 已提交
85
    nickname,
86
    role: role,
DCloud_JSON's avatar
DCloud_JSON 已提交
87 88
    mobile,
    email,
89
    tags: tags,
DCloud_JSON's avatar
DCloud_JSON 已提交
90 91 92
    status
  }

93 94 95 96 97 98 99 100
  const realData = Object.keys(data).reduce((res, key) => {
    const item = data[key]
    if (item) {
      res[key] = item
    }
    return res
  }, {})

DCloud_JSON's avatar
DCloud_JSON 已提交
101 102
  // 更新用户名时验证用户名是否重新
  if (username) {
103 104 105
    const {
      userMatched
    } = await findUser({
DCloud_JSON's avatar
DCloud_JSON 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119
      userQuery: {
        username
      },
      authorizedApp
    })
    if (userMatched.filter(user => user._id !== uid).length) {
      throw {
        errCode: ERROR.ACCOUNT_EXISTS
      }
    }
  }

  if (password) {
    const passwordUtils = new PasswordUtils({
study夏羽's avatar
study夏羽 已提交
120
      clientInfo: this.getUniversalClientInfo(),
DCloud_JSON's avatar
DCloud_JSON 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133
      passwordSecret: this.config.passwordSecret
    })
    const {
      passwordHash,
      version
    } = passwordUtils.generatePasswordHash({
      password
    })

    data.password = passwordHash
    data.password_secret_version = version
  }

134
  await userCollection.doc(uid).update(realData)
DCloud_JSON's avatar
DCloud_JSON 已提交
135 136 137 138 139

  return {
    errCode: 0
  }
}