request.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // uni-app请求封装
  2. import {TOKEN_KEYAuto} from '@/utils/courseTool.js'
  3. import store from '@/store/index.js'
  4. export default class Request {
  5. http(router, data = {}, method, contentType) {
  6. let that = this;
  7. //let path = 'http://localhost:7014';
  8. // let path = 'https://test.userapp.store.cdwjyyh.com';
  9. // let path = 'https://user.test.ylrztop.com/api';
  10. let path = 'https://userapp.klbycp.com/store'; //百域承品
  11. let path2 = 'https://userapp.klbycp.com'; //百域承品
  12. // let path = 'http://vab6e976.natappfree.cc/store'; //本地
  13. // let path2 = 'http://vab6e976.natappfree.cc'; //本地
  14. // let path = 'http://jc5cb469.natappfree.cc/store'; //本地
  15. // let path2 = 'http://jc5cb469.natappfree.cc'; //本地
  16. let token = uni.getStorageSync('AppToken');
  17. //console.log('token',token)
  18. let type = 0
  19. uni.setStorageSync('requestPath', path2)
  20. // uni.showLoading({
  21. // title: '加载中'
  22. // });
  23. if (router.indexOf("/app/fs/course/courseFavorite/toggle") != -1) {
  24. path = path2
  25. }
  26. if (router.indexOf("/live/liveData/like") != -1) {
  27. path = 'https://im.fhhx.runtzh.com';
  28. }
  29. if (router.indexOf("/app/order/getMyMergedOrderList") != -1 || router.indexOf("/app/order/deleteOrder") != -
  30. 1) {
  31. path = path2
  32. }
  33. if (router.indexOf("/app/order/getMergedAfterSalesList") != -1) {
  34. path = path2
  35. }
  36. if (router.indexOf("/live") !== -1 || router.indexOf("/liveAPP") !== -1) {
  37. // path = 'https://userapp.klbycp.com';
  38. path = path2
  39. router = router.replace('/liveAPP', '')
  40. type = 4
  41. }
  42. if (router.indexOf("/publicApp") !== -1) {
  43. router = router.replace('/publicApp', '')
  44. // path = 'https://userapp.klbycp.com';
  45. path = path2
  46. }
  47. if (router.indexOf("/patient/getPatientList") !== -1) {
  48. // path = 'https://userapp.klbycp.com';
  49. path = path2
  50. }
  51. if (router.indexOf("/getGotoWxAppLiveLink") !== -1) {
  52. // path = 'https://userapp.klbycp.com/companyapp';
  53. path = `${path2}/companyapp`;
  54. }
  55. if (router.indexOf("/companyapp") != -1) {
  56. // router =router.replace('/companyapp','')
  57. // path = 'https://userapp.klbycp.com' //百域承品
  58. path = path2 //本地
  59. type = 1
  60. token = uni.getStorageSync('ManageToken');
  61. }
  62. //积分商城
  63. if (router.indexOf("/integral") !== -1) {
  64. path = path2
  65. }
  66. // 企微看课
  67. if (router.indexOf("/course_auto") != -1) {
  68. type = 2
  69. router = router.replace('/course_auto', '')
  70. // path = 'https://userapp.klbycp.com' //百域承品
  71. path = path2 //本地
  72. token = uni.getStorageSync(TOKEN_KEYAuto)
  73. //console.log("TOKEN_KEYAuto===", TOKEN_KEYAuto, token)
  74. }
  75. if (router.indexOf("/course_uniapp") != -1) {
  76. type = 3
  77. router = router.replace('/course_uniapp', '')
  78. // path = 'https://userapp.klbycp.com' //百域承品
  79. path = path2
  80. token = uni.getStorageSync('TOKEN_WEXIN')
  81. }
  82. return new Promise((resolve, reject) => {
  83. // token = uni.getStorageSync('AppToken');
  84. var httpContentType = 'application/x-www-form-urlencoded';
  85. if (contentType != undefined) {
  86. //application/json;charset=UTF-8
  87. httpContentType = contentType;
  88. }
  89. var routers = router;
  90. //console.log("---qxj request url:"+`${path}${router}`+" params:"+JSON.stringify(data)+" \n AppToken:"+token);
  91. // 请求
  92. uni.request({
  93. header: {
  94. // 'Content-Type': 'application/x-www-form-urlencoded',
  95. 'Content-Type': httpContentType,
  96. 'appid': wx.getAccountInfoSync().miniProgram.appId,
  97. 'AppToken': token
  98. },
  99. url: `${path}${router}`,
  100. data: data,
  101. method: method,
  102. success: (res) => {
  103. if (type !== 0 && (res.data.code == 401 || res.data.code == 4001 || res.data
  104. .code == 4004)) {
  105. store.commit('setCoureLogin', 2);
  106. uni.removeStorageSync("userinfos")
  107. // uni.removeStorageSync('userInfo');
  108. uni.removeStorageSync('TOKEN_WEXIN');
  109. if (type == 2) {
  110. uni.removeStorageSync(TOKEN_KEYAuto)
  111. }
  112. if (type == 1) {
  113. uni.removeStorageSync('ManageToken');
  114. uni.navigateTo({
  115. url: '/pages_manage/login'
  116. })
  117. }
  118. resolve({
  119. code: 401,
  120. data: null
  121. });
  122. return
  123. }
  124. //收到开发者服务器成功返回的回调函数
  125. if (res.data.code == 401) { //没有权限直接退出到登录界面
  126. let pages = getCurrentPages();
  127. pages.forEach(function(element) {
  128. if (element != undefined && element.route ==
  129. "pages/auth/login") {
  130. resolve(res.data)
  131. return;
  132. }
  133. });
  134. // let url = pages[ pages.length - 1]; //当前页页面实例
  135. // //如果登录界面已打开,自动关闭
  136. // if(url!=undefined&&url.route=="pages/auth/numberlogin"){
  137. // resolve(res.data)
  138. // return;
  139. // }
  140. uni.navigateTo({
  141. url: '/pages/auth/login',
  142. success: () => {
  143. uni.hideLoading();
  144. },
  145. fail: () => {
  146. uni.hideLoading();
  147. }
  148. })
  149. return;
  150. }
  151. if (res.data.token && type == 0) {
  152. uni.setStorageSync('AppToken', res.data.token)
  153. }
  154. resolve(res.data)
  155. },
  156. fail: (res) => {
  157. //接口调用失败的回调函数
  158. },
  159. complete: (res) => {
  160. //接口调用结束的回调函数(调用成功、失败都会执行)
  161. if (res.data.code == 401) {
  162. return false
  163. }
  164. uni.hideLoading();
  165. }
  166. })
  167. })
  168. }
  169. }