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