http.js 6.1 KB

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