request.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // 预发布地址
  2. let httpTrackLog = 'https://hyperion-track-app.mokamrp.com';
  3. let httpServiceURL = '';
  4. let version = __wxConfig.envVersion;
  5. switch (version) {
  6. case 'develop':
  7. httpServiceURL = 'https://hyperion-novel-qa-app.mokamrp.com';
  8. // httpServiceURL = "http://172.16.24.193:10111";
  9. // httpServiceURL = 'https://iaa-app.mokamrp.com';
  10. break;
  11. case 'trial':
  12. httpServiceURL = 'https://test-iaa-app.mokamrp.com';
  13. // httpServiceURL = 'https://iaa-app.mokamrp.com';
  14. break;
  15. case 'release': //正式版
  16. httpServiceURL = 'https://iaa-app.mokamrp.com';
  17. break;
  18. default: //未知,默认调用正式版
  19. httpServiceURL = 'https://iaa-app.mokamrp.com';
  20. break;
  21. }
  22. // 普通POST请求
  23. export const post = (url, body = {}) => {
  24. return new Promise((resolve, reject) => {
  25. wx.request({
  26. url: url,
  27. data: body,
  28. method: 'POST',
  29. dataType: 'json',
  30. success: (res) => {
  31. if (res.statusCode == 200 && res.data.code == 200) {
  32. resolve(res.data.data);
  33. } else {
  34. reject(res);
  35. }
  36. },
  37. fail: (res) => {
  38. console.log(url, 'error--------', res);
  39. reject(res);
  40. },
  41. });
  42. });
  43. };
  44. // 普通Get请求
  45. export const get = (url) => {
  46. return new Promise((resolve, reject) => {
  47. wx.request({
  48. url: url,
  49. method: 'GET',
  50. success: (res) => {
  51. if (res.statusCode == 200 && res.data.code == 200) {
  52. resolve(res.data);
  53. } else {
  54. reject(res);
  55. }
  56. },
  57. fail: (res) => {
  58. reject(res);
  59. },
  60. });
  61. });
  62. };
  63. /**
  64. * 打点请求
  65. */
  66. export function uploadTrackLog(url, body) {
  67. if (!body) {
  68. body = {};
  69. }
  70. return post(httpTrackLog + url, body);
  71. }
  72. export const requestLogin = (data) => {
  73. return new Promise((resolve, reject) => {
  74. wx.request({
  75. url: httpServiceURL + '/user/loginWx',
  76. data: data,
  77. method: 'POST',
  78. dataType: 'json',
  79. header: {},
  80. success: function (res) {
  81. if (res.data.code == 200) {
  82. console.log(res);
  83. resolve(res.data.data);
  84. } else {
  85. wx.showToast({
  86. title: res.data.msg || res.data.error || '登录失败...',
  87. icon: 'none',
  88. });
  89. console.log('login fail', res);
  90. reject(res.data);
  91. }
  92. },
  93. fail: function (res) {
  94. console.log('login fail', res);
  95. wx.showToast({
  96. title: '登录失败...',
  97. icon: 'none',
  98. });
  99. },
  100. });
  101. });
  102. };
  103. export const requestAll = (requestUrl, body, method = 'POST', customHeaders = {}) => {
  104. console.log("body",body)
  105. let app = getApp().globalData;
  106. let url = httpServiceURL + requestUrl;
  107. if (method === 'GET') {
  108. if (body) {
  109. url += '?';
  110. for (const k in body) {
  111. url += `&${k}=${body[k]}`;
  112. }
  113. }
  114. }
  115. // 封装请求函数
  116. const doRequest = () => {
  117. return new Promise((resolve, reject) => {
  118. wx.request({
  119. url: url,
  120. data: body,
  121. method: method,
  122. dataType: 'json',
  123. header: {
  124. ...customHeaders,
  125. token: customHeaders.token || app.token || '',
  126. },
  127. success: function (res) {
  128. if (res.data.code == 200) {
  129. // 是否需要解密
  130. console.log(url, '<------>', res.data.data);
  131. if (typeof res.data.data === 'number') {
  132. resolve(res.data.data);
  133. } else {
  134. resolve(res.data.data || []);
  135. }
  136. }
  137. else if(res.data.code==20001){
  138. //令牌过期,重新登录
  139. reject({code: 20001, msg: '令牌过期'});
  140. }
  141. else {
  142. console.log('error:', res.data, url);
  143. reject(res.data);
  144. }
  145. },
  146. fail: function (res) {
  147. wx.showToast({
  148. title: '网络出错,请重试...',
  149. icon: 'none',
  150. });
  151. reject(res);
  152. },
  153. });
  154. });
  155. };
  156. // 执行请求,如果token过期则重新登录后重试
  157. return doRequest().catch(async error => {
  158. if (error.code === 20001) {
  159. // 重新登录
  160. const app = getApp();
  161. try {
  162. await app.login();
  163. // 重新执行请求
  164. return doRequest();
  165. } catch (loginError) {
  166. console.error('重新登录失败:', loginError);
  167. throw loginError;
  168. }
  169. }
  170. throw error;
  171. });
  172. };
  173. /**
  174. * 图文服务
  175. */
  176. export function tuwenRequest(url, body) {
  177. if (!body) {
  178. body = {};
  179. }
  180. return tuwenPost('https://kratos.mokamrp.com' + url, body);
  181. }
  182. export const tuwenPost = (url, body = {}) => {
  183. const app = getApp()
  184. return new Promise((resolve, reject) => {
  185. wx.request({
  186. url: url,
  187. data: body,
  188. method: 'POST',
  189. dataType: 'json',
  190. header: {
  191. 'Authorization': 'tanlongjiefuckdick'
  192. },
  193. success: (res) => {
  194. if (res.statusCode == 200 && res.data.code == 200) {
  195. let result = JSON.parse(res.data.data)
  196. resolve(result)
  197. } else {
  198. reject(res)
  199. }
  200. },
  201. fail: (res) => {
  202. console.log(url, 'error--------', res)
  203. reject(res)
  204. }
  205. })
  206. })
  207. }
  208. /**
  209. * 投诉
  210. */
  211. export function workGet(url, body) {
  212. if (!body) {
  213. body = {}
  214. }
  215. return get('https://moka-volta-kratos-workwx-h5.mokamrp.com' + url, body)
  216. }
  217. export function workPost(url, body) {
  218. if (!body) {
  219. body = {}
  220. }
  221. return post('https://moka-volta-kratos-workwx-h5.mokamrp.com' + url, body)
  222. }