From 0d90e20ec20f0aeee642756a7ebf9e8514174124 Mon Sep 17 00:00:00 2001
From: wangrong <wangrong@shsening.com>
Date: 星期一, 26 十月 2026 21:15:20 +0800
Subject: [PATCH] 微信小程序插件支持
---
src/modules/user/service/login.ts | 84 +++++++++++++++++++++++++++++++++++++++--
1 files changed, 79 insertions(+), 5 deletions(-)
diff --git a/src/modules/user/service/login.ts b/src/modules/user/service/login.ts
index f1260bd..4964cfe 100644
--- a/src/modules/user/service/login.ts
+++ b/src/modules/user/service/login.ts
@@ -78,12 +78,85 @@
* @param encryptedData
* @param iv
*/
- async miniPhone(code, encryptedData, iv) {
+ async miniPhone(code, encryptedData, iv, loginCode?) {
const phone = await this.userWxService.miniPhone(code, encryptedData, iv);
- if (phone) {
- return await this.phone(phone);
- } else {
+ if (!phone) {
throw new CoolCommException('鑾峰緱鎵嬫満鍙峰け璐ワ紝璇锋鏌ラ厤缃�');
+ }
+ const token = await this.phone(phone);
+ await this.bindMiniOpenid(phone, loginCode);
+ return {
+ ...token,
+ phone,
+ };
+ }
+
+ /**
+ * 灏忕▼搴忓啀娆℃墦寮�鏃讹紝鐢� openid 闈欓粯鐧诲綍
+ */
+ async miniAuto(code: string) {
+ if (!code) {
+ return null;
+ }
+ const session = await this.userWxService.miniSession(code);
+ if (!session?.openid || session.errcode) {
+ return null;
+ }
+ const wx = await this.userWxEntity.findOneBy({
+ openid: Equal(session.openid),
+ });
+ if (!wx?.unionid) {
+ return null;
+ }
+ const user = await this.userInfoEntity.findOneBy({
+ unionid: Equal(wx.unionid),
+ });
+ if (!user?.phone || user.status === 2) {
+ return null;
+ }
+ const token = await this.token({ id: user.unionid });
+ return {
+ ...token,
+ phone: user.phone,
+ nickName: user.nickName,
+ avatarUrl: user.avatarUrl,
+ unionid: user.unionid,
+ id: user.id,
+ };
+ }
+
+ /**
+ * 鎶婂綋鍓嶅井淇� openid 璁板埌鎵嬫満鍙疯处鍙蜂笂锛屼緵涓嬫闈欓粯鐧诲綍
+ */
+ async bindMiniOpenid(phone: string, loginCode?: string) {
+ if (!loginCode) {
+ return;
+ }
+ try {
+ const session = await this.userWxService.miniSession(loginCode);
+ if (!session?.openid || session.errcode) {
+ return;
+ }
+ const user = await this.userInfoEntity.findOneBy({
+ phone: Equal(phone),
+ });
+ if (!user) {
+ return;
+ }
+ const unionid = user.unionid || session.unionid || phone;
+ const exist = await this.userWxEntity.findOneBy({
+ openid: Equal(session.openid),
+ });
+ await this.userWxEntity.save({
+ id: exist?.id,
+ openid: session.openid,
+ unionid,
+ nickName: user.nickName,
+ avatarUrl: user.avatarUrl,
+ type: 0,
+ });
+ } catch (err) {
+ // 缁戝畾 openid 澶辫触涓嶅奖鍝嶆湰娆℃墜鏈哄彿鐧诲綍
}
}
@@ -115,7 +188,8 @@
user = {
phone,
unionid: phone,
- loginType: 2,
+ loginType: 0,
+ departmentId: 1,
nickName: phone.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2'),
};
await this.userInfoEntity.insert(user);
--
Gitblit v1.9.1