家政小程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. const wxApi = {
  2. /**
  3. * @description 统一接口调用
  4. * @param {String} api 微信接口名称
  5. * @param {Object} options 微信接口传参
  6. */
  7. _api(api, options) {
  8. options = options || {};
  9. for (let i in options) {
  10. options[i] === null && delete options[i];
  11. }
  12. if (!wx[api]) return this._error('call', 'wxApi', api, 'not a wxapi');
  13. const success = options.success;
  14. options.success = res => {
  15. res.data && (res = res.data);
  16. success && success(res);
  17. };
  18. !options.fail && (options.fail = res => {
  19. this._error('api', api, res);
  20. });
  21. wx[api](options);
  22. },
  23. /**
  24. * @description 统一错误处理
  25. * @param {String} type 错误类型
  26. * @param {String} info 错误描述
  27. * @param {Object} res 具体错误数据
  28. * @param {Int} level 错误等级
  29. */
  30. _error(type, info, res, level) {
  31. console.log({
  32. type: type,
  33. info: info,
  34. res: res,
  35. level: level
  36. });
  37. },
  38. showToast(params, type) {
  39. params = {
  40. title: params.title || '',
  41. mask: params.mask || true,
  42. icon: params.icon || 'none'
  43. }
  44. if (type && type === 'loading') {
  45. this._api('showLoading', params);
  46. } else {
  47. params.duration = 2000;
  48. this._api('showToast', params);
  49. }
  50. },
  51. hideToast(type) {
  52. if (type && type === 'loading') {
  53. this._api('hideLoading');
  54. } else {
  55. this._api('hideToast');
  56. }
  57. },
  58. getLocation(cb) {
  59. const _this = this;
  60. this.getSetting({
  61. success(res) {
  62. if (!res.authSetting['scope.userLocation']) {
  63. _this.authorize({
  64. scope: 'scope.userLocation',
  65. success() {
  66. wx.getLocation({
  67. type: 'wgs84',
  68. success(res) {
  69. cb && cb(res);
  70. }
  71. });
  72. },
  73. fail(err) {
  74. global.router.goto({
  75. path: '/pages/auth/index',
  76. type: global.ROUTER_TYPE.CLOSE_ALL,
  77. query: {
  78. type: 'getLocation'
  79. }
  80. });
  81. }
  82. });
  83. } else {
  84. wx.getLocation({
  85. type: 'wgs84',
  86. success(res) {
  87. cb && cb(res);
  88. }
  89. });
  90. }
  91. },
  92. fail(err) {
  93. _this.showToast({
  94. title: '获取用户设置失败,请稍后重试'
  95. });
  96. }
  97. })
  98. },
  99. getSetting(params) {
  100. this._api('getSetting', params);
  101. },
  102. authorize(params) {
  103. this._api('authorize', params);
  104. },
  105. showConfirm(title, content, success, confirmText, cancelText = '取消', confirmColor = '', cancelColor = '') {
  106. this._api('showModal', {
  107. title: title,
  108. content: content,
  109. cancelText: cancelText,
  110. cancelColor: cancelColor,
  111. confirmText: confirmText,
  112. confirmColor: confirmColor,
  113. success: success
  114. });
  115. },
  116. openSetting(success) {
  117. this._api('openSetting', {
  118. success: success
  119. });
  120. },
  121. scanCode(params) {
  122. this._api('scanCode', params);
  123. },
  124. openLocation(params) {
  125. this._api('openLocation', params);
  126. },
  127. requestPayment(params) {
  128. this._api('requestPayment', params);
  129. }
  130. };
  131. export default wxApi;