http.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import Vue from 'vue'
  2. //const BASE_URL = 'https://ymt.prpiano.com/api'
  3. //const BASE_URL = 'https://ht.test.prpiano.com/api'
  4. //xw
  5. //const BASE_URL = 'http://10.0.52.100:82'
  6. //dh
  7. var BASE_URL
  8. var BASE_URL2
  9. //lb
  10. //const BASE_URL = 'http://10.0.126.219:82'
  11. if (process.env.NODE_ENV === 'development') {
  12. //线上 https://bytesailing.cn/api/storage/cloudApi
  13. BASE_URL = 'https://cfm.bytesail.cn/api/storage/cloudApi'
  14. BASE_URL2 = 'https://cfm.bytesail.cn/api/storage/cloudApi'
  15. } else {
  16. //线上
  17. BASE_URL = 'https://cfm.bytesail.cn/api/storage/cloudApi'
  18. BASE_URL2 = 'https://cfm.bytesail.cn/api/storage/cloudApi'
  19. }
  20. /**
  21. * 封装get方法
  22. * @param url
  23. * @param data
  24. * @returns {Promise}
  25. */
  26. export function get(url, req = {}) {
  27. return new Promise((resolve, reject) => {
  28. if (Vue.prototype.$token) {
  29. var hander = {
  30. 'Authorization':'Basic c2FiZXI6c2FiZXJfc2VjcmV0',
  31. }
  32. } else {
  33. var hander = {}
  34. }
  35. uni.request({
  36. url: BASE_URL + url,
  37. data: req,
  38. method: 'GET',
  39. header: hander,
  40. success: (res) => {
  41. resolve(res.data);
  42. },
  43. fail: (err) => {
  44. return Promise.reject(err);
  45. }
  46. });
  47. })
  48. }
  49. /**
  50. * 封装post请求
  51. * @param url
  52. * @param data
  53. * @returns {Promise}
  54. */
  55. var urlList = []
  56. export function post(url, req = {}, headers) {
  57. console.log(Vue.prototype.$token,'token')
  58. var httpUrl
  59. if(url.indexOf('/cloudApi') == -1){
  60. httpUrl = BASE_URL
  61. }else{
  62. httpUrl = BASE_URL2
  63. }
  64. if(urlList.indexOf(url) == -1 || url == '/wx/inventory/selectFabricWTypeDetails' || '/wx/inventory/selectInkEW'){
  65. urlList.push(url)
  66. setTimeout(()=>{
  67. urlList.splice(urlList.indexOf(url),1)
  68. },500)
  69. }else{
  70. return Promise.reject('请勿频繁提交');
  71. }
  72. return new Promise((resolve, reject) => {
  73. if (Vue.prototype.$token) {
  74. var hander = {
  75. 'Blade-Auth': 'bearer ' + Vue.prototype.$token.access_token,
  76. 'Authorization':'Basic c2FiZXI6c2FiZXJfc2VjcmV0',
  77. 'Content-Type':'application/json;charset=UTF-8',
  78. }
  79. } else {
  80. var hander = {}
  81. }
  82. if(headers) hander = headers
  83. uni.request({
  84. url: httpUrl + url,
  85. data: req,
  86. method: 'POST',
  87. header: hander,
  88. success: (res) => {
  89. console.log(res)
  90. if (res.data.code == 401) {
  91. uni.showToast({
  92. icon: "error",
  93. title: '前往登录',
  94. duration: 2000
  95. });
  96. Vue.prototype.$token = null
  97. uni.redirectTo({
  98. url: "/pages/user/login"
  99. })
  100. } else if (res.data.code == 500) {
  101. uni.showToast({
  102. icon: "error",
  103. title: res.data.msg,
  104. duration: 2000
  105. });
  106. } else if(res.data.code == 200){
  107. resolve(res.data);
  108. } else if(!res.data.code){
  109. uni.showToast({
  110. icon: "error",
  111. title: '服务器错误',
  112. duration: 2000
  113. });
  114. } else {
  115. uni.showToast({
  116. icon: "error",
  117. title: res.data.msg,
  118. duration: 2000
  119. });
  120. }
  121. },
  122. fail: (err) => {
  123. uni.showToast({
  124. icon: "error",
  125. title: '服务器错误',
  126. duration: 2000
  127. });
  128. return Promise.reject(err);
  129. }
  130. });
  131. })
  132. }
  133. export default {
  134. post,
  135. get
  136. }