This commit is contained in:
@@ -69,17 +69,29 @@
|
|||||||
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 weChatScanService from '@/utils/wxsdk.js';
|
import { initWxConfig, wxScan, isInWechat } from '@/utils/wxsdk.js';
|
||||||
|
|
||||||
const userStore = useUserStore();
|
const userStore = useUserStore();
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(async () => {
|
||||||
const token = uni.getStorageSync('token');
|
const token = uni.getStorageSync('token');
|
||||||
if (token) {
|
if (token) {
|
||||||
uni.reLaunch({ url: '/pages/index/index' });
|
uni.reLaunch({ url: '/pages/index/index' });
|
||||||
}
|
}
|
||||||
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('');
|
||||||
@@ -128,49 +140,37 @@ const handleScanLogin = async () => {
|
|||||||
// #ifdef H5
|
// #ifdef H5
|
||||||
try {
|
try {
|
||||||
// 检查是否在微信环境
|
// 检查是否在微信环境
|
||||||
if (!weChatScanService.isInWechat()) {
|
if (!isInWechat()) {
|
||||||
uni.showToast({ title: '请在微信中打开', icon: 'none' });
|
uni.showToast({ title: '请在微信中打开', icon: 'none' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化微信 SDK
|
// 调用扫一扫
|
||||||
const initResult = await weChatScanService.initWxSDK();
|
const resultStr = await wxScan();
|
||||||
console.log('微信SDK初始化结果:', initResult);
|
console.log('扫码结果:', resultStr);
|
||||||
|
|
||||||
if (!initResult.success) {
|
let value = '';
|
||||||
uni.showToast({ title: initResult.error || '扫一扫功能暂不可用', icon: 'none' });
|
|
||||||
return;
|
// 解析二维码内容
|
||||||
|
if (resultStr.includes('?')) {
|
||||||
|
const params = new URLSearchParams(resultStr.split('?')[1]);
|
||||||
|
value = params.get('cardNum') || '';
|
||||||
}
|
}
|
||||||
|
|
||||||
// 调用扫一扫
|
if (!value) {
|
||||||
const scanResult = await weChatScanService.scanQRCode();
|
const match = resultStr.match(/=([^=]+)$/);
|
||||||
console.log('扫码结果:', scanResult);
|
value = match ? match[1] : '';
|
||||||
|
}
|
||||||
|
|
||||||
if (scanResult.success) {
|
if (value) {
|
||||||
const resultStr = scanResult.result;
|
identifier.value = value;
|
||||||
let value = '';
|
handleLogin();
|
||||||
|
} else {
|
||||||
// 解析二维码内容
|
uni.showToast({ title: '无效的二维码', icon: 'none' });
|
||||||
if (resultStr.includes('?')) {
|
|
||||||
const params = new URLSearchParams(resultStr.split('?')[1]);
|
|
||||||
value = params.get('cardNum') || '';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!value) {
|
|
||||||
const match = resultStr.match(/=([^=]+)$/);
|
|
||||||
value = match ? match[1] : '';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (value) {
|
|
||||||
identifier.value = value;
|
|
||||||
handleLogin();
|
|
||||||
} else {
|
|
||||||
uni.showToast({ title: '无效的二维码', icon: 'none' });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('扫码错误:', err);
|
console.error('扫码错误:', err);
|
||||||
uni.showToast({ title: err.message || '扫码失败', icon: 'none' });
|
uni.showToast({ title: '扫码取消或失败', icon: 'none' });
|
||||||
}
|
}
|
||||||
// #endif
|
// #endif
|
||||||
// #ifndef H5
|
// #ifndef H5
|
||||||
|
|||||||
157
utils/wxsdk.js
157
utils/wxsdk.js
@@ -1,108 +1,67 @@
|
|||||||
import * as wx from 'jweixin-module';
|
// utils/wxsdk.js —— 封装微信扫一扫
|
||||||
import { wechatApi } from '@/api/index.js';
|
import jweixin from 'jweixin-module'
|
||||||
|
import { wechatApi } from '@/api/index.js'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信 JS-SDK 初始化和扫码功能封装
|
* 初始化微信 JS-SDK
|
||||||
*/
|
*/
|
||||||
class WeChatScanService {
|
export async function initWxConfig() {
|
||||||
constructor() {
|
// iOS 微信 SPA 需用首次进入的 URL 签名
|
||||||
this.isWxReady = false;
|
const url = window.location.href.split('#')[0]
|
||||||
this.config = {
|
|
||||||
debug: true, // 开发环境设置为 true 便于调试
|
|
||||||
jsApiList: ['scanQRCode']
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
try {
|
||||||
* 初始化微信 JS-SDK
|
// 调用后端接口获取签名
|
||||||
*/
|
const data = await wechatApi.getJssdkConfig(url)
|
||||||
async initWxSDK() {
|
const { app_id, timestamp, nonce_str, signature } = data
|
||||||
try {
|
|
||||||
console.log('[WxSDK] 开始初始化...');
|
|
||||||
|
|
||||||
// 获取当前页面 URL (去掉 hash 部分)
|
|
||||||
const currentUrl = window.location.href.split('#')[0];
|
|
||||||
console.log('[WxSDK] 当前URL:', currentUrl);
|
|
||||||
|
|
||||||
// 调用后端接口获取签名
|
|
||||||
const signatureData = await wechatApi.getJssdkConfig(currentUrl);
|
|
||||||
console.log('[WxSDK] 获取到签名配置:', signatureData);
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
wx.config({
|
|
||||||
debug: this.config.debug,
|
|
||||||
appId: signatureData.app_id,
|
|
||||||
timestamp: signatureData.timestamp,
|
|
||||||
nonceStr: signatureData.nonce_str,
|
|
||||||
signature: signatureData.signature,
|
|
||||||
jsApiList: this.config.jsApiList
|
|
||||||
});
|
|
||||||
|
|
||||||
wx.ready(() => {
|
|
||||||
console.log('[WxSDK] 初始化成功');
|
|
||||||
this.isWxReady = true;
|
|
||||||
resolve({ success: true });
|
|
||||||
});
|
|
||||||
|
|
||||||
wx.error((res) => {
|
|
||||||
console.error('[WxSDK] 初始化失败:', res);
|
|
||||||
this.isWxReady = false;
|
|
||||||
reject({ success: false, error: res.errMsg || '微信配置失败' });
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} catch (e) {
|
|
||||||
console.error('[WxSDK] 初始化异常:', e);
|
|
||||||
return { success: false, error: e.message || '初始化失败' };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 调用微信扫一扫功能
|
|
||||||
*/
|
|
||||||
async scanQRCode(options = {}) {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!this.isWxReady) {
|
jweixin.config({
|
||||||
reject({ success: false, error: '微信 JS-SDK 未就绪,请先初始化' });
|
debug: true,
|
||||||
return;
|
appId: app_id,
|
||||||
}
|
timestamp: timestamp,
|
||||||
|
nonceStr: nonce_str,
|
||||||
const defaultOptions = {
|
signature: signature,
|
||||||
needResult: 1,
|
jsApiList: ['scanQRCode']
|
||||||
scanType: ['qrCode', 'barCode']
|
})
|
||||||
};
|
jweixin.ready(() => {
|
||||||
|
console.log('微信 SDK 就绪')
|
||||||
const scanOptions = { ...defaultOptions, ...options };
|
resolve()
|
||||||
|
})
|
||||||
wx.scanQRCode({
|
jweixin.error((err) => {
|
||||||
needResult: scanOptions.needResult,
|
console.error('wx.config 失败', err)
|
||||||
scanType: scanOptions.scanType,
|
reject(err)
|
||||||
success: (res) => {
|
})
|
||||||
console.log('[WxSDK] 扫码成功:', res);
|
})
|
||||||
resolve({
|
} catch (e) {
|
||||||
success: true,
|
console.error('获取微信签名失败', e)
|
||||||
result: res.resultStr || res.result,
|
throw e
|
||||||
data: res
|
|
||||||
});
|
|
||||||
},
|
|
||||||
fail: (err) => {
|
|
||||||
console.error('[WxSDK] 扫码失败:', err);
|
|
||||||
reject({
|
|
||||||
success: false,
|
|
||||||
error: err,
|
|
||||||
message: '扫码失败,请重试'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 检查是否在微信环境中
|
|
||||||
*/
|
|
||||||
isInWechat() {
|
|
||||||
return /micromessenger/i.test(navigator.userAgent);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 导出单例
|
/**
|
||||||
export default new WeChatScanService();
|
* 调起微信扫一扫
|
||||||
|
* @returns {Promise<string>} 扫码结果字符串
|
||||||
|
*/
|
||||||
|
export function wxScan() {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
jweixin.scanQRCode({
|
||||||
|
needResult: 1, // 1 = 返回结果给开发者
|
||||||
|
scanType: ['qrCode', 'barCode'], // 同时支持二维码和条形码
|
||||||
|
success(res) {
|
||||||
|
console.log('扫码成功:', res)
|
||||||
|
resolve(res.resultStr)
|
||||||
|
},
|
||||||
|
fail(err) {
|
||||||
|
console.error('扫码失败:', err)
|
||||||
|
reject(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查是否在微信环境中
|
||||||
|
*/
|
||||||
|
export function isInWechat() {
|
||||||
|
return /micromessenger/i.test(navigator.userAgent)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user