http-2.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 (this.$storage.getStorageSync('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. console.log(res)
  91. if (res.data.code == 401) {
  92. uni.showToast({
  93. icon: "error",
  94. title: '前往登录',
  95. duration: 2000
  96. });
  97. Vue.prototype.$token = null
  98. uni.redirectTo({
  99. url: "/pages/user/login"
  100. })
  101. } else if (res.data.code == 500) {
  102. uni.showToast({
  103. icon: "error",
  104. title: res.data.msg,
  105. duration: 2000
  106. });
  107. } else if(res.data.code == 200){
  108. resolve(res.data);
  109. } else if(!res.data.code){
  110. uni.showToast({
  111. icon: "error",
  112. title: '服务器错误',
  113. duration: 2000
  114. });
  115. } else {
  116. uni.showToast({
  117. icon: "error",
  118. title: res.data.msg,
  119. duration: 2000
  120. });
  121. }
  122. },
  123. fail: (err) => {
  124. uni.showToast({
  125. icon: "error",
  126. title: '服务器错误',
  127. duration: 2000
  128. });
  129. return Promise.reject(err);
  130. }
  131. });
  132. })
  133. }
  134. export default {
  135. post,
  136. get
  137. }