config.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. 'use strict';
  2. const {
  3. PlatformType
  4. } = require('./consts.js')
  5. const configCenter = require('uni-config-center')
  6. const OauthConfig = {
  7. 'weixin-mp': ['mp-weixin', 'oauth', 'weixin'],
  8. 'weixin-h5': ['web', 'oauth', 'weixin-h5']
  9. }
  10. class ConfigBase {
  11. constructor() {
  12. this._ready = false
  13. this._uniId = null
  14. const uniIdConfig = configCenter({
  15. pluginId: 'uni-id'
  16. })
  17. this._uniId = uniIdConfig.config()
  18. this._ready = true
  19. }
  20. getAppConfig(appid) {
  21. if (Array.isArray(this._uniId)) {
  22. return this._uniId.find((item) => {
  23. return (item.dcloudAppid === appid)
  24. })
  25. }
  26. return this._uniId
  27. }
  28. get ready() {
  29. return this._ready
  30. }
  31. }
  32. class AppConfig extends ConfigBase {
  33. constructor() {
  34. super()
  35. }
  36. get(appid, platform) {
  37. if (!this.isSupport(platform)) {
  38. return null
  39. }
  40. let appConfig = this.getAppConfig(appid)
  41. if (!appConfig) {
  42. return null
  43. }
  44. return this.getOauthConfig(appConfig, platform)
  45. }
  46. isSupport(platformName) {
  47. return (AppConfig.Support_Platforms.indexOf(platformName) >= 0)
  48. }
  49. getOauthConfig(appConfig, platformName) {
  50. let tree = OauthConfig[platformName]
  51. let node = appConfig
  52. for (let i = 0; i < tree.length; i++) {
  53. let nodeName = tree[i]
  54. if (node[nodeName]) {
  55. node = node[nodeName]
  56. } else {
  57. node = null
  58. break
  59. }
  60. }
  61. if (node && node.appid && node.appsecret) {
  62. return {
  63. appid: node.appid,
  64. secret: node.appsecret
  65. }
  66. }
  67. return null
  68. }
  69. }
  70. AppConfig.Support_Platforms = [PlatformType.WEIXIN_MP, PlatformType.WEIXIN_H5]
  71. module.exports = {
  72. AppConfig
  73. };