config.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. const {
  2. getWeixinPlatform
  3. } = require('./weixin')
  4. const createConfig = require('uni-config-center')
  5. const requiredConfig = {
  6. 'web.weixin-h5': ['appid', 'appsecret'],
  7. 'web.weixin-web': ['appid', 'appsecret'],
  8. 'app.weixin': ['appid', 'appsecret'],
  9. 'mp-weixin.weixin': ['appid', 'appsecret'],
  10. 'app.qq': ['appid', 'appsecret'],
  11. 'mp-alipay.alipay': ['appid', 'privateKey'],
  12. 'app.apple': ['bundleId']
  13. }
  14. const uniIdConfig = createConfig({
  15. pluginId: 'uni-id'
  16. })
  17. class ConfigUtils {
  18. constructor ({
  19. context
  20. } = {}) {
  21. this.context = context
  22. this.clientInfo = context.getClientInfo()
  23. const {
  24. appId,
  25. uniPlatform
  26. } = this.clientInfo
  27. this.appId = appId
  28. switch (uniPlatform) {
  29. case 'app':
  30. case 'app-plus':
  31. this.platform = 'app'
  32. break
  33. case 'web':
  34. case 'h5':
  35. this.platform = 'web'
  36. break
  37. default:
  38. this.platform = uniPlatform
  39. break
  40. }
  41. }
  42. getConfigArray () {
  43. let configContent
  44. try {
  45. configContent = require('uni-config-center/uni-id/config.json')
  46. } catch (error) {
  47. throw new Error('Invalid config file\n' + error.message)
  48. }
  49. if (configContent[0]) {
  50. return Object.values(configContent)
  51. }
  52. configContent.isDefaultConfig = true
  53. return [configContent]
  54. }
  55. getAppConfig () {
  56. const configArray = this.getConfigArray()
  57. return configArray.find(item => item.dcloudAppid === this.appId) || configArray.find(item => item.isDefaultConfig)
  58. }
  59. getPlatformConfig () {
  60. const appConfig = this.getAppConfig()
  61. if (!appConfig) {
  62. throw new Error(
  63. `Config for current app (${this.appId}) was not found, please check your config file or client appId`)
  64. }
  65. const platform = this.platform
  66. if (
  67. (this.platform === 'app' && appConfig['app-plus']) ||
  68. (this.platform === 'web' && appConfig.h5)
  69. ) {
  70. throw new Error(
  71. `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`
  72. )
  73. }
  74. const defaultConfig = {
  75. tokenExpiresIn: 7200,
  76. tokenExpiresThreshold: 1200,
  77. passwordErrorLimit: 6,
  78. passwordErrorRetryTime: 3600
  79. }
  80. return Object.assign(defaultConfig, appConfig, appConfig[platform])
  81. }
  82. getOauthProvider ({
  83. provider
  84. } = {}) {
  85. const clientPlatform = this.platform
  86. let oatuhProivder = provider
  87. if (provider === 'weixin' && clientPlatform === 'web') {
  88. const weixinPlatform = getWeixinPlatform.call(this.context)
  89. if (weixinPlatform === 'h5' || weixinPlatform === 'web') {
  90. oatuhProivder = 'weixin-' + weixinPlatform // weixin-h5 公众号,weixin-web pc端
  91. }
  92. }
  93. return oatuhProivder
  94. }
  95. getOauthConfig ({
  96. provider
  97. } = {}) {
  98. const config = this.getPlatformConfig()
  99. const clientPlatform = this.platform
  100. const oatuhProivder = this.getOauthProvider({
  101. provider
  102. })
  103. const requireConfigKey = requiredConfig[`${clientPlatform}.${oatuhProivder}`] || []
  104. if (!config.oauth || !config.oauth[oatuhProivder]) {
  105. throw new Error(`Config param required: ${clientPlatform}.oauth.${oatuhProivder}`)
  106. }
  107. const oauthConfig = config.oauth[oatuhProivder]
  108. requireConfigKey.forEach((item) => {
  109. if (!oauthConfig[item]) {
  110. throw new Error(`Config param required: ${clientPlatform}.oauth.${oatuhProivder}.${item}`)
  111. }
  112. })
  113. return oauthConfig
  114. }
  115. getHooks () {
  116. if (uniIdConfig.hasFile('hooks/index.js')) {
  117. return require(
  118. uniIdConfig.resolve('hooks/index.js')
  119. )
  120. }
  121. return {}
  122. }
  123. }
  124. module.exports = ConfigUtils