http.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. var httpUrl
  58. if(url.indexOf('/cloudApi') == -1){
  59. httpUrl = BASE_URL
  60. }else{
  61. httpUrl = BASE_URL2
  62. }
  63. if(urlList.indexOf(url) == -1 || url == '/wx/inventory/selectFabricWTypeDetails' || '/wx/inventory/selectInkEW'){
  64. urlList.push(url)
  65. setTimeout(()=>{
  66. urlList.splice(urlList.indexOf(url),1)
  67. },500)
  68. }else{
  69. return Promise.reject('请勿频繁提交');
  70. }
  71. return new Promise((resolve, reject) => {
  72. if (Vue.prototype.$token) {
  73. var hander = {
  74. 'Blade-Auth': 'bearer ' + Vue.prototype.$token.access_token,
  75. 'Authorization':'Basic c2FiZXI6c2FiZXJfc2VjcmV0',
  76. 'Content-Type':'application/json;charset=UTF-8',
  77. }
  78. } else {
  79. var hander = {}
  80. }
  81. if(headers) hander = headers
  82. uni.request({
  83. url: httpUrl + url,
  84. data: req,
  85. method: 'POST',
  86. header: hander,
  87. success: (res) => {
  88. if (res.data.code == 401) {
  89. uni.showToast({
  90. icon: "error",
  91. title: '前往登录',
  92. duration: 2000
  93. });
  94. Vue.prototype.$token = null
  95. uni.redirectTo({
  96. url: "/pages/user/login"
  97. })
  98. } else if (res.data.code == 500) {
  99. uni.showToast({
  100. icon: "error",
  101. title: res.data.msg,
  102. duration: 2000
  103. });
  104. } else if(res.data.code == 200){
  105. resolve(res.data);
  106. } else if(!res.data.code){
  107. uni.showToast({
  108. icon: "error",
  109. title: '服务器错误',
  110. duration: 2000
  111. });
  112. } else {
  113. uni.showToast({
  114. icon: "error",
  115. title: res.data.msg,
  116. duration: 2000
  117. });
  118. }
  119. },
  120. fail: (err) => {
  121. uni.showToast({
  122. icon: "error",
  123. title: '服务器错误',
  124. duration: 2000
  125. });
  126. return Promise.reject(err);
  127. }
  128. });
  129. })
  130. }
  131. export default {
  132. post,
  133. get
  134. }