edit_guidance.js 14 KB

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