vue 微信公众号 前端开发

注意:
1)路由:mode: 'history'
2)保证h5页面域名和公众号后台js安全域名一致

前端配置文件 wxConfig.js

import Vue from 'vue';
import wx from 'weixin-js-sdk';

Vue.use(wx);

export default {
  data() {
    return {
      appId: 'wx485*********e74',
      responseType: 'code',
      scope: 'snsapi_userinfo',
      baseURI: 'https://open.weixin.qq.com/connect/oauth2/authorize',
    };
  },
  async created() {
  },
  methods: {
    /**
     * 获取code
     * @param locationSearch
     * @return {*}
     */
    getCode(locationSearch) {
      let url = decodeURIComponent(locationSearch);
      let theRequest = {};
      if (url.indexOf('?') !== -1) {
        let str = url.substr(1);
        let strs = str.split('&');
        for (let i = 0; i < strs.length; i++) {
          theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1];
        }
        let urlParams = Object.assign(theRequest, this.$route.query);
        return urlParams;
      }
    }
  }
};

获取code和公众号用户信息

/**
 * 获取code
 * @return {Object}
 */
getRequest() {
  let urlParams = this.getCode(location.search);
  if (urlParams.code) {
    this.getWxUserInfo(urlParams.code);
  } else {
    window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx48******de74&redirect_uri=配置的重定向地址&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`
  }
},

/**
 * 获取公众号用户信息
 * @param code
 * @return {Promise<void>}
 */
async getWxUserInfo (code) {
  let params = {
    code,
    state: '123'
  };
  let token = await this.$http.get(`/wechat/userInfo`, params);
  this.setToken(token);
  const account = await this.$http.get('/wechat/accounts');
  this.setAccount(account);
  window.location.href = '/';
}
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在搭建微信公众号前端vue环境之前,首先需要准备一个微信公众平台测试账号。这个测试账号可以通过个人微信扫描二维码登录来获得,不需要注册。接下来,我们可以按照以下步骤来搭建vue环境: 1. 确保你已经安装了Node.js和npm(Node包管理器)。 2. 打开命令行终端,进入你的项目文件夹。 3. 使用以下命令来初始化一个基于vue的项目: ``` npm init -y ``` 4. 接下来,安装vue-cli,它是一个vue脚手架工具,可以帮助我们快速搭建项目: ``` npm install -g @vue/cli ``` 5. 初始化一个新的vue项目: ``` vue create your-project-name ``` 其中,"your-project-name"是你项目的名称,可以自行替换。 6. 在初始化过程中,你可以选择一些项目配置选项,例如使用默认配置或手动选择特定的特性。根据你的需求进行选择。 7. 初始化完成后,进入到项目目录: ``` cd your-project-name ``` 8. 运行以下命令来启动项目: ``` npm run serve ``` 9. 现在,你的vue环境已经搭建完成。你可以根据项目需求进行开发,编写vue组件、路由、数据交互等等。 请注意,以上步骤是搭建基于vue微信公众号前端环境的一般方法。具体的配置和开发流程可能会根据你的实际应用场景而有所不同。因此,在进行微信公众号前端开发之前,建议你仔细阅读微信公众平台的开发文档,并根据文档中的指引进行配置和开发工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值