article-edit.js 12 KB

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