http.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. var method = '' // 请求方式
  43. // localStorage.setItem("env","ywxk")
  44. if(process.env.NODE_ENV === 'production'){
  45. Config.baseUrl='https://xty-api.wucits.com'
  46. // Config.baseUrl='https://oy-api.wucits.com'
  47. }else{
  48. //开发环境读配置
  49. if(Config.isLocal==2){
  50. // Config.baseUrl='http://125.124.170.221:8000'
  51. Config.baseUrl='http://zhxy.com'
  52. }else{
  53. Config.baseUrl='http://zhxy.com'
  54. // Config.baseUrl='http://125.124.170.221:8000'
  55. }
  56. }
  57. var url = Config.baseUrl + params.url // 请求路径
  58. var data = {};
  59. if (params.method) {
  60. method = params.method
  61. } else if (params.data != undefined) {
  62. method = 'POST'
  63. } else {
  64. method = 'GET'
  65. }
  66. // token
  67. if (uni.getStorageSync('token')) data.token = uni.getStorageSync('token');
  68. // 门店id
  69. if (uni.getStorageSync('store_info')) data.store_id = uni.getStorageSync('store_info').store_id;
  70. // 参数
  71. if (params.data != undefined) Object.assign(data, params.data);
  72. if (params.async === false) {
  73. console.log("zheli lema");
  74. //同步
  75. return new Promise((resolve, reject) => {
  76. uni.request({
  77. url: url,
  78. method: method,
  79. data: data,
  80. header: params.header || {
  81. // 'Accept': 'application/json',
  82. 'content-type': 'application/json',
  83. Authorization: uni.getStorageSync('token') ? uni.getStorageSync('token') :
  84. 'zhxy'
  85. },
  86. dataType: params.dataType || 'json',
  87. responseType: params.responseType || 'text',
  88. success: (res) => {
  89. console.log(res, "success res");
  90. // try {
  91. // res.data = JSON.parse(res.data);
  92. // } catch (e) {
  93. // //TODO handle the exception
  94. // console.log('api error:', e);
  95. // }
  96. if (res.data.code == -3 && store.state.siteState > 0) {
  97. store.commit('setSiteState', -3)
  98. Util.redirectTo('/pages_tool/storeclose/storeclose', {}, 'reLaunch');
  99. return;
  100. }
  101. if (res.data.refreshtoken) {
  102. uni.setStorage({
  103. key: 'token',
  104. data: res.data.refreshtoken
  105. });
  106. }
  107. if (res.data.code == -10009 || res.data.code == -10010) {
  108. uni.removeStorage({
  109. key: 'token'
  110. })
  111. }
  112. // resolve(res.data)
  113. console.log(res.data,"res.data");
  114. if(res.data.code==200 || res.data.code==403){
  115. //typeof params.success == 'function' && params.success(res.data);
  116. resolve(res.data)
  117. } else {
  118. uni.showToast({
  119. title: res.data.msg+url,
  120. 'icon': 'none',
  121. duration: 3000
  122. })
  123. }
  124. },
  125. fail: (res) => {
  126. if (res.errMsg && res.errMsg == 'request:fail url not in domain list') {
  127. uni.showToast({
  128. title: Config.baseUrl + '不在request 合法域名列表中',
  129. 'icon': 'none',
  130. duration: 10000
  131. });
  132. return;
  133. }
  134. reject(res);
  135. },
  136. complete: (res) => {
  137. console.log(res, "complete complete");
  138. if ((res.errMsg && res.errMsg != "request:ok") || (res.statusCode && [200,
  139. 500
  140. ].indexOf(res.statusCode) == -1)) {
  141. uni.showToast({
  142. title: Config.baseUrl + '请求失败',
  143. 'icon': 'none',
  144. duration: 10000
  145. })
  146. return;
  147. }
  148. }
  149. });
  150. });
  151. } else {
  152. //异步
  153. uni.showLoading({
  154. title: '加载中'
  155. });
  156. console.log("token是什么",uni.getStorageSync('token'));
  157. uni.request({
  158. url: url,
  159. method: method,
  160. data: data,
  161. header: params.header || {
  162. // 'Accept': 'application/json',
  163. 'content-type': 'application/json',
  164. Authorization: uni.getStorageSync('token') ? uni.getStorageSync('token') : 'zhxy'
  165. },
  166. dataType: params.dataType || 'json',
  167. responseType: params.responseType || 'text',
  168. success: (res) => {
  169. // try {
  170. // res.data = JSON.parse(res.data);
  171. // } catch (e) {
  172. // //TODO handle the exception
  173. // console.log('api error:', e);
  174. // }
  175. if (res.data.code == -3 && store.state.siteState > 0) {
  176. store.commit('setSiteState', -3)
  177. Util.redirectTo('/pages_tool/storeclose/storeclose', {}, 'reLaunch');
  178. return;
  179. }
  180. if (res.data.refreshtoken) {
  181. uni.setStorage({
  182. key: 'token',
  183. data: res.data.refreshtoken
  184. });
  185. }
  186. if (res.data.code == -10009 || res.data.code == -10010) {
  187. uni.removeStorage({
  188. key: 'token'
  189. })
  190. }
  191. if (res.data.code == 200 || res.data.code == 403) {
  192. typeof params.success == 'function' && params.success(res.data);
  193. } else {
  194. uni.showToast({
  195. title: res.data.msg ,
  196. 'icon': 'none',
  197. duration: 3000
  198. })
  199. }
  200. uni.hideLoading();
  201. },
  202. fail: (res) => {
  203. if (res.errMsg && res.errMsg == 'request:fail url not in domain list') {
  204. uni.showToast({
  205. title: Config.baseUrl + '不在request 合法域名列表中',
  206. 'icon': 'none',
  207. duration: 10000
  208. });
  209. return;
  210. }
  211. typeof params.fail == 'function' && params.fail(res);
  212. uni.hideLoading();
  213. },
  214. complete: (res) => {
  215. if ((res.errMsg && res.errMsg != "request:ok") || (res.statusCode && [200, 500].indexOf(
  216. res.statusCode) == -1)) {
  217. uni.showToast({
  218. title: Config.baseUrl + '请求失败',
  219. 'icon': 'none',
  220. duration: 10000
  221. })
  222. return;
  223. }
  224. typeof params.complete == 'function' && params.complete(res);
  225. }
  226. });
  227. }
  228. }
  229. }