request.js 5.2 KB

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