Skip to content

Let's use AI to Earn!

feat: 更换微信公众号登录逻辑

牛文政e031efc7893c
diff --git a/.env b/.env
index 6eb539af..6ef8bdf2 100644
--- a/.env
+++ b/.env
@@ -6,7 +6,7 @@ VITE_APP_FILE_HOST = 'https://ai-to-earn.oss-cn-beijing.aliyuncs.com/'
# VITE_APP_URL = 'https://attapi.yikart.cn/api'
# 测试
-VITE_APP_URL = 'https://wkknliyjrzzn.sealosbja.site/api'
+VITE_APP_URL = 'https://apitest.aitoearn.cn/api'
# 调试
# VITE_APP_URL = 'http://127.0.0.1:3000/api'
diff --git a/src/api/types/user-t.ts b/src/api/types/user-t.ts
index fd950dba..261f561c 100644
--- a/src/api/types/user-t.ts
+++ b/src/api/types/user-t.ts
@@ -1,8 +1,9 @@
export interface PhoneLoginParams {
- phone: 'string';
- code: 'string';
- invitePhone?: 'string';
- inviteCode?: 'string';
+ phone: string;
+ code: string;
+ openId?: string; // 微信公众号的openId
+ invitePhone?: string;
+ inviteCode?: string;
}
export interface IUserInfo {
diff --git a/src/api/user.ts b/src/api/user.ts
index f3e16875..5f7bcd19 100644
--- a/src/api/user.ts
+++ b/src/api/user.ts
@@ -8,6 +8,15 @@
import http from './request';
import { IRefreshToken, IUserInfo, PhoneLoginParams } from '@/api/types/user-t';
+export interface GzhLoginTyp {
+ phone: string;
+ token: string;
+ openId: string;
+ exp: number;
+ userInfo: IUserInfo;
+ status: -1 | 0 | 1; // 失败 登录中 登录成功
+}
+
export const userApi = {
// 手机号验证码登录
phoneLogin(data: PhoneLoginParams) {
@@ -47,12 +56,14 @@ export const userApi = {
// 轮询登录
wxGzhQrcodelogin(data: { ticket: string; key: string }) {
- return http.post<{
- token: string;
- exp: number;
- userInfo: IUserInfo;
- status: -1 | 0 | 1; // 未存在 未登录 已登录
- }>('/user/gzh/qrcode/login', data, {
+ return http.post<GzhLoginTyp>('/user/gzh/qrcode/login', data, {
+ isToken: false,
+ });
+ },
+
+ // 公众号手机号登录
+ phoneGzhLogin(data: PhoneLoginParams) {
+ return http.post<IRefreshToken>('/user/login/gzh/code/phone', data, {
isToken: false,
});
},
diff --git a/src/views/login/components/qrcodeLogin.tsx b/src/views/login/components/qrcodeLogin.tsx
index c9444009..030e3a17 100644
--- a/src/views/login/components/qrcodeLogin.tsx
+++ b/src/views/login/components/qrcodeLogin.tsx
@@ -6,14 +6,13 @@
* @Description: 微信二维码登录
*/
import { forwardRef, useImperativeHandle, useState, useEffect } from 'react';
-import { userApi } from '@/api/user';
+import { GzhLoginTyp, userApi } from '@/api/user';
import { Button, Modal, Form, Input, message } from 'antd';
import { ReloadOutlined } from '@ant-design/icons';
import styles from '../login.module.scss';
import { useUserStore } from '@/store/user';
import { useNavigate } from 'react-router-dom';
import GetCode from '@/components/GetCode';
-import { PhoneLoginParams } from '@/api/types/user-t';
export interface PubItemRef {
init: (pubRecord: any) => Promise<void>;
@@ -29,7 +28,7 @@ export default forwardRef<PubItemRef>((props, ref) => {
const [showMask, setShowMask] = useState(false);
const [showPhoneModal, setShowPhoneModal] = useState(false);
const [phoneForm] = Form.useForm();
- const [countdown, setCountdown] = useState(0);
+ const [openId, setOpenId] = useState('');
const [isFirstLogin, setIsFirstLogin] = useState(false);
const userStore = useUserStore();
const navigate = useNavigate();
@@ -52,10 +51,26 @@ export default forwardRef<PubItemRef>((props, ref) => {
if (ticketInfo.ticket) {
loginTimer = setInterval(async () => {
const res = await userApi.wxGzhQrcodelogin(ticketInfo);
- if (res.token !== '' && res.userInfo) {
- LoginSuccess(res);
+ console.log('GzhQrcode ======== ', res);
+
+ const { openId, token, phone, status } = res;
+
+ if (status === -1) {
+ message.error('登录失败,请重新扫码登录');
+ setShowMask(true);
+ clearInterval(loginTimer);
+ return;
+ }
+ setOpenId(openId);
+
+ // 登录完成
+ if (!token && !phone && res.userInfo) {
+ completeLogin(res);
clearInterval(loginTimer);
+ return;
}
+
+ loginByPhone(res);
}, 2000); // 每2秒检查一次
}
@@ -77,20 +92,13 @@ export default forwardRef<PubItemRef>((props, ref) => {
}
}
- const LoginSuccess = (res: any) => {
- if (!res) return;
-
- // 检查是否是首次登录
- if (res.userInfo && !res.userInfo.phone) {
- setIsFirstLogin(true);
- setShowPhoneModal(true);
- return;
- }
-
- completeLogin(res);
+ const loginByPhone = (res: GzhLoginTyp) => {
+ if (!res || !res.openId) return;
+ setIsFirstLogin(true);
+ setShowPhoneModal(true);
};
-
- const completeLogin = (res: any) => {
+
+ const completeLogin = (res: GzhLoginTyp) => {
window.ipcRenderer.invoke('ICP_USER_ADD', res.userInfo);
userStore.setToken(res);
userStore.getUserInfo(res.userInfo);
@@ -103,34 +111,24 @@ export default forwardRef<PubItemRef>((props, ref) => {
const handlePhoneSubmit = async () => {
try {
const values = await phoneForm.validateFields();
- // 调用绑定手机号接口
- const res = await userApi.upUserPhone({
+ // 调手机号登录
+ const res = await userApi.phoneGzhLogin({
phone: values.phone,
- code: values.code
+ code: values.code,
+ openId: openId,
});
-
+
if (res) {
message.success('手机号绑定成功');
setShowPhoneModal(false);
// 完成登录流程
- completeLogin(userStore.token);
+ completeLogin(values);
}
} catch (error) {
message.error('手机号绑定失败');
}
};
- /**
- * 登录
- */
- async function wxGzhQrcodelogin() {
- const res = await userApi.wxGzhQrcodelogin(ticketInfo);
- console.log('---- res', res);
- if (res.token !== '' && res.userInfo) {
- LoginSuccess(res);
- }
- }
-
useImperativeHandle(ref, () => ({
init: async (info: any) => {},
}));
@@ -194,57 +192,64 @@ export default forwardRef<PubItemRef>((props, ref) => {
closable={false}
maskClosable={false}
>
- <Form
- form={phoneForm}
- layout="vertical"
- onFinish={handlePhoneSubmit}
- >
+ <Form form={phoneForm} layout="vertical" onFinish={handlePhoneSubmit}>
<Form.Item
name="phone"
label="手机号"
rules={[
{ required: true, message: '请输入手机号' },
- { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号' }
+ { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号' },
]}
>
<Input placeholder="请输入手机号" />
</Form.Item>
-
+
<Form.Item
name="code"
label="验证码"
rules={[
{ required: true, message: '请输入验证码' },
- { len: 6, message: '验证码长度为6位' }
+ { len: 6, message: '验证码长度为6位' },
]}
>
- <div style={{ display: 'flex', alignItems: 'center', position: 'relative' }}>
+ <div
+ style={{
+ display: 'flex',
+ alignItems: 'center',
+ position: 'relative',
+ }}
+ >
<Input placeholder="请输入验证码" style={{ flex: 1 }} />
{/* <div style={{ marginLeft: '8px', width: '120px' }}> */}
- <GetCode
- onGetCode={async (unlock: () => void) => {
- const validateRes = await phoneForm
- .validateFields(['phone'])
- .catch(() => unlock());
- if (!validateRes) return;
- const res = await userApi.getUserCode({
- phone: phoneForm.getFieldValue('phone'),
+ <GetCode
+ onGetCode={async (unlock: () => void) => {
+ const validateRes = await phoneForm
+ .validateFields(['phone'])
+ .catch(() => unlock());
+ if (!validateRes) return;
+ const res = await userApi.getUserCode({
+ phone: phoneForm.getFieldValue('phone'),
+ });
+ if (!res) return;
+ message.success('验证码已发送');
+ if (typeof res === 'string') {
+ phoneForm.setFieldsValue({
+ code: res,
});
- if (!res) return;
- message.success('验证码已发送');
- if (typeof res === 'string') {
- phoneForm.setFieldsValue({
- code: res,
- });
- }
- }}
- />
+ }
+ }}
+ />
{/* </div> */}
</div>
</Form.Item>
-
+
<Form.Item>
- <Button type="primary" htmlType="submit" block style={{ marginTop: '10px' }}>
+ <Button
+ type="primary"
+ htmlType="submit"
+ block
+ style={{ marginTop: '10px' }}
+ >
确认绑定
</Button>
</Form.Item>