/** * 微信工具使用示例 */ import WechatUtils, { WechatAuth, WechatImage, WechatLocation } from './wechat.js'; // 1. 初始化微信工具 const wechat = await WechatUtils.init('your_wechat_appid'); // 2. 微信登录授权 function handleWechatLogin() { // 检查是否有授权回调 const authResult = WechatAuth.handleAuthCallback(); if (authResult) { // 有code,发送到后端获取用户信息 WechatAuth.getAccessToken(authResult.code) .then(userInfo => { console.log('用户信息:', userInfo); // 保存用户信息到本地存储或状态管理 }) .catch(error => { console.error('登录失败:', error); }); } else { // 没有code,跳转到授权页面 const redirectUri = window.location.href; WechatAuth.authorize(redirectUri, 'snsapi_userinfo'); } } // 3. 选择并上传图片 async function handleImageUpload() { try { // 选择图片 const localIds = await WechatImage.chooseImage({ count: 3, sizeType: ['compressed'], sourceType: ['album', 'camera'] }); // 上传图片 const serverIds = []; for (const localId of localIds) { const serverId = await WechatImage.uploadImage(localId); serverIds.push(serverId); } console.log('上传成功,服务器ID:', serverIds); return serverIds; } catch (error) { console.error('图片上传失败:', error); } } // 4. 获取地理位置 async function handleGetLocation() { try { const location = await WechatLocation.getLocation('gcj02'); console.log('当前位置:', location); // 在地图中显示位置 WechatLocation.openLocation({ latitude: location.latitude, longitude: location.longitude, name: '我的位置', address: '当前所在位置' }); } catch (error) { console.error('获取位置失败:', error); } } // 5. 设置分享内容 function setupShare() { const shareData = { title: '超级好车补', desc: '专项购车补贴', link: window.location.href, imgUrl: 'https://your-domain.com/share-icon.jpg' }; wechat.share.shareToFriend(shareData); wechat.share.shareToTimeline(shareData); } export { handleWechatLogin, handleImageUpload, handleGetLocation, setupShare };