微信官方方法

目录

获取openid

 获取定位

 打开地图

 选择图片

 微信扫描二维码


获取openid

//页面创建时调用
async created() {
    await this.getCode();
  },

//定义方法
async getCode() {
      // 截取路径中的code,如果没有就去微信授权,如果已经获取到了就直接传code给后台获取openId
      var code = this.getUrlParam("code");
      localStorage.setItem("href", encodeURIComponent(window.location.href.split('#')[0]));
      var local = window.location.href;
      // APPID
      await post(getAppId, {}).then((res) => {
        //  localStorage.setItem("appid", 'wx6fcef86f414d771c');//生产环境
        //  localStorage.setItem("appid",'wxef0143b24aa380f7');//开发环境
        console.log(res.data);
        localStorage.setItem("appid", res.data);
      });
      var APPID = localStorage.getItem("appid");
      // code判断要是为空,获取,不为空传递给后台
      if (!code) {
        window.location.href =
          "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
          APPID +
          "&redirect_uri=" +
          encodeURIComponent(local) +
          "&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect";
      } else {
        await this.getOpenId(code);
      }
    },
    getOpenId(code) {
      const data = {
        code: code,
        openid: localStorage.getItem("openid"),
        isAdmin: 0
      }
      // let url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + APPID + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
      let url = getOpenId + "?CODE=" + code;
      post(getOpenId, JSON.stringify(data)).then((res) => {
        this.wxUser = res.data;
        localStorage.setItem("openid", res.data.openid);
      });
    },

 获取定位

使用gcj02可用于地图点位展示打开地图

wx.getLocation({
            type: 'gcj02', 
            // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
            success: (res) => {
              localStorage.setItem("latitude", res.latitude);
              localStorage.setItem("longitude", res.longitude);
            }, fail: (e) => {
            }
          });

 打开地图

wx.openLocation({
                        latitude: Number(this.lat), // 纬度,浮点数,范围为90 ~ -90
                        longitude: Number(this.lon), // 经度,浮点数,范围为180 ~ -180。
                        name: this.deptName, // 位置名
                        address: this.address, // 地址详情说明
                        scale: 19, // 地图缩放级别,整型值,范围从1~28。默认为最大
                    });

 选择图片

       wx.chooseImage({
          count: 1, // 默认9
          sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
          sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
          success: function (res) {
            var localId = res.localIds[0]; // 返回选定照片的本地 ID 列表,localId可以作为 img 标签的 src 属性显示图片
            wxData.getLocalImgData({
              localId: localId, success: (res) => {
                let localData = res.localData; ('jgp', 'jpeg');
                if (localId) {
                  post(userUuidVehicles, { uuid: 1, baseStr: localData }).then(
                    res => {
                      let data = res.data?.data || ''
                      console.log(data);
                      that.modelFrom = data ? { ...data, vin: data.clsbdh } : this.modelFrom
                    }
                  )
                }
              }
            });
          }
        });

 微信扫描二维码

wx.scanQRCode({
                needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
                scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
                success: function (res) {
                  var result = res.resultStr; // 当needResult 为 1 时,扫码返回的结果
                  window.location.href = result;
                }
              });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值