request.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import Vue from 'vue'
  2. import md5 from 'js-md5'
  3. import Storage from '@/utils/storage'
  4. import * as Foundation from '@/ui-utils/Foundation'
  5. import store from '@/store'
  6. import checkToken from '@/utils/checkToken'
  7. /** api 模式 */
  8. const api_mode = 'prod'
  9. export const API = {
  10. // base: 'https://api.base.wdklian.com',
  11. // buyer: 'https://api.buyer.wdklian.com',
  12. base: 'http://wdkl.natapp1.cc',
  13. buyer: 'http://192.168.1.199:7002'
  14. }
  15. const config = {
  16. timeout: 15000,
  17. baseURL: api_mode === 'prod' ? API.buyer : API.base,
  18. headers: {
  19. 'Content-Type':'application/x-www-form-urlencoded'
  20. }
  21. }
  22. export const Method = {
  23. GET: 'GET',
  24. POST: 'POST',
  25. PUT: 'PUT',
  26. DELETE: 'DELETE'
  27. }
  28. function service(options) {
  29. let url = config.baseURL + '/' + options.url
  30. if (options.url.includes("http")){
  31. url = options.url
  32. }
  33. let headers = config.headers
  34. const uuid = Storage.getItem('uuid')
  35. headers['uuid'] = uuid
  36. if (options.config.isJson){
  37. headers['Content-Type']='application/json'
  38. } else {
  39. headers['Content-Type']='application/x-www-form-urlencoded'
  40. }
  41. if (options.config.loading){
  42. uni.showLoading()
  43. }
  44. console.log('this.api_mode===', api_mode)
  45. if (options.config.needToken && api_mode === 'prod'){
  46. let accessToken = Storage.getItem('access_token')
  47. if (!accessToken) {
  48. uni.reLaunch({
  49. url: '/pages/login/index'
  50. })
  51. return
  52. }
  53. let uid = Storage.getItem('uid')
  54. let nonce = Foundation.randomString(6)
  55. let timestamp = parseInt(new Date().getTime() / 1000)
  56. let sign = md5(uid + nonce + timestamp + accessToken)
  57. if (url.includes('?')) {
  58. url = `${url}&uid=${uid}&nonce=${nonce}&timestamp=${timestamp}&sign=${sign}`
  59. } else {
  60. url = `${url}?uid=${uid}&nonce=${nonce}&timestamp=${timestamp}&sign=${sign}`
  61. }
  62. }
  63. return new Promise((resolve,reject)=>{
  64. // console.log(options, config);
  65. uni.request({
  66. url: url,
  67. data: options.params,
  68. header: headers,
  69. timeout: config.timeout,
  70. method: options.config.method,
  71. success(res) {
  72. uni.hideLoading()
  73. if (res.statusCode == 200){
  74. resolve(res.data)
  75. } else if (res.statusCode == 403){
  76. if (!Storage.getItem('refresh_token')) {
  77. console.log('444444444444444')
  78. uni.reLaunch({
  79. url: '/pages/login/index'
  80. })
  81. resolve()
  82. return
  83. }
  84. store.dispatch('cleanCartStoreAction')
  85. store.dispatch('removeUserAction')
  86. store.dispatch('removeAccessTokenAction')
  87. store.dispatch('removeRefreshTokenAction')
  88. Storage.removeItem('login_result')
  89. Storage.removeItem('refresh_token')
  90. Storage.removeItem('access_token_timeout')
  91. Storage.removeItem('refresh_token_timeout')
  92. Vue.prototype.$checkHasLogin()
  93. } else{
  94. if (url.indexOf("/wx_mobile") === -1) {
  95. uni.showToast({
  96. title: res.data.message,
  97. icon: 'none'
  98. })
  99. }
  100. reject(res)
  101. }
  102. },
  103. fail(err){
  104. uni.hideLoading()
  105. console.log('错误消息====', err)
  106. uni.showToast({
  107. title: res.data.message,
  108. icon: 'none'
  109. })
  110. reject(err)
  111. }
  112. });
  113. })
  114. }
  115. export default function request(options){
  116. if (api_mode === 'prod') {
  117. if(/passport\/mini-program\/*|passport\/login-binder\/wap\/*|passport\/login\/*/.test(options.url)){
  118. return service(options)
  119. }else{
  120. return checkToken(options).then(()=>{
  121. return service(options)
  122. })
  123. }
  124. } else {
  125. return service(options)
  126. }
  127. }