config.js 3.7 KB

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