upload-diet.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //压缩完后的图片
  2. var compressImages = {};
  3. var dd = dialog({contentType:'load', skin:'bk-popup', content:'保存中...'});
  4. var Request = GetRequest();
  5. var id = Request['id'],
  6. code = decodeURIComponent(Request['type']),
  7. time = decodeURIComponent(Request['time']),
  8. val1 = decodeURIComponent(Request['val']);
  9. var lastDate = '';
  10. var images_old='';
  11. var imagesStr = decodeURIComponent(Request['image']);
  12. if(imagesStr){
  13. var arr = imagesStr.split(',');
  14. var newArr = $.map(arr,function(o,i){
  15. return getImgUrl(o)
  16. })
  17. images_old = newArr.join(',')
  18. }
  19. $(function() {
  20. //从后台那边获取签名等信息
  21. var params = {};
  22. params.pageUrl = window.location.href;
  23. $.ajax(server + "weixin/getSign", {
  24. data: params,
  25. dataType: "json",
  26. type: "post",
  27. success: function(res){
  28. if (res.status == 200) {
  29. var t = res.data.timestamp;
  30. var noncestr = res.data.noncestr;
  31. var signature = res.data.signature;
  32. wx.config({
  33. // debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  34. appId: appId, // 必填,公众号的唯一标识
  35. timestamp: t, // 必填,生成签名的时间戳
  36. nonceStr: noncestr, // 必填,生成签名的随机串
  37. signature: signature,// 必填,签名,见附录1
  38. jsApiList: [
  39. 'chooseImage',
  40. 'uploadImage'
  41. ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  42. });
  43. }
  44. }
  45. });
  46. /* 选择日期 */
  47. var opt1 = {
  48. preset: 'date',
  49. theme: 'ios',
  50. lang: 'zh',
  51. dateFormat: 'yy-mm-dd',
  52. minDate: new Date(1900, 01, 01)
  53. };
  54. $('input[data-time=date]').mobiscroll(opt1);
  55. if (id) {
  56. var date = new Date(time.replace(/\-/g, "/"))
  57. $("#date").val(date.Format("yyyy-MM-dd"));
  58. } else {
  59. var date = new Date()
  60. $("#date").val(date.Format("yyyy-MM-dd"));
  61. }
  62. if (id) {
  63. $('#ys_val').val(val1);
  64. var imgArr = images_old.split(',');
  65. for (var i = 0, len = imgArr.length; i < len; i++) {
  66. appendFile(imgArr[i]);
  67. }
  68. }
  69. document.getElementById("goback").addEventListener("tap", function() {
  70. var date = document.getElementById("date").value;
  71. var val = document.getElementById("ys_val").value;
  72. var images = getImages();
  73. lastDate = date.substring(0,10);
  74. if (val && date) {
  75. } else {
  76. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'请填写完整信息'}).show();
  77. return;
  78. }
  79. var data = {};
  80. data.record_date = date + ' 00:00:00';
  81. data.content = val;
  82. if(images.length == 0){
  83. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'请选择图片'}).show();
  84. return;
  85. }
  86. else{
  87. dd.showModal();
  88. uploadImage(data);
  89. }
  90. });
  91. });
  92. //微信sdk配置出错
  93. wx.error(function (res) {
  94. alert("wx.error:" + res.errMsg);
  95. });
  96. var serverId = "";
  97. function chooseImage(){
  98. wx.chooseImage({
  99. count: 3-getImages().length,
  100. success: function (res) {
  101. for (var i in res.localIds) {
  102. if(i < 3) appendFile(res.localIds[i]);
  103. }
  104. }
  105. });
  106. }
  107. function uploadImage(data){
  108. var images = getWxImages();
  109. serverId = "";
  110. if (images[0].length > 0) {
  111. var i = 0, length = images[0].length;
  112. function upload() {
  113. wx.uploadImage({
  114. localId: images[0][i],
  115. isShowProgressTips: 0,
  116. success: function (res) {
  117. i++;
  118. serverId =serverId + "," + res.serverId;
  119. if (i < length) {
  120. upload();
  121. }
  122. if(i == images[0].length){
  123. serverId = serverId + "," + images[1].join(',');
  124. doSubmit(data);
  125. }
  126. },
  127. fail: function (res) {
  128. alert('上传图片失败');
  129. dd.close()
  130. }
  131. })
  132. }
  133. upload();
  134. }else{
  135. serverId = images[1].join(',');
  136. doSubmit(data);
  137. }
  138. }
  139. //提交咨询
  140. function doSubmit(data) {
  141. if(serverId.substr(0,1) == ','){
  142. var data1 = serverId.substr(1);
  143. if(data1.substr(-1) == ','){
  144. data.mediaIds = data1.substring(0,data1.length - 1);
  145. }else{
  146. data.mediaIds = data1;
  147. }
  148. }else{
  149. data.mediaIds = serverId;
  150. }
  151. if (id) {
  152. var params = {
  153. id:id,
  154. type:1,
  155. value1:data.record_date,
  156. value2:data.content,
  157. value3:data.mediaIds,
  158. mediaIds:data.mediaIds
  159. }
  160. sendPost("patient/health_record/modifyHealthCare", params, "json", "post", addDietFailed, addDietSuccess);
  161. } else {
  162. //发送ajax请求
  163. sendPost("patient/health_record/add_diet", data, "json", "post", addDietFailed, addDietSuccess);
  164. }
  165. }
  166. function addDietFailed(res) {
  167. dd.close();
  168. if (res && res.msg) {
  169. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:res.msg}).show();
  170. } else {
  171. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'提交失败'}).show();
  172. }
  173. }
  174. function addDietSuccess(res) {
  175. if (res.status == 200) {
  176. dd.close().remove();
  177. setTimeout(function(){
  178. window.location.href='yinshi-view.html?dateT=' + lastDate;
  179. },1000)
  180. } else {
  181. //非200则为失败
  182. addDietFailed(res);
  183. }
  184. }
  185. //查看图片
  186. function viewImg(dom) {
  187. var $img = $(dom);
  188. var thissrc = $img.attr("data-src");
  189. var mWid = $(window).width();
  190. var mHei = $(window).height();
  191. 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>';
  192. $("body").append(nHtml);
  193. $(".delimgpop").click(function() {
  194. $(this).remove()
  195. });
  196. };
  197. //删除图片
  198. function delImg(dom) {
  199. var $li = $(dom).parent();
  200. $li.remove();
  201. $("#image_tips").text(getImages().length + "/3");
  202. var amount = getImages().length;
  203. if(amount <= 2){
  204. $("#add_img_li").show();
  205. }
  206. return;
  207. }
  208. // 添加文件
  209. function appendFile(p) {
  210. var amount = getImages().length;
  211. if (amount >= 2) {
  212. $("#add_img_li").hide();
  213. }
  214. if(amount < 3){
  215. var $add_img_li = $("#add_img_li");
  216. 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>');
  217. $add_img_li.before($li);
  218. $("#image_tips").text(getImages().length + "/3");
  219. }
  220. }
  221. //获取需要上传的图片
  222. function getImages() {
  223. var images = [];
  224. $("#img_ul").find("img").each(function() {
  225. var imgSrc = $(this).attr("data-src");
  226. images.push(imgSrc);
  227. });
  228. return images;
  229. }
  230. //获取需要微信上传的图片
  231. function getWxImages() {
  232. var images = [];
  233. var noimages = [];
  234. $("#img_ul").find("img").each(function() {
  235. var imgSrc = $(this).attr("data-src");
  236. if(imgSrc.indexOf('group1/')>-1){
  237. noimages.push(imgSrc);
  238. }else{
  239. images.push(imgSrc);
  240. }
  241. });
  242. return [images,noimages];
  243. }