request.js 3.0 KB

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