config.js 3.8 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 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 127 128 129 130 131 132 133 134 135
const {
  getWeixinPlatform
} = require('./weixin')
const createConfig = require('uni-config-center')

const requiredConfig = {
  'web.weixin-h5': ['appid', 'appsecret'],
  'web.weixin-web': ['appid', 'appsecret'],
  'app.weixin': ['appid', 'appsecret'],
  'mp-weixin.weixin': ['appid', 'appsecret'],
  'app.qq': ['appid', 'appsecret'],
  'mp-alipay.alipay': ['appid', 'privateKey'],
  'app.apple': ['bundleId']
}

const uniIdConfig = createConfig({
  pluginId: 'uni-id'
})

class ConfigUtils {
  constructor ({
    context
  } = {}) {
    this.context = context
    this.clientInfo = context.getClientInfo()
    const {
      appId,
      uniPlatform
    } = this.clientInfo
    this.appId = appId
    switch (uniPlatform) {
      case 'app':
      case 'app-plus':
        this.platform = 'app'
        break
      case 'web':
      case 'h5':
        this.platform = 'web'
        break
      default:
        this.platform = uniPlatform
        break
    }
  }

  getConfigArray () {
    let configContent
    try {
      configContent = require('uni-config-center/uni-id/config.json')
    } catch (error) {
      throw new Error('Invalid config file\n' + error.message)
    }
    if (configContent[0]) {
      return Object.values(configContent)
    }
    configContent.isDefaultConfig = true
    return [configContent]
  }

  getAppConfig () {
    const configArray = this.getConfigArray()
    return configArray.find(item => item.dcloudAppid === this.appId) || configArray.find(item => item.isDefaultConfig)
  }

  getPlatformConfig () {
    const appConfig = this.getAppConfig()
    if (!appConfig) {
      throw new Error(
        `Config for current app (${this.appId}) was not found, please check your config file or client appId`)
    }
    const platform = this.platform
    if (
      (this.platform === 'app' && appConfig['app-plus']) ||
      (this.platform === 'web' && appConfig.h5)
    ) {
      throw new Error(
        `Client platform is ${this.platform}, but ${this.platform === 'web' ? 'h5' : 'app-plus'} was found in config. Please refer to: https://uniapp.dcloud.net.cn/uniCloud/uni-id-summary?id=m-to-co`
      )
    }

    const defaultConfig = {
      tokenExpiresIn: 7200,
      tokenExpiresThreshold: 1200,
      passwordErrorLimit: 6,
      passwordErrorRetryTime: 3600
    }
    return Object.assign(defaultConfig, appConfig, appConfig[platform])
  }

  getOauthProvider ({
    provider
  } = {}) {
    const clientPlatform = this.platform
    let oatuhProivder = provider
    if (provider === 'weixin' && clientPlatform === 'web') {
      const weixinPlatform = getWeixinPlatform.call(this.context)
      if (weixinPlatform === 'h5' || weixinPlatform === 'web') {
        oatuhProivder = 'weixin-' + weixinPlatform // weixin-h5 公众号,weixin-web pc端
      }
    }
    return oatuhProivder
  }

  getOauthConfig ({
    provider
  } = {}) {
    const config = this.getPlatformConfig()
    const clientPlatform = this.platform
    const oatuhProivder = this.getOauthProvider({
      provider
    })
    const requireConfigKey = requiredConfig[`${clientPlatform}.${oatuhProivder}`] || []
    if (!config.oauth || !config.oauth[oatuhProivder]) {
      throw new Error(`Config param required: ${clientPlatform}.oauth.${oatuhProivder}`)
    }
    const oauthConfig = config.oauth[oatuhProivder]
    requireConfigKey.forEach((item) => {
      if (!oauthConfig[item]) {
        throw new Error(`Config param required: ${clientPlatform}.oauth.${oatuhProivder}.${item}`)
      }
    })
    return oauthConfig
  }

  getHooks () {
    if (uniIdConfig.hasFile('hooks/index.js')) {
      return require(
        uniIdConfig.resolve('hooks/index.js')
      )
    }
    return {}
  }
}

module.exports = ConfigUtils