uni.request封装请求

http说明:

  1. 请求参数 options
    1. options={ url: url, method: GET,data:data,config:‘content-type的类型’,loading:”请求中” }
    2. options.loading为string类型 是”loading的文本”,不传loading就不显示
  2. token过期,清除token,获取新的token,再一次发送请求获取数据
  3. 网络断开,请求20秒超时,超时显示 “网络繁忙”

    1. var num = 0;
    2. export const http = (options) => {
    3. num = num + 1;
    4. // 加载中
    5. if (options.loading) {
    6. uni.showLoading({
    7. title: options.loading,
    8. })
    9. }
    10. return new Promise((resolve, reject) => {
    11. const request = uni.request({
    12. url: BASE_URL + options.url,
    13. method: options.method || "GET",
    14. timeout: 20000,
    15. header: {
    16. token: uni.getStorageSync("token"),
    17. childrenId: store.state.childrenId || "",
    18. "content-type": options.config || "" // 处理表单提交
    19. },
    20. data: options.data || {},
    21. success: async (res) => {
    22. // debugger
    23. if (res.data.code === 10001) {
    24. console.log("token过期");
    25. // 清除token
    26. try {
    27. uni.removeStorageSync("token");
    28. } catch (e) {
    29. console.log(e);
    30. }
    31. // 重新获取token
    32. await loadLoginState();
    33. //重新请求原来的接口获取数据
    34. await resolve(http(options))
    35. } else {
    36. // console.log("成功数据",res.data)
    37. //返回后端响应的数据
    38. resolve(res.data);
    39. }
    40. },
    41. fail: async (err) => {
    42. await uni.getNetworkType({
    43. complete: (res) => {
    44. if (res.networkType&&res.networkType == "none") {
    45. uni.showToast({
    46. icon: "none",
    47. title: "网络已断开"
    48. });
    49. //抛出错误----网络错误
    50. reject({
    51. errMsg: "网络已断开",
    52. code: 1,
    53. });
    54. } else {
    55. uni.showToast({
    56. icon: "none",
    57. title: "网络繁忙~~"
    58. });
    59. reject(err);
    60. }
    61. },
    62. });
    63. },
    64. complete: () => {
    65. num = num - 1;
    66. if (num === 0) {
    67. uni.hideLoading();
    68. }
    69. }
    70. });
    71. });
    72. };

    文件上传请求封装

  4. 文件上传请求参数options

    1. options = {url:”请求路径”, filePath:”文件路径”, fileName:”fileName文件名”, formData:”formData”, loading:”loading的文本” }
    2. loading为string类型 ,不传loading就不显示
  5. token过期,清除token,获取新的token,再一次发送请求获取数据
  6. 如果请求中网络断开,则超时1分钟报错返回,否则等网络连接继续接收数据返回
  1. let uploadNum = 1;
  2. export const uploadFile = (options) => {
  3. uploadNum = uploadNum + 1;
  4. return new Promise(async (resolve, reject) => {
  5. //显示loading
  6. if (options.loading) {
  7. uni.showLoading({
  8. title: options.loading,
  9. })
  10. }
  11. const uploadFile = await uni.uploadFile({
  12. url: BASE_URL + options.url, //仅为示例,非真实的接口地址
  13. filePath: options.filePath,
  14. header: {
  15. token: uni.getStorageSync("token"),
  16. childrenId: store.state.childrenId || ""
  17. },
  18. timeout: 60000,
  19. name: options.fileName,
  20. formData: options.formData,
  21. success: async (res) => {
  22. //获取的数据是json字符,装成js对象
  23. var res = JSON.parse(res.data);
  24. if (res.code === 10001) {
  25. console.log("token过期");
  26. // 清除token
  27. try {
  28. uni.removeStorageSync("token");
  29. } catch (e) {
  30. // console.log(e);
  31. }
  32. // 重新获取token
  33. await loadLoginState();
  34. //重新请求
  35. await resolve(uploadFile(options))
  36. } else {
  37. // console.log(res,"成功")
  38. resolve(res);
  39. }
  40. },
  41. complete: () => {
  42. uploadNum = uploadNum - 1;
  43. if (uploadNum <= 1) {
  44. uni.hideLoading();
  45. }
  46. },
  47. fail: async (err) => {
  48. console.log("失败", err)
  49. await uni.getNetworkType({
  50. complete: (res) => {
  51. if (res.networkType && res.networkType == "none") {
  52. uni.showToast({
  53. icon: "none",
  54. title: "网络已断开"
  55. });
  56. reject({
  57. errMsg: "网络已断开",
  58. code: 1,
  59. });
  60. } else {
  61. //抛出错误
  62. reject(err);
  63. uni.showToast({
  64. icon: "none",
  65. title: "网络繁忙~~"
  66. });
  67. }
  68. },
  69. });
  70. }
  71. });
  72. });
  73. };