http.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import Config from './config.js'
  2. import Util from './util.js'
  3. import store from '@/store/index.js'
  4. // #ifdef H5
  5. const app_type = 'h5';
  6. const app_type_name = 'H5';
  7. // #endif
  8. // #ifdef MP-WEIXIN
  9. const app_type = 'weapp';
  10. const app_type_name = '微信小程序';
  11. // #endif
  12. // #ifdef MP-ALIPAY
  13. const app_type = 'aliapp';
  14. const app_type_name = '支付宝小程序';
  15. // #endif
  16. // #ifdef MP-BAIDU
  17. const app_type = 'baiduapp';
  18. const app_type_name = '百度小程序';
  19. // #endif
  20. // #ifdef MP-TOUTIAO
  21. const app_type = 'MP-TOUTIAO';
  22. const app_type_name = '头条小程序';
  23. // #endif
  24. // #ifdef MP-QQ
  25. const app_type = 'MP-QQ';
  26. const app_type_name = 'QQ小程序';
  27. // #endif
  28. // #ifdef APP-PLUS
  29. const app_type = 'app';
  30. const app_type_name = 'APP';
  31. // #endif
  32. export default {
  33. sendRequest(params) {
  34. if (!Config.baseUrl) {
  35. uni.showToast({
  36. title: '未配置请求域名',
  37. 'icon': 'none',
  38. duration: 10000
  39. });
  40. return;
  41. }
  42. if (params.confirm) {
  43. return uni.showModal({
  44. title: params.confirm.title,
  45. content: params.confirm.message,
  46. success: res => {
  47. if (res.confirm) {
  48. delete params.confirm;
  49. this.sendRequest(params);
  50. }
  51. }
  52. })
  53. }
  54. var method = '' // 请求方式
  55. let baseUrl = ""
  56. // localStorage.setItem("env","ywxk")
  57. if (process.env.NODE_ENV === 'production') {
  58. Config.baseUrl = Config.schoolBaseData.baseUrl
  59. } else {
  60. //开发环境读配置
  61. if (Config.isLocal == 2) {
  62. Config.baseUrl = 'http://125.124.170.221:8000'
  63. // Config.baseUrl='http://zhxy.com'
  64. } else {
  65. // Config.baseUrl='http://zhxy.com'
  66. Config.baseUrl = 'http://125.124.170.221:8000'
  67. }
  68. }
  69. var url = Config.baseUrl + params.url // 请求路径
  70. var data = {};
  71. if (params.method) {
  72. method = params.method
  73. } else if (params.data != undefined) {
  74. method = 'POST'
  75. } else {
  76. method = 'GET'
  77. }
  78. // token
  79. if (uni.getStorageSync('token')) data.token = uni.getStorageSync('token');
  80. // 门店id
  81. if (uni.getStorageSync('store_info')) data.store_id = uni.getStorageSync('store_info').store_id;
  82. // 参数
  83. if (params.data != undefined) Object.assign(data, params.data);
  84. if (params.async === false) {
  85. console.log("zheli lema");
  86. //同步
  87. return new Promise((resolve, reject) => {
  88. uni.request({
  89. url: url,
  90. method: method,
  91. data: data,
  92. header: params.header || {
  93. // 'Accept': 'application/json',
  94. 'content-type': 'application/json',
  95. Authorization: uni.getStorageSync('token') ? uni.getStorageSync('token') :
  96. 'zhxy'
  97. },
  98. dataType: params.dataType || 'json',
  99. responseType: params.responseType || 'text',
  100. success: (res) => {
  101. console.log(res, "success res");
  102. // try {
  103. // res.data = JSON.parse(res.data);
  104. // } catch (e) {
  105. // //TODO handle the exception
  106. // console.log('api error:', e);
  107. // }
  108. if (res.data.code == -3 && store.state.siteState > 0) {
  109. store.commit('setSiteState', -3)
  110. Util.redirectTo('/pages_tool/storeclose/storeclose', {}, 'reLaunch');
  111. return;
  112. }
  113. if (res.data.refreshtoken) {
  114. uni.setStorage({
  115. key: 'token',
  116. data: res.data.refreshtoken
  117. });
  118. }
  119. if (res.data.code == -10009 || res.data.code == -10010) {
  120. uni.removeStorage({
  121. key: 'token'
  122. })
  123. }
  124. // resolve(res.data)
  125. console.log(res.data, "res.data");
  126. if (res.data.code == 200 || res.data.code == 403) {
  127. //typeof params.success == 'function' && params.success(res.data);
  128. resolve(res.data)
  129. } else {
  130. uni.showToast({
  131. title: res.data.msg + url,
  132. 'icon': 'none',
  133. duration: 3000
  134. })
  135. }
  136. },
  137. fail: (res) => {
  138. if (res.errMsg && res.errMsg == 'request:fail url not in domain list') {
  139. uni.showToast({
  140. title: Config.baseUrl + '不在request 合法域名列表中',
  141. 'icon': 'none',
  142. duration: 10000
  143. });
  144. return;
  145. }
  146. reject(res);
  147. },
  148. complete: (res) => {
  149. console.log(res, "complete complete");
  150. if ((res.errMsg && res.errMsg != "request:ok") || (res.statusCode && [200,
  151. 500
  152. ].indexOf(res.statusCode) == -1)) {
  153. uni.showToast({
  154. title: Config.baseUrl + '请求失败',
  155. 'icon': 'none',
  156. duration: 10000
  157. })
  158. return;
  159. }
  160. }
  161. });
  162. });
  163. } else {
  164. //异步
  165. params.showLoading && uni.showLoading({
  166. title: '加载中'
  167. });
  168. // console.log("token是什么", url,uni.getStorageSync('token'));
  169. uni.request({
  170. url: url,
  171. method: method,
  172. data: data,
  173. header: params.header || {
  174. // 'Accept': 'application/json',
  175. 'content-type': 'application/json',
  176. Authorization: uni.getStorageSync('token') ? uni.getStorageSync('token') : 'zhxy'
  177. },
  178. dataType: params.dataType || 'json',
  179. responseType: params.responseType || 'text',
  180. success: (res) => {
  181. // try {
  182. // res.data = JSON.parse(res.data);
  183. // } catch (e) {
  184. // //TODO handle the exception
  185. // console.log('api error:', e);
  186. // }
  187. if (res.data.code == -3 && store.state.siteState > 0) {
  188. store.commit('setSiteState', -3)
  189. Util.redirectTo('/pages_tool/storeclose/storeclose', {}, 'reLaunch');
  190. return;
  191. }
  192. if (res.data.refreshtoken) {
  193. uni.setStorage({
  194. key: 'token',
  195. data: res.data.refreshtoken
  196. });
  197. }
  198. if (res.data.code == -10009 || res.data.code == -10010) {
  199. uni.removeStorage({
  200. key: 'token'
  201. })
  202. }
  203. if (res.data.code == 401) {
  204. uni.setStorageSync("token", '')
  205. uni.reLaunch({
  206. url: "/pages/login/login"
  207. })
  208. }
  209. if (res.data.code == 200 || res.data.code == 403) {
  210. typeof params.success == 'function' && params.success(res.data);
  211. } else {
  212. uni.showToast({
  213. title: res.data.msg,
  214. 'icon': 'none',
  215. duration: 3000
  216. })
  217. }
  218. uni.hideLoading();
  219. },
  220. fail: (res) => {
  221. if (res.errMsg && res.errMsg == 'request:fail url not in domain list') {
  222. uni.showToast({
  223. title: Config.baseUrl + '不在request 合法域名列表中',
  224. 'icon': 'none',
  225. duration: 10000
  226. });
  227. return;
  228. }
  229. typeof params.fail == 'function' && params.fail(res);
  230. params.fail && params.fail(res.data);
  231. uni.hideLoading();
  232. },
  233. complete: (res) => {
  234. if ((res.errMsg && res.errMsg != "request:ok") || (res.statusCode && [200, 500].indexOf(
  235. res.statusCode) == -1)) {
  236. uni.showToast({
  237. title: Config.baseUrl + '请求失败',
  238. 'icon': 'none',
  239. duration: 10000
  240. })
  241. return;
  242. }
  243. typeof params.complete == 'function' && params.complete(res);
  244. }
  245. });
  246. }
  247. }
  248. }