123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- $(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 = $('<li>' + '<img src="' + p + '" /><div class="del-wrap"><img src="../images/shanchu02_btn.png" class="del-icon"/></div><p class="catalog-name"></p></li>');
- $('#add_img_li').before($li);
- }
- var gallery = null;
- function previewImage(sid) {
- var pswpElement = document.querySelectorAll('.pswp')[0];
- var w = $(window).width(), h = $(window).height();
- var items = _.map(getImages(),function(url) {
- return {
- src: url,
- w: w,
- h: h,
- initialPosition: { x: 0, y:0 }
- }
- });
- var options = {
- // optionName: 'option value'
- // for example:
- index: sid||0 // start at first slide
- };
- options.mainClass = 'pswp--minimal--dark';
- options.barsSize = {top:0,bottom:0};
- options.captionEl = false;
- options.fullscreenEl = false;
- options.shareEl = false;
- options.bgOpacity = 0.85;
- options.tapToToggleControls = false;
- options.pinchToClose = false;
- options.clickToCloseNonZoomable = false;
- options.tapToClose = false;
- options.errorMsg = '<div class="pswp__error-msg">对不起,该图片加载失败</div>';
- // Initializes and opens PhotoSwipe
- gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
- gallery.init();
- }
|