SoFunction
Updated on 2025-04-09

Detailed explanation of common usage methods for using weixin-js-sdk in vue

Link:/wiki/?title=%E5%BE%AE%E4%BF%A1JS-SDK%E6%8E%A5%E5%8F%A3#..A5..A4.E4.B8..9A..95.E5.85.A5JS.E6.96.87..B6

1. Import dependency packages

npm install weixin-js-sdk

2. Determine whether it is in the WeChat browser

<script>
var ua = ();
var isWeixin = ('micromessenger') != -1;
var isAndroid = ('android') != -1;
var isIos = (('iphone') != -1) || (('ipad') != -1);
if(!isWeixin) {
  = '<title>Feel sorry,Something went wrong</title><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0"><link rel="stylesheet" type="text/css" href="/open/libs/weui/0.4.1/" rel="external nofollow" >';
  = '<div class="weui_msg"><div class="weui_icon_area"><i class="weui_icon_info weui_icon_msg"></i></div><div class="weui_text_area"><h4 class="weui_msg_title">Please open the link on the WeChat client</h4></div></div>';
} 

Quote in:

import env from "./env";//Running environment

Log in on WeChat, exchange code for openid, and use this method on the start page:

<script>
methods:{
 // Log in on WeChat    wxLogin() {
      var that = this;
      axios
        .get("/common/loginAuth")
        .then(function(res) {
          ("Returned link address in the background", );
           = ;//Return the link address returned by the background        })
        .catch(function(error) {});
    },
//Exchange user information    postCode(res) {
      var that = this;
      axios
        .post("/common/getUserInfo", {
          code: res
        })
        .then(function(res) {
          ("openid", );// Code like the background exchange for openid and save it        })
        .catch(function(error) {
          (error);
        });
    }},
created() {
    var r = ;//Get the current link and split the current link    //The current link address is the parameter returned by the background. If you split the code in the link, you can get the openid through the postCode() method. If you don't, start WeChat login through the wxLogin() method.    if (("?") != -1) {
      r = ("?");
      r = r[1].split("&");
      r = r[0].split("=");
      r = r[1];
    } else {
      ();
    }
    if (r) {
      (r);
    } else {
      ();
    }
  },
</script>

3. Use front-end page

import wx from 'weixin-js-sdk'
({
method: 'post',
url: 'url',
data:{ url:('#')[0] } //Provide authorization url parameters to the server, and do not need the part after #}).then((res)=>{
({
debug: true, // Turn on debug mode,appId: , // Required, the unique identification of the enterprise number. Fill in the enterprise number here corpidtimestamp: , // Required to generate a signature time stampnonceStr: , // Required to generate a random string of signaturessignature: ,// Required, signature, see Appendix 1jsApiList: ['scanQRCode'] // Required, list of JS interfaces that need to be used, all JS interface columns});
})

This article about this article explaining the common usage methods of using weixin-js-sdk in vue. This is the end. For more related content of vue weixin-js-sdk, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!