You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 년 전
1 년 전
1 년 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. const toolHelper = require("../utils/toolHelper.js");
  2. import {getApi} from '../utils/dataHelper'
  3. const Base64 = require("../utils/base64");
  4. let header = {
  5. 'Accept': 'application/json',
  6. 'Accept-charset': 'utf-8'
  7. }
  8. const CLIENTID = 'miniapp'
  9. const CLIENTSECRET = 'miniapp_secret'
  10. function queryData(data) {
  11. var str = '';
  12. for (var i in data) {
  13. str += i + "=" + data[i] + '&';
  14. }
  15. if (str) {
  16. str = '?' + str;
  17. str = str.substr(0, str.length - 1);
  18. }
  19. return str;
  20. }
  21. const http = {};
  22. http.get = function get(url, data, cb) {
  23. console.log(getApi(), url, data)
  24. if (url != '/blade-auth/token' && url != '/grants/wechat/consumerinfopublic/list' && url != '/grants/wechat/consumerinfopublic/detail' && url != '/grants/wechat/guide') {
  25. if (!wx.getStorageSync('token')) {
  26. wx.showToast({
  27. title: '请先登录',
  28. icon: 'none'
  29. })
  30. return false
  31. }
  32. }
  33. header.Authorization = `Basic ${Base64.Base64.encode(`${CLIENTID}:${CLIENTSECRET}`)}`;
  34. header['content-type'] = 'application/x-www-form-urlencoded';
  35. if (wx.getStorageSync('token')) {
  36. header['Blade-Auth'] = `bearer ${wx.getStorageSync('token')}`
  37. }
  38. wx.showLoading({
  39. title: '加载中',
  40. })
  41. wx.request({
  42. url: getApi() + url + queryData(data),
  43. method: 'get',
  44. header: header,
  45. success: function (res) {
  46. console.log(res.data)
  47. wx.hideToast();
  48. let resData = res.data;
  49. switch (res.data.code) {
  50. case 200:
  51. toolHelper.isFunction(cb) && cb(resData);
  52. break;
  53. case 401:
  54. wx.showToast({
  55. icon: "none",
  56. title: '请重新登录'
  57. });
  58. setTimeout(() => {
  59. wx.clearStorage()
  60. wx.reLaunch({
  61. url: '/pages/my/index/index',
  62. })
  63. })
  64. break;
  65. default:
  66. wx.showToast({
  67. icon: "none",
  68. title: resData.msg
  69. });
  70. }
  71. },
  72. fail(res) {
  73. wx.showToast({
  74. title: '请求出错,请联系相关人员',
  75. icon: 'none'
  76. })
  77. wx.hideLoading();
  78. },
  79. complete(res) {
  80. wx.hideLoading();
  81. }
  82. })
  83. }
  84. http.post = function post(url, data, cb) {
  85. console.log(getApi(), url, data)
  86. if (url != '/blade-auth/token') {
  87. if (!wx.getStorageSync('token')) {
  88. wx.showToast({
  89. title: '请先登录',
  90. icon: 'none'
  91. })
  92. return false
  93. }
  94. header['content-type'] = 'application/json';
  95. } else {
  96. header['content-type'] = 'application/x-www-form-urlencoded';
  97. }
  98. header.Authorization = `Basic ${Base64.Base64.encode(`${CLIENTID}:${CLIENTSECRET}`)}`;
  99. if (wx.getStorageSync('token')) {
  100. header['Blade-Auth'] = `bearer ${wx.getStorageSync('token')}`
  101. }
  102. wx.request({
  103. url: getApi() + url,
  104. method: 'post',
  105. data: data,
  106. header: header,
  107. formData: data,
  108. success: function (res) {
  109. let resData = res.data;
  110. switch (res.data.code) {
  111. case 200:
  112. toolHelper.isFunction(cb) && cb(resData);
  113. break;
  114. case 401:
  115. wx.showModal({
  116. title: '错误信息',
  117. content: resData.msg,
  118. confirmText: '知道了',
  119. showCancel: false,
  120. success(res1) {
  121. if (res1.confirm) {
  122. wx.clearStorage()
  123. wx.reLaunch({
  124. url: '/pages/my/index/index',
  125. })
  126. }
  127. }
  128. })
  129. break;
  130. default:
  131. wx.showToast({
  132. icon: "none",
  133. title: resData.msg
  134. });
  135. }
  136. },
  137. fail(err) {
  138. wx.showToast({
  139. title: '请求出错,请联系相关人员',
  140. icon: 'none'
  141. })
  142. },
  143. complete(res) {
  144. }
  145. });
  146. }
  147. module.exports = http;