request.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import * as types from '@/utils/types'
  2. import tools from "@/utils/tools.js"
  3. const API_URL={
  4. 'DEV':'http://127.0.0.1:8082/charging-parking',
  5. 'TEST':'http://192.168.31.248:8082/charging-parking',
  6. 'PRO':'https://youdian.hbjp.com.cn/charging-parking'
  7. }
  8. //当前环境
  9. const BASEURL=API_URL['PRO']
  10. const service = function(obj) {
  11. obj.method = obj.method || 'get';
  12. if (obj.url === undefined) {
  13. return;
  14. }
  15. //uni.showNavigationBarLoading() //在当前页面显示导航条加载动画
  16. return new Promise((resolve, reject) => {
  17. const header = obj.header ? obj.header : {};
  18. header['content-type'] = header['content-type'] || 'application/x-www-form-urlencoded';
  19. //store.state.access_token && (header['token'] = store.state.access_token);
  20. uni.request({
  21. url: BASEURL + obj.url,
  22. data: obj.data,
  23. method: obj.method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  24. header, // 设置请求的 header
  25. success: function(res) {
  26. // success
  27. resolve(res.data);
  28. },
  29. fail: function(error) {
  30. //console.log(error);
  31. uni.showToast({
  32. title: '出现网络问题',
  33. icon: "none"
  34. })
  35. reject(error); //请求失败
  36. },
  37. })
  38. })
  39. }
  40. //const request = service;
  41. export default service; //暴露出来供其他文件引用