|
|
1 tahun lalu | |
|---|---|---|
| .. | ||
| public | 1 tahun lalu | |
| src | 1 tahun lalu | |
| .gitignore | 1 tahun lalu | |
| .npmrc | 1 tahun lalu | |
| README-wechat-integration.md | 1 tahun lalu | |
| README.md | 1 tahun lalu | |
| babel.config.js | 1 tahun lalu | |
| package.json | 1 tahun lalu | |
| postcss.config.js | 1 tahun lalu | |
| shims-uni.d.ts | 1 tahun lalu | |
| shims-vue.d.ts | 1 tahun lalu | |
| tsconfig.json | 1 tahun lalu | |
| vue.config.js | 1 tahun lalu | |
本文档介绍如何在智慧校园移动应用(mobile工程)中集成微信JS-SDK功能,实现微信浏览器内的原生功能调用。
确保后端已实现以下接口:
POST /system/wechat/config # 获取JS-SDK配置
POST /system/wechat/upload # 上传微信媒体文件
GET /system/wechat/download # 下载微信媒体文件
GET /system/wechat/userinfo # 获取微信用户信息
mobile/
├── public/
│ └── index.html # 已添加微信JS-SDK脚本引用
├── src/
│ ├── api/
│ │ └── wechat.ts # 微信API接口定义
│ ├── utils/
│ │ └── wechat.js # 微信JS-SDK工具类
│ └── components/
│ └── WeChatDemo.vue # 功能演示组件
└── README-wechat-integration.md # 本文档
<template>
<view>
<button @click="initWeChatSDK">初始化微信SDK</button>
<button @click="scanQRCode" :disabled="!isInitialized">扫一扫</button>
<button @click="shareToFriends" :disabled="!isInitialized">分享给朋友</button>
</view>
</template>
<script>
import weChatSDK from '@/utils/wechat.js';
export default {
data() {
return {
isInitialized: false
};
},
methods: {
async initWeChatSDK() {
try {
await weChatSDK.init();
this.isInitialized = true;
console.log('微信SDK初始化成功');
} catch (error) {
console.error('初始化失败:', error);
}
},
async scanQRCode() {
try {
const result = await weChatSDK.scanQRCode();
console.log('扫码结果:', result.resultStr);
} catch (error) {
console.error('扫码失败:', error);
}
},
async shareToFriends() {
try {
await weChatSDK.shareAppMessage({
title: '智慧校园',
desc: '校园管理系统',
link: window.location.href,
imgUrl: 'https://example.com/logo.png'
});
console.log('分享配置成功');
} catch (error) {
console.error('分享失败:', error);
}
}
}
};
</script>
init(options)初始化微信JS-SDK配置
参数:
options.url (String, 可选): 当前页面URL,默认自动获取options.jsApiList (Array, 可选): 需要使用的API列表,默认使用全部options.debug (Boolean, 可选): 是否开启调试模式,默认根据环境返回: Promise
await weChatSDK.init({
debug: false,
jsApiList: ['chooseImage', 'scanQRCode']
});
isWeChatBrowser()检查是否在微信浏览器中
返回: Boolean
getCurrentUrl()获取当前页面URL(去除hash部分)
返回: String
shareTimeline(shareData)分享到朋友圈
参数:
shareData.title (String): 分享标题shareData.link (String): 分享链接shareData.imgUrl (String): 分享图标shareAppMessage(shareData)分享给朋友
参数:
shareData.title (String): 分享标题shareData.desc (String): 分享描述shareData.link (String): 分享链接shareData.imgUrl (String): 分享图标scanQRCode(options)微信扫一扫
参数:
options.needResult (Number, 可选): 是否返回扫描结果,默认1options.scanType (Array, 可选): 扫码类型,默认['qrCode', 'barCode']返回: Promise<{resultStr: string}>
chooseImage(options)选择图片
参数:
options.count (Number, 可选): 图片数量,默认1options.sizeType (Array, 可选): 图片尺寸,默认['original', 'compressed']options.sourceType (Array, 可选): 图片来源,默认['album', 'camera']返回: Promise<{localIds: string[]}>
previewImage(options)预览图片
参数:
options.current (String): 当前显示图片的http链接options.urls (Array): 需要预览的图片http链接列表getLocation(options)获取地理位置
参数:
options.type (String, 可选): 坐标类型,默认'gcj02'返回: Promise<{latitude: number, longitude: number, accuracy: number}>
openLocation(location)打开地图
参数:
location.latitude (Number): 纬度location.longitude (Number): 经度location.name (String, 可选): 位置名称location.address (String, 可选): 地址详情location.scale (Number, 可选): 地图缩放级别,默认14getNetworkType()获取网络状态
返回: Promise<{networkType: string}>
chooseWXPay(payInfo)微信支付
参数:
payInfo.timestamp (String): 时间戳payInfo.nonceStr (String): 随机字符串payInfo.package (String): 订单详情扩展字符串payInfo.signType (String, 可选): 签名方式,默认'MD5'payInfo.paySign (String): 签名checkJsApi(jsApiList)检查当前客户端版本是否支持指定JS接口
closeWindow()关闭当前网页窗口
getWeChatConfig(url: string)获取微信JS-SDK配置信息
uploadWeChatMedia(mediaId: string, type: string)上传微信临时素材
downloadWeChatMedia(mediaId: string)下载微信临时素材
getWeChatUserInfo(code: string)获取微信用户信息
createWeChatPay(orderData: any)生成微信支付订单
后端需要实现微信JS-SDK配置接口,返回格式:
{
"code": 200,
"data": {
"appId": "wx1234567890abcdef",
"timestamp": 1640995200,
"nonceStr": "Wm3WZYTPz0wzccnW",
"signature": "0f9de62fce790f9a083d5c99e95740ceb90c27ed"
},
"msg": "success"
}
确保项目配置文件中的API地址正确:
// mobile/src/common/config.js
export const REQUEST_CONFIG = {
baseURL: process.env.NODE_ENV === 'development' ? '/api' : '/prod-api'
};
项目提供了完整的功能演示组件 WeChatDemo.vue,包含:
使用方法:
<template>
<view>
<WeChatDemo />
</view>
</template>
<script>
import WeChatDemo from '@/components/WeChatDemo.vue';
export default {
components: {
WeChatDemo
}
};
</script>
微信JS-SDK不存在跨域问题,但需要确保:
await weChatSDK.init({
debug: true // 开启调试模式
});
原因: 签名验证失败 解决:
原因: 接口未授权或不在jsApiList中 解决:
原因: config配置有误 解决:
使用微信开发者工具进行调试:
维护者: 开发团队
最后更新: 2025-01-03
文档版本: v1.0.0