request.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.buyer,
  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. if (options.config.needToken){
  45. let accessToken = Storage.getItem('access_token')
  46. if (!accessToken) {
  47. uni.reLaunch({
  48. url: '/pages/login/index'
  49. })
  50. return
  51. }
  52. let uid = Storage.getItem('uid')
  53. let nonce = Foundation.randomString(6)
  54. let timestamp = parseInt(new Date().getTime() / 1000)
  55. let sign = md5(uid + nonce + timestamp + accessToken)
  56. if (url.includes('?')) {
  57. url = `${url}&uid=${uid}&nonce=${nonce}&timestamp=${timestamp}&sign=${sign}`
  58. } else {
  59. url = `${url}?uid=${uid}&nonce=${nonce}&timestamp=${timestamp}&sign=${sign}`
  60. }
  61. }
  62. return new Promise((resolve,reject)=>{
  63. // console.log(options, config);
  64. uni.request({
  65. url: url,
  66. data: options.params,
  67. header: headers,
  68. timeout: config.timeout,
  69. method: options.config.method,
  70. success(res) {
  71. uni.hideLoading()
  72. if (res.statusCode == 200){
  73. resolve(res.data)
  74. } else if (res.statusCode == 403){
  75. if (!Storage.getItem('refresh_token')) {
  76. console.log('444444444444444')
  77. uni.reLaunch({
  78. url: '/pages/login/index'
  79. })
  80. resolve()
  81. return
  82. }
  83. store.dispatch('removeUserAction')
  84. store.dispatch('removeAccessTokenAction')
  85. store.dispatch('removeRefreshTokenAction')
  86. Storage.removeItem('login_result')
  87. Storage.removeItem('refresh_token')
  88. Storage.removeItem('access_token_timeout')
  89. Storage.removeItem('refresh_token_timeout')
  90. Vue.prototype.$checkHasLogin()
  91. } else{
  92. if (url.indexOf("/wx_mobile") === -1) {
  93. uni.showToast({
  94. title: res.data.message,
  95. icon: 'none'
  96. })
  97. }
  98. reject(res)
  99. }
  100. },
  101. fail(err){
  102. uni.hideLoading()
  103. console.log('错误消息====', err)
  104. uni.showToast({
  105. title: res.data.message,
  106. icon: 'none'
  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. }