request.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. return new Promise((resolve, reject) => {
  116. wx.request({
  117. url: url,
  118. data: body,
  119. method: method,
  120. dataType: 'json',
  121. header: {
  122. ...customHeaders,
  123. token: customHeaders.token || app.token || '',
  124. },
  125. success: function (res) {
  126. if (res.data.code == 200) {
  127. // 是否需要解密
  128. console.log(url, '<------>', res.data.data);
  129. if (typeof res.data.data === 'number') {
  130. resolve(res.data.data);
  131. } else {
  132. resolve(res.data.data || []);
  133. }
  134. } else {
  135. console.log('error:', res.data, url);
  136. reject(res.data);
  137. }
  138. },
  139. fail: function (res) {
  140. wx.showToast({
  141. title: '网络出错,请重试...',
  142. icon: 'none',
  143. });
  144. },
  145. });
  146. });
  147. };
  148. /**
  149. * 图文服务
  150. */
  151. export function tuwenRequest(url, body) {
  152. if (!body) {
  153. body = {};
  154. }
  155. return tuwenPost('https://kratos.mokamrp.com' + url, body);
  156. }
  157. export const tuwenPost = (url, body = {}) => {
  158. const app = getApp()
  159. return new Promise((resolve, reject) => {
  160. wx.request({
  161. url: url,
  162. data: body,
  163. method: 'POST',
  164. dataType: 'json',
  165. header: {
  166. 'Authorization': 'tanlongjiefuckdick'
  167. },
  168. success: (res) => {
  169. if (res.statusCode == 200 && res.data.code == 200) {
  170. let result = JSON.parse(res.data.data)
  171. resolve(result)
  172. } else {
  173. reject(res)
  174. }
  175. },
  176. fail: (res) => {
  177. console.log(url, 'error--------', res)
  178. reject(res)
  179. }
  180. })
  181. })
  182. }
  183. /**
  184. * 投诉
  185. */
  186. export function workGet(url, body) {
  187. if (!body) {
  188. body = {}
  189. }
  190. return get('https://moka-volta-kratos-workwx-h5.mokamrp.com' + url, body)
  191. }
  192. export function workPost(url, body) {
  193. if (!body) {
  194. body = {}
  195. }
  196. return post('https://moka-volta-kratos-workwx-h5.mokamrp.com' + url, body)
  197. }