微信就是個坑!!!接觸過淘寶,支付寶,順豐,銀行,保險的對接,但這輩子估計都碰不上比微信更坑的了!!!
文檔API各種低級錯誤。
DEMO都是錯的,說好的參數小寫,demo里面是大寫。
SHA1傳參加密的樣例,用他給的串,怎么都加密都加不出他的結果。
甭管什么異常,調失敗了就一條,寫個錯誤碼的枚舉能費多少時間?微信大爺!
文檔里漏到的條件更不用說了。
原本以為很簡單的事,頂多兩小時能搞定,寫到ACIONT里,按其要求,后端生成參數,直接寫到VIEW里。(這會微信支會的審核還沒下來)
通不過,怎么檢查都通不過。聯系客服各種踢皮球,官方郵件對面應該就是具干屍。
因為本身項目和微信緩存的緣故,調試困難,無奈寫個純HTML+js的實現。(壓根找不到微信清web緩存的地方,為了繞過微信的緩存,寫靜態頁,寫一個html1,測過后再寫個html2……)
還是不過,文檔的條件條條都符合(文檔加密的大小寫是錯的,我是對的,文檔里加密的樣例是錯的,我也是對的)
已經放棄了,這么簡單個問題,前前后后居然浪費了一星期,很羞慚的面對領導,說微信坑爹,領導也不知信於不信!!與之對比的是,其他同事的接口實現,那叫個順利,我都准備卷鋪蓋了。
后來某一天,順手到后台一看,頓時傻眼。
文檔沒有的條件1,要開通微信支付。
文檔沒有的條件2,要開啟共享收貨地址。
文檔沒有的條件3,必須在設置的授權目錄之下發起API調用。
把靜態頁改成VIEW,加路由,調到對應路徑下,通過!大爺,您可終於過了!
項目真正用的時候,還是要寫在后台,前台JS代碼並未封裝和優化。
貼代碼

<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <script src="~/Scripts/jquery-1.10.2.js"></script> <script src="~/Scripts/components/core-min.js"></script> <script src="~/Scripts/components/sha1.js"></script> <script> function getappid() { return "appid"; //換成自已的appid } function getQueryString(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; } function getTimeStamp() { var timestamp = new Date().getTime(); var timestampstring = timestamp.toString();//一定要轉換字符串 oldTimeStamp = timestampstring; return timestampstring; } //得到隨機字符串 function getNonceStr() { var $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; var maxPos = $chars.length; var noceStr = ""; for (i = 0; i < 32; i++) { noceStr += $chars.charAt(Math.floor(Math.random() * maxPos)); } oldNonceStr = noceStr; return noceStr; } //獲取CODE var getcodeobj = { appid: getappid(), redirect_uri: "", response_type: "code", scope: "snsapi_base", state: "1" }; //拼接url傳參字符串 function perapara(objvalues, isencode) { var parastring = ""; for (var key in objvalues) { isencode = isencode || false; if (isencode) { parastring += (key + "=" + encodeURIComponent(objvalues[key]) + "&"); } else { parastring += (key + "=" + objvalues[key] + "&"); } } parastring = parastring.substr(0, parastring.length - 1); return parastring; } //得到用戶code function getcode() { var code = getQueryString("code"); if (!code) { var getcodeparas = $.extend(getcodeobj, { redirect_uri: window.location.href }); window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?" + perapara(getcodeparas) + "#wechat_redirect"; } else { return code; } } //得到用戶accesstoken function getaccesstoken(code) { var url = "/wechat.oauth/GetAccessToken"; $.ajax({ type: "POST", //默認是GET dataType: "text", url: url, data: "code=" + code, async: false, //異步 cache: false, //不加載緩存 success: function (obj) { access_tokenstring = obj; isaccget = true; }, error: function (req, msg, ex) { $("#showerror").val(req.responseText.toString()); } }); } function getSign(beforesingstring) { sign = CryptoJS.SHA1(beforesingstring).toString(); return sign; } var signparasobj = { "accesstoken": "", "appid": getappid(), "noncestr": "", "timestamp": "", "url": "" }; </script> </head> <body> <div> <label>showerror</label> <textarea id="showerror"></textarea> <!--<div id="showerror"></div>--> <label>code</label><input type="text" id="txtcode" /><br /> <textarea id="txtinfo"></textarea> <label>accesstoken</label><input type="text" id="txtaccesstoken" /> </div> <div id="showtestresult"></div> <label for="redhref">href測試</label> <input type="text" id="redhref" /><br /> <label for="redhref">加密前參數</label> <input name="44" id="signpre" type="text" /><br /> <input name="44" id="thisurl" type="text" /><br /> <input name="33" id="thisurl2" type="text" /><br /> <input type="button" id="getaddress2" onclick="editAddress()" value="得到地址方式2" /><br /> <input name="address1" id="address1" type="text" /><br /> <input name="address2" id="address2" type="text" /><br /> <input name="address3" id="address3" type="text" /><br /> <div id="divinfo"></div> <div id="resvalues">aaaaaaaaS</div> <script> var codestring = ""; var access_tokenstring = ""; var oldTimeStamp;//保存timestamp,提交用 var oldNonceStr; //保存nonceStr,提交用 var sign; var isaccget = false; $(document).ready( function () { codestring = getcode(); $("#thisurl").val(window.location.href); $("#txtcode").val(codestring); getaccesstoken(codestring); $("#txtaccesstoken").val(access_tokenstring); }); function editAddress() { var showobj2 = txtinfo != null ? txtinfo : document.getElementById("txtinfo"); showobj2.value = '進入微信事件'; $("#thisurl2").val(window.location.href); //簽名 var signparas = $.extend(signparasobj, { "accesstoken": access_tokenstring, "noncestr": getNonceStr(), "timestamp": getTimeStamp(), "url": window.location.href }); $("#signpre").val(perapara(signparas)); //簽名 var signstring = getSign(perapara(signparas)); if (isaccget) { WeixinJSBridge.invoke('editAddress', { "appId": getappid(), "scope": "jsapi_address", "signType": "sha1", "addrSign": signstring, "timeStamp": oldTimeStamp, "nonceStr": oldNonceStr } , function (res) { var ff = ''; var obj = resvalues != null ? resvalues : document.getElementById('resvalues'); if (res == null) { obj.innerText = '測試返回為空'; } else { for (var key in res) { var js = 'res.' + key + ' = ' + res[key].toString(); ff = ff + js; } obj.innerText = ff; document.form1.address1.value = res.proviceFirstStageName; document.form1.address2.value = res.addressCitySecondStageName; } }); } } </script> </body> </html>
引jq,CryptoJS加密包。
ajax調用的后台action
public string GetAccessToken(string code) { string appId = “你懂的”; string secret = “你懂的”;
var result = Senparc.Weixin.MP.AdvancedAPIs.OAuth.GetAccessToken(appId, secret, code);
return result.access_token;
}
調用SDK用的 Senparc.Weixin
各位同道,見微信最好繞道。
//部分回復有疑問
這是目前微信最新的接口文檔。
https://res.wx.qq.com/paymchres/zh_CN/htmledition/download/bussiness-course2/wxm-payment-share-address1eec8d.pdf
加密的部分依然是錯的。
訪問路徑的設置,文檔里也絲毫未提。
更舊的版本,大小寫都是錯的。
這是文檔里加密的部分,不心細看的可以試試,神仙才能加出這種結果。
SHA1(accesstoken=OezXcEiiBSKSxW0eoylIeBFk1b8VbNtfWA
LJ5g6aMgZHaqZwK4euEskSn78Qd5pLsfQtuMdgmhajVM5QDm24W8X3
tJ18kz5mhmkUcI3RoLm7qGgh1cEnCHejWQo8s5L3VvsFAdawhFxUuLmg
h5FRA&appid=wx17ef1eaef46752cb&noncestr=123456×tamp=1384841012&url=http://open.weixin.qq.com/)=ca604c740945587544a9cc25e58dd090f200e6fb