upload-file.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. $(function() {
  2. //从后台那边获取签名等信息
  3. var params = {};
  4. params.pageUrl = window.location.href;
  5. $.ajax(server + "weixin/getSign", {
  6. data: params,
  7. dataType: "json",
  8. type: "post",
  9. success: function(res){
  10. if (res.status == 200) {
  11. var t = res.data.timestamp;
  12. var noncestr = res.data.noncestr;
  13. var signature = res.data.signature;
  14. wx.config({
  15. //debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  16. appId: appId, // 必填,公众号的唯一标识
  17. timestamp: t, // 必填,生成签名的时间戳
  18. nonceStr: noncestr, // 必填,生成签名的随机串
  19. signature: signature,// 必填,签名,见附录1
  20. jsApiList: [
  21. 'chooseImage',
  22. 'uploadImage'
  23. ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  24. });
  25. }
  26. }
  27. });
  28. });
  29. //微信sdk配置出错
  30. wx.error(function (res) {
  31. alert("wx.error:" + res.errMsg);
  32. });
  33. function chooseImage(){
  34. wx.chooseImage({
  35. success: function (res) {
  36. var d = dialog({contentType:'load', skin:'bk-popup', content:'正在努力上传图片,请耐心等待~'}).showModal();
  37. uploadImagesPromise(res.localIds).then(function(serverIds){
  38. return getReqPromises(_.map(serverIds,function(id) {
  39. return {
  40. url: 'patient/archives/event/uploadImg',
  41. data: {
  42. mediaId: id
  43. },
  44. reqType: 'POST'
  45. }
  46. })).then(function(datas) {
  47. //dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'上传成功!'}).show();
  48. _.each(datas,function(o) {
  49. if(o.status==200) {
  50. if(o.url) {
  51. appendFile(o.url);
  52. }
  53. }
  54. });
  55. d.close();
  56. $('#img_ul li>img:not("[data-id]")').eq(0).closest('li').trigger('click');
  57. })
  58. },function() {
  59. d.close();
  60. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'上传失败!'}).show();
  61. }).catch(function(e) {
  62. d.close();
  63. console && console.log(e);
  64. })
  65. }
  66. });
  67. }
  68. //获取需要上传的图片
  69. function getImages() {
  70. var images = [];
  71. $("#img_ul").find("img:not(.del-icon)").each(function() {
  72. var imgSrc = $(this).attr("src");
  73. images.push(imgSrc);
  74. });
  75. return images;
  76. }
  77. function uploadImagesPromise(localIds) {
  78. return new Promise(function(resolve, reject) {
  79. var serverIds = [];
  80. function syncUpload() {
  81. if (!localIds.length) {
  82. resolve(serverIds);
  83. } else {
  84. var localId = localIds.pop();
  85. wx.uploadImage({
  86. localId: localId,
  87. isShowProgressTips: 0,
  88. success: function(res) {
  89. serverIds.push(res.serverId);
  90. syncUpload();
  91. },
  92. fail: function (res) {
  93. reject(res)
  94. }
  95. });
  96. }
  97. }
  98. syncUpload();
  99. });
  100. }
  101. // 添加文件
  102. function appendFile(p) {
  103. 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>');
  104. $('#add_img_li').before($li);
  105. }
  106. var gallery = null;
  107. function previewImage(sid) {
  108. var pswpElement = document.querySelectorAll('.pswp')[0];
  109. var w = $(window).width(), h = $(window).height();
  110. var items = _.map(getImages(),function(url) {
  111. return {
  112. src: url,
  113. w: w,
  114. h: h,
  115. initialPosition: { x: 0, y:0 }
  116. }
  117. });
  118. var options = {
  119. // optionName: 'option value'
  120. // for example:
  121. index: sid||0 // start at first slide
  122. };
  123. options.mainClass = 'pswp--minimal--dark';
  124. options.barsSize = {top:0,bottom:0};
  125. options.captionEl = false;
  126. options.fullscreenEl = false;
  127. options.shareEl = false;
  128. options.bgOpacity = 0.85;
  129. options.tapToToggleControls = false;
  130. options.pinchToClose = false;
  131. options.clickToCloseNonZoomable = false;
  132. options.tapToClose = false;
  133. options.errorMsg = '<div class="pswp__error-msg">对不起,该图片加载失败</div>';
  134. // Initializes and opens PhotoSwipe
  135. gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
  136. gallery.init();
  137. }