cshenfu
2023-05-08 2469d92f4c189eb595216707f4b4f0786fadb23f
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
'use strict';
 
const {
  PlatformType
} = require('./consts.js')
 
const configCenter = require('uni-config-center')
 
const OauthConfig = {
  'mp-weixin': ['oauth', 'weixin'],
  'h5-weixin': ['oauth', 'weixin']
}
 
class TaskConfig {
 
  constructor(options) {
    this._dcloudAppid = options.dcloudAppid
    this._appid = options.appid
    this._secret = options.secret
    this._platform = options.platform
    this._tasks = options.tasks
    this._timeout = 1000 * 10
  }
 
  get dcloudAppid() {
    return this._dcloudAppid
  }
 
  get appid() {
    return this._appid
  }
 
  get secret() {
    return this._secret
  }
 
  get platform() {
    return this._platform
  }
 
  get tasks() {
    return this._tasks
  }
}
 
class ConfigBase {
 
  constructor() {
    const uniIdConfig = configCenter({
      pluginId: 'uni-id'
    })
    const openBridgeConfig = configCenter({
      pluginId: 'uni-open-bridge'
    })
 
    this._uniId = uniIdConfig.config()
    this._openBridge = openBridgeConfig.config()
 
    this._ready = true
  }
 
  getAppConfig(appid) {
    if (Array.isArray(this._uniId)) {
      return this._uniId.find((item) => {
        return (item.dcloudAppid === appid)
      })
    }
 
    if (this._uniId.dcloudAppid === appid) {
      return this._uniId
    }
 
    return null
  }
 
  inWhitelist(ip) {
    return (this.ipWhitelist.indexOf(ip) > -1)
  }
 
  get openBridge() {
    return this._openBridge
  }
 
  get ipWhitelist() {
    return this._openBridge.ipWhitelist
  }
 
  get ready() {
    return this._ready
  }
}
 
class OpenBridgeConfig extends ConfigBase {
 
  constructor() {
    super()
 
    this._tasks = []
 
    this.resolve()
  }
 
  get tasks() {
    return this._tasks
  }
 
  resolve() {
    this._tasks.length = 0
 
    const appids = Object.keys(this.openBridge.schedule)
 
    for (let i = 0; i < appids.length; i++) {
      const appid = appids[i]
      let appConfig = this.getAppConfig(appid)
 
      if (appConfig != null) {
        const schedule = this.openBridge.schedule[appid]
        if (schedule) {
          this.resolveSchedule(appid, appConfig, schedule)
        }
      }
    }
  }
 
  resolveSchedule(dcloudAppid, appConfig, schedule) {
    if (schedule.enable !== true) {
      return
    }
 
    const schedulePlatforms = Object.keys(schedule)
 
    for (let i = 0; i < schedulePlatforms.length; i++) {
      const platformName = schedulePlatforms[i]
      const scheduleTask = schedule[platformName]
 
      if (!scheduleTask.enable) {
        continue
      }
 
      if (!this.isSupport(platformName)) {
        continue
      }
 
      const oauthConfig = this.getOauthConfig(appConfig, platformName)
      if (!oauthConfig) {
        continue
      }
 
      this._tasks.push({
        platform: platformName,
        tasks: scheduleTask.tasks,
        dcloudAppid: dcloudAppid,
        appid: oauthConfig.appid,
        secret: oauthConfig.secret
      })
    }
  }
 
  isSupport(platformName) {
    return (OpenBridgeConfig.Support_Platforms.indexOf(platformName) >= 0)
  }
 
  getOauthConfig(appConfig, platformName) {
    const platformConfig = appConfig[platformName]
    if (!platformConfig) {
      return null
    }
 
    let tree = OauthConfig[platformName]
    let node = platformConfig
    for (let i = 0; i < tree.length; i++) {
      let nodeName = tree[i]
      if (node[nodeName]) {
        node = node[nodeName]
      } else {
        node = null
        break
      }
    }
 
    if (node && node.appid && node.appsecret) {
      return {
        appid: node.appid,
        secret: node.appsecret
      }
    }
 
    return null
  }
}
 
OpenBridgeConfig.Support_Platforms = ['mp-weixin', 'h5-weixin']
 
 
module.exports = {
  OpenBridgeConfig
};