host.js

  1. // 配置地址 host.js
  2. // const HOST = "https://xxxx.com"; //生成环境
  3. const HOST = "http://xxxxx.com"; // 测试环境
  4. export default HOST;

addr.js

  1. // 接口统一管理 addr.js
  2. import HOST from "./host";
  3. const addr = {
  4. LOGIN: `${HOST}/login`, // 登录
  5. LOGOUT: `${HOST}/logout`, // 退出
  6. };
  7. export default addr;

https.js

  1. // 方法请求统一处理 https.js
  2. // TODO: 可以统一处理下get post逻辑
  3. class Http {
  4. constructor() {
  5. this.requestParameter = {
  6. url: "",
  7. data: {},
  8. method: "GET",
  9. header: {
  10. "content-type": "application/x-www-form-urlencoded",
  11. },
  12. };
  13. }
  14. async get(url, data) {
  15. return new Promise((resolve, reject) => {
  16. wx.request(
  17. Object.assign({}, this.requestParameter, {
  18. url: url,
  19. data: data,
  20. fail: (e) => {
  21. console.log(`\u8BF7\u6C42\u5931\u8D25\uFF01\r\n \u9519\u8BEF\u4FE1\u606F\uFF1A${e.errMsg}`);
  22. resolve("");
  23. },
  24. success: (e) => {
  25. if (e.statusCode == 200) {
  26. if (res.isok) {
  27. resolve(res);
  28. } else {
  29. reject(res.Msg || "接口异常");
  30. }
  31. } else {
  32. resolve("");
  33. }
  34. },
  35. })
  36. );
  37. });
  38. }
  39. async post(url, data) {
  40. return new Promise((resolve, reject) => {
  41. wx.request(
  42. Object.assign({}, this.requestParameter, {
  43. url: url,
  44. data: data,
  45. method: "POST",
  46. fail: (e) => {
  47. console.log(e);
  48. resolve("");
  49. },
  50. success: (res) => {
  51. if (res.statusCode == 200) {
  52. if (res.data.isok) {
  53. resolve(res);
  54. } else {
  55. reject(res.data.Msg || "接口异常");
  56. }
  57. } else {
  58. reject(res.errMsg);
  59. }
  60. },
  61. })
  62. );
  63. });
  64. }
  65. }
  66. export default Http;

tools.js

  1. // 工具库 tools.js
  2. var Tools = function () { }
  3. //显示弹窗(确定)
  4. Tools.showModalCancle = function (msg, title = "") {
  5. return new Promise(function (resolve, reject) {
  6. wx.showModal({
  7. title: title || "提示",
  8. content: msg,
  9. showCancel: false,
  10. success: res => {
  11. resolve(res)
  12. }
  13. })
  14. })
  15. }
  16. export default Tools;

service.js

  1. // 请求方法封装 service.js
  2. import addr from "../addr";
  3. import Http from "../https";
  4. import { tools } from "../core";
  5. export default class Service extends Http {
  6. constructor() {
  7. super();
  8. this.loginFailCount = 3;
  9. }
  10. // 检测是否登录
  11. static sessionCheck() {
  12. return new Promise((resolve, reject) => {
  13. wx.checkSession({
  14. async success() {
  15. resolve(await Service.login());
  16. },
  17. async fail() {
  18. resolve(await Service.login());
  19. },
  20. });
  21. });
  22. }
  23. // 调取微信code
  24. static login() {
  25. return new Promise((resolve, reject) => {
  26. wx.login({
  27. success: (res) => {
  28. if (res.code) {
  29. resolve(res.code);
  30. } else {
  31. console.log(res, "code");
  32. resolve(false);
  33. }
  34. },
  35. fail: (e) => {
  36. console.log(e, "code");
  37. tools.showModalCancle(e.errMsg);
  38. },
  39. });
  40. });
  41. }
  42. // 登录成功逻辑信息
  43. static async processLoginSuccess(data) {
  44. // TODO: 处理用户信息逻辑
  45. return data;
  46. }
  47. // 登录失败逻辑信息
  48. static async processLoginAbnor() {
  49. if (this.loginFailCount <= 0) {
  50. await tools.showModalCancle("请尝试刷新页面");
  51. this.loginFailCount = 3;
  52. return;
  53. }
  54. this.loginFailCount--;
  55. return await Service._getUserInfo();
  56. }
  57. // 获取用户信息
  58. static async _getUserInfo() {
  59. const code = await Service.sessionCheck();
  60. let errMsg = "";
  61. if (code) {
  62. const { data } = await new Http().post(addr.MiniGetSign, { code });
  63. if (data) {
  64. if (data.isok) {
  65. return await Service.processLoginSuccess(data);
  66. } else if (!data.isok) {
  67. return await Service.processLoginAbnor();
  68. }
  69. } else {
  70. errMsg = "服务器异常";
  71. }
  72. } else {
  73. errMsg = "login获取code失败";
  74. }
  75. await tools.showModalCancle(errMsg || "未知的错误信息");
  76. }
  77. // 微信返回coke
  78. async getSessionCheck() {
  79. const code = await Service.sessionCheck();
  80. return code;
  81. }
  82. /**
  83. * @method 获取登录信息接口
  84. * @param {*}
  85. */
  86. async getUserInfo() {
  87. if (global.userInfo) return global.userInfo;
  88. return await Service._getUserInfo();
  89. }
  90. /**
  91. * @method 退出登录
  92. * @param {*}
  93. */
  94. async logout(params) {
  95. return await this.post(addr.LOGOUT, params);
  96. }
  97. }

core.js

  1. // 提供组件引入js调用接口与工具方法
  2. import Service from "./service/service";
  3. import Tools from "../tools.js";
  4. class Core {
  5. constructor() {
  6. this.service = new Service();
  7. this.tools = Tools;
  8. }
  9. }
  10. const core = new Core();
  11. const service = core.service;
  12. const tools = core.tools;
  13. export default core;
  14. export { service, tools };