初步封装

  1. //http类
  2. // 每次请求接口时都会根据code判断当前的问题(判断登录状态),例如:code=4xx时,可能是token过期或用户点击了取消
  3. // 当token没有过期,并存在时才会进行后面的操作。token可以通过调用登录接口获取
  4. class Http {
  5. // 静态变量,可以通过Http.request的方式使用
  6. static request(params) {
  7. // 给传入变量设置默认值,定义数据
  8. const method = params.method || 'GET',
  9. data = params.data || {},
  10. apiBaseUrl = params.apiBaseUrl || {},
  11. hideLoading = params.hideLoading || false,
  12. token = uni.getStorageSync('token') || '',
  13. employeeId = uni.getStorageSync('employeeId') || '1',
  14. companyId = uni.getStorageSync('companyId') || '1';
  15. // 如果当前请求接口时,屏幕出现提示: 加载中...
  16. if (!hideLoading) {
  17. uni.showLoading({
  18. title: '加载中...'
  19. });
  20. }
  21. // 返回一个promise对象(接口的请求,为了不影响页面的显示等,使用promise)
  22. return new Promise((resolve, reject) => {
  23. // 调用源生的request,请求数据
  24. uni.request({
  25. url: `${apiBaseUrl}${params.url}`,
  26. method,
  27. // 请求头设置
  28. header: {
  29. 'content-type': 'application/json',
  30. 'Token': token,//携带的token
  31. 'employeeId': employeeId,//员工id
  32. 'companyId': companyId
  33. },
  34. data,
  35. // 成功时返回信息,res中就是保存信息的对象
  36. success: res => {
  37. //动态判断是否跳出当前函数,相当于return,但不会直接导致这之后的代码不运行
  38. typeof params.success == "function" && params.success(res.data);
  39. if (res.data.code == 200) {
  40. // 成功时返回数据
  41. resolve(res.data.data)
  42. } else if (res.data.code == 500) {
  43. // 服务器出错时返回数据
  44. let message = ''
  45. // 将服务器错误时的信息,显示到页面上
  46. if (res.data.message) {
  47. message = res.data.message
  48. } else if (res.data.data.message) {
  49. message = res.data.data.message
  50. }
  51. uni.showModal({
  52. content: "" + message,
  53. showCancel: false
  54. });
  55. // 请求出错
  56. } else if (res.data.code == 401 || res.data.code == 403) {
  57. // 提示出错的原因
  58. uni.showModal({
  59. content: '您尚未登录或者登录已过期,请点击确定返回登录',
  60. // 这里是根据用户的点击,例如showModual的这两个属性"cancelText":"取消","confirmText":"登录",
  61. // 通过用户的点击,res会返回不同的数据,用来判断如何进行下一步操作
  62. success: function(res) {
  63. // 点击登录用户将跳转到登录也买你
  64. if (res.confirm) {
  65. //token登录过期删除token
  66. uni.removeStorageSync('token');
  67. // 返回登录页面
  68. uni.navigateTo({
  69. url: '/pages/login/login'
  70. })
  71. } else if (res.cancel) {
  72. console.log('用户点击取消');
  73. }
  74. }
  75. });
  76. reject(false)
  77. }
  78. },
  79. // 请求失败时
  80. fail: (err) => {
  81. // 提示网络连接错误
  82. uni.showModal({
  83. content: "网络连接错误"
  84. });
  85. typeof params.fail == "function" && params.fail(e.data);
  86. // 返回错误信息
  87. reject(err);
  88. },
  89. // 无论请求成功还是失败都会执行complete中的信息
  90. complete: () => {
  91. // 隐藏加载中的提示信息
  92. if (!hideLoading) {
  93. uni.hideLoading();
  94. }
  95. // 跳出函数,相当于return false
  96. typeof params.complete == "function" && params.complete();
  97. return;
  98. }
  99. })
  100. })
  101. }
  102. }
  103. export {
  104. Http
  105. }

再给不同接口进行封装

  1. import {
  2. Http
  3. } from '../utils/http.js';
  4. import config from '../config/config.js';
  5. // 公司热门产品列表
  6. export function fetchProductCompanyHot(id) {
  7. return Http.request({
  8. apiBaseUrl: config.apiBusinessUrl,
  9. url: `/business/product/company/hot/${id}`,
  10. method: 'POST'
  11. })
  12. }
  13. // 查询产品详情
  14. export function fetchProductDetail(id) {
  15. return Http.request({
  16. apiBaseUrl: config.apiProductUrl,
  17. url: `/product/frontProduct/detail/${id}`,
  18. method: 'POST'
  19. })
  20. }
  21. // 查询ta的推荐产品列表
  22. export function fetchTaRecommend(data) {
  23. return Http.request({
  24. apiBaseUrl: config.apiBusinessUrl,
  25. url: '/business/product/taRecommend/list',
  26. method: 'POST',
  27. data
  28. })
  29. }
  30. // 根据规格查询产品价格
  31. export function fetchPriceByStandar(data) {
  32. return Http.request({
  33. apiBaseUrl: config.apiProductUrl,
  34. url: '/product/frontProduct/price',
  35. method: 'POST',
  36. hideLoading: true,
  37. data
  38. })
  39. }
  40. // 新增留言
  41. export function createMessage(data) {
  42. return Http.request({
  43. apiBaseUrl: config.apiBusinessUrl,
  44. url: '/business/message/addMessage',
  45. method: 'POST',
  46. data
  47. })
  48. }
  49. // 根据员工id获取产品列表
  50. export function fetchUserProduct() {
  51. return Http.request({
  52. apiBaseUrl: config.apiBusinessUrl,
  53. url: `/business/product/userProduct/list/`,
  54. method: 'POST'
  55. })
  56. }
  57. // 生成邀请二维码
  58. export function qrCode() {
  59. return Http.request({
  60. apiBaseUrl: config.apiBusinessUrl,
  61. url: '/app/qrCode',
  62. method: 'POST'
  63. })
  64. }