http.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 = Util.isWeiXin() ? 'wechat' : 'h5';
  6. const app_type_name = Util.isWeiXin() ? '微信公众号' : '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. var method = params.data != undefined ? 'POST' : 'GET', // 请求方式
  35. url = Config.baseUrl + params.url, // 请求路径
  36. data = {
  37. app_type,
  38. app_type_name
  39. };
  40. if (uni.getStorageSync('token')) data.token = uni.getStorageSync('token');
  41. if (uni.getStorageSync('site_id')) data.site_id = uni.getStorageSync('site_id');
  42. // 参数
  43. if (params.data != undefined) Object.assign(data, params.data);
  44. if (params.async === false) {
  45. //同步
  46. return new Promise((resolve, reject) => {
  47. uni.request({
  48. url: url,
  49. method: method,
  50. data: data,
  51. header: params.header || {
  52. 'content-type': 'application/x-www-form-urlencoded;application/json'
  53. },
  54. dataType: params.dataType || 'json',
  55. responseType: params.responseType || 'text',
  56. success: (res) => {
  57. if (res.data.refreshtoken) {
  58. uni.setStorage({
  59. key: 'token',
  60. data: res.data.refreshtoken
  61. });
  62. }
  63. if (res.data.code == -10009 || res.data.code == -10010) {
  64. uni.removeStorage({
  65. key: 'token'
  66. })
  67. }
  68. resolve(res.data);
  69. },
  70. fail: (res) => {
  71. reject(res);
  72. },
  73. complete: (res) => {
  74. reject(res);
  75. }
  76. });
  77. });
  78. } else {
  79. //异步
  80. uni.request({
  81. url: url,
  82. method: method,
  83. data: data,
  84. header: params.header || {
  85. 'content-type': 'application/x-www-form-urlencoded;application/json'
  86. },
  87. dataType: params.dataType || 'json',
  88. responseType: params.responseType || 'text',
  89. success: (res) => {
  90. if (res.data.code == -10011 && store.state.siteState > 0) {
  91. store.commit('setSiteState', -10011)
  92. Util.redirectTo('/pages/storeclose/storeclose/storeclose', {}, 'reLaunch');
  93. return;
  94. }
  95. if (res.data.refreshtoken) {
  96. uni.setStorage({
  97. key: 'token',
  98. data: res.data.refreshtoken
  99. });
  100. }
  101. if (res.data.code == -10009 || res.data.code == -10010) {
  102. uni.removeStorage({
  103. key: 'token'
  104. });
  105. }
  106. typeof params.success == 'function' && params.success(res.data);
  107. },
  108. fail: (res) => {
  109. typeof params.fail == 'function' && params.fail(res);
  110. },
  111. complete: (res) => {
  112. // console.log(res,'0.0')
  113. typeof params.complete == 'function' && params.complete(res);
  114. }
  115. });
  116. }
  117. }
  118. }