upload-diet.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //压缩完后的图片
  2. var compressImages = {};
  3. var dd = dialog({contentType:'load', skin:'bk-popup', content:'保存中...'});
  4. $(function() {
  5. /* 选择日期 */
  6. $('input')
  7. var opt1 = {
  8. preset: 'date',
  9. theme: 'ios',
  10. lang: 'zh',
  11. minDate: new Date(1900, 01, 01)
  12. };
  13. $('input[data-time=date]').mobiscroll(opt1);
  14. //从后台那边获取签名等信息
  15. var params = {};
  16. params.pageUrl = server + "wx/html/ssgg/html/add-diet-record.html";
  17. $.ajax(server + "weixin/getSign", {
  18. data: params,
  19. dataType: "json",
  20. type: "post",
  21. success: function(res){
  22. if (res.status == 200) {
  23. var t = res.data.timestamp;
  24. var noncestr = res.data.noncestr;
  25. var signature = res.data.signature;
  26. wx.config({
  27. // debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  28. appId: appId, // 必填,公众号的唯一标识
  29. timestamp: t, // 必填,生成签名的时间戳
  30. nonceStr: noncestr, // 必填,生成签名的随机串
  31. signature: signature,// 必填,签名,见附录1
  32. jsApiList: [
  33. 'chooseImage',
  34. 'uploadImage'
  35. ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  36. });
  37. }
  38. }
  39. });
  40. document.getElementById("goback").addEventListener("tap", function() {
  41. var date = document.getElementById("date").value;
  42. var val = document.getElementById("ys_val").value;
  43. var images = getImages();
  44. if (val && date) {
  45. } else {
  46. dialog({contentType:'tipsbox', skin:'bk-popup' , content:'请填写完整信息'}).show();
  47. return;
  48. }
  49. var data = {};
  50. data.record_date = date;
  51. data.content = val;
  52. if(images.length == 0){
  53. dialog({contentType:'tipsbox', skin:'bk-popup' , content:'请选择图片'}).show();
  54. return;
  55. }
  56. else{
  57. dd.showModal();
  58. uploadImage(data);
  59. }
  60. });
  61. });
  62. //微信sdk配置出错
  63. wx.error(function (res) {
  64. alert("wx.error:" + res.errMsg);
  65. });
  66. var serverId = "";
  67. function chooseImage(){
  68. wx.chooseImage({
  69. count: 3-getImages().length,
  70. success: function (res) {
  71. for (var i in res.localIds) {
  72. if(i < 3) appendFile(res.localIds[i]);
  73. }
  74. }
  75. });
  76. }
  77. function uploadImage(data){
  78. var images = getImages();
  79. if (images.length == 0) {
  80. return;
  81. }
  82. var i = 0, length = images.length;
  83. serverId = "";
  84. function upload() {
  85. wx.uploadImage({
  86. localId: images[i],
  87. isShowProgressTips: 0,
  88. success: function (res) {
  89. i++;
  90. if(serverId.length == 0){
  91. serverId = res.serverId;
  92. }
  93. else{
  94. serverId =serverId + "," + res.serverId;
  95. }
  96. if (i < length) {
  97. upload();
  98. }
  99. if(i == images.length){
  100. doSubmit(data);
  101. }
  102. },
  103. fail: function (res) {
  104. alert(JSON.stringify(res));
  105. }
  106. });
  107. }
  108. upload();
  109. }
  110. //提交咨询
  111. function doSubmit(data) {
  112. data.mediaIds = serverId;
  113. //发送ajax请求
  114. sendPost("patient/health_record/add_diet", data, "json", "post", addDietFailed, addDietSuccess);
  115. }
  116. function addDietFailed(res) {
  117. dd.close();
  118. if (res && res.msg) {
  119. dialog({contentType:'tipsbox', skin:'bk-popup' , content:res.msg}).show();
  120. } else {
  121. dialog({contentType:'tipsbox', skin:'bk-popup' , content:'提交失败'}).show();
  122. }
  123. }
  124. function addDietSuccess(res) {
  125. if (res.status == 200) {
  126. dd.close().remove();
  127. window.history.back();
  128. } else {
  129. //非200则为失败
  130. addDietFailed(res);
  131. }
  132. }
  133. //查看图片
  134. function viewImg(dom) {
  135. var $img = $(dom);
  136. var thissrc = $img.attr("data-src");
  137. var mWid = $(window).width();
  138. var mHei = $(window).height();
  139. var nHtml = '<div class="delimgpop"><div class="del-img-box"><div class="del-img-con"><img class="del-pop-img" src="' + thissrc + '" style="max-width:' + mWid + 'px; max-height:' + mHei + 'px;"></div></div></div>';
  140. $("body").append(nHtml);
  141. $(".delimgpop").click(function() {
  142. $(this).remove()
  143. });
  144. };
  145. //删除图片
  146. function delImg(dom) {
  147. var $li = $(dom).parent();
  148. $li.remove();
  149. $("#image_tips").text(getImages().length + "/3");
  150. var amount = getImages().length;
  151. if(amount <= 2){
  152. $("#add_img_li").show();
  153. }
  154. return;
  155. }
  156. // 添加文件
  157. function appendFile(p) {
  158. var amount = getImages().length;
  159. if (amount >= 2) {
  160. $("#add_img_li").hide();
  161. }
  162. if(amount < 3){
  163. var $add_img_li = $("#add_img_li");
  164. var $li = $('<li>' + ' <img src="' + p + '" data-src="' + p + '" onclick="viewImg(this)">' + ' <a href="javascript:;" class="del-img" onclick="delImg(this)"><i class="iconfont icon-laji"></i></a>' + '</li>');
  165. $add_img_li.before($li);
  166. $("#image_tips").text(getImages().length + "/3");
  167. }
  168. }
  169. //获取需要上传的图片
  170. function getImages() {
  171. var images = [];
  172. $("#img_ul").find("img").each(function() {
  173. var imgSrc = $(this).attr("data-src");
  174. images.push(imgSrc);
  175. });
  176. return images;
  177. }