|
@@ -4,13 +4,13 @@ import { goWechatAuth } from '@/state';
|
|
|
import { ref } from 'vue';
|
|
|
/** 微信Code签名 */
|
|
|
const WX_CODE_SIGN = 'WXCODESIGN';
|
|
|
-/**
|
|
|
- * 签名有效时间 3分钟
|
|
|
- */
|
|
|
+/** 签名有效时间 3分钟 */
|
|
|
const SIGN_VALID_TIME = 1000 * 60 * 3;
|
|
|
+/** 微信OpenId */
|
|
|
+const WX_OPEN_ID = 'WXOPENID';
|
|
|
|
|
|
/**
|
|
|
- * 获取微信微信Code码
|
|
|
+ * 获取微信微信Code码 | 获取微信OPENID - 用于微信授权
|
|
|
*/
|
|
|
export default function useAuthCode() {
|
|
|
// 微信Code
|
|
@@ -21,7 +21,7 @@ export default function useAuthCode() {
|
|
|
* 获取微信appId
|
|
|
*/
|
|
|
const getAppId = async () => {
|
|
|
- if(wxAppId.value) return
|
|
|
+ if (wxAppId.value) return;
|
|
|
const { data } = await request.get('/edu-app/open/paramConfig/wechatAppId');
|
|
|
// 判断是否有微信appId
|
|
|
if (data) {
|
|
@@ -45,25 +45,11 @@ export default function useAuthCode() {
|
|
|
return true;
|
|
|
};
|
|
|
|
|
|
- const getOpenId = async (code: string) => {
|
|
|
- await getAppId()
|
|
|
- if(!wxAppId.value) {
|
|
|
- return
|
|
|
- }
|
|
|
- const { data } = await request.get('/edu-app/open/paramConfig/wechatOpenId', {
|
|
|
- params: {
|
|
|
- code,
|
|
|
- appId: wxAppId.value
|
|
|
- }
|
|
|
- });
|
|
|
- return data;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 去微信获取Code
|
|
|
* @param url 当前页面的url
|
|
|
*/
|
|
|
- const getWeChatAuthCode = async (url: string) => {
|
|
|
+ const getWeChatAuthCode = async (url?: string) => {
|
|
|
const authUrl = url || window.location.href;
|
|
|
// 判断是否在微信浏览器中打开
|
|
|
if (!browser().weixin) {
|
|
@@ -81,39 +67,103 @@ export default function useAuthCode() {
|
|
|
return;
|
|
|
}
|
|
|
// 设置签名
|
|
|
- sessionStorage.setItem(WX_CODE_SIGN, Date.now().toString());
|
|
|
+ onWeChatSign('SET', Date.now().toString());
|
|
|
goWechatAuth(wxAppId.value, authUrl);
|
|
|
} else {
|
|
|
weChatCode.value = code;
|
|
|
+ // onWeChatSign('REMOVE');
|
|
|
}
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * 获取微信有效Code
|
|
|
+ * 微信Code操作
|
|
|
+ * @param type 操作类型
|
|
|
*/
|
|
|
- const getAuthCode = () => {
|
|
|
- return weChatCode.value;
|
|
|
+ const onWeChatCode = (type: 'GET' | 'REMOVE') => {
|
|
|
+ if (type === 'GET') {
|
|
|
+ return weChatCode.value;
|
|
|
+ }
|
|
|
+ weChatCode.value = '';
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * 移除微信Code
|
|
|
+ * 微信sign操作
|
|
|
+ * @param type 操作类型
|
|
|
+ * @param value 值
|
|
|
*/
|
|
|
- const removeWeChatCode = () => {
|
|
|
- weChatCode.value = '';
|
|
|
+ const onWeChatSign = (type: 'GET' | 'REMOVE' | 'SET', value?: string) => {
|
|
|
+ if (type === 'GET') {
|
|
|
+ return sessionStorage.getItem(WX_CODE_SIGN);
|
|
|
+ } else if (type === 'REMOVE') {
|
|
|
+ sessionStorage.removeItem(WX_CODE_SIGN);
|
|
|
+ } else if (type === 'SET') {
|
|
|
+ sessionStorage.setItem(WX_CODE_SIGN, value || '');
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取缓存微信OpenId
|
|
|
+ * @param type 操作类型
|
|
|
+ * @param value 值
|
|
|
+ */
|
|
|
+ const onWeChatCatchOpenId = (
|
|
|
+ type: 'GET' | 'REMOVE' | 'SET',
|
|
|
+ value?: string
|
|
|
+ ) => {
|
|
|
+ if (type === 'GET') {
|
|
|
+ return sessionStorage.getItem(WX_OPEN_ID);
|
|
|
+ } else if (type === 'REMOVE') {
|
|
|
+ sessionStorage.removeItem(WX_OPEN_ID);
|
|
|
+ } else if (type === 'SET') {
|
|
|
+ sessionStorage.setItem(WX_OPEN_ID, value || '');
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * 移除微信签名
|
|
|
+ * 获取微信OpenId
|
|
|
+ * @param url 当前页面的url
|
|
|
+ * @param code 微信Code
|
|
|
*/
|
|
|
- const removeWeChatSign = () => {
|
|
|
- sessionStorage.removeItem(WX_CODE_SIGN);
|
|
|
+ const getOpenId = async (url?: string, code?: string) => {
|
|
|
+ // 获取缓存OpenId
|
|
|
+ const openId = onWeChatCatchOpenId('GET');
|
|
|
+ if (openId) {
|
|
|
+ return openId;
|
|
|
+ }
|
|
|
+ console.log(url, 'url');
|
|
|
+ // debugger
|
|
|
+ // 如果没有code - 去获取code
|
|
|
+ if (!code) {
|
|
|
+ await getWeChatAuthCode(url);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!code && !weChatCode.value) {
|
|
|
+ console.warn('没有获取到微信Code');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ await getAppId();
|
|
|
+ if (!wxAppId.value) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const { data } = await request.post(
|
|
|
+ '/edu-app/open/paramConfig/wechatOpenId',
|
|
|
+ {
|
|
|
+ data: {
|
|
|
+ code: code || weChatCode.value,
|
|
|
+ appId: wxAppId.value
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+ onWeChatCatchOpenId('SET', data);
|
|
|
+ return data;
|
|
|
};
|
|
|
|
|
|
return {
|
|
|
getOpenId,
|
|
|
getWeChatAuthCode,
|
|
|
- getAuthCode,
|
|
|
- removeWeChatSign,
|
|
|
- removeWeChatCode
|
|
|
+ onWeChatCode,
|
|
|
+ onWeChatSign,
|
|
|
+ onWeChatCatchOpenId
|
|
|
};
|
|
|
}
|