$(function() { //从后台那边获取签名等信息 var params = {}; params.pageUrl = window.location.href; $.ajax(server + "weixin/getSign", { data: params, dataType: "json", type: "post", success: function(res){ if (res.status == 200) { var t = res.data.timestamp; var noncestr = res.data.noncestr; var signature = res.data.signature; wx.config({ //debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 appId: appId, // 必填,公众号的唯一标识 timestamp: t, // 必填,生成签名的时间戳 nonceStr: noncestr, // 必填,生成签名的随机串 signature: signature,// 必填,签名,见附录1 jsApiList: [ 'chooseImage', 'uploadImage' ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2 }); } } }); }); //微信sdk配置出错 wx.error(function (res) { alert("wx.error:" + res.errMsg); }); function chooseImage(){ wx.chooseImage({ success: function (res) { var d = dialog({contentType:'load', skin:'bk-popup', content:'正在努力上传图片,请耐心等待~'}).showModal(); uploadImagesPromise(res.localIds).then(function(serverIds){ return getReqPromises(_.map(serverIds,function(id) { return { url: 'patient/archives/event/uploadImg', data: { mediaId: id }, reqType: 'POST' } })).then(function(datas) { //dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'上传成功!'}).show(); _.each(datas,function(o) { if(o.status==200) { if(o.url) { appendFile(o.url); } } }); d.close(); $('#img_ul li>img:not("[data-id]")').eq(0).closest('li').trigger('click'); }) },function() { d.close(); dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'上传失败!'}).show(); }).catch(function(e) { d.close(); console && console.log(e); }) } }); } //获取需要上传的图片 function getImages() { var images = []; $("#img_ul").find("img:not(.del-icon)").each(function() { var imgSrc = $(this).attr("src"); images.push(imgSrc); }); return images; } function uploadImagesPromise(localIds) { return new Promise(function(resolve, reject) { var serverIds = []; function syncUpload() { if (!localIds.length) { resolve(serverIds); } else { var localId = localIds.pop(); wx.uploadImage({ localId: localId, isShowProgressTips: 0, success: function(res) { serverIds.push(res.serverId); syncUpload(); }, fail: function (res) { reject(res) } }); } } syncUpload(); }); } // 添加文件 function appendFile(p) { var $li = $('