index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import store from '@/store/index';
  2. import config from '@/config.js'
  3. import http from './interface'
  4. import carpool from './carpool.js'
  5. import user from './user.js'
  6. import code from './code.js'
  7. import app from './app.js'
  8. /**
  9. * 将业务所有接口统一起来便于维护
  10. * 如果项目很大可以将 url 独立成文件,接口分成不同的模块
  11. *
  12. */
  13. //实例
  14. /* export const goods = (data) => {
  15. //设置请求前拦截器
  16. http.interceptor.request = (config) => {
  17. config.header = {
  18. token: '12345647sdfads'
  19. }
  20. }
  21. //设置请求结束后拦截器
  22. http.interceptor.response = (response) => {
  23. //判断返回状态 执行相应操作
  24. console.log(response)
  25. return response;
  26. }
  27. return http.request({
  28. //baseUrl: 'https://unidemo.dcloud.net.cn/',
  29. url: '/home/v1/goods',
  30. method:'POST',
  31. dataType: 'json',
  32. data,
  33. })
  34. } */
  35. //------------------------------------------------------------------------------------------
  36. //--------------------------------------- 拦截器 ----------------------------------
  37. //------------------------------------------------------------------------------------------
  38. //服务器地址
  39. http.config.baseUrl = config.server
  40. //设置请求前拦截器
  41. http.interceptor.request = (config) => {
  42. //添加通用参数
  43. config.header = {
  44. "token": uni.getStorageSync('userData').token
  45. }
  46. }
  47. //设置请求结束后拦截器
  48. http.interceptor.response = (res) => {
  49. console.log("拦截",res);
  50. let code = res.statusCode;
  51. let err = null;
  52. //未连接到网络
  53. if(!code){
  54. uni.showModal({
  55. title: '提示',
  56. content: 'APP服务端未响应!',
  57. confirmColor: '#3CC51F',
  58. showCancel: false,
  59. success: function (msg) {
  60. if (msg.confirm) {
  61. }
  62. }
  63. });
  64. return res;
  65. }
  66. if(code != 200){
  67. //判断网络请求状态 执行相应操作
  68. switch(true){
  69. case code>=200 && code<300:
  70. break;
  71. case code>=300 && code<400:
  72. break;
  73. case code>=400 && code<500:
  74. err = "客户端请求错误!"
  75. break;
  76. case code==504:
  77. err = "网络请求超时!"
  78. break;
  79. case code>=500 && code<600:
  80. err = "服务器响应错误!"
  81. break;
  82. }
  83. if(err){
  84. uni.showModal({
  85. title: '提示',
  86. content: err,
  87. confirmColor: '#3CC51F',
  88. showCancel: false,
  89. success: function (msg) {
  90. if (msg.confirm) {
  91. }
  92. }
  93. });
  94. return res;
  95. }
  96. }
  97. //服务器返回错误的格式
  98. if(typeof resData.code == "undefined"){
  99. uni.showModal({
  100. title: '提示',
  101. content: '服务器返回错误',
  102. confirmColor: '#3CC51F',
  103. showCancel: false,
  104. success: function (msg) {
  105. if (msg.confirm) {
  106. }
  107. }
  108. });
  109. return res;
  110. }
  111. //判断返回状态 执行相应操作
  112. switch(res.data.code){
  113. //未登录
  114. case -1:
  115. let txt = "您还未登录"
  116. //如果本地登录状态时true则提示登录信息过期
  117. //如果本地登录状态是false则提示登录
  118. if(store.state.hasLogin){
  119. txt = "登录过期,请重新登录"
  120. //取消登录状态
  121. store.dispatch("logout");
  122. }
  123. console.log();
  124. uni.showModal({
  125. title: '',
  126. content: txt,
  127. confirmText: "去登录",
  128. confirmColor: '#3CC51F',
  129. success: function (msg) {
  130. if (msg.confirm) {
  131. uni.navigateTo({
  132. url: '/pages/user/login/login'
  133. });
  134. } else if (msg.cancel) {
  135. }
  136. }
  137. });
  138. break;
  139. //异常
  140. case 9999:
  141. uni.showModal({
  142. title: '提示',
  143. content: '远程端返回一个错误',
  144. confirmColor: '#3CC51F',
  145. showCancel: false,
  146. success: function (msg) {
  147. if (msg.confirm) {
  148. }
  149. }
  150. });
  151. break;
  152. default:
  153. break;
  154. }
  155. return res;
  156. }
  157. // 默认全部导出 import api from '@/common/vmeitime-http/'
  158. export default {
  159. user,
  160. code,
  161. carpool,
  162. app
  163. }