Micro-channel using the basic jssdk

How to use jssdk not, much more difficult for people who are not familiar, the biggest pit in the course of its configuration.

       Use jssdk need to note the following:

       1. js need to set up port security domain in the micro-channel public platform. It should be noted here is that you need to set the domain name, use the ip is not acceptable. This setting can be modified while only three times a month, we have to identify and then modify.

2. Background need appid, appsecret first get to access_token, before you can get ticket by access_token. It is best to save ticket by redis. It has described this micro-channel.

3. If you need to download the media interface, that http://file.api.weixin.qq.com/cgi-bin/media/get?access_token="+access_token+"&media_id="+media_id;

You also need to cache access_token.

The following steps are specific interactive, background using golang build request.

Background method:

func GetTicket() []byte {
	url := "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" +
		"&appid=" + config.WxAppId +
		"&secret=" + config.WxAppSecret
	token := WxGet(url)
	if token == nil{
		fmt.Println("获取token错误")
	}
	var wxToken dao.WXToken
	json.Unmarshal(token,&wxToken)
	fmt.Println(wxToken.AccessToken)
	//保存微信accesstoken
	if wxToken.AccessToken != "" {
		redispool.RedisSETString("wx_access_token",wxToken.AccessToken,7200)
	}
	ticketUrl := "https://api.weixin.qq.com/cgi-bin/ticket/getticket?" +
		"access_token=" + wxToken.AccessToken + "&type=jsapi"
	ticket := WxGet(ticketUrl)
	if ticket == nil{
		fmt.Println("获取ticket错误")
	}
	fmt.Println("ticker:",ticket)
	var wxTicket dao.WXTicket
	json.Unmarshal(ticket,&wxTicket)
	fmt.Println("wxTicket:",wxTicket)
	return ticket
}

func WxGet(u string) []byte {
	resp, err := http.Get(u)
	defer resp.Body.Close()
	if err != nil {
		fmt.Println(err)
		return nil
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println(err)
		return nil
	}
	fmt.Println("body",string(body))
	return body
}

Reception method:

$.get("/ticket",function(data,status){
            console.log("Data: " + data.ticket + "\nStatus: " + status);
            var timestamp = Date.parse(new Date());
            timestamp = timestamp / 1000;
            console.log("time:",timestamp);
            var u = uuid();
            console.log("uuid:",u);
            console.log("ticket:",data.ticket);
            var l = location.href.split('#')[0]
            var stringTemp = "jsapi_ticket=" + data.ticket +
                    "&noncestr=" + u +
                    "×tamp=" + timestamp +
                    "&url=" + l;
            var s = hex_sha1(stringTemp);
            console.log("sign:",s)
            wx.config({
                debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
                appId: '', // 必填,公众号的唯一标识
                timestamp: timestamp, // 必填,生成签名的时间戳
                nonceStr: u, // 必填,生成签名的随机串
                signature: s,// 必填,签名,见附录1
                jsApiList: ["startRecord","stopRecord","onVoiceRecordEnd","playVoice","pauseVoice","stopVoice"
                    ,"onVoicePlayEnd","uploadVoice","downloadVoice","chooseImage","previewImage","uploadImage","downloadImage"] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
            });
        });

Best used to generate url sign of location.href.split ( '#') [0 ] to get your website, and your website needs and js the same security domain.

       

Published 48 original articles · won praise 17 · views 50000 +

Guess you like

Origin blog.csdn.net/aixinaxc/article/details/78776221