1.1 測試公衆號註冊:html
http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/loginjava
1.2 微信公衆號開發指南:web
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1445241432ajax
1.3 微信全局返回碼說明:算法
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433747234數據庫
1.4 微信公衆號接口權限說明:json
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433401084api
2.1.1 正確填寫下列信息緩存
注意:安全
URL必須以http:// 或者https:// 開頭,分別支持80端口和443端口;
Token必須爲英文或者數字,長度爲3-32字符;
2.1.2 開發過程當中,須要注意接口的調用次數,
2.1.3 注意檢查URL和JS接口安全域名,能夠保持一致,也能夠不同,我的建議最好保持一致;
Eg:
微信網頁受權access_token和普通access_token區別
https://blog.csdn.net/benben_2015/article/details/79703508
微信獲取用戶信息的兩個接口和兩個ACCESS_TOKEN
https://www.cnblogs.com/sxmny/articles/4969119.html
Appid 應用ID
AppSecret 應用密匙
調用接口:
URL: https://api.weixin.qq.com/cgi-bin/token
參數: grant_type : 「client_credential」--------獲取access_token填寫client_credential Appid : Appid---------第三方用戶惟一憑證
AppSecret : AppSecret-----------第三方用戶惟一憑證密鑰,即appsecret
返回參數: access_token---------獲取到的憑證
expires_in----------憑證有效事件,單位:秒
注:公衆號對此接口的調用是有次數限制的,默認是2000,花錢可增長次數,獲取的access_token的有效期默認是2小時,故而,在開發過程當中,可將調用此接口的access_token設爲全局變量,並將次接口的調用方法設爲全局自動調用,獨立出來;
Eg(將獲取access_token的接口獨立,並調整爲100分鐘自動刷新):
package com.gzh.api.utils;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicInteger;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import cn.hutool.http.HttpUtil;
/**
* access_token 自動刷新
* @author Administrator
*
*/
public class WxToken {
public static String globalTokenObject = "";//調取接口的返回值參數(access_token、expires_in)
public static String globalToken = "";// access_token
public static AtomicInteger atomicInteger = new AtomicInteger();//計數器,統計接口調用的次數
public static String appid = "**************";
public static String appsecret = "*************";
static {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
Map<String,Object> paramMap = Maps.newHashMap();
paramMap.put("appid", appid);
paramMap.put("secret", appsecret);
paramMap.put("grant_type", "client_credential");
globalTokenObject = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token", paramMap);
JSONObject wxToken = JSONObject.parseObject(globalTokenObject);
globalToken = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + wxToken.getString("access_token") + "&type=jsapi");
atomicInteger.incrementAndGet();
}
}, 0, 2*50*60*1000);
}
}
使用微信打開以下地址https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
參數:appid---------appid
redirect_uri-------系統路徑,需與微信公中號綁定路徑一致
response_type-------「code」字符串
scope------「snsapi_userinfo」字符串
state------「1」字符串
(注意這個接口中有個參數scope 默認有2個值snsapi_base和snsapi_userinfo,這個接口會根據scope 來生成不一樣的code而且獲取不一樣做用的access_token ,無論scope傳什麼值都能在獲得對應access_token的同時獲得open_id, 若是你只須要獲得opend_id 那使用snsapi_base參數到此結束了,若是須要獲取用戶的其餘信息好比 暱稱 地址 就要snsapi_userinfo 會彈出受權)
eg:
直接在微信中訪問,系統中用以下方法接收:
//獲取用戶code --var code = getCode(‘code’);
Function getCode(name){
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
}
便可拿到用戶Code;
簡單的實現微信獲取openid
https://blog.csdn.net/z880698223/article/details/78485243/
1.調用https://open.weixin.qq.com/connect/oauth2/authorize接口獲取到code
《參數2.3》
2.獲得code做爲一個參數調用https://api.weixin.qq.com/sns/oauth2/access_token接口獲取到openid 《參照2.5》
調用接口:
URL: https://api.weixin.qq.com/sns/oauth2/access_token
參數:
grant_type : 「authorization_code」--------填寫client_credential
Appid : Appid---------第三方用戶惟一憑證
AppSecret : AppSecret-----------第三方用戶惟一憑證密鑰,即appsecret
Code:Code----------標識不一樣的用戶code 《參照2.3》
返回參數:
Openid
access_token
EG:
https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
調用接口:
URL: https://api.weixin.qq.com/sns/userinfo
參數:
Openid-----《參照2.5》
Lang----「zh_CN」字符串
access_token------《參照2》>(注:此access_token網上資料說須要普通access_token,及調用《2.2》接口返回的普通access_token,可是實際中發現調用《2.5》接口返回的網頁受權access_token也能夠拿到用戶信息,故而實際開發中可換着調用調試,我的建議使用普通access_token,由於開文提出將此參數定義爲全局變量,故而對代碼一致性維護較方便)
返回參數:
Subscribe--------用戶是否訂閱該公衆號標識,值爲0時,表明此用戶沒有關注該公衆號,拉取不到其他信息
Openid----------用戶的標識,對當前公衆號惟一
Nickname-----------用戶的暱稱
Sex----------用戶的性別,值爲1時是男性,值爲2時是女性,值爲0時是未知
City----------用戶所在城市
Country-------------用戶所在國家
Province-----------用戶所在省份
Language---------用戶的語言,簡體中文爲zh_CN
Headimgurl-----------用戶頭像,最後一個數值表明正方形頭像大小(有0、4六、6四、9六、132數值可選,0表明640*640正方形頭像),用戶沒有頭像時該項爲空。若用戶更換頭像,原有頭像URL將失效
subscribe_time-------------用戶關注時間,爲時間戳。若是用戶曾屢次關注,則取最後關注時間
unionid------------只有在用戶將公衆號綁定到微信開放平臺賬號後,纔會出現該字段
remark-----------公衆號運營者對粉絲的備註,公衆號運營者可在微信公衆平臺用戶管理界面對粉絲添加備註
groupid ------------- 用戶所在的分組ID(兼容舊的用戶分組接口)
tagid_list ------------用戶被打上的標籤ID列表
subscribe_scene----------- 返回用戶關注的渠道來源,ADD_SCENE_SEARCH 公衆號搜索,ADD_SCENE_ACCOUNT_MIGRATION 公衆號遷移,ADD_SCENE_PROFILE_CARD 名片分享,ADD_SCENE_QR_CODE 掃描二維碼,ADD_SCENEPROFILE LINK 圖文頁內名稱點擊,ADD_SCENE_PROFILE_ITEM 圖文頁右上角菜單,ADD_SCENE_PAID 支付後關注,ADD_SCENE_OTHERS 其餘
qr_scene ------------二維碼掃碼場景(開發者自定義)
qr_scene_str------------ 二維碼掃碼場景描述(開發者自定義)
微信公衆平臺jsapi開發教程(1)獲取:
https://blog.csdn.net/linfanhehe/article/details/52354848
生成簽名以前必須先了解一下jsapi_ticket,jsapi_ticket是公衆號用於調用微信JS接口的臨時票據。正常狀況下,jsapi_ticket的有效期爲7200秒,經過access_token來獲取。因爲獲取jsapi_ticket的api調用次數很是有限,頻繁刷新jsapi_ticket會致使api調用受限,影響自身業務,開發者必須在本身的服務全局緩存jsapi_ticket 。《參照2.2》和咱們獲取普通access_token相似,因爲有訪問次數的限制,咱們將次參數調爲全局自動觸發;
接口:
URL: https://api.weixin.qq.com/cgi-bin/ticket/getticket
參數: access_token-----《參照2.2》
Type----「jsapi」字符串
返回參數:ticket------------調用微信
jsapi
的憑證票
全部須要使用JS-SDK的頁面必須先注入配置信息,不然將沒法調用(同一個url僅需調用一次,對於變化url的SPA的web app可在每次url變化時進行調用,目前Android微信客戶端不支持pushState的H5新特性,因此使用pushState來實現web app的頁面會致使簽名失敗,此問題會在Android6.2中修復)。
微信調取音頻、視頻、分享等接口前,須要先初始化驗證
JS:
app.ajax('/wxInitConfig.action',{'url':location.href},function(configDate){
wx.config({
debug: true, // 開啓調試模式,調用的全部api的返回值會在客戶端alert出來,若要查看傳入的參數,能夠在pc端打開,參數信息會經過log打出,僅在pc端時纔會打印。
appId: configDate.appid, // 必填,公衆號的惟一標識
timestamp: configDate.timestamp, // 必填,生成簽名的時間戳
nonceStr: configDate.nonceStr, // 必填,生成簽名的隨機串
signature: configDate.signature,// 必填,簽名,見附錄1
jsApiList: ['onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareTimeline','onMenuShareAppMessage','startRecord','stopRecord','onVoiceRecordEnd','playVoice','stopVoice','onVoicePlayEnd','uploadVoice','onMenuShareQZone'] // 必填,須要使用的JS接口列表,全部JS接口列表見附錄2 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115
});
});
ACTION:
public void wxInitConfig(){
JSONObject wxToken = JSONObject.parseObject(WxToken.globalToken);《參照2.2代碼,獲取全局ticket》
String jsapi_ticket = wxToken.getString("ticket");
//此處若是沒有正常獲取到jsapi_ticket,請查看返回碼,在微信公衆號中查找錯誤信息
//前臺傳過來的當前URL訪問路徑
String url = Dto.getParam().getString("url");
//算法生成時間戳、簽名等信息,見下列代碼,直接附源碼,很少說
Map<String, String> ret = Sign.sign(jsapi_ticket, url);
for (@SuppressWarnings("rawtypes") Map.Entry entry : ret.entrySet()) {
System.err.println(entry.getKey().toString() + " : " + entry.getValue());
Dto.getResult().set(entry.getKey().toString(), entry.getValue());
}
Dto.getResult().set("appid", wxProp.get("appid"));
this.renderJson(Dto.getResult().getData());
}
///////////////算法生成時間戳、簽名等信息/////////////////////////////
////過程當中可能調用MessageDigest 包,請自行Baidu下載////////////////
package com.gzh.api.utils;
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;
public class Sign {
public static void main(String[] args) {
String jsapi_ticket = "jsapi_ticket";
// 注意 URL 必定要動態獲取,不能 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 = "";
//注意這裏參數名必須所有小寫,且必須有序
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);
}
}
參考微信開發說明文檔
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115
/* 開啓錄音*/
wx.startRecord({
success:function() {
alert(「接口調用成功」);
},
Fail:function(){
alert(「接口調用失敗」);//能夠檢查access_token權限或者彈出調試,具體問題具體分析《參照2.8》,開啓調試模式
},
Cancel:function(){
alert(「用戶拒絕提供錄音權限」);
}
});
/* 中止錄音*/
wx.stopRecord({
success: function (res) {
var localId = res.localId;
}
});
/* 監聽錄音自動中止(因爲微信有錄音時限限制,一次錄音最大時長爲1分鐘,若超過此時=時限未中止錄音,則執行此接口方法)*/
wx.onVoiceRecordEnd({//錄音時間超過一分鐘沒有中止的時候會執行 complete 回調complete: function (res) {var localId = res.localId;}});
注:我在此處有一個錄音時長大於一分鐘的問題,目前沒有好的解決方案,樓主在此處的解決方案是:當執行上述大於一分鐘回調時,從新開啓錄音接口,直至手動關閉錄音;將幾個錄音的localId
存起來,一併發至後臺獲取錄音文件,合成一份音頻文件,合成代碼網上找,但這個方法有一個Bug,在從新開啓錄音,即轉換的這個時間段,有大概1~2秒的錄音失效期;
/* 播放錄音 */
wx.playVoice({localId: '' //須要播放的音頻的本地ID,由stopRecord接口得到});
/* 暫停播放錄音 */
wx.pauseVoice({localId: '' //須要暫停的音頻的本地ID,由stopRecord接口得到});
/* 中止播放錄音 */
wx.stopVoice({localId: '' //須要中止的音頻的本地ID,由stopRecord接口得到});
/* 監聽錄音播放完畢接口 */
wx.onVoicePlayEnd({success: function (res) {var localId = res.localId; //返回音頻的本地ID}});
/* 上傳錄音接口 */
wx.uploadVoice({localId: '', //須要上傳的音頻的本地ID,由stopRecord接口得到isShowProgressTips: 1, //默認爲1,顯示進度提示success: function (res) {var serverId = res.serverId; //返回音頻的服務器端ID}});
備註:上傳語音有效期3天,可用微信多媒體接口下載語音到本身的服務器,此處得到的 serverId 即 media_id,參考文檔 .目前多媒體文件下載接口的頻率限制爲10000次/天,如須要調高頻率,請登陸微信公衆平臺,在開發 - 接口權限的列表中,申請提升臨時上限。
根據serverId獲取微信服務器上的錄音文件;
調用接口:
URL: http://file.api.weixin.qq.com/cgi-bin/media/get
參數: access_token------參照《2.2》
media_id----------serverId
返回參數是文件流,需自行解析;附實例;
Eg:
/* (non-Javadoc)
* @see com.gzh.api.service.ReciteContentService#saveRecite(com.sdp.core.dt.DtoParam)
*/
@Override
public DtoResult saveRecite(DtoParam param) throws Exception{
/** 參數接收*/
String contentName = Dto.getParam().getString("contentName");//詩詞名稱
String contentId = Dto.getParam().getString("contentId");//詩詞ID
String fansId = Dto.getParam().getString("fansId");//粉絲ID
String serverId = Dto.getParam().getString("serverId");
Record isFans = api.findFirst("select * from gzh_personalcenter where loginid = ?", fansId);
if(isFans != null && isFans.get("department") != null && !isFans.get("department").equals("")){
String[] serverIds = Str.isNotAnyEmpty(serverId)?serverId.split(","):null;/* 當傳過來的serverId有效時執行*/
if(serverIds != null && serverIds.length > 0){
System.err.println("正在上傳錄音文件...");
long time1 = System.currentTimeMillis();
/* 刪除舊錄音文件及數據*/
List<Record> deleteList = api.find("select * from gzh_recitecontent where fansid = ? and contentid = ?", fansId, contentId);
for(Record re : deleteList){
File file = new File(fileRoot + re.getStr("path"));
if(file.exists()) file.delete();
api.deleteById("gzh_recitecontent", re.getStr("id"));
}
//String token = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token", paramMap);
JSONObject wxToken = JSONObject.parseObject(WxToken.globalTokenObject);
List<String> tempRecites = new ArrayList<>();/* amr文件集合*/
List<String> newRecites = new ArrayList<>();/* mp3文件集合*/
/* 進行臨時目錄建立*/
String tempResPath = fileRoot + tempPath;
if(!new File(tempResPath).exists()) new File(tempResPath).mkdirs();
/* 進行錄音文件目錄建立*/
String realResPath = fileRoot + fileStore + recitePath + "/" + contentId + "/" + fansId;//文件實際存儲目錄
if(!new File(realResPath).exists()) new File(realResPath).mkdirs();//先進行目錄建立操做
int size = 0;/* 記錄全部文件輸入流大小*/
/* 定義輸出流寫入參數*/
FileOutputStream fileOutputStream = null;
InputStream inputStream = null;
String fileName = ID.get();/* 文件名稱*/
/* 將文件分批取出並保存爲MP3文件*/
for(int i = 0; i < serverIds.length; i++){
try{
/* 從微信服務器獲取錄音文件,格式爲amr*/
String url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token="
+ wxToken.getString("access_token") + "&media_id=" + serverIds[i];
URL urlGet = new URL(url);
HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
http.setRequestMethod("GET"); // 必須是get方式請求
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
// System.setProperty("sun.net.client.defaultConnectTimeout", "3000");// 鏈接超時30秒
// System.setProperty("sun.net.client.defaultReadTimeout", "3000"); // 讀取超時30秒
http.connect();
// 獲取文件轉化爲byte流
inputStream = http.getInputStream();
size += inputStream.available();
String tempFileName = tempResPath + "/" + fileName + (i+1) + ".amr";
String recitFileName = tempResPath + "/" + fileName + (i+1) + ".mp3";
if(serverIds.length == 1){
recitFileName = realResPath + "/" + fileName + ".mp3";
}
tempRecites.add(tempFileName);
newRecites.add(recitFileName);
byte[] data = new byte[1024 * 1024];
int len = 0;
fileOutputStream = new FileOutputStream(tempFileName);
while ((len = inputStream.read(data)) != -1) {
fileOutputStream.write(data, 0, len);
}
/* 關閉流資源*/
if(inputStream != null) inputStream.close();
if(fileOutputStream != null) fileOutputStream.close();
/* amr格式轉換成mp3格式*/
File source = new File(tempFileName);
File target = new File(recitFileName);
AudioAttributes audio = new AudioAttributes();
Encoder encoder = new Encoder();
audio.setCodec("libmp3lame");
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("mp3");
attrs.setAudioAttributes(audio);
encoder.encode(source, target, attrs);/* 執行轉換*/
}catch(Exception e) {
System.err.println(e.getMessage());
}
}
System.err.println("輸入流大小:" + size);
File file = new File(realResPath + "/" + fileName + ".mp3");
if(newRecites.size() > 1){
try {
combine(file.getPath(), newRecites);
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
System.err.println("音頻大小:" + file.length()/1024 + "KB");
//信息保存數據庫表
Record record = new Record();
record.set("id", ID.get());
record.set("contentname", contentName);//詩詞名稱
record.set("contentid", contentId);//詩詞ID
record.set("fansid", fansId);//粉絲ID
record.set("path", fileStore + recitePath + "/" + contentId + "/" + fansId + "/" + fileName + ".mp3");//吟誦錄音路徑
record.set("uploaddate", new Timestamp(new java.util.Date().getTime()));//吟誦時間
api.save(getTableName(), record);
long time2 = System.currentTimeMillis();
System.out.println("上傳時間:" + (time2 - time1)/1000 + "s");
/* 刪除臨時文件*/
for(int i = 0; i < tempRecites.size(); i++){
File tempFile = new File(tempRecites.get(i));
if(tempFile.exists()){
System.out.println("刪除AMR臨時文件結果[" + tempRecites.get(i) + ":" + tempFile.delete() + "]");
}
if(tempRecites.size() > 1){
tempFile = new File(newRecites.get(i));
if(tempFile.exists()){
System.out.println("刪除MP3臨時文件結果[" + newRecites.get(i) + ":" + tempFile.delete() + "]");
}
}
}
}
}else{
Dto.getResult().setSuccessStatus("請完善我的信息");
}
return Dto.getResult();
}
/*將多個MP3文件合併*/
private static boolean combine(String outFile, List<String> inFiles) throws Exception{
File[] files = new File[inFiles.size()];
for(int i = 0; i < files.length; i++){
files[i] = new File(inFiles.get(i));
}
FileInputStream fis = null;
FileOutputStream fos = new FileOutputStream(outFile, true);//合併其實就是文件的續寫,寫成true
for (int i = 0; i < files.length; i++){
fis = new FileInputStream(files[i]);
int len = 0;
for (byte[] buf = new byte[1024 * 1024]; (len = fis.read(buf)) != -1;){
fos.write(buf, 0, len);
}
fis.close();
}
fos.close();
return true;
}
/* 下載錄音接口 */
wx.downloadVoice({serverId: '', //須要下載的音頻的服務器端ID,由uploadVoice接口得到isShowProgressTips: 1, //默認爲1,顯示進度提示success: function (res) {var localId = res.localId; //返回音頻的本地ID}});
/* 此下載接口只是返回微信服務器端的文件*/localId ,而後調用播放接口讀取localId ,可是微信服務器端的錄音文件只保留3天,請知曉!