request.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import config from './config.js';
  2. import store from '@/store/store.js';
  3. const visit = function(params) {
  4. if (!params) params = new Object;
  5. params.app_id = config.appId;
  6. params.device = config.dev;
  7. let url = config.apiUrl + 'miniapp/visit'; //仅仅请求 不响应服务器的操作
  8. uni.request({
  9. url: url,
  10. data: params,
  11. method: 'POST',
  12. success() {
  13. console.log('日志写入成功');
  14. },
  15. fail() {
  16. console.log('日志写入失败');
  17. },
  18. });
  19. }
  20. const post = function(url, params) {
  21. if (!params) params = new Object;
  22. params.device = config.dev;
  23. params.app_id = config.appId;
  24. return new Promise((resolve, reject) => {
  25. uni.request({
  26. url: config.apiUrl + url, //仅为示例,并非真实接口地址。
  27. data: params,
  28. method: 'POST',
  29. success: (res) => {
  30. uni.hideLoading();
  31. if (res.statusCode == 200) {
  32. if (res.data.code == 200) {
  33. if (res.data.code == 100) { //未登录的情况
  34. store.commit('setMember', null);
  35. uni.removeStorageSync('member-token');
  36. reject({
  37. code: res.data.code,
  38. msg: res.data.msg
  39. });
  40. } else {
  41. resolve(res.data.data);
  42. }
  43. } else {
  44. uni.showToast({
  45. icon: 'none',
  46. title: res.data.msg + '#' + res.data.code
  47. });
  48. reject({
  49. code: res.data.code,
  50. msg: res.data.msg
  51. });
  52. }
  53. } else {
  54. uni.showToast({
  55. icon: 'none',
  56. title: "网络异常"
  57. });
  58. reject({
  59. code: 500,
  60. msg: '网络异常'
  61. });
  62. }
  63. },
  64. fail: (res) => {
  65. uni.hideLoading();
  66. uni.showToast({
  67. icon: 'none',
  68. title: "服务器繁忙"
  69. });
  70. reject({
  71. code: 500,
  72. msg: '服务器繁忙'
  73. });
  74. }
  75. });
  76. });
  77. }
  78. //需要登录的使用这个
  79. const api = function(url, params) {
  80. if (!params) params = new Object;
  81. let token = uni.getStorageSync('member-token');
  82. params.token = token;
  83. return this.post(url, params);
  84. }
  85. export default {
  86. post,
  87. api,
  88. visit
  89. }