This commit is contained in:
@@ -69,7 +69,7 @@
|
|||||||
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 } from '@/utils/env.js';
|
import { APP_ID } from '@/utils/env.js';
|
||||||
import { wxScan, isInWechat } from '@/utils/wxsdk.js';
|
import { initWxConfig, wxScan, isInWechat } from '@/utils/wxsdk.js';
|
||||||
|
|
||||||
const userStore = useUserStore();
|
const userStore = useUserStore();
|
||||||
|
|
||||||
@@ -80,6 +80,18 @@
|
|||||||
}
|
}
|
||||||
handleWechatCallback();
|
handleWechatCallback();
|
||||||
getPathDeviceId();
|
getPathDeviceId();
|
||||||
|
|
||||||
|
// 初始化微信 SDK (仅在微信浏览器内执行)
|
||||||
|
// #ifdef H5
|
||||||
|
if (isInWechat()) {
|
||||||
|
try {
|
||||||
|
await initWxConfig();
|
||||||
|
console.log('微信 SDK 初始化成功');
|
||||||
|
} catch (e) {
|
||||||
|
console.error('微信 SDK 初始化失败', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// #endif
|
||||||
});
|
});
|
||||||
|
|
||||||
const identifier = ref('');
|
const identifier = ref('');
|
||||||
@@ -158,7 +170,7 @@ const handleScanLogin = async () => {
|
|||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('扫码错误:', err);
|
console.error('扫码错误:', err);
|
||||||
uni.showToast({ title: '扫码取消或失败', icon: 'none' });
|
uni.showToast({ title: JSON.stringify(err), icon: 'none' });
|
||||||
}
|
}
|
||||||
// #endif
|
// #endif
|
||||||
// #ifndef H5
|
// #ifndef H5
|
||||||
|
|||||||
@@ -1,4 +1,40 @@
|
|||||||
// utils/wxsdk.js —— 封装微信扫一扫
|
// utils/wxsdk.js —— 封装微信扫一扫
|
||||||
|
import wx from '@/node_modules/jweixin-module/lib/index'
|
||||||
|
import { wechatApi } from '@/api/index.js'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化微信 JS-SDK
|
||||||
|
*/
|
||||||
|
export async function initWxConfig() {
|
||||||
|
const url = window.location.href.split('#')[0]
|
||||||
|
|
||||||
|
try {
|
||||||
|
const data = await wechatApi.getJssdkConfig(url)
|
||||||
|
const { app_id, timestamp, nonce_str, signature } = data
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
wx.config({
|
||||||
|
debug: true,
|
||||||
|
appId: app_id,
|
||||||
|
timestamp: timestamp,
|
||||||
|
nonceStr: nonce_str,
|
||||||
|
signature: signature,
|
||||||
|
jsApiList: ['scanQRCode']
|
||||||
|
})
|
||||||
|
wx.ready(() => {
|
||||||
|
console.log('微信 SDK 就绪')
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
wx.error((err) => {
|
||||||
|
console.error('wx.config 失败', err)
|
||||||
|
reject(err)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
} catch (e) {
|
||||||
|
console.error('获取微信签名失败', e)
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 调起微信扫一扫
|
* 调起微信扫一扫
|
||||||
@@ -6,11 +42,12 @@
|
|||||||
*/
|
*/
|
||||||
export function wxScan() {
|
export function wxScan() {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
uni.scanCode({
|
wx.scanQRCode({
|
||||||
|
needResult: 1,
|
||||||
scanType: ['qrCode', 'barCode'],
|
scanType: ['qrCode', 'barCode'],
|
||||||
success(res) {
|
success(res) {
|
||||||
console.log('扫码成功:', res)
|
console.log('扫码成功:', res)
|
||||||
resolve(res.result)
|
resolve(res.resultStr)
|
||||||
},
|
},
|
||||||
fail(err) {
|
fail(err) {
|
||||||
console.error('扫码失败:', err)
|
console.error('扫码失败:', err)
|
||||||
|
|||||||
Reference in New Issue
Block a user