|
- import wx from '@/common/js/weixin-js-sdk.js';
- // 参考文档 https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html
- export default {
- methods: {
- data(){
- return {
- wxInit:false
- }
- },
- // 初始化wx
- initWx(arr = ["getLocation"]) {
- const promise = new Promise((resolve, reject) => {
- // let url = window.location.href
- let url = /(Android)/i.test(navigator.userAgent) ? location.href.split('#')[0] : window
- .localStorage
- .getItem('scanUrl')
- this.HTTP_GET("weixinapi/getweixinwebaccess_token?url=" + encodeURIComponent(url)).then((
- success) => {
- if (!success) {
- resolve(false)
- return
- }
- wx.config({
- debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
- appId: success.appid, // 必填,公众号的唯一标识
- timestamp: success.timestamp, // 必填,生成签名的时间戳
- nonceStr: success.noncestr, // 必填,生成签名的随机串
- signature: success.certificate, // 必填,签名
- jsApiList: arr // 必填,需要使用的JS接口列表
- });
- wx.ready(() => {
- this.wxInit = true
- // this.TOAST("wx初始化成功")
- resolve(true)
- })
- wx.error(() => {
- this.TOAST("wx初始化失败")
- resolve(false)
- })
- })
- })
- return promise
- },
- // 获取定位
- async getLocation() {
- return new Promise(async (resolve) => {
- if (!this.wxInit) {
- let res = await this.initWx(["getLocation"])
- if(!res){
- resolve(false)
- return
- }
- }
- if(!wx.getLocation){
- this.TOAST("获取定位失败")
- resolve(false)
- }
- wx.getLocation({
- type: "gcj02", // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
- success: function(res) {
- var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
- var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
- var speed = res.speed; // 速度,以米/每秒计
- var accuracy = res.accuracy; // 位置精度
- resolve({
- lat:latitude,
- lng:longitude
- })
- },
- fail: function(error) {
- this.TOAST("获取定位失败!")
- resolve(false)
- }
- });
- })
- },
- // 拍照
- async chooseImage() {
- return new Promise(async (resolve) => {
- if (!this.wxInit) {
- let res = await this.initWx(["chooseImage"])
- if(!res){
- resolve(false)
- return
- }
- }
- if(!wx.chooseImage){
- this.TOAST("无效方法")
- resolve(false)
- }
- wx.chooseImage({
- count: 1, // 默认9
- sizeType: ['original'], // 可以指定是原图还是压缩图,默认二者都有
- sourceType: ['camera'], // 可以指定来源是相册还是相机,默认二者都有
- success: function(res) {
- var localIds = res.localIds;// 返回选定照片的本地ID列表,localId可以作为img标签的src属性显示图片
- resolve(localIds)
- },
- fail: function(error) {
- this.TOAST("定位失败")
- resolve(false)
- }
- });
- })
- },
- // 预览图片
- async previewImage(url,urlArr) {
- return new Promise(async (resolve) => {
- if (!this.wxInit) {
- let res = await this.initWx(["previewImage"])
- if(!res){
- resolve(false)
- return
- }
- }
- if(!wx.previewImage){
- this.TOAST("无效方法")
- resolve(false)
- }
- wx.previewImage({
- current: url, // 当前显示图片的http链接
- urls: urlArr // 需要预览的图片http链接列表
- });
- })
- },
-
- }
- }
|