http.js 2.8 KB

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