最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)微信公眾平臺(tái)朋友圈分享功能詳細(xì)代碼

 更新時(shí)間:2017年11月02日 14:32:57   作者:劉曉春  
這篇文章主要介紹了Java實(shí)現(xiàn)微信公眾平臺(tái)朋友圈分享功能詳細(xì)代碼,小編覺得挺不錯(cuò)的,這里分享給大家,供需要的朋友參考。

  其實(shí)分享的方法在微信官網(wǎng)有較為詳細(xì)的文檔說明,現(xiàn)就其中一些比較繞的步驟進(jìn)行總結(jié),有問題隨時(shí)交流哈。

  首先微信其實(shí)已經(jīng)自帶分享到朋友圈,朋友,qq空間等功能,對(duì)于開發(fā)微信專門提供了一個(gè)接口,可以根據(jù)需要修改一些配置。例如修改要分享內(nèi)容的頭像,鏈接,描述等。

開發(fā)步驟:

1.在公眾平臺(tái)配置js-sdk接口

“公眾號(hào)設(shè)置”——“功能設(shè)置”——“JS接口安全域名”

2.在要分享的頁面引入js

http://res.wx.qq.com/open/js/jweixin-1.0.0.js
https://res.wx.qq.com/open/js/jweixin-1.0.0.js

3.然后就是寫自己的js

包括3個(gè)部分
1)權(quán)限驗(yàn)證配置

wx.config({ 
  debug: true, // 開啟調(diào)試模式,調(diào)用的所有api的返回值會(huì)在客戶端alert出來,若要查看傳入的參數(shù),可以在pc端打開,參數(shù)信息會(huì)通過log打出,僅在pc端時(shí)才會(huì)打印。 
  appId: '', // 必填,公眾號(hào)的唯一標(biāo)識(shí) 
  timestamp: , // 必填,生成簽名的時(shí)間戳 
  nonceStr: '', // 必填,生成簽名的隨機(jī)串 
  signature: '',// 必填,簽名,見附錄1 
  jsApiList: [] // 必填,需要使用的JS接口列表,所有JS接口列表見附錄2 
});

2)分享處理

wx.ready(function(){ 
// 朋友圈 
wx.onMenuShareTimeline({ 
  title: '', // 分享標(biāo)題 
  link: '', // 分享鏈接 
  imgUrl: '', // 分享圖標(biāo) 
  success: function () {  
    // 用戶確認(rèn)分享后執(zhí)行的回調(diào)函數(shù) 
  }, 
  cancel: function () {  
    // 用戶取消分享后執(zhí)行的回調(diào)函數(shù) 
  } 
}); 
//朋友 
wx.onMenuShareAppMessage({ 
  title: '', // 分享標(biāo)題 
  desc: '', // 分享描述 
  link: '', // 分享鏈接 
  imgUrl: '', // 分享圖標(biāo) 
  type: '', // 分享類型,music、video或link,不填默認(rèn)為link 
  dataUrl: '', // 如果type是music或video,則要提供數(shù)據(jù)鏈接,默認(rèn)為空 
  success: function () {  
    // 用戶確認(rèn)分享后執(zhí)行的回調(diào)函數(shù) 
  }, 
  cancel: function () {  
    // 用戶取消分享后執(zhí)行的回調(diào)函數(shù) 
  } 
}); 
});

3)錯(cuò)誤處理

wx.error(function(res){ 
 
  // config信息驗(yàn)證失敗會(huì)執(zhí)行error函數(shù),如簽名過期導(dǎo)致驗(yàn)證失敗,具體錯(cuò)誤信息可以打開config的debug模式查看,也可以在返回的res參數(shù)中查看,對(duì)于SPA可以在這里更新簽名。 
 
});

2)3)直接寫自己的參數(shù)即可,至于1) 的參數(shù),可通過下面的類來獲取。

import java.util.UUID; 
import java.util.Map; 
import java.util.HashMap; 
import java.util.Formatter; 
import java.security.MessageDigest; 
import java.security.NoSuchAlgorithmException; 
import java.io.UnsupportedEncodingException;  
class Sign { 
  public static void main(String[] args) { 
    String jsapi_ticket = "jsapi_ticket"; 
    // 注意 URL 一定要?jiǎng)討B(tài)獲取,不能 hardcode 
    String url = "http://example.com"; 
    Map<String, String> ret = sign(jsapi_ticket, url); 
    for (Map.Entry entry : ret.entrySet()) { 
      System.out.println(entry.getKey() + ", " + entry.getValue()); 
    } 
  }; 
  public static Map<String, String> sign(String jsapi_ticket, String url) { 
    Map<String, String> ret = new HashMap<String, String>(); 
    String nonce_str = create_nonce_str(); 
    String timestamp = create_timestamp(); 
    String string1; 
    String signature = ""; 
    //注意這里參數(shù)名必須全部小寫,且必須有序 
    string1 = "jsapi_ticket=" + jsapi_ticket + 
         "&noncestr=" + nonce_str + 
         "×tamp=" + timestamp + 
         "&url=" + url; 
    System.out.println(string1); 
    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("url", url); 
    ret.put("jsapi_ticket", jsapi_ticket); 
    ret.put("nonceStr", nonce_str); 
    ret.put("timestamp", timestamp); 
    ret.put("signature", signature); 
    return ret; 
  } 
  private static String byteToHex(final byte[] hash) { 
    Formatter formatter = new Formatter(); 
    for (byte b : hash) 
    { 
      formatter.format("%02x", b); 
    } 
    String result = formatter.toString(); 
    formatter.close(); 
    return result; 
  } 
  private static String create_nonce_str() { 
    return UUID.randomUUID().toString(); 
  } 
  private static String create_timestamp() { 
    return Long.toString(System.currentTimeMillis() / 1000); 
  } 
} 

上述類中動(dòng)態(tài)獲取URL的方法:

String url = request.getRequestURL().toString(); 
String param = request.getQueryString(); 
url = url + "?" + param; 

總結(jié)

以上就是本文關(guān)于Java實(shí)現(xiàn)微信公眾平臺(tái)朋友圈分享功能詳細(xì)代碼的全部內(nèi)容,希望對(duì)大家有所幫助。歡迎參閱:Java編程通過list接口實(shí)現(xiàn)數(shù)據(jù)的增刪改查代碼示例、Java多線程之線程通信生產(chǎn)者消費(fèi)者模式及等待喚醒機(jī)制代碼詳解Java編程BigDecimal用法實(shí)例分享等,有什么問題可以隨時(shí)留言,小編會(huì)再接再厲,把更多更好的,有用的代碼分享給大家。

相關(guān)文章

最新評(píng)論

富平县| 云霄县| 靖西县| 泸州市| 吴旗县| 建平县| 溆浦县| 新河县| 贵溪市| 伊金霍洛旗| 达日县| 乌恰县| 鲁甸县| 红河县| 临江市| 措勤县| 忻城县| 望奎县| 弋阳县| 西平县| 清远市| 长岭县| 沧州市| 大同市| 乐至县| 阿拉尔市| 顺昌县| 洛宁县| 延长县| 梓潼县| 拜城县| 华阴市| 青浦区| 怀仁县| 澄江县| 石河子市| 印江| 嘉义市| 宁津县| 合江县| 盐池县|