This commit is contained in:
@@ -68,7 +68,6 @@
|
|||||||
import { ref, onMounted } from 'vue';
|
import { ref, onMounted } from 'vue';
|
||||||
import { authApi } from '@/api/index.js';
|
import { authApi } from '@/api/index.js';
|
||||||
import { useUserStore } from '@/store/index.js';
|
import { useUserStore } from '@/store/index.js';
|
||||||
import { APP_ID, USE_WECHAT_AUTH } from '@/utils/env.js';
|
|
||||||
import { initWxSdk } from '@/utils/wxsdk.js';
|
import { initWxSdk } from '@/utils/wxsdk.js';
|
||||||
|
|
||||||
const userStore = useUserStore();
|
const userStore = useUserStore();
|
||||||
@@ -78,7 +77,6 @@
|
|||||||
if (token) {
|
if (token) {
|
||||||
uni.reLaunch({ url: '/pages/index/index' });
|
uni.reLaunch({ url: '/pages/index/index' });
|
||||||
}
|
}
|
||||||
handleWechatCallback();
|
|
||||||
getPathDeviceId();
|
getPathDeviceId();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -88,22 +86,6 @@
|
|||||||
const inputFocus = ref(false);
|
const inputFocus = ref(false);
|
||||||
const showError = ref(false);
|
const showError = ref(false);
|
||||||
|
|
||||||
const getCode = () => {
|
|
||||||
const params = new URLSearchParams(window.location.search);
|
|
||||||
return params.get('code');
|
|
||||||
};
|
|
||||||
|
|
||||||
const clearCode = () => {
|
|
||||||
window.history.replaceState({}, '', window.location.pathname);
|
|
||||||
};
|
|
||||||
|
|
||||||
const redirectToWxAuth = (assetToken) => {
|
|
||||||
sessionStorage.setItem('assetToken', assetToken);
|
|
||||||
const redirectUri = encodeURIComponent('https://cmp-c.boss160.cn/');
|
|
||||||
const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${APP_ID}&redirect_uri=${redirectUri}&response_type=code&scope=snsapi_userinfo&state=wechat_auth#wechat_redirect`;
|
|
||||||
window.location.href = url;
|
|
||||||
};
|
|
||||||
|
|
||||||
const clearInput = () => {
|
const clearInput = () => {
|
||||||
identifier.value = '';
|
identifier.value = '';
|
||||||
showError.value = false;
|
showError.value = false;
|
||||||
@@ -136,9 +118,20 @@
|
|||||||
scanType: ['qrCode', 'barCode'],
|
scanType: ['qrCode', 'barCode'],
|
||||||
success: (res) => {
|
success: (res) => {
|
||||||
const result = res.resultStr;
|
const result = res.resultStr;
|
||||||
const match = result.match(/=([^=]+)$/);
|
let value = '';
|
||||||
if (match && match[1]) {
|
|
||||||
identifier.value = match[1];
|
if (result.includes('?')) {
|
||||||
|
const params = new URLSearchParams(result.split('?')[1]);
|
||||||
|
value = params.get('cardNum') || '';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!value) {
|
||||||
|
const match = result.match(/=([^=]+)$/);
|
||||||
|
value = match ? match[1] : '';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value) {
|
||||||
|
identifier.value = value;
|
||||||
handleLogin();
|
handleLogin();
|
||||||
} else {
|
} else {
|
||||||
uni.showToast({ title: '无效的二维码', icon: 'none' });
|
uni.showToast({ title: '无效的二维码', icon: 'none' });
|
||||||
@@ -170,58 +163,17 @@
|
|||||||
userStore.setAssetToken(verifyData.asset_token);
|
userStore.setAssetToken(verifyData.asset_token);
|
||||||
userStore.setIdentifier(identifier.value);
|
userStore.setIdentifier(identifier.value);
|
||||||
|
|
||||||
if (!USE_WECHAT_AUTH) {
|
const loginData = await authApi.wechatLogin(verifyData.asset_token, 'mock_code');
|
||||||
const loginData = await authApi.wechatLogin(verifyData.asset_token, 'mock_code');
|
|
||||||
userStore.setToken(loginData.token);
|
|
||||||
uni.setStorageSync('identifier', userStore.state.identifier);
|
|
||||||
|
|
||||||
if (loginData.need_bind_phone) {
|
|
||||||
uni.redirectTo({ url: '/pages/bind/bind?fromLogin=true' });
|
|
||||||
} else {
|
|
||||||
uni.redirectTo({ url: '/pages/index/index' });
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
redirectToWxAuth(verifyData.asset_token);
|
|
||||||
} catch (e) {
|
|
||||||
console.error('登录失败', e);
|
|
||||||
loading.value = false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleWechatCallback = async () => {
|
|
||||||
if (!USE_WECHAT_AUTH) return;
|
|
||||||
|
|
||||||
const code = getCode();
|
|
||||||
const assetToken = sessionStorage.getItem('assetToken');
|
|
||||||
|
|
||||||
if (!code || !assetToken) return;
|
|
||||||
|
|
||||||
loading.value = true;
|
|
||||||
clearCode();
|
|
||||||
|
|
||||||
try {
|
|
||||||
const loginData = await authApi.wechatLogin(assetToken, code);
|
|
||||||
userStore.setToken(loginData.token);
|
userStore.setToken(loginData.token);
|
||||||
uni.setStorageSync('identifier', userStore.state.identifier);
|
uni.setStorageSync('identifier', userStore.state.identifier);
|
||||||
|
|
||||||
// #ifdef H5
|
|
||||||
if (loginData.user_info) {
|
|
||||||
userStore.setUserInfo({
|
|
||||||
avatar: loginData.user_info.avatar || '',
|
|
||||||
nickname: loginData.user_info.nickname || ''
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// #endif
|
|
||||||
|
|
||||||
if (loginData.need_bind_phone) {
|
if (loginData.need_bind_phone) {
|
||||||
uni.redirectTo({ url: '/pages/bind/bind?fromLogin=true' });
|
uni.redirectTo({ url: '/pages/bind/bind?fromLogin=true' });
|
||||||
} else {
|
} else {
|
||||||
uni.redirectTo({ url: '/pages/index/index' });
|
uni.redirectTo({ url: '/pages/index/index' });
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('微信登录失败', e);
|
console.error('登录失败', e);
|
||||||
loading.value = false;
|
loading.value = false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,6 +2,4 @@ export const BASE_URL = 'https://cmp-api.boss160.cn';
|
|||||||
|
|
||||||
export const APP_TYPE = 'official_account';
|
export const APP_TYPE = 'official_account';
|
||||||
|
|
||||||
export const APP_ID = 'wx404dc822ce15c5f0';
|
export const APP_ID = 'wx404dc822ce15c5f0';
|
||||||
|
|
||||||
export const USE_WECHAT_AUTH = true;
|
|
||||||
Reference in New Issue
Block a user