diff --git a/package.json b/package.json index f955551..ec2b551 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,8 @@ "@vitejs/plugin-vue": "^6.0.1", "vite": "^7.2.2", "@element-plus/icons-vue": "^2.3.2", - "element-plus": "^2.11.7" + "element-plus": "^2.11.7", + "@google/genai": "^1.27.0" }, "engines": { "node": ">=16.0.0", diff --git a/packages/utils/src/utils/wechaBus copy.js b/packages/utils/src/utils/wechaBus copy.js new file mode 100644 index 0000000..ac6d655 --- /dev/null +++ b/packages/utils/src/utils/wechaBus copy.js @@ -0,0 +1,56 @@ +export class WechatBus { + constructor(wx) { + this.wx = wx; + } + + objectToUrlParams(obj, prefix = '') { + const params = [] + for (const key in obj) { + if (obj.hasOwnProperty(key)) { + const value = obj[key] + const paramKey = prefix ? `${prefix}[${key}]` : key + + if (value !== null && value !== undefined) { + if (typeof value === 'object' && !Array.isArray(value)) { + const nestedParams = this.objectToUrlParams(value, paramKey) + params.push(...nestedParams) + } else if (Array.isArray(value)) { + value.forEach((item, index) => { + if (typeof item === 'object') { + const nestedParams = this.objectToUrlParams(item, `${paramKey}[${index}]`) + params.push(...nestedParams) + } else { + params.push(`${encodeURIComponent(paramKey)}[${index}]=${encodeURIComponent(item)}`) + } + }) + } else { + params.push(`${encodeURIComponent(paramKey)}=${encodeURIComponent(value)}`) + } + } + } + } + + return params + } + // 导航到微信小程序页面并且携带通信参数 + BusWechartForNavigate(url, data = {}) { + let finalUrl = url + if (data && typeof data === 'object' && Object.keys(data).length > 0) { + const params = this.objectToUrlParams(data) + if (params.length > 0) { + const separator = url.includes('?') ? '&' : '?' + finalUrl = url + separator + params.join('&') + } + } + this.wx.navigateTo({ + url: finalUrl + }) + } + //通过共享存储通信 + BusWechartForStorageSync(key,data = {}) { + wx.miniProgram.setStorageSync({ + key: key, + data: data + }) + } +} \ No newline at end of file diff --git a/packages/utils/src/utils/wechaBus.js b/packages/utils/src/utils/wechaBus.js index 0dd8da7..cd6ea8f 100644 --- a/packages/utils/src/utils/wechaBus.js +++ b/packages/utils/src/utils/wechaBus.js @@ -1,32 +1,58 @@ -const wx = window.wx || window.jWeixin - -class WechatBus { +export class WechatBus { constructor() { + this.wx = null this.isReady = false - this.messageHandlers = new Map() - this.eventListeners = new Map() this.init() } init() { - if (wx) { - this.isReady = true - console.log('WechatBus initialized successfully') - } else { - console.warn('Wechat JSSDK not loaded') + if (typeof window !== 'undefined') { + this.wx = window.wx || window.jWeixin + if (this.wx && this.wx.miniProgram) { + this.isReady = true + console.log('WechatBus initialized successfully') + } else { + console.warn('Wechat JSSDK not loaded or miniProgram not available') + } } } checkReady() { - if (!this.isReady) { - throw new Error('Wechat JSSDK is not ready') + if (!this.isReady || !this.wx || !this.wx.miniProgram) { + throw new Error('Wechat JSSDK is not ready. Please ensure JSSDK is loaded.') } } + loadJSSDK(src = 'https://res.wx.qq.com/open/js/jweixin-1.6.0.js') { + return new Promise((resolve, reject) => { + if (typeof window === 'undefined') { + reject(new Error('Window object not available')) + return + } + + if (window.wx || window.jWeixin) { + this.init() + resolve(true) + return + } + + const script = document.createElement('script') + script.src = src + script.onload = () => { + this.init() + resolve(true) + } + script.onerror = () => { + reject(new Error('Failed to load Wechat JSSDK')) + } + document.head.appendChild(script) + }) + } + config(config) { this.checkReady() return new Promise((resolve, reject) => { - wx.config({ + this.wx.config({ debug: config.debug || false, appId: config.appId, timestamp: config.timestamp, @@ -35,24 +61,139 @@ class WechatBus { jsApiList: config.jsApiList || [] }) - wx.ready(() => { - this.isReady = true + this.wx.ready(() => { console.log('Wechat JSSDK config ready') resolve(true) }) - wx.error((res) => { + this.wx.error((res) => { console.error('Wechat JSSDK config error:', res) reject(res) }) }) } + objectToUrlParams(obj, prefix = '') { + const params = [] + + for (const key in obj) { + if (obj.hasOwnProperty(key)) { + const value = obj[key] + const paramKey = prefix ? `${prefix}[${key}]` : key + + if (value !== null && value !== undefined) { + if (typeof value === 'object' && !Array.isArray(value)) { + const nestedParams = this.objectToUrlParams(value, paramKey) + params.push(...nestedParams) + } else if (Array.isArray(value)) { + value.forEach((item, index) => { + if (typeof item === 'object') { + const nestedParams = this.objectToUrlParams(item, `${paramKey}[${index}]`) + params.push(...nestedParams) + } else { + params.push(`${encodeURIComponent(paramKey)}[${index}]=${encodeURIComponent(item)}`) + } + }) + } else { + params.push(`${encodeURIComponent(paramKey)}=${encodeURIComponent(value)}`) + } + } + } + } + + return params + } + + navigateTo(url, data = {}) { + this.checkReady() + let finalUrl = url + + if (data && typeof data === 'object' && Object.keys(data).length > 0) { + const params = this.objectToUrlParams(data) + if (params.length > 0) { + const separator = url.includes('?') ? '&' : '?' + finalUrl = url + separator + params.join('&') + } + } + + return new Promise((resolve, reject) => { + this.wx.miniProgram.navigateTo({ + url: finalUrl, + success: (res) => resolve(res), + fail: (error) => reject(error) + }) + }) + } + + navigateBack(delta = 1) { + this.checkReady() + return new Promise((resolve, reject) => { + this.wx.miniProgram.navigateBack({ + delta, + success: (res) => resolve(res), + fail: (error) => reject(error) + }) + }) + } + + redirectTo(url, data = {}) { + this.checkReady() + let finalUrl = url + + if (data && typeof data === 'object' && Object.keys(data).length > 0) { + const params = this.objectToUrlParams(data) + if (params.length > 0) { + const separator = url.includes('?') ? '&' : '?' + finalUrl = url + separator + params.join('&') + } + } + + return new Promise((resolve, reject) => { + this.wx.miniProgram.redirectTo({ + url: finalUrl, + success: (res) => resolve(res), + fail: (error) => reject(error) + }) + }) + } + + switchTab(url) { + this.checkReady() + return new Promise((resolve, reject) => { + this.wx.miniProgram.switchTab({ + url, + success: (res) => resolve(res), + fail: (error) => reject(error) + }) + }) + } + + reLaunch(url, data = {}) { + this.checkReady() + let finalUrl = url + + if (data && typeof data === 'object' && Object.keys(data).length > 0) { + const params = this.objectToUrlParams(data) + if (params.length > 0) { + const separator = url.includes('?') ? '&' : '?' + finalUrl = url + separator + params.join('&') + } + } + + return new Promise((resolve, reject) => { + this.wx.miniProgram.reLaunch({ + url: finalUrl, + success: (res) => resolve(res), + fail: (error) => reject(error) + }) + }) + } + postMessage(data) { this.checkReady() return new Promise((resolve, reject) => { try { - wx.miniProgram.postMessage({ + this.wx.miniProgram.postMessage({ data }) resolve(true) @@ -63,130 +204,21 @@ class WechatBus { }) } - navigateTo(url) { - this.checkReady() - return new Promise((resolve, reject) => { - try { - wx.miniProgram.navigateTo({ - url, - success: (res) => resolve(res), - fail: (error) => reject(error) - }) - } catch (error) { - console.error('Navigate to error:', error) - reject(error) - } - }) - } - - navigateBack(delta = 1) { - this.checkReady() - return new Promise((resolve, reject) => { - try { - wx.miniProgram.navigateBack({ - delta, - success: (res) => resolve(res), - fail: (error) => reject(error) - }) - } catch (error) { - console.error('Navigate back error:', error) - reject(error) - } - }) - } - - redirectTo(url) { - this.checkReady() - return new Promise((resolve, reject) => { - try { - wx.miniProgram.redirectTo({ - url, - success: (res) => resolve(res), - fail: (error) => reject(error) - }) - } catch (error) { - console.error('Redirect to error:', error) - reject(error) - } - }) - } - - switchTab(url) { - this.checkReady() - return new Promise((resolve, reject) => { - try { - wx.miniProgram.switchTab({ - url, - success: (res) => resolve(res), - fail: (error) => reject(error) - }) - } catch (error) { - console.error('Switch tab error:', error) - reject(error) - } - }) - } - - reLaunch(url) { - this.checkReady() - return new Promise((resolve, reject) => { - try { - wx.miniProgram.reLaunch({ - url, - success: (res) => resolve(res), - fail: (error) => reject(error) - }) - } catch (error) { - console.error('ReLaunch error:', error) - reject(error) - } - }) - } - getEnv() { this.checkReady() - return wx.miniProgram.getEnv() + return this.wx.miniProgram.getEnv() } getAccountInfoSync() { this.checkReady() - return wx.getAccountInfoSync() - } - - on(event, handler) { - if (!this.eventListeners.has(event)) { - this.eventListeners.set(event, new Set()) - } - this.eventListeners.get(event).add(handler) - } - - off(event, handler) { - if (this.eventListeners.has(event)) { - if (handler) { - this.eventListeners.get(event).delete(handler) - } else { - this.eventListeners.delete(event) - } - } - } - - emit(event, data) { - if (this.eventListeners.has(event)) { - this.eventListeners.get(event).forEach(handler => { - try { - handler(data) - } catch (error) { - console.error(`Event handler error for ${event}:`, error) - } - }) - } + return this.wx.getAccountInfoSync() } setStorageSync(key, data) { this.checkReady() return new Promise((resolve, reject) => { try { - wx.miniProgram.setStorageSync({ + this.wx.miniProgram.setStorageSync({ key, data }) @@ -202,7 +234,7 @@ class WechatBus { this.checkReady() return new Promise((resolve, reject) => { try { - const data = wx.miniProgram.getStorageSync({ + const data = this.wx.miniProgram.getStorageSync({ key }) resolve(data) @@ -217,7 +249,7 @@ class WechatBus { this.checkReady() return new Promise((resolve, reject) => { try { - wx.miniProgram.removeStorageSync({ + this.wx.miniProgram.removeStorageSync({ key }) resolve(true) @@ -232,7 +264,7 @@ class WechatBus { this.checkReady() return new Promise((resolve, reject) => { try { - wx.miniProgram.clearStorageSync() + this.wx.miniProgram.clearStorageSync() resolve(true) } catch (error) { console.error('Clear storage error:', error) @@ -251,7 +283,7 @@ class WechatBus { ...options } - wx.chooseImage({ + this.wx.chooseImage({ ...defaultOptions, success: (res) => resolve(res), fail: (error) => reject(error) @@ -262,7 +294,7 @@ class WechatBus { previewImage(options) { this.checkReady() return new Promise((resolve, reject) => { - wx.previewImage({ + this.wx.previewImage({ ...options, success: (res) => resolve(res), fail: (error) => reject(error) @@ -270,18 +302,6 @@ class WechatBus { }) } - uploadImage(localId, isShowProgressTips = 1) { - this.checkReady() - return new Promise((resolve, reject) => { - wx.uploadImage({ - localId, - isShowProgressTips, - success: (res) => resolve(res), - fail: (error) => reject(error) - }) - }) - } - getLocation(options = {}) { this.checkReady() return new Promise((resolve, reject) => { @@ -290,7 +310,7 @@ class WechatBus { ...options } - wx.getLocation({ + this.wx.getLocation({ ...defaultOptions, success: (res) => resolve(res), fail: (error) => reject(error) @@ -301,7 +321,7 @@ class WechatBus { openLocation(options) { this.checkReady() return new Promise((resolve, reject) => { - wx.openLocation({ + this.wx.openLocation({ ...options, success: (res) => resolve(res), fail: (error) => reject(error) @@ -318,7 +338,7 @@ class WechatBus { ...options } - wx.scanQRCode({ + this.wx.scanQRCode({ ...defaultOptions, success: (res) => resolve(res), fail: (error) => reject(error) @@ -329,7 +349,7 @@ class WechatBus { chooseWXPay(options) { this.checkReady() return new Promise((resolve, reject) => { - wx.chooseWXPay({ + this.wx.chooseWXPay({ ...options, success: (res) => resolve(res), fail: (error) => reject(error) @@ -337,24 +357,10 @@ class WechatBus { }) } - onMenuShareAppMessage(options) { - this.checkReady() - wx.onMenuShareAppMessage({ - ...options - }) - } - - onMenuShareTimeline(options) { - this.checkReady() - wx.onMenuShareTimeline({ - ...options - }) - } - updateAppMessageShareData(options) { this.checkReady() return new Promise((resolve, reject) => { - wx.updateAppMessageShareData({ + this.wx.updateAppMessageShareData({ ...options, success: (res) => resolve(res), fail: (error) => reject(error) @@ -365,7 +371,7 @@ class WechatBus { updateTimelineShareData(options) { this.checkReady() return new Promise((resolve, reject) => { - wx.updateTimelineShareData({ + this.wx.updateTimelineShareData({ ...options, success: (res) => resolve(res), fail: (error) => reject(error) @@ -375,32 +381,22 @@ class WechatBus { closeWindow() { this.checkReady() - wx.closeWindow() + this.wx.closeWindow() } hideMenuItems(menuList = []) { this.checkReady() - wx.hideMenuItems({ + this.wx.hideMenuItems({ menuList }) } showMenuItems(menuList = []) { this.checkReady() - wx.showMenuItems({ + this.wx.showMenuItems({ menuList }) } - - hideAllNonBaseMenuItem() { - this.checkReady() - wx.hideAllNonBaseMenuItem() - } - - showAllNonBaseMenuItem() { - this.checkReady() - wx.showAllNonBaseMenuItem() - } } const wechatBus = new WechatBus()