1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| import axios from 'axios' import wx from 'weixin-js-sdk' import Env from './env'
interface getLocationCallback { latitude: number, longitude: number, speed: number, accuracy: number } interface openLocationOption { latitude: number longitude: number name: string address: string } class WXSdk { constructor(jsApiList = ['getLocation', 'openLocation', 'chooseImage', 'uploadImage', 'translateVoice', 'startRecord', 'stopRecord'], url = document.location) { axios.get(`${Env.DATA_SERVER}/weixin/signature/new?url=${url}`).then(response => { let data = response.data as any wx.config({ debug: !Env.PRODUCTION_MODE, appId: Env.WX_APP_ID, timestamp: data.timestamp, nonceStr: data.nonceStr, signature: data.signature, jsApiList: jsApiList }) wx.error(res => { console.error(res) }) }) } public uploadImage(localId: string): Promise<any> { return new Promise((resolve, reject) => { this.call('uploadImage', { localId: localId, isShowProgressTips: 1, success: function (res) { resolve(res) } }) }) } public chooseImage(): Promise<any> { let me = this return new Promise((resolve, reject) => { me.call('chooseImage', { count: 1, sizeType: ['compressed'], sourceType: ['camera'], success: function (res) { resolve(res.localIds) } }) }) } public getLocation(): Promise<getLocationCallback> { return new Promise<getLocationCallback>((resolve, rejct) => { this.call('getLocation', { type: 'gcj02', success: function (res) { resolve(res as any) } }) }) } public openLocation(params: openLocationOption): void { this.call('openLocation', params) } public startRecord(): void { this.call('startRecord') } public async stopNTranslate(): Promise<string> { let voiceId = await this.stopRecord() return await this.translateVoice(voiceId) } private stopRecord(): Promise<string> { return new Promise<string>((resolve, reject) => { this.call('stopRecord', { success: function (res) { var localId = res.localId; resolve(localId) } }) }) } private translateVoice(localId: string): Promise<string> { return new Promise((resolve, reject) => { this.call('translateVoice', { localId: localId, isShowProgressTips: 1, success: function (res) { resolve(res.translateResult); } }) }) } private call(methodName: string, methodParams?: any) { wx.ready(() => { wx[methodName].call({}, methodParams) }) } }
export default new WXSdk()
|