kyy
2025-02-11 bb05da47c66341aa5d65c965528271d03f37a1e1
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
'use strict';
 
const {
  ProviderType
} = require('./consts.js')
 
const configCenter = require('uni-config-center')
 
// 多维数据为兼容uni-id以前版本配置
const OauthConfig = {
  'weixin-app': [
    ['app', 'oauth', 'weixin'],
    ['app-plus', 'oauth', 'weixin']
  ],
  'weixin-mp': [
    ['mp-weixin', 'oauth', 'weixin']
  ],
  'weixin-h5': [
    ['web', 'oauth', 'weixin-h5'],
    ['h5-weixin', 'oauth', 'weixin'],
    ['h5', 'oauth', 'weixin']
  ],
  'weixin-web': [
    ['web', 'oauth', 'weixin-web']
  ],
  'qq-app': [
    ['app', 'oauth', 'qq'],
    ['app-plus', 'oauth', 'qq']
  ],
  'qq-mp': [
    ['mp-qq', 'oauth', 'qq']
  ]
}
 
const Support_Platforms = [
  ProviderType.WEIXIN_MP,
  ProviderType.WEIXIN_H5,
  ProviderType.WEIXIN_APP,
  ProviderType.WEIXIN_WEB,
  ProviderType.QQ_MP,
  ProviderType.QQ_APP
]
 
class ConfigBase {
 
  constructor() {
    const uniIdConfigCenter = configCenter({
      pluginId: 'uni-id'
    })
 
    this._uniIdConfig = uniIdConfigCenter.config()
  }
 
  getAppConfig(appid) {
    if (Array.isArray(this._uniIdConfig)) {
      return this._uniIdConfig.find((item) => {
        return (item.dcloudAppid === appid)
      })
    }
    return this._uniIdConfig
  }
}
 
class AppConfig extends ConfigBase {
 
  constructor() {
    super()
  }
 
  get(appid, platform) {
    if (!this.isSupport(platform)) {
      return null
    }
 
    let appConfig = this.getAppConfig(appid)
    if (!appConfig) {
      return null
    }
 
    return this.getOauthConfig(appConfig, platform)
  }
 
  isSupport(platformName) {
    return (Support_Platforms.indexOf(platformName) >= 0)
  }
 
  getOauthConfig(appConfig, platformName) {
    let treePath = OauthConfig[platformName]
    let node = this.findNode(appConfig, treePath)
    if (node && node.appid && node.appsecret) {
      return {
        appid: node.appid,
        secret: node.appsecret
      }
    }
    return null
  }
 
  findNode(treeNode, arrayPath) {
    let node = treeNode
    for (let treePath of arrayPath) {
      for (let name of treePath) {
        const currentNode = node[name]
        if (currentNode) {
          node = currentNode
        } else {
          node = null
          break
        }
      }
      if (node === null) {
        node = treeNode
      } else {
        break
      }
    }
    return node
  }
}
 
 
module.exports = {
  AppConfig
};