微信开发

企业微信扫码登陆

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<script src="https://rescdn.qqmail.com/node/ww/wwopenmng/js/sso/wwLogin-1.0.0.js"></script>
<script>
  window.WwLogin({
    "id" : "wx_reg",  
    "appid" : "ww96c32bcef2a78ed2",
    "agentid" : "1000003",
    "redirect_uri" : 'http%3a%2f%2fdoc.41.yr966.com',
    "state" : "123456",
    "href" : "",
  });
</script>

微信 JSSDK

后端

 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
from django.http import JsonResponse
import json
import requests
import os
import time
from wechatpy import WeChatClient
from wechatpy.utils import random_string, to_text

def jsapi_signature(request):
    nonceStr = random_string(32)
    timestamp = to_text(int(time.time()))
    url = request.GET.get("url")

    config = read_config()
    WECHAT_APPID = config["WECHAT_APPID"]
    WECHAT_SECRET = config["WECHAT_SECRET"]

    client = WeChatClient(WECHAT_APPID, WECHAT_SECRET)
    ticket_response = client.jsapi.get_ticket()
    signature = client.jsapi.get_jsapi_signature(
        nonceStr, ticket_response["ticket"], timestamp, url
    )
    ret_dict = {
        "appId": config["WECHAT_APPID"],
        "nonceStr": nonceStr,
        "timestamp": timestamp,
        "signature": signature,
    }
    return JsonResponse(ret_dict)

前端

 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
// <script type="text/javascript" src="https://cdn.bootcss.com/jquery/2.1.3/jquery.min.js"></script>
// <script type="text/javascript" src="https://res2.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
// <script type="text/javascript" src="/js/share.js"></script>

$(function () {
  var title = document.title.substring(0, document.title.length - 7);
  var imgUrl = $("img:first").attr('src') || location.origin + '/favicon.png';
  var desc = $("meta[name='description']").attr('content');

  var shareobj = {
    title: title,
    desc: desc,
    link: location.href,
    imgUrl: imgUrl
  };

  var timeObj = {
    title: title,
    link: location.href,
    imgUrl: imgUrl
  }

  var url = location.origin + "/api/jsapi_signature?&url=" + encodeURIComponent(location.href);

  var ua = navigator.userAgent.toLowerCase();
  var isWeixin = ua.indexOf('micromessenger') != -1;
  if (isWeixin) {
    $.getJSON(url, function (res) {
      wx.config({
        debug: false,
        appId: res.appId,
        timestamp: res.timestamp,
        nonceStr: res.nonceStr,
        signature: res.signature,
        jsApiList: ['onMenuShareAppMessage', 'onMenuShareTimeline']
      });

      wx.ready(function () {
        wx.onMenuShareAppMessage(shareobj);
        wx.onMenuShareTimeline(timeObj);
      });
    });
  }

});