bianjituanduimuban.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. var action,
  2. code ,
  3. reference, //标记入口
  4. patiCode,
  5. teamList,
  6. teamId,
  7. docInfo,
  8. tName,
  9. tId,
  10. is_sys = false,
  11. content_details,
  12. uploadImages = [],
  13. imMsg = '',
  14. imgUrls = [],
  15. teams= '';
  16. var $text_count = $("#text_count"),
  17. text_count = 0,
  18. $img_count = $("#img_count"),
  19. img_count = 0,
  20. $view_btn = $("#view_btn"),
  21. $teamDown = $('.team-down'),
  22. $comtop = $('.demo-comtop'),
  23. $linMask = $('.lin-mask'),
  24. $linSelGroup = $('.lin-sel-group'),
  25. $action_btn = $("#action_btn")
  26. $inTeams = $("#inTeams"),
  27. $teamNameCon = $('.team-name-con');
  28. mui.init();
  29. mui.plusReady(function(){
  30. docInfo = JSON.parse(plus.storage.getItem("docInfo"));
  31. var self = plus.webview.currentWebview();
  32. action = self.action;
  33. reference = self.reference;
  34. imMsg = self.msg;
  35. code = self.code;
  36. teamList = self.teamList;
  37. patiCode = self.patiCode;
  38. teamId = self.teamId;
  39. tName = self.tName;
  40. tId = self.tId;
  41. content_details = self.content_details;
  42. if (imMsg) {
  43. $("textarea").val(imMsg);
  44. }
  45. var title = (action == "add") ? "新增" : "编辑";
  46. $("#title").text(title);
  47. var action_text = "";
  48. if(reference != "mine"){
  49. getTeams();
  50. var text = "是否保存为您的模板";
  51. $("#save_as_self").find(".c-list-key").text(text);
  52. $("#save_as_self").show();
  53. action_text = "发送";
  54. }else{
  55. getTeams();
  56. if(action == "add"){
  57. plus.nativeUI.showWaiting();
  58. action_text = "新增";
  59. }else{
  60. action_text = "保存";
  61. }
  62. }
  63. $action_btn.text(action_text);
  64. getDetail();
  65. bindEvents();
  66. });
  67. //获取团队
  68. function getTeams () {
  69. sendGet( 'doctor/team/guidance/getDoctorTeams', {}, null, function(res){
  70. if(res.status == 200){
  71. var d = [],
  72. html = '';
  73. if (teamList) {
  74. d = teamList;
  75. teams = JSON.stringify(d);
  76. } else {
  77. // d = res.teamList;
  78. // teams = JSON.stringify(res.teamList);
  79. teams = JSON.stringify([{
  80. teamName: tName,
  81. teamId: tId
  82. }]);
  83. }
  84. for (var i = 0, len = d.length; i < len; i++) {
  85. html += d[i].teamName + ',';
  86. }
  87. if (action == 'add') {
  88. html = tName;
  89. } else {
  90. html = html.substring( 0, html.length - 1);
  91. }
  92. $teamNameCon.html(html);
  93. template.helper( 'checkData', function (v) {
  94. if (teamList) {
  95. for (var i = 0,len = teamList.length; i < len; i++) {
  96. if (v == teamList[i].teamId) {
  97. return 'selected';
  98. }
  99. }
  100. } else {
  101. if (v == tId) {
  102. return 'selected';
  103. }
  104. // return 'selected';
  105. }
  106. });
  107. $('#demo').html(template( 'seleceTmp', {data: res.teamList}));
  108. $('#demo').mobiscroll().select({
  109. theme: 'ios',
  110. display: 'bottom',
  111. lang: 'zh',
  112. onSelect: function ( valueText, inst) {
  113. var vArr = valueText.split(','),
  114. idArr = inst._tempValue,
  115. t = [];
  116. for (var i = 0; i < idArr.length; i++) {
  117. t.push({
  118. teamName: vArr[i].trim(),
  119. teamId: parseInt(idArr[i])
  120. })
  121. }
  122. teams = JSON.stringify(t);
  123. $teamNameCon.html(inst._value);
  124. }
  125. });
  126. } else {
  127. mui.toast(res.msg);
  128. }
  129. plus.nativeUI.closeWaiting();
  130. }, 'GET', '', true);
  131. }
  132. function getDetail(){
  133. if(content_details){
  134. $("#temp_name").val(content_details.title);
  135. $("textarea").val(content_details.content);
  136. $("#text_count").text(content_details.content.length);
  137. imgUrls = content_details.imagesUrl;
  138. if(imgUrls){ //填充模板中图片
  139. for(i=0; i<imgUrls.length; i++){
  140. fillImage(getImgUrl(imgUrls[i]));
  141. }
  142. }else{
  143. imgUrls = [];
  144. }
  145. }
  146. }
  147. function getPhoto(url){
  148. uploadImages.push(url);
  149. fillImage(url);
  150. }
  151. function fillImage(url){
  152. img_count ++;
  153. $img_count.text(img_count);
  154. var html = '<div class="c-position-r fl mr15 mt20"><img class="upload_img" src="'+url+'" width="65" height="65">'+
  155. '<div class="delete-icon"><img src="../images/delete_icon.png" width="18"></div></div>';
  156. $("#img_wrap").append(html);
  157. }
  158. //上传图片
  159. var upload_count = 0;
  160. function uploadImg(cb){
  161. var len = uploadImages.length;
  162. if(upload_count < len){
  163. var task = plus.uploader.createUpload(server + "/upload/fastDFSImag", {
  164. method: "post"
  165. }, function(t, sta) {
  166. if(sta == 200) {
  167. var msg = t.responseText;
  168. var oImg = JSON.parse(msg);
  169. // var imgUrl = oImg.urls;
  170. // var re = new RegExp("\\\\", "g");
  171. // imgUrl = imgUrl.replace(re, "/");
  172. // imgUrls.push(imgUrl);
  173. // uploadImg(cb);
  174. if(oImg.status == 200){
  175. imgUrls.push(oImg.data);
  176. upload_count ++;
  177. uploadImg(cb);
  178. }else{
  179. mui.toast("上传图片失败!");
  180. plus.nativeUI.closeWaiting();
  181. }
  182. // setTimeout(function () {
  183. // uploadImg(cb);
  184. // },300);
  185. } else {
  186. mui.toast("上传图片失败!");
  187. plus.nativeUI.closeWaiting();
  188. }
  189. });
  190. var url = uploadImages[upload_count];
  191. // upload_count ++;
  192. task.addFile(url, {key: "file"});
  193. task.start();
  194. }else{
  195. cb();
  196. }
  197. }
  198. function showGroupSel (e, isShow) {
  199. isShow = isShow || $('.lin-mask:hidden').length != 0;
  200. $linMask.toggle(isShow);
  201. $linSelGroup.toggle(isShow);
  202. }
  203. function bindEvents(){
  204. //团队选择
  205. $inTeams.on('click', function (){
  206. $('#demo').trigger('click');
  207. });
  208. $linMask.on( 'tap', function (e) {
  209. showGroupSel(e);
  210. var lis = $linSelGroup.find('[class=checked]'),
  211. ts = [],
  212. html = "";
  213. $.each( lis, function () {
  214. ts.push({
  215. teamName: $(this).attr('data-name'),
  216. teamId: $(this).attr('data-id')
  217. });
  218. html += '<span>' + $(this).attr('data-name') + '</span>';
  219. });
  220. $teamNameCon.html(html);
  221. teams = JSON.stringify(ts);
  222. });
  223. $linSelGroup.on('tap', 'li', function(){
  224. if ($(this).hasClass('checked')) {
  225. $(this).removeClass('checked');
  226. } else {
  227. $(this).addClass('checked');
  228. }
  229. });
  230. $("#add").on('click', function(){
  231. if(img_count < 9){
  232. var num = 9 - img_count;
  233. getAutoRecCompressImageLocalPath( getPhoto, num);
  234. }else{
  235. dialog({
  236. contentType: 'tipsbox',
  237. skin: 'bk-popup',
  238. content: '最多只能上传9张图片',
  239. closeTime: 2000
  240. }).showModal();
  241. }
  242. });
  243. //图片删除功能
  244. $(document).on('tap', '.delete-icon', function(){
  245. var $this = $(this);
  246. //获得图片路径
  247. var url = $this.parent().find(".upload_img").attr("src");
  248. for(var i=0; i<imgUrls.length; i++) {
  249. if(url.indexOf(imgUrls[i]) > -1) {
  250. imgUrls.splice(i, 1);
  251. break;
  252. }
  253. }
  254. for(var j=0; j<uploadImages.length; j++){
  255. if(uploadImages[j] == url){
  256. uploadImages.splice(j, 1);
  257. break;
  258. }
  259. }
  260. img_count --;
  261. $img_count.text(img_count);
  262. $this.parent().remove();
  263. });
  264. //文本框
  265. $("textarea").on('keyup', function(){
  266. var count = $(this).val().length;
  267. $text_count.text(count);
  268. });
  269. $("textarea").on('change', function(){
  270. var count = $(this).val().length;
  271. $text_count.text(count);
  272. });
  273. //预览功能
  274. $view_btn.on('tap', function(){
  275. var content = $("textarea").val(),
  276. $images_list = $(".upload_img"),
  277. images = [];
  278. for(i=0; i<img_count; i++){
  279. var item = $images_list[i];
  280. images.push($(item).attr("src"));
  281. }
  282. if(content.length == 0){
  283. dialog({
  284. contentType: 'tipsbox',
  285. skin: 'bk-popup',
  286. content: '请填先写模板内容!',
  287. closeTime: 2000
  288. }).showModal();
  289. return false;
  290. }
  291. openWebview("yulantuanduimuban.html",{
  292. teams: teams,
  293. code: code,
  294. detail:{content: content, images: images, imgUrls: imgUrls, uploadImages: uploadImages, title: $("#temp_name").val()},
  295. reference: reference,
  296. patiCode: patiCode,
  297. checked: $(".mui-switch").hasClass("mui-active"),
  298. teamId: teamId
  299. });
  300. });
  301. // $(".mui-switch").on( 'tap', function () {
  302. // if ($(this).hasClass("mui-active")) {
  303. // $('#mbName').show();
  304. // } else {
  305. // $('#mbName').hide();
  306. // }
  307. // });
  308. //action 按钮
  309. $action_btn.on('tap', function(){
  310. var content = $.trim($("textarea").val()),
  311. tArr = JSON.parse(teams);
  312. var title = $.trim($("#temp_name").val());
  313. var checked = $(".mui-switch").hasClass("mui-active");
  314. if(checked && title.length == 0){
  315. dialog({
  316. contentType: 'tipsbox',
  317. skin: 'bk-popup',
  318. content: '模板名称不能为空!',
  319. closeTime: 2000
  320. }).showModal();
  321. return false;
  322. }
  323. if(content.length == 0){
  324. dialog({
  325. contentType: 'tipsbox',
  326. skin: 'bk-popup',
  327. content: '指导内容不能为空!',
  328. closeTime: 2000
  329. }).showModal();
  330. return false;
  331. }
  332. if(tArr.length == 0){
  333. dialog({
  334. contentType: 'tipsbox',
  335. skin: 'bk-popup',
  336. content: '请选择所属团队!',
  337. closeTime: 2000
  338. }).showModal();
  339. return false;
  340. }
  341. //IM入口
  342. var url = "";
  343. var params = {
  344. content: $("textarea").val(),
  345. title: $("#temp_name").val()
  346. };
  347. if(reference != "mine"){
  348. dialog({
  349. content: "发出后无法变更,是否确认发送给居民?",
  350. okValue: "继续发送",
  351. ok: function(){
  352. plus.nativeUI.showWaiting();
  353. //先将新增的图片上传然后再处理其他业务
  354. uploadImg(function(){
  355. params.images = imgUrls?imgUrls.join(","):"";
  356. params.teamInfo = teams;
  357. params.isLeader = docInfo.isLeader;
  358. if(checked){
  359. //先将内容保存为自己模板
  360. params.saveAsGuidance = 1;
  361. } else {
  362. params.saveAsGuidance = 0;
  363. }
  364. if(action == "add"){
  365. url = "doctor/team/guidance/saveTeamGuidance";
  366. }else{
  367. //编辑
  368. url = "doctor/team/guidance/modifyTeamGuidance";
  369. params.guidanceCode = code;
  370. params.isLeader = docInfo.isLeader;
  371. }
  372. var reqInfo = [];
  373. if(checked){
  374. reqInfo.push({url: url, data: params, reqType: 'POST'});
  375. }
  376. //团队模板发送时只和医生点击哪个团队中的模板有关,指导绑定的另外的团队没有关系
  377. var list = JSON.parse(teams);
  378. var chooseTeam = _.find(list, {teamId: parseInt(tId)}),
  379. arr = [];
  380. if(chooseTeam){
  381. arr.push(chooseTeam);
  382. }else{
  383. //匹配不了说明医生修改了指导所属的 团队,并且入口团队并不在选中的列表中。则默认选择选中团队中的第一个团队来发送
  384. arr.push(list[0]);
  385. }
  386. reqInfo.push({
  387. url: "doctor/team/guidance/sendTeamGuidance",
  388. data: {
  389. patient: patiCode,
  390. content: $("textarea").val(),
  391. guidanceCode: code || "",
  392. images: imgUrls?imgUrls.join(","):"",
  393. // teamId: teamId
  394. teamId: JSON.stringify(arr)
  395. }
  396. });
  397. if(reqInfo.length == 1) {
  398. return getReqPromises(reqInfo, true).then(function(res) {
  399. if(res[0].status == 200){
  400. mui.toast("发送成功");
  401. otherBackAction();
  402. } else {
  403. mui.toast("发送失败请重试 !");
  404. }
  405. plus.nativeUI.closeWaiting();
  406. })
  407. }else{
  408. return getReqPromise(reqInfo[0].url, reqInfo[0].data, "POST", true).then(function(res) {
  409. if(res.status == 200) {
  410. reqInfo[1].data.guidanceCode = res.guidanceCode;
  411. getReqPromise(reqInfo[1].url, reqInfo[1].data, "POST", true).then(function(res1) {
  412. if(res1.status == 200) {
  413. mui.toast("发送成功!");
  414. otherBackAction();
  415. } else {
  416. mui.toast( "保存成功,发送失败!")
  417. }
  418. })
  419. } else {
  420. mui.toast(res.msg || "保存失败无法发送!")
  421. }
  422. plus.nativeUI.closeWaiting();
  423. })
  424. }
  425. });
  426. },
  427. cancelValue : "不了,谢谢",
  428. cancel: function(){}
  429. }).showModal();
  430. }else{ //“我的”入口
  431. //先将新增的图片上传然后再处理其他业务
  432. plus.nativeUI.showWaiting();
  433. uploadImg(function(){
  434. params.images = imgUrls ? imgUrls.join(","): "";
  435. params.teamInfo = teams;
  436. if((action == "add") || is_sys){
  437. //新增模板或将系统模板保存为自己的
  438. url = "doctor/team/guidance/saveTeamGuidance";
  439. }else{
  440. params.saveAsGuidance = 0;
  441. params.guidanceCode = code;
  442. params.isLeader = docInfo.isLeader;
  443. //编辑
  444. url = "doctor/team/guidance/modifyTeamGuidance";
  445. }
  446. sendPost(url, params, null, function(res){
  447. if(res.status == 200){
  448. mineBackAction();
  449. }
  450. mui.toast(res.msg);
  451. plus.nativeUI.closeWaiting();
  452. }, 'POST', '', true);
  453. });
  454. }
  455. });
  456. }
  457. function otherBackAction(){
  458. var self = plus.webview.currentWebview(),
  459. opener = self.opener(),
  460. grant_opener = opener.opener();
  461. if(opener.id == "jkzd" || opener.id == "guidance_list"){ //新增
  462. if(grant_opener.id == "guidance_info"){
  463. //im->info->list->add
  464. var pre_opener = grant_opener.opener();
  465. mui.fire(pre_opener, "update");
  466. pre_opener.show();
  467. mui.later(function(){
  468. grant_opener.close();
  469. opener.close();
  470. self.close();
  471. }, 300);
  472. }else{
  473. //IM/huanzhexinxi->新增页面
  474. mui.fire(grant_opener, "update");
  475. grant_opener.show();
  476. mui.later(function(){
  477. opener.close();
  478. self.close();
  479. }, 300);
  480. }
  481. }else if(grant_opener.id == "jkzd" || grant_opener.id == "guidance_list"){ //详情 -> 编辑
  482. var pre_opener = grant_opener.opener();
  483. if(pre_opener.id == "guidance_info"){
  484. //im/huanzhexinxi -> info->list->detail->edit
  485. mui.fire(pre_opener.opener(), "update");
  486. pre_opener.opener().show();
  487. mui.later(function(){
  488. opener.close();
  489. grant_opener.close();
  490. pre_opener.close();
  491. self.close();
  492. }, 300);
  493. }else{
  494. //IM/huanzhexinxi->list->detail->edit
  495. mui.fire(pre_opener, "update");
  496. pre_opener.show();
  497. mui.later(function(){
  498. opener.close();
  499. grant_opener.close();
  500. self.close();
  501. }, 300);
  502. }
  503. }else{ //sys->detail->edit
  504. var pre_opener = grant_opener.opener(),
  505. pre_pre_opener = pre_opener.opener();
  506. if(pre_pre_opener.id == "guidance_info"){
  507. //IM/huanzhexinxi->info->list->sys->detail->edit
  508. mui.fire(pre_pre_opener.opener(), "update");
  509. pre_pre_opener.opener().show();
  510. mui.later(function(){
  511. pre_pre_opener.close();
  512. pre_opener.close();
  513. grant_opener.close();
  514. opener.close();
  515. self.close();
  516. }, 300);
  517. }else{
  518. //IM/huanzhexinxi->list->sys->detail->edit
  519. // var entrance = grant_opener.opener().opener();
  520. mui.fire(pre_pre_opener, "update");
  521. pre_pre_opener.show();
  522. mui.later(function(){
  523. pre_opener.close();
  524. grant_opener.close();
  525. opener.close();
  526. self.close();
  527. }, 300);
  528. }
  529. }
  530. }
  531. function mineBackAction(){
  532. var self = plus.webview.currentWebview(),
  533. opener = self.opener(),
  534. pre_opener = opener.opener();
  535. if(opener.id == "guidance_list"){ //mine->add
  536. mui.fire(opener, 'refresh');
  537. // opener.reload(true);
  538. self.close();
  539. mui.back();
  540. } else if(opener.id == "chakantuanduimuban"){
  541. mui.fire(pre_opener, "refresh");
  542. // pre_opener.reload(true);
  543. mui.later(function(){
  544. opener.close();
  545. self.close();
  546. }, 300);
  547. pre_opener.show();
  548. } else if (opener.id == 'p2dzixun' || opener.id == 'p2p') {
  549. mui.fire(pre_opener, "refresh");
  550. mui.later(function(){
  551. self.close();
  552. }, 300);
  553. opener.show();
  554. }else{ //mine->sys->detail->edit
  555. var pre_pre_opener = pre_opener.opener();
  556. mui.fire(pre_pre_opener, "refresh");
  557. mui.later(function(){
  558. pre_opener.close();
  559. opener.close();
  560. self.close();
  561. }, 300);
  562. pre_pre_opener.show();
  563. }
  564. }