edit_guidance.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. var action,
  2. code ,
  3. reference, //标记入口
  4. patiCode,
  5. is_sys = false,
  6. content_details,
  7. uploadImages = [],
  8. imMsg = '',
  9. signCode,
  10. imgUrls = [];
  11. var $text_count = $("#text_count"),
  12. text_count = 0,
  13. $img_count = $("#img_count"),
  14. img_count = 0,
  15. $view_btn = $("#view_btn"),
  16. $action_btn = $("#action_btn");
  17. mui.init();
  18. mui.plusReady(function(){
  19. var self = plus.webview.currentWebview();
  20. action = self.action;
  21. reference = self.reference;
  22. imMsg = self.msg;
  23. is_sys = self.is_sys;
  24. code = self.code;
  25. patiCode = self.patiCode;
  26. signCode = self.sign;
  27. console.log(signCode);
  28. content_details = self.content_details;
  29. if (imMsg) {
  30. $("textarea").val(imMsg);
  31. var count = imMsg.length;
  32. $text_count.text(count);
  33. }
  34. var title = (action == "add") ? "新增" : "编辑";
  35. $("#title").text(title);
  36. var action_text = "";
  37. if(reference != "mine"){
  38. var text = "是否将变更保存到原模板";
  39. if(is_sys || action == "add"){
  40. text = "是否保存为您的模板";
  41. }
  42. $("#save_as_self").find(".c-list-key").text(text);
  43. $("#save_as_self").show();
  44. action_text = "发送";
  45. }else{
  46. if(action == "add"){
  47. action_text = "新增";
  48. }else{
  49. action_text = "保存";
  50. }
  51. }
  52. $action_btn.text(action_text);
  53. getDetail();
  54. bindEvents();
  55. });
  56. function getDetail(){
  57. if(content_details){
  58. $("#temp_name").val(content_details.modelName);
  59. $("textarea").val(content_details.content);
  60. $("#text_count").text(content_details.content.length);
  61. imgUrls = content_details.imagesUrls;
  62. if(imgUrls){ //填充模板中图片
  63. for(i=0; i<imgUrls.length; i++){
  64. fillImage(getImgUrl(imgUrls[i]));
  65. }
  66. }else{
  67. imgUrls = [];
  68. }
  69. }
  70. }
  71. function getPhoto(url){
  72. uploadImages.push(url);
  73. fillImage(url);
  74. }
  75. function fillImage(url){
  76. img_count ++;
  77. $img_count.text(img_count);
  78. var html = '<div class="c-position-r fl mr15 mt20"><img class="upload_img" src="'+ url+'" width="65" height="65">'+
  79. '<div class="delete-icon"><img src="../images/delete_icon.png" width="18"></div></div>';
  80. $("#img_wrap").append(html);
  81. }
  82. //上传图片
  83. var upload_count = 0;
  84. function uploadImg(cb){
  85. var len = uploadImages.length;
  86. if(upload_count < len){
  87. var task = plus.uploader.createUpload(server + "/upload/fastDFSImag", {
  88. method: "post"
  89. }, function(t, sta) {
  90. if(sta == 200) {
  91. var msg = t.responseText;
  92. var oImg = JSON.parse(msg);
  93. // var imgUrl = oImg.urls;
  94. // var re = new RegExp("\\\\", "g");
  95. // imgUrl = imgUrl.replace(re, "/");
  96. // imgUrls.push(imgUrl);
  97. // uploadImg(cb);
  98. if(oImg.status == 200){
  99. imgUrls.push(oImg.data);
  100. upload_count ++;
  101. uploadImg(cb);
  102. }else{
  103. mui.toast("上传图片失败!");
  104. plus.nativeUI.closeWaiting();
  105. }
  106. } else {
  107. mui.toast("上传图片失败!");
  108. plus.nativeUI.closeWaiting();
  109. }
  110. });
  111. var url = uploadImages[upload_count];
  112. task.addFile(url, {key: "file"});
  113. task.start();
  114. }else{
  115. cb();
  116. }
  117. }
  118. function bindEvents(){
  119. $("#add").on('click', function(){
  120. if(img_count < 9){
  121. // alert(img_count);
  122. var num = 9 - (img_count || 0);
  123. getAutoRecCompressImageLocalPath( getPhoto, num);
  124. }else{
  125. dialog({
  126. contentType: 'tipsbox',
  127. skin: 'bk-popup',
  128. content: '最多只能上传9张图片',
  129. closeTime: 2000
  130. }).showModal();
  131. }
  132. });
  133. //图片删除功能
  134. $(document).on('tap', '.delete-icon', function(){
  135. var $this = $(this);
  136. //获得图片路径
  137. var url = $this.parent().find(".upload_img").attr("src");
  138. for(var i=0; i<imgUrls.length; i++) {
  139. // if(imgUrls[i] == url) {
  140. if(url.indexOf(imgUrls[i])> -1){
  141. imgUrls.splice(i, 1);
  142. break;
  143. }
  144. }
  145. for(var j=0; j<uploadImages.length; j++){
  146. if(uploadImages[j] == url){
  147. uploadImages.splice(j, 1);
  148. break;
  149. }
  150. }
  151. img_count --;
  152. $img_count.text(img_count);
  153. $this.parent().remove();
  154. });
  155. //文本框
  156. $("textarea").on('keyup', function(){
  157. var count = $(this).val().length;
  158. $text_count.text(count);
  159. });
  160. //预览功能
  161. $view_btn.on('tap', function(){
  162. var content = $("textarea").val(),
  163. $images_list = $(".upload_img"),
  164. images = [];
  165. for(i=0; i<img_count; i++){
  166. var item = $images_list[i];
  167. images.push($(item).attr("src"));
  168. }
  169. if(content.length == 0){
  170. dialog({
  171. contentType: 'tipsbox',
  172. skin: 'bk-popup',
  173. content: '请填先写模板内容!',
  174. closeTime: 2000
  175. }).showModal();
  176. return false;
  177. }
  178. openWebview("preview_detail.html",{
  179. code: code,
  180. detail:{content: content, images: images, imgUrls: imgUrls, uploadImages: uploadImages, title: $("#temp_name").val()},
  181. reference: reference,
  182. patiCode: patiCode,
  183. is_sys: is_sys,
  184. checked: $(".mui-switch").hasClass("mui-active")
  185. });
  186. });
  187. //action 按钮
  188. $action_btn.on('tap', function(){
  189. var content = $.trim($("textarea").val());
  190. if(content.length == 0){
  191. dialog({
  192. contentType: 'tipsbox',
  193. skin: 'bk-popup',
  194. content: '指导内容不能为空!',
  195. closeTime: 2000
  196. }).showModal();
  197. return false;
  198. }
  199. var title = $.trim($("#temp_name").val());
  200. if(title.length == 0){
  201. dialog({
  202. contentType: 'tipsbox',
  203. skin: 'bk-popup',
  204. content: '模板名称不能为空!',
  205. closeTime: 2000
  206. }).showModal();
  207. return false;
  208. }
  209. //IM入口
  210. var url = "";
  211. var params = {
  212. content: $("textarea").val(),
  213. modelName: $("#temp_name").val()
  214. };
  215. if(reference != "mine"){
  216. dialog({
  217. content: "发出后无法变更,是否确认发送给居民?",
  218. okValue: "继续发送",
  219. ok: function(){
  220. plus.nativeUI.showWaiting();
  221. //先将新增的图片上传然后再处理其他业务
  222. uploadImg(function(){
  223. params.imagesUrl = imgUrls?imgUrls.join(","):"";
  224. var checked = $(".mui-switch").hasClass("mui-active");
  225. var reqInfo = [];
  226. if(checked) {
  227. if(is_sys || (action == "add")){
  228. //先将内容保存为自己模板
  229. url = "/doctor/guidance_temp/add";
  230. }else{
  231. url = "/doctor/guidance_temp/modify";
  232. params.code = code;
  233. }
  234. reqInfo.push({url: url, data: params, reqType: 'POST'});
  235. }
  236. reqInfo.push({
  237. url: "/doctor/health/guidance/add",
  238. data: {
  239. patient: patiCode,
  240. content: $("textarea").val(),
  241. modelCode: code || "",
  242. tzCode:signCode||"",
  243. images: imgUrls?imgUrls.join(","):""
  244. }
  245. });
  246. if(reqInfo.length == 1) {
  247. return getReqPromises(reqInfo, true).then(function(res) {
  248. if(res[0].status == 200){
  249. console.log(reqInfo);
  250. mui.toast("发送成功");
  251. otherBackAction();
  252. } else {
  253. mui.toast("发送失败请重试 !");
  254. }
  255. plus.nativeUI.closeWaiting();
  256. })
  257. }else{
  258. return getReqPromise(reqInfo[0].url, reqInfo[0].data, "POST", true).then(function(res) {
  259. if(res.status == 200) {
  260. reqInfo[1].data.modelCode = res.modelCode;
  261. getReqPromise(reqInfo[1].url, reqInfo[1].data, "POST", true).then(function(res1) {
  262. if(res1.status == 200) {
  263. mui.toast("发送成功!");
  264. otherBackAction();
  265. } else {
  266. mui.toast( "保存成功,发送失败!")
  267. }
  268. })
  269. } else {
  270. mui.toast(res.msg || "保存失败无法发送!")
  271. }
  272. plus.nativeUI.closeWaiting();
  273. })
  274. }
  275. });
  276. },
  277. cancelValue : "不了,谢谢",
  278. cancel: function(){}
  279. }).showModal();
  280. }else{ //“我的”入口
  281. //先将新增的图片上传然后再处理其他业务
  282. plus.nativeUI.showWaiting();
  283. uploadImg(function(){
  284. params.imagesUrl = imgUrls ? imgUrls.join(","): "";
  285. if((action == "add") || is_sys){
  286. //新增模板或将系统模板保存为自己的
  287. url = "/doctor/guidance_temp/add";
  288. }else{
  289. //编辑
  290. url = "/doctor/guidance_temp/modify";
  291. params.code = code;
  292. }
  293. sendPost(url, params, null, function(res){
  294. if(res.status == 200){
  295. mineBackAction();
  296. }
  297. mui.toast(res.msg);
  298. plus.nativeUI.closeWaiting();
  299. }, 'POST', '', true);
  300. });
  301. }
  302. });
  303. }
  304. function otherBackAction(){
  305. var self = plus.webview.currentWebview(),
  306. opener = self.opener(),
  307. grant_opener = opener.opener();
  308. if(opener.id == "jkzd" || opener.id == "guidance_list"){ //新增
  309. if(grant_opener.id == "guidance_info"){
  310. //im->info->list->add
  311. var pre_opener = grant_opener.opener();
  312. mui.fire(pre_opener, "update");
  313. pre_opener.show();
  314. mui.later(function(){
  315. grant_opener.close();
  316. opener.close();
  317. self.close();
  318. }, 300);
  319. }else{
  320. //IM/huanzhexinxi->新增页面
  321. mui.fire(grant_opener, "update");
  322. grant_opener.show();
  323. mui.later(function(){
  324. opener.close();
  325. self.close();
  326. }, 300);
  327. }
  328. }else if(grant_opener.id == "jkzd" || grant_opener.id == "guidance_list"){ //详情 -> 编辑
  329. var pre_opener = grant_opener.opener();
  330. if(pre_opener.id == "guidance_info"){
  331. //im/huanzhexinxi -> info->list->detail->edit
  332. mui.fire(pre_opener.opener(), "update");
  333. pre_opener.opener().show();
  334. mui.later(function(){
  335. opener.close();
  336. grant_opener.close();
  337. pre_opener.close();
  338. self.close();
  339. }, 300);
  340. }else{
  341. //IM/huanzhexinxi->list->detail->edit
  342. mui.fire(pre_opener, "update");
  343. pre_opener.show();
  344. mui.later(function(){
  345. opener.close();
  346. grant_opener.close();
  347. self.close();
  348. }, 300);
  349. }
  350. }else{ //sys->detail->edit
  351. var pre_opener = grant_opener.opener(),
  352. pre_pre_opener = pre_opener.opener();
  353. if(pre_pre_opener.id == "guidance_info"){
  354. //IM/huanzhexinxi->info->list->sys->detail->edit
  355. mui.fire(pre_pre_opener.opener(), "update");
  356. pre_pre_opener.opener().show();
  357. mui.later(function(){
  358. pre_pre_opener.close();
  359. pre_opener.close();
  360. grant_opener.close();
  361. opener.close();
  362. self.close();
  363. }, 300);
  364. }else{
  365. //IM/huanzhexinxi->list->sys->detail->edit
  366. // var entrance = grant_opener.opener().opener();
  367. mui.fire(pre_pre_opener, "update");
  368. pre_pre_opener.show();
  369. mui.later(function(){
  370. pre_opener.close();
  371. grant_opener.close();
  372. opener.close();
  373. self.close();
  374. }, 300);
  375. }
  376. }
  377. }
  378. function mineBackAction(){
  379. var self = plus.webview.currentWebview(),
  380. opener = self.opener(),
  381. pre_opener = opener.opener();
  382. if(opener.id == "guidance_list" || opener.id == "jkzd"){ //mine->add
  383. // mui.fire(opener, 'refresh');
  384. opener.reload(true);
  385. self.close();
  386. }else if(pre_opener.id == "guidance_list" || pre_opener.id == "jkzd"){ //mine->detail->edit
  387. // mui.fire(pre_opener, "refresh");
  388. pre_opener.reload(true);
  389. mui.later(function(){
  390. opener.close();
  391. self.close();
  392. }, 300);
  393. pre_opener.show();
  394. } else if (opener.id == 'p2dzixun' || opener.id == 'p2p') {
  395. mui.fire(pre_opener, "refresh");
  396. mui.later(function(){
  397. self.close();
  398. }, 300);
  399. opener.show();
  400. }else{ //mine->sys->detail->edit
  401. var pre_pre_opener = pre_opener.opener();
  402. // mui.fire(pre_pre_opener, "refresh");
  403. pre_pre_opener.reload(true);
  404. mui.later(function(){
  405. pre_opener.close();
  406. opener.close();
  407. self.close();
  408. }, 300);
  409. pre_pre_opener.show();
  410. }
  411. }