微信開發(三)微信分享朋友朋友圈

這裏基於微信的都是須要用到
  1. 參考如下文檔獲取access_token(有效期7200秒,開發者必須在本身的服務全局緩存access_token):../15/54ce45d8d30b6bf6758f68d2e95bc627.html

點擊打開連接
javascript

微信分享朋友朋友圈這裏須要用到這個其實比較簡單主要的配置就是

1服務器綁定域名,html

2.引入js( https://res.wx.qq.com/open/js/jweixin-1.0.0.jsjava


3.經過config接口注入權限驗證配置ajax

(注意這裏簽名返回值放在前臺ajax是由於二次分享的時候url會改變因此這樣否則直接把簽名的值放在request值裏面會致使二次分享失敗)api

function getConfigData(){
	var url =window.location.href;
	url = encodeURIComponent(url);
	var data ="url="+url;
	$.ajax({
		type:'post',
		url:'後臺驗籤url',
		data:data,
		asyc:true,
		success:function(data){
			if(data){
				wx.config({
				    debug: false, // 開啓調試模式,調用的全部api的返回值會在客戶端alert出來,若要查看傳入的參數,能夠在pc端打開,參數信息會經過log打出,僅在pc端時纔會打印。
				    appId: data.appId, // 必填,公衆號的惟一標識
				    timestamp:data.timestamp, // 必填,生成簽名的時間戳
				    nonceStr: data.nonceStr, // 必填,生成簽名的隨機串
				    signature: data.signature,// 必填,簽名,見附錄1
				    jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage'] // 必填,須要使用的JS接口列表,全部JS接口列表見附錄2
				});
			}
			wxReady();
		},
		error:function(){
			alert("系統繁忙請稍後再試");
		}
	})
}

wx.ready(function(){
	wx.onMenuShareTimeline({
	    title: share_title, // 分享標題
	    link:share_url, // 分享連接
	    imgUrl: act_image_url, // 分享圖標
	    success: function () { 
	    	
	    },
	    cancel: function () { 
	        // 用戶取消分享後執行的回調函數
	    }
	});
	wx.onMenuShareAppMessage({
	    title: share_title, // 分享標題
	    desc: act_message, // 分享描述
	    link: share_url, // 分享連接
	    imgUrl: act_image_url, // 分享圖標
	    type: 'link', // 分享類型,music、video或link,不填默認爲link
	    dataUrl: '', // 若是type是music或video,則要提供數據連接,默認爲空
	    success: function () { 
	    	
	    	
	    },
	    cancel: function () { 
	        // 用戶取消分享後執行的回調函數
	    }
	});
});
後臺代碼

生成簽名緩存

@RequestMapping(value="/getConfigData",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,String> getConfigData(HttpServletRequest request){
		String id =request.getParameter("id");
		String url = request.getParameter("url");
		String path = request.getContextPath(); 
		String basePath ="xiangm"
		String shareUrl =basePath+"/act/toActView/"+id+".html";
		Map<String,String> map =Snippet.sign(url);
		map.put("shareUrl", shareUrl);
		map.put("appId",Snippet.getAppid());
		return map;
	}

 
public static Map<String, String> sign(String url) {
        Map<String, String> ret = new HashMap<String, String>();
        String nonce_str = create_nonce_str();
        String timestamp = create_timestamp();
        String jsapi_ticket="";
		try {
			jsapi_ticket = getJsapiTicket();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        String string1;
        String signature = "";

        //注意這裏參數名必須所有小寫,且必須有序
        string1 = "jsapi_ticket=" + jsapi_ticket +
                  "&noncestr=" + nonce_str +
                  "×tamp=" + timestamp +"&url="+url;
       

        try
        {
            MessageDigest crypt = MessageDigest.getInstance("SHA-1");
            crypt.reset();
            crypt.update(string1.getBytes("UTF-8"));
            signature = byteToHex(crypt.digest());
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }

       
        ret.put("jsapi_ticket", jsapi_ticket);
        ret.put("nonceStr", nonce_str);
        ret.put("timestamp", timestamp);
        ret.put("signature", signature);

        return ret;
    }
相關文章
相關標籤/搜索