host.js
// 配置地址 host.js
// const HOST = "https://xxxx.com"; //生成环境
const HOST = "http://xxxxx.com"; // 测试环境
export default HOST;
addr.js
// 接口统一管理 addr.js
import HOST from "./host";
const addr = {
LOGIN: `${HOST}/login`, // 登录
LOGOUT: `${HOST}/logout`, // 退出
};
export default addr;
https.js
// 方法请求统一处理 https.js
// TODO: 可以统一处理下get post逻辑
class Http {
constructor() {
this.requestParameter = {
url: "",
data: {},
method: "GET",
header: {
"content-type": "application/x-www-form-urlencoded",
},
};
}
async get(url, data) {
return new Promise((resolve, reject) => {
wx.request(
Object.assign({}, this.requestParameter, {
url: url,
data: data,
fail: (e) => {
console.log(`\u8BF7\u6C42\u5931\u8D25\uFF01\r\n \u9519\u8BEF\u4FE1\u606F\uFF1A${e.errMsg}`);
resolve("");
},
success: (e) => {
if (e.statusCode == 200) {
if (res.isok) {
resolve(res);
} else {
reject(res.Msg || "接口异常");
}
} else {
resolve("");
}
},
})
);
});
}
async post(url, data) {
return new Promise((resolve, reject) => {
wx.request(
Object.assign({}, this.requestParameter, {
url: url,
data: data,
method: "POST",
fail: (e) => {
console.log(e);
resolve("");
},
success: (res) => {
if (res.statusCode == 200) {
if (res.data.isok) {
resolve(res);
} else {
reject(res.data.Msg || "接口异常");
}
} else {
reject(res.errMsg);
}
},
})
);
});
}
}
export default Http;
tools.js
// 工具库 tools.js
var Tools = function () { }
//显示弹窗(确定)
Tools.showModalCancle = function (msg, title = "") {
return new Promise(function (resolve, reject) {
wx.showModal({
title: title || "提示",
content: msg,
showCancel: false,
success: res => {
resolve(res)
}
})
})
}
export default Tools;
service.js
// 请求方法封装 service.js
import addr from "../addr";
import Http from "../https";
import { tools } from "../core";
export default class Service extends Http {
constructor() {
super();
this.loginFailCount = 3;
}
// 检测是否登录
static sessionCheck() {
return new Promise((resolve, reject) => {
wx.checkSession({
async success() {
resolve(await Service.login());
},
async fail() {
resolve(await Service.login());
},
});
});
}
// 调取微信code
static login() {
return new Promise((resolve, reject) => {
wx.login({
success: (res) => {
if (res.code) {
resolve(res.code);
} else {
console.log(res, "code");
resolve(false);
}
},
fail: (e) => {
console.log(e, "code");
tools.showModalCancle(e.errMsg);
},
});
});
}
// 登录成功逻辑信息
static async processLoginSuccess(data) {
// TODO: 处理用户信息逻辑
return data;
}
// 登录失败逻辑信息
static async processLoginAbnor() {
if (this.loginFailCount <= 0) {
await tools.showModalCancle("请尝试刷新页面");
this.loginFailCount = 3;
return;
}
this.loginFailCount--;
return await Service._getUserInfo();
}
// 获取用户信息
static async _getUserInfo() {
const code = await Service.sessionCheck();
let errMsg = "";
if (code) {
const { data } = await new Http().post(addr.MiniGetSign, { code });
if (data) {
if (data.isok) {
return await Service.processLoginSuccess(data);
} else if (!data.isok) {
return await Service.processLoginAbnor();
}
} else {
errMsg = "服务器异常";
}
} else {
errMsg = "login获取code失败";
}
await tools.showModalCancle(errMsg || "未知的错误信息");
}
// 微信返回coke
async getSessionCheck() {
const code = await Service.sessionCheck();
return code;
}
/**
* @method 获取登录信息接口
* @param {*}
*/
async getUserInfo() {
if (global.userInfo) return global.userInfo;
return await Service._getUserInfo();
}
/**
* @method 退出登录
* @param {*}
*/
async logout(params) {
return await this.post(addr.LOGOUT, params);
}
}
core.js
// 提供组件引入js调用接口与工具方法
import Service from "./service/service";
import Tools from "../tools.js";
class Core {
constructor() {
this.service = new Service();
this.tools = Tools;
}
}
const core = new Core();
const service = core.service;
const tools = core.tools;
export default core;
export { service, tools };