index.obj.js 3.8 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1 2 3 4 5 6 7 8
const uniIdCommon = require('uni-id-common')
const uniCaptcha = require('uni-captcha')
const {
  checkClientInfo,
  getType
} = require('./common/utils')
const ConfigUtils = require('./lib/utils/config')
const {
雪洛's avatar
雪洛 已提交
9
  isUniIdError
DCloud_JSON's avatar
DCloud_JSON 已提交
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
} = require('./common/error')
const middleware = require('./middleware/index')

const {
  registerAdmin,
  registerUser
} = require('./module/register/index')
const {
  addUser
} = require('./module/admin/index')
const {
  login,
  loginBySms,
  loginByUniverify,
  loginByWeixin,
  loginByAlipay,
  loginByQQ,
  loginByApple
} = require('./module/login/index')
const {
  logout
} = require('./module/logout/index')
const {
  bindMobileBySms,
  bindMobileByUniverify,
  bindMobileByMpWeixin,
  bindAlipay,
  bindApple,
  bindQQ,
  bindWeixin
} = require('./module/relate/index')
const {
  updatePwd,
  resetPwdBySms,
  closeAccount
} = require('./module/account/index')
const {
  createCaptcha,
  refreshCaptcha,
  sendSmsCode
} = require('./module/verify/index')
const {
  refreshToken,
  setPushCid
} = require('./module/utils/index')
const {
  getInvitedUser,
  acceptInvite
} = require('./module/fission')
const {
  authorizeAppLogin,
  removeAuthorizedApp,
  setAuthorizedApp
} = require('./module/multi-end')
const {
  getSupportedLoginType
} = require('./module/dev/index')

module.exports = {
  async _before () {
    const clientInfo = this.getClientInfo()
    // 检查clientInfo,无appId和platform时本云对象无法正常运行
    checkClientInfo(clientInfo)
    let clientPlatform = clientInfo.platform
    switch (clientPlatform) {
      case 'app':
      case 'app-plus':
        clientPlatform = 'app'
        break
      case 'web':
      case 'h5':
        clientPlatform = 'h5'
        break
      default:
        break
    }

    this.clientPlatform = clientPlatform

    // 挂载uni-id实例到this上,方便后续调用
    this.uniIdCommon = uniIdCommon.createInstance({
      clientInfo
    })

    // 包含uni-id配置合并等功能的工具集
    this.config = new ConfigUtils({
      appId: clientInfo.appId,
      platform: clientPlatform
    }).getPlatformConfig()

    // 挂载uni-captcha到this上,方便后续调用
    this.uniCaptcha = uniCaptcha

    // 挂载中间件
    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.accessControl()
  },
  _after (error, result) {
    if (error) {
      // 处理中间件内抛出的标准响应对象
      if (error.errCode && getType(error) === 'object') {
        const errCode = error.errCode
雪洛's avatar
雪洛 已提交
127
        if (!isUniIdError(errCode)) {
DCloud_JSON's avatar
DCloud_JSON 已提交
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
          return error
        }
        return {
          errCode,
          errMsg: error.errMsg || this.t(errCode, error.errMsgValue)
        }
      }
      throw error
    }
    return Object.assign(this.response, result)
  },
  registerAdmin,
  addUser,
  authorizeAppLogin,
  removeAuthorizedApp,
  setAuthorizedApp,
  registerUser,
  login,
  loginBySms,
  loginByUniverify,
  loginByWeixin,
  loginByAlipay,
  loginByQQ,
  loginByApple,
  logout,
  bindMobileBySms,
  bindMobileByUniverify,
  bindMobileByMpWeixin,
  bindWeixin,
  bindQQ,
  bindAlipay,
  bindApple,
  updatePwd,
  resetPwdBySms,
  closeAccount,
  createCaptcha,
  refreshCaptcha,
  sendSmsCode,
  refreshToken,
  acceptInvite,
  getInvitedUser,
  setPushCid,
  getSupportedLoginType
}