config.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. 'use strict';
  2. const {
  3. ProviderType
  4. } = require('./consts.js')
  5. const configCenter = require('uni-config-center')
  6. // 多维数据为兼容uni-id以前版本配置
  7. const OauthConfig = {
  8. 'weixin-app': [
  9. ['app', 'oauth', 'weixin'],
  10. ['app-plus', 'oauth', 'weixin']
  11. ],
  12. 'weixin-mp': [
  13. ['mp-weixin', 'oauth', 'weixin']
  14. ],
  15. 'weixin-h5': [
  16. ['web', 'oauth', 'weixin-h5'],
  17. ['h5-weixin', 'oauth', 'weixin'],
  18. ['h5', 'oauth', 'weixin']
  19. ],
  20. 'weixin-web': [
  21. ['web', 'oauth', 'weixin-web']
  22. ],
  23. 'qq-app': [
  24. ['app', 'oauth', 'qq'],
  25. ['app-plus', 'oauth', 'qq']
  26. ],
  27. 'qq-mp': [
  28. ['mp-qq', 'oauth', 'qq']
  29. ]
  30. }
  31. const Support_Platforms = [
  32. ProviderType.WEIXIN_MP,
  33. ProviderType.WEIXIN_H5,
  34. ProviderType.WEIXIN_APP,
  35. ProviderType.WEIXIN_WEB,
  36. ProviderType.QQ_MP,
  37. ProviderType.QQ_APP
  38. ]
  39. class ConfigBase {
  40. constructor() {
  41. const uniIdConfigCenter = configCenter({
  42. pluginId: 'uni-id'
  43. })
  44. this._uniIdConfig = uniIdConfigCenter.config()
  45. }
  46. getAppConfig(appid) {
  47. if (Array.isArray(this._uniIdConfig)) {
  48. return this._uniIdConfig.find((item) => {
  49. return (item.dcloudAppid === appid)
  50. })
  51. }
  52. return this._uniIdConfig
  53. }
  54. }
  55. class AppConfig extends ConfigBase {
  56. constructor() {
  57. super()
  58. }
  59. get(appid, platform) {
  60. if (!this.isSupport(platform)) {
  61. return null
  62. }
  63. let appConfig = this.getAppConfig(appid)
  64. if (!appConfig) {
  65. return null
  66. }
  67. return this.getOauthConfig(appConfig, platform)
  68. }
  69. isSupport(platformName) {
  70. return (Support_Platforms.indexOf(platformName) >= 0)
  71. }
  72. getOauthConfig(appConfig, platformName) {
  73. let treePath = OauthConfig[platformName]
  74. let node = this.findNode(appConfig, treePath)
  75. if (node && node.appid && node.appsecret) {
  76. return {
  77. appid: node.appid,
  78. secret: node.appsecret
  79. }
  80. }
  81. return null
  82. }
  83. findNode(treeNode, arrayPath) {
  84. let node = treeNode
  85. for (let treePath of arrayPath) {
  86. for (let name of treePath) {
  87. const currentNode = node[name]
  88. if (currentNode) {
  89. node = currentNode
  90. } else {
  91. node = null
  92. break
  93. }
  94. }
  95. if (node === null) {
  96. node = treeNode
  97. } else {
  98. break
  99. }
  100. }
  101. return node
  102. }
  103. }
  104. module.exports = {
  105. AppConfig
  106. };