runtimeProcess.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. const myApi = require('./my')
  2. const utils = require('./utils.js')
  3. const globalVar = require('./config').global
  4. const hasProxy = typeof Proxy !== 'undefined'
  5. let _Proxy = function() { }
  6. if (hasProxy) { _Proxy = Proxy }
  7. let myProxy = null
  8. /**
  9. * runtime error catch
  10. */
  11. function warnApi(api) {
  12. const iscanIuse = globalVar.canIUse(api)
  13. // my下有此方法,但目前不可用
  14. if (!iscanIuse || api === 'getFileSystemManager') {
  15. utils.warn(
  16. `支付宝暂不支持${api}`,
  17. {
  18. apiName: api,
  19. errorType: 0,
  20. type: 'api',
  21. },
  22. )
  23. return function() {
  24. console.error(`支付宝暂不支持${api}`)
  25. }
  26. }
  27. }
  28. module.exports = function(obj = {}) {
  29. if (myProxy) {
  30. return myProxy
  31. }
  32. const _obj = Object.assign({}, obj, myApi)
  33. if (!hasProxy) {
  34. Object.keys(myApi)
  35. .forEach((attr) => {
  36. Object.defineProperty(_obj, attr, {
  37. get() {
  38. let ret
  39. if (myApi[attr]) {
  40. ret = function(o = {}, args = '') {
  41. if (args) {
  42. return myApi[attr].fn(o, args)
  43. }
  44. return myApi[attr].fn(o)
  45. }
  46. } else {
  47. const helpFn = warnApi(attr)
  48. ret = obj[attr] || helpFn
  49. }
  50. return ret
  51. },
  52. })
  53. })
  54. return _obj
  55. }
  56. myProxy = new _Proxy(_obj, {
  57. get(target, attr) {
  58. let ret
  59. if (typeof attr === 'string' && myApi[attr]) {
  60. if (typeof myApi[attr].fn === 'function') {
  61. ret = function(opts = {}, args = '') {
  62. if (args) {
  63. return myApi[attr].fn(opts, args)
  64. }
  65. return myApi[attr].fn(opts)
  66. }
  67. } else {
  68. ret = myApi[attr]
  69. }
  70. } else {
  71. const helpFn = warnApi(attr)
  72. ret = target[attr] || helpFn
  73. }
  74. return ret
  75. },
  76. })
  77. return myProxy
  78. }
  79. /**
  80. * for bindgetuserinfo open-type of button
  81. */
  82. myApi.getUserInfoWrap = {
  83. fn(e = {}, fn) {
  84. globalVar.getAuthCode({
  85. scopes: 'auth_user',
  86. success: () => {
  87. globalVar.getAuthUserInfo({
  88. success(userInfo) {
  89. fn && fn({
  90. ...e,
  91. detail: {
  92. userInfo,
  93. },
  94. })
  95. },
  96. })
  97. },
  98. fail(res) {
  99. fn && fn({
  100. ...e,
  101. detail: res,
  102. })
  103. },
  104. })
  105. },
  106. }
  107. /**
  108. * for bindgetphonenumber open-type of button
  109. */
  110. myApi.getPhoneNumberWrap = {
  111. fn(e = {}, fn) {
  112. globalVar.getPhoneNumber({
  113. success: (res) => {
  114. const encryptedData = res.response
  115. e = {
  116. ...e,
  117. detail: encryptedData,
  118. res,
  119. }
  120. fn && fn(e)
  121. },
  122. fail: (res) => {
  123. e = {
  124. ...e,
  125. detail: {},
  126. res,
  127. }
  128. fn && fn(e)
  129. },
  130. })
  131. },
  132. }