pay-employee/dist/dev/mp-weixin/common/store/useWeAppAuthStore.js

107 lines
3.9 KiB
JavaScript
Raw Normal View History

2025-07-08 16:49:39 +08:00
"use strict";
const common_vendor = require("../vendor.js");
const gen_Apis = require("../../gen/Apis.js");
const common_libraries_apiLoading = require("../libraries/apiLoading.js");
2025-12-15 18:27:46 +08:00
const wxworkId = "ww96c806131a95c739";
const wxAppId = "wx04d2d27ca198367b";
2025-07-08 16:49:39 +08:00
const useWeAppAuthStore = common_vendor.defineStore("we_app_auth", () => {
const loading = common_vendor.ref(true);
const hasError = common_vendor.ref(false);
const data = common_vendor.ref({
2025-12-15 18:27:46 +08:00
user: { id: 0, name: "", phone: "" },
2025-07-16 09:34:46 +08:00
work_info: { session_key: "", openid: "" },
environment: ""
2025-07-08 16:49:39 +08:00
});
function login(app) {
2025-07-09 00:34:00 +08:00
const res = common_vendor.index.getSystemInfoSync();
2025-07-16 09:34:46 +08:00
data.value.environment = res == null ? void 0 : res.environment;
console.log(res == null ? void 0 : res.environment, "res?.environment");
2025-07-09 00:34:00 +08:00
if ((res == null ? void 0 : res.environment) === "wxwork") {
2025-07-16 09:34:46 +08:00
common_vendor.index.qy.login({
success: function(res2) {
console.log(res2);
getWorkToken(app, res2.code);
2025-07-09 00:34:00 +08:00
}
});
} else {
2025-07-16 09:34:46 +08:00
common_vendor.index.login({
2025-07-09 00:34:00 +08:00
provider: "weixin",
//使用微信登录
success: function(loginRes) {
console.log(loginRes);
getWXToken(app, loginRes.code);
}
});
}
}
const getWorkToken = (app, code) => {
2025-07-16 09:34:46 +08:00
gen_Apis.Apis.Login.Auth.WorkLogin({ code, app_id: wxworkId }).then((res) => {
2025-12-15 18:27:46 +08:00
var _a, _b, _c, _d;
console.log("企业微信登录", res == null ? void 0 : res.data);
2025-07-09 00:34:00 +08:00
data.value.user = (_a = res.data) == null ? void 0 : _a.user;
2025-07-16 09:34:46 +08:00
data.value.work_info = (_b = res.data) == null ? void 0 : _b.work_info;
2025-07-09 00:34:00 +08:00
loading.value = false;
2025-07-16 09:34:46 +08:00
common_vendor.index.setStorageSync("ACCESS_TOKEN_CUSTOMER", (_d = (_c = res == null ? void 0 : res.data) == null ? void 0 : _c.token) == null ? void 0 : _d.token);
2025-07-09 00:34:00 +08:00
app == null ? void 0 : app.appContext.config.globalProperties.$isResolve();
}).catch(() => {
loading.value = false;
hasError.value = true;
2025-07-08 16:49:39 +08:00
});
2025-07-09 00:34:00 +08:00
};
const getWXToken = (app, code) => {
2025-07-16 09:34:46 +08:00
gen_Apis.Apis.Login.Auth.Login({ code, app_id: wxAppId }).then((res) => {
2025-12-15 18:27:46 +08:00
var _a, _b, _c;
2025-07-09 00:34:00 +08:00
console.log("登录", res == null ? void 0 : res.data);
data.value.user = (_a = res.data) == null ? void 0 : _a.user;
loading.value = false;
common_vendor.index.setStorageSync("ACCESS_TOKEN_CUSTOMER", (_c = (_b = res == null ? void 0 : res.data) == null ? void 0 : _b.token) == null ? void 0 : _c.token);
app == null ? void 0 : app.appContext.config.globalProperties.$isResolve();
}).catch(() => {
loading.value = false;
hasError.value = true;
});
};
2025-12-15 18:27:46 +08:00
function me(fun) {
2025-07-08 16:49:39 +08:00
common_libraries_apiLoading.getApiLoading(gen_Apis.Apis.Login.Auth.Me, {}).then((res) => {
data.value.user = res.data.user;
console.log("me", res.data);
loading.value = false;
hasError.value = false;
2025-12-15 18:27:46 +08:00
return fun();
2025-07-08 16:49:39 +08:00
});
}
2025-07-16 09:34:46 +08:00
const handleUserLoginWxwork = async (from_data) => {
var _a, _b;
common_libraries_apiLoading.getApiLoading(gen_Apis.Apis.Login.Auth.BindWork, {
app_id: wxAppId,
session_key: (_b = (_a = data == null ? void 0 : data.value) == null ? void 0 : _a.work_info) == null ? void 0 : _b.session_key,
iv: from_data == null ? void 0 : from_data.iv,
encrypted_data: from_data == null ? void 0 : from_data.encryptedData
}).then((res) => {
2025-12-15 18:27:46 +08:00
me(() => {
common_vendor.index.navigateBack({ delta: 1 });
});
2025-07-16 09:34:46 +08:00
});
};
2025-07-08 16:49:39 +08:00
const handleUserLogin = async (from_data) => {
2025-07-09 00:34:00 +08:00
common_libraries_apiLoading.getApiLoading(gen_Apis.Apis.Login.Auth.BindPhoneNumber, {
2025-07-16 09:34:46 +08:00
app_id: wxAppId,
2025-07-09 00:34:00 +08:00
...from_data
}).then((res) => {
2025-12-15 18:27:46 +08:00
me(() => {
common_vendor.index.navigateBack({ delta: 1 });
});
2025-07-08 16:49:39 +08:00
});
};
return {
loading,
hasError,
data,
login,
me,
2025-07-16 09:34:46 +08:00
handleUserLogin,
handleUserLoginWxwork
2025-07-08 16:49:39 +08:00
};
});
exports.useWeAppAuthStore = useWeAppAuthStore;