article-edit.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. mui.init();
  2. var articleId,
  3. articleObj;
  4. var coverImg,
  5. uploadCovImg,
  6. contentImgs = [],
  7. uploadImgs = [], //存储需要上传的图片
  8. imgCount,
  9. selectedScore;
  10. var docInfo;
  11. var picker = new mui.PopPicker();
  12. mui.plusReady(function(){
  13. var self = plus.webview.currentWebview();
  14. docInfo = JSON.parse(plus.storage.getItem("docInfo"));
  15. articleId = self.articleId;
  16. messageCode = self.messageCode||"";
  17. getArticleDetail();
  18. templateHelper();
  19. });
  20. function getArticleDetail(){
  21. var url = "/third/jkEdu/Article/getArticalById",
  22. params = {
  23. articleId: articleId,
  24. messageCode:messageCode,
  25. userType: 1
  26. };
  27. sendGet(url, params, null, function(res){
  28. if(res.status == 200){
  29. var data = res.data;
  30. articleObj = data;
  31. getSecondCategory(data.firstLevelCategoryId, data.secondLevelCategoryId);
  32. $("#contentPanel").html(data.articleContent);
  33. var $imgs = $("#contentPanel img"),
  34. len = $imgs.length,
  35. text = data.articleContent&&data.articleContent.split('<img')&&data.articleContent.split('<img')[0],
  36. imgList = [];
  37. for(i=0; i<len; i++){
  38. var url = $($imgs[i]).attr("src");
  39. contentImgs.push(url)
  40. imgList.push(getImgUrl(url));
  41. }
  42. coverImg = data.articleCover;
  43. data.articleCover = getImgUrl(coverImg)
  44. data.content = text;
  45. data.imgs = imgList;
  46. data.imgCount = len;
  47. var html = template("info-panel", data);
  48. $("#infoPanel").empty().append(html);
  49. bindEvents();
  50. }else{
  51. mui.toast(res.msg);
  52. }
  53. }, true);
  54. }
  55. function getSecondCategory(firstlevelId, selectedId){
  56. var url = "/third/jkEdu/Article/getCategoryList",
  57. params = {
  58. categoryLevel: 2,
  59. firstlevelId: firstlevelId
  60. };
  61. sendGet(url, params, null, function(res){
  62. if(res.status == 200){
  63. var list = _.map(res.data, function(o){
  64. var b = {};
  65. b.value = o.categoryid;
  66. b.text = o.categoryname;
  67. return b;
  68. });
  69. picker.setData(list);
  70. if(selectedId){
  71. picker.pickers[0].setSelectedValue(selectedId, 500);
  72. }
  73. }else{
  74. mui.toast(res.msg);
  75. }
  76. }, true);
  77. }
  78. //上传封面图片,上传
  79. function uploadCoverImg(cb){
  80. var task = plus.uploader.createUpload(server+ "/upload/image",{
  81. method: "post"
  82. }, function(t, sta){
  83. if(sta == 200){
  84. var msg = t.responseText;
  85. var oImg = JSON.parse(msg);
  86. if(oImg.status == 200){
  87. var imgUrl = oImg.urls;
  88. var re = new RegExp("\\\\", "g");
  89. imgUrl = imgUrl.replace(re, "/");
  90. cb(imgUrl);
  91. }else{
  92. mui.toast("上传图片失败!");
  93. plus.nativeUI.closeWaiting();
  94. }
  95. }else{
  96. mui.toast("上传图片失败!");
  97. plus.nativeUI.closeWaiting();
  98. }
  99. });
  100. task.addFile(coverImg, {});
  101. task.start();
  102. }
  103. //上传内容图片
  104. var upload_count = 0;
  105. function uploadImg(cb){
  106. var len = uploadImgs.length;
  107. if(upload_count < len){
  108. var task = plus.uploader.createUpload(server + "/upload/fastDFSImag", {
  109. method: "post"
  110. }, function(t, sta) {
  111. if(sta == 200) {
  112. var msg = t.responseText;
  113. var oImg = JSON.parse(msg);
  114. if(oImg.status == 200){
  115. contentImgs.push(oImg.data);
  116. upload_count ++;
  117. // uploadImgs.pop();
  118. uploadImg(cb);
  119. }else{
  120. mui.toast("上传图片失败!");
  121. plus.nativeUI.closeWaiting();
  122. return false;
  123. }
  124. } else {
  125. mui.toast("上传图片失败!");
  126. plus.nativeUI.closeWaiting();
  127. return false;
  128. }
  129. });
  130. var url = uploadImgs[upload_count];
  131. task.addFile(url, {key: "file"});
  132. task.start();
  133. }else{
  134. cb();
  135. }
  136. }
  137. function saveArticle(url, params){
  138. var str = "";
  139. for(i=0; i<contentImgs.length; i++){
  140. str += '<img style="display: block; max-width: 100%;" src="' + contentImgs[i] + '"/ >';
  141. }
  142. params.content += str;
  143. sendPost(url, params, null, function(res){
  144. if(res.status == 200){
  145. mui.toast("保存成功");
  146. //返回列表页
  147. var view = plus.webview.getWebviewById("myArticle");
  148. var current = plus.webview.getWebviewById("articleDetail");
  149. var shenhexiaoxi = plus.webview.getWebviewById("shenhexiaoxi");
  150. if(view){
  151. mui.fire(view,"reloadPage");
  152. }
  153. if(current){
  154. mui.fire(current,"reload");
  155. }
  156. if(shenhexiaoxi){
  157. mui.fire(shenhexiaoxi,"reloadPage");
  158. }
  159. mui.later(function(){
  160. mui.back();
  161. }, 300);
  162. mui.back();
  163. }else{
  164. mui.toast(res.msg);
  165. }
  166. plus.nativeUI.closeWaiting();
  167. }, 'POST', '', true);
  168. }
  169. function bindEvents(){
  170. $("#infoPanel").on('click', "#category", function(){
  171. var $this = $(this);
  172. picker.show(function (selectedItems) {
  173. $this.val(selectedItems[0].text);
  174. $this.attr("data-id", selectedItems[0].value)
  175. });
  176. });
  177. //监听选中框修改
  178. $("#infoPanel").on('click', "[name=userange]", function(){
  179. selectedScore = $(this).val();
  180. })
  181. //添加封面
  182. $("#infoPanel").on('click', "#addCover", function(){
  183. var $this = $(this);
  184. if(coverImg != '' && coverImg) {
  185. mui.toast("封面仅限一张!");
  186. return false;
  187. }
  188. getAutoRecCompressImageLocalPath(function(url){
  189. coverImg = url;
  190. uploadCovImg = coverImg
  191. var html = '<div class="ui-col-0 mr10 ptb10 img-div2" id="imgCover"><img src="'+url+'" class="image"><img class="delete-icon t0" src="../images/delete_icon.png"></div>';
  192. $this.parent().before(html);
  193. }, 1, 1);
  194. });
  195. //封面删除功能
  196. $('#infoPanel').on('click', '#imgCover .delete-icon', function(e){
  197. window.event? window.event.cancelBubble = true : e.stopPropagation();
  198. var $this = $(this);
  199. //获得图片路径
  200. coverImg = '';
  201. uploadCovImg = '';
  202. $this.parent().remove();
  203. });
  204. //添加文章内容中的图片
  205. $("#infoPanel").on('click', "#addContentImg", function(){
  206. var $this = $(this);
  207. imgCount = $(".img-count").text();
  208. var num = 9 - parseInt(imgCount);
  209. if(num == 0){
  210. mui.toast("最多只能选择9张图片!")
  211. return false;
  212. }
  213. getAutoRecCompressImageLocalPath(function(url){
  214. imgCount++;
  215. $(".img-count").text(imgCount);
  216. uploadImgs.push(url);
  217. // plus.nativeUI.showWaiting();
  218. // uploadImg(function() {
  219. // mui.toast("内容图上传成功!");
  220. // plus.nativeUI.closeWaiting();
  221. // })
  222. var html = template("img-tmp", {url: url});
  223. $this.parent().before(html);
  224. }, num, 9);
  225. });
  226. //内容图删除功能
  227. $('#infoPanel').on('click', '#imgContent .delete-icon', function(){
  228. var $this = $(this);
  229. //获得图片路径
  230. var urls = $this.parent().find(".image").attr("src");
  231. $this.parent().remove();
  232. var imgCount = parseInt($(".img-count").text());
  233. imgCount--;
  234. $(".img-count").text(imgCount);
  235. for(var i = 0, len = contentImgs.length; i < len; i++) {
  236. if(urls.indexOf(contentImgs[i]) > -1) {
  237. contentImgs.splice(i, 1);
  238. break;
  239. }
  240. }
  241. for(var i = 0, len = uploadImgs.length; i < len; i++) {
  242. if(uploadImgs[i].indexOf(urls) > -1) {
  243. uploadImgs.splice(i, 1);
  244. break;
  245. }
  246. }
  247. });
  248. //文章删除功能
  249. $("#deleteArticle").click(function(){
  250. dialog({
  251. content: "确定要删除此篇文章吗?",
  252. okValue: '确定',
  253. ok: function(){
  254. var url = "/third/jkEdu/Article/removeArticle",
  255. params = {
  256. ids: articleId
  257. };
  258. plus.nativeUI.showWaiting();
  259. sendPost(url, params, null, function(res){
  260. plus.nativeUI.closeWaiting();
  261. if(res.status == 200){
  262. mui.toast("删除文章成功!");
  263. setTimeout(function() {
  264. var view = plus.webview.getWebviewById("myArticle");
  265. var current = plus.webview.getWebviewById("articleDetail");
  266. if(view){
  267. mui.fire(view,"reloadPage");
  268. }
  269. if(current){
  270. current.close();
  271. mui.fire(view,"reloadPage");
  272. }
  273. mui.later(function(){
  274. mui.back();
  275. }, 300);
  276. mui.back();
  277. }, 1000)
  278. }else{
  279. mui.toast(res.msg);
  280. }
  281. }, 'POST', '', true);
  282. },
  283. cancelValue: '我再想想',
  284. cancel: function(){}
  285. }).show()
  286. });
  287. //提交审核按钮
  288. $("#saveArticle").on('click', function(){
  289. var reqUrl = "/doctor/jkEdu/article/PC/saveArticle",
  290. params = {
  291. articleId: articleId,
  292. articleTitle: $("#title").val() || '',
  293. articlelevel: articleObj.articleType,
  294. secondLevelCategoryId: $("#category").attr("data-id"),
  295. secondLevelCategoryName: $("#category").val(),
  296. firstLevelCategoryId: articleObj.firstLevelCategoryId,
  297. firstLevelCategoryName: articleObj.firstLevelCategoryName,
  298. content: $("#content").html(),
  299. imageUrl: coverImg,
  300. operatorRoleCode: docInfo.code,
  301. opertorRoleLevel: docInfo.level,
  302. userScope: selectedScore || articleObj.userScope,
  303. roleType: 1, //1、普通医生,2、管理员
  304. currentUserRole: docInfo.hospital,
  305. currentUserRoleLevel: 4
  306. };
  307. // 封面图更改
  308. if(uploadCovImg){
  309. plus.nativeUI.showWaiting();
  310. uploadCoverImg(function(url){
  311. plus.nativeUI.closeWaiting();
  312. params.imageUrl = url;
  313. if(uploadImgs.length > 0){
  314. uploadImg(function() {
  315. saveArticle(reqUrl, params);
  316. });
  317. }else{
  318. saveArticle(reqUrl, params);
  319. }
  320. });
  321. } else if(uploadImgs.length > 0){
  322. uploadImg(function() {
  323. saveArticle(reqUrl, params);
  324. });
  325. }else{
  326. saveArticle(reqUrl, params);
  327. }
  328. });
  329. // 预览功能
  330. $('#preview').on('tap',function(){
  331. var articleTitle = $.trim($.trim($("#title").val())),
  332. secondLevelCategoryId = $("#category").attr("data-id"),
  333. secondLevelCategoryName = $.trim($("#category").val()),
  334. content = $.trim($('#content').html()),
  335. userScope = selectedScore || articleObj.userScope;
  336. if(articleTitle){
  337. if(secondLevelCategoryName){
  338. if(content){
  339. if(userScope){
  340. mui.openWindow({
  341. id: "preview",
  342. url: "preview.html",
  343. extras: {
  344. articleTitle: articleTitle,
  345. secondLevelCategoryName: secondLevelCategoryName,
  346. imgCover: coverImg,//封面图
  347. content: content,//内容
  348. imgContent: uploadImgs,//未上传内容图
  349. uploadImgs: contentImgs,// 已上传内容图
  350. userScope: userScope//使用范围
  351. }
  352. })
  353. }else{
  354. mui.toast('使用范围不能为空');
  355. return false;
  356. }
  357. }else{
  358. mui.toast('内容不能为空');
  359. return false;
  360. }
  361. }else{
  362. mui.toast('分类不能为空');
  363. return false;
  364. }
  365. }else{
  366. mui.toast('文章标题不能为空');
  367. return false;
  368. }
  369. })
  370. }
  371. function templateHelper(){
  372. template.helper("getImgUrl", function(str){
  373. if(str){
  374. return getImgUrl(str);
  375. }else{
  376. return "";
  377. }
  378. });
  379. }