1 var images = { 2 localId : [], 3 serverId : [] 4 };//傳給後臺的參數 5 //修改頭像 6 $.ajax({ 7 url: "", 8 dataType: "json", 9 data: { 10 url: location.href.split('#')[0] 11 }, 12 type: "post", 13 success: function(data) { 14 var dataObj = eval("(" + data + ")"); 15 wx.config({ 16 debug: false, // 開啓調試模式,調用的全部api的返回值會在客戶端alert出來,若要查看傳入的參數,能夠在pc端打開,參數信息會經過log打出,僅在pc端時纔會打印。 17 appId: dataObj.results.data.appId, // 必填,公衆號的惟一標識 18 timestamp: dataObj.results.data.timestamp, // 必填,生成簽名的時間戳 19 nonceStr: dataObj.results.data.nonceStr, // 必填,生成簽名的隨機串 20 signature: dataObj.results.data.signature, // 必填,簽名,見附錄1 21 jsApiList: ['downloadImage', 'chooseImage', 'uploadImage'] 22 // 必填,須要使用的JS接口列表,全部JS接口列表見附錄2 23 }); 24 //微信JS-SDK說明文檔地址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115 25 wx.ready(function() { 26 // 若是須要在頁面加載時就調用相關接口,則須把相關接口放在ready函數中調用來確保正確執行。 27 $(".setheadImg").click(function() { 28 wx.chooseImage({ 29 debug: false, 30 count: 1, // 限制圖片最多選1張 31 sizeType: ['compressed'], // 指定是壓縮圖 32 success: function(res) { 33 var localIds = res.localIds; 34 if(localIds.length == 1) { 35 $(".setheadImg").attr("src", localIds[0]); 36 // 微信的坑點iOS微信6.5.3及其以後的版本window.__wxjs_is_wkwebview爲true時是使用WKWebview,爲false或者「undefine」時是UIWebviewWKWebview 37 // 不支持res.localIds顯示圖片,必須用res.localData 38 if(window.__wxjs_is_wkwebview) { 39 wx.getLocalImgData({ 40 localId: localIds[0], 41 success: function(res) { 42 if(res.localData != "") { 43 $(".setheadImg").attr("src", res.localData);//頭像圖片的容器 44 } 45 } 46 }); 47 } 48 syncUpload(localIds); 49 50 } 51 }, 52 error: function(res) { 53 alert(res) 54 } 55 }); 56 }); 57 // sync upload pic 58 var syncUpload = function(localIds) { 59 var localId = localIds.pop(); 60 wx.uploadImage({ 61 localId: localId, 62 isShowProgressTips: 1, 63 success: function(res) { 64 var serverId = res.serverId; // 返回圖片的服務器端ID 65 images.serverId.push(serverId); 66 if(localIds.length > 0) { 67 syncUpload(localIds); 68 } 69 } 70 }); 71 } 72 }, function(data) { 73 alert('系統繁忙,請刷新頁面!') 74 }); 75 }, 76 error: function(data) { 77 alert('系統繁忙,請刷新頁面!') 78 } 79 })