http-2.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 = 'http://36.134.91.96:10001/api'
  14. BASE_URL2 = 'http://36.134.91.96:10001/api'
  15. } else {
  16. //线上
  17. BASE_URL = 'http://36.134.91.96:10001/api'
  18. BASE_URL2 = 'http://36.134.91.96:10001/api'
  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 (this.$storage.getStorageSync('token')) {
  29. var hander = {
  30. 'Blade-Auth': 'bearer ' + this.$storage.getStorageSync('token'),
  31. 'Authorization':'Basic c2FiZXI6c2FiZXJfc2VjcmV0',
  32. 'Content-Type':'application/json;charset=UTF-8',
  33. }
  34. } else {
  35. var hander = {}
  36. }
  37. uni.request({
  38. url: BASE_URL + url,
  39. data: req,
  40. method: 'GET',
  41. header: hander,
  42. success: (res) => {
  43. resolve(res.data);
  44. },
  45. fail: (err) => {
  46. return Promise.reject(err);
  47. }
  48. });
  49. })
  50. }
  51. /**
  52. * 封装post请求
  53. * @param url
  54. * @param data
  55. * @returns {Promise}
  56. */
  57. var urlList = []
  58. export function post(url, req = {}, headers) {
  59. var httpUrl
  60. if(url.indexOf('/cloudApi') == -1){
  61. httpUrl = BASE_URL
  62. }else{
  63. httpUrl = BASE_URL2
  64. }
  65. if(urlList.indexOf(url) == -1 || url == '/wx/inventory/selectFabricWTypeDetails' || '/wx/inventory/selectInkEW'){
  66. urlList.push(url)
  67. setTimeout(()=>{
  68. urlList.splice(urlList.indexOf(url),1)
  69. },500)
  70. }else{
  71. return Promise.reject('请勿频繁提交');
  72. }
  73. return new Promise((resolve, reject) => {
  74. if (Vue.prototype.$token) {
  75. var hander = {
  76. 'Blade-Auth': 'bearer ' + this.$storage.getStorageSync('token'),
  77. 'Authorization':'Basic c2FiZXI6c2FiZXJfc2VjcmV0',
  78. 'Content-Type':'application/json;charset=UTF-8',
  79. }
  80. } else {
  81. var hander = {}
  82. }
  83. if(headers) hander = headers
  84. uni.request({
  85. url: httpUrl + url,
  86. data: req,
  87. method: 'POST',
  88. header: hander,
  89. success: (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. }