PageController.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. class PageController extends BaseController {
  5. //展示某个项目的单个页面
  6. public function index(){
  7. import("Vendor.Parsedown.Parsedown");
  8. $page_id = I("page_id/d");
  9. $page = D("Page")->where(" page_id = '$page_id' ")->find();
  10. $login_user = $this->checkLogin(false);
  11. if (!$this->checkItemVisit($login_user['uid'] , $page['item_id'])) {
  12. $this->message(L('no_permissions'));
  13. return;
  14. }
  15. $Parsedown = new \Parsedown();
  16. $page['page_md_content'] = $page['page_content'];
  17. //$page['page_html_content'] = $Parsedown->text(htmlspecialchars_decode($page['page_content']));
  18. $this->assign("page" , $page);
  19. $this->display();
  20. }
  21. //返回单个页面的源markdown代码
  22. public function md(){
  23. $page_id = I("page_id/d");
  24. $page = D("Page")->where(" page_id = '$page_id' ")->find();
  25. echo $page['page_content'];
  26. }
  27. //编辑页面
  28. public function edit(){
  29. $login_user = $this->checkLogin();
  30. $page_id = I("page_id/d");
  31. $item_id = I("item_id/d");
  32. $page_history_id = I("page_history_id/d");
  33. $copy_page_id = I("copy_page_id/d");
  34. if ($page_id > 0 ) {
  35. if ($page_history_id) {
  36. $page = D("PageHistory")->where(" page_history_id = '$page_history_id' ")->find();
  37. $page_content = gzuncompress(base64_decode($page['page_content']));
  38. $page['page_content'] = $page_content ? $page_content : $page['page_content'] ;
  39. }else{
  40. $page = D("Page")->where(" page_id = '$page_id' ")->find();
  41. }
  42. $default_cat_id = $page['cat_id'];
  43. }
  44. //如果是复制接口
  45. elseif ($copy_page_id) {
  46. $copy_page = D("Page")->where(" page_id = '$copy_page_id' ")->find();
  47. $page['page_title'] = $copy_page['page_title']."-copy";
  48. $page['page_content'] = $copy_page['page_content'];
  49. $page['item_id'] = $copy_page['item_id'];
  50. $default_cat_id = $copy_page['cat_id'];
  51. }else{
  52. //查找用户上一次设置的目录
  53. $last_page = D("Page")->where(" author_uid ='$login_user[uid]' and $item_id = '$item_id' ")->order(" addtime desc ")->limit(1)->find();
  54. $default_cat_id = $last_page['cat_id'];
  55. }
  56. $item_id = $page['item_id'] ?$page['item_id'] :$item_id;
  57. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  58. $this->message(L('no_permissions'));
  59. return;
  60. }
  61. $Catalog = D("Catalog")->where(" cat_id = '$default_cat_id' ")->find();
  62. if ($Catalog['parent_cat_id']) {
  63. $default_second_cat_id = $Catalog['parent_cat_id'];
  64. $default_child_cat_id = $default_cat_id;
  65. }else{
  66. $default_second_cat_id = $default_cat_id;
  67. }
  68. $this->assign("api_doc_templ" , 'MdTemplate/api-doc.'.LANG_SET);
  69. $this->assign("database_doc_templ" , 'MdTemplate/database.'.LANG_SET);
  70. $this->assign("page" , $page);
  71. $this->assign("item_id" , $item_id);
  72. $this->assign("default_second_cat_id" , $default_second_cat_id);
  73. $this->assign("default_child_cat_id" , $default_child_cat_id);
  74. $this->display();
  75. }
  76. //保存
  77. public function save(){
  78. $login_user = $this->checkLogin();
  79. $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
  80. $page_title = I("page_title") ?I("page_title") : L("default_title");
  81. $page_comments = I("page_comments") ?I("page_comments") :'';
  82. $page_content = I("page_content");
  83. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  84. $item_id = I("item_id/d")? I("item_id/d") : 0;
  85. $s_number = I("s_number/d")? I("s_number/d") : 99;
  86. $login_user = $this->checkLogin();
  87. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  88. $this->message(L('no_permissions'));
  89. return;
  90. }
  91. $data['page_title'] = $page_title ;
  92. $data['page_content'] = $page_content ;
  93. $data['page_comments'] = $page_comments ;
  94. $data['s_number'] = $s_number ;
  95. $data['item_id'] = $item_id ;
  96. $data['cat_id'] = $cat_id ;
  97. $data['addtime'] = time();
  98. $data['author_uid'] = $login_user['uid'] ;
  99. $data['author_username'] = $login_user['username'];
  100. if ($page_id > 0 ) {
  101. //在保存前先把当前页面的版本存档
  102. $page = D("Page")->where(" page_id = '$page_id' ")->find();
  103. $insert_history = array(
  104. 'page_id'=>$page['page_id'],
  105. 'item_id'=>$page['item_id'],
  106. 'cat_id'=>$page['cat_id'],
  107. 'page_title'=>$page['page_title'],
  108. 'page_comments'=>$page['page_comments'],
  109. 'page_content'=>base64_encode( gzcompress($page['page_content'], 9)),
  110. 's_number'=>$page['s_number'],
  111. 'addtime'=>$page['addtime'],
  112. 'author_uid'=>$page['author_uid'],
  113. 'author_username'=>$page['author_username'],
  114. );
  115. D("PageHistory")->add($insert_history);
  116. $ret = D("Page")->where(" page_id = '$page_id' ")->save($data);
  117. //统计该page_id有多少历史版本了
  118. $Count = D("PageHistory")->where(" page_id = '$page_id' ")->Count();
  119. if ($Count > 20 ) {
  120. //每个单页面只保留最多20个历史版本
  121. $ret = D("PageHistory")->where(" page_id = '$page_id' ")->limit("20")->order("page_history_id desc")->select();
  122. D("PageHistory")->where(" page_id = '$page_id' and page_history_id < ".$ret[19]['page_history_id'] )->delete();
  123. }
  124. //更新项目时间
  125. D("Item")->where(" item_id = '$item_id' ")->save(array("last_update_time"=>time()));
  126. $return = D("Page")->where(" page_id = '$page_id' ")->find();
  127. }else{
  128. $page_id = D("Page")->add($data);
  129. //更新项目时间
  130. D("Item")->where(" item_id = '$item_id' ")->save(array("last_update_time"=>time()));
  131. $return = D("Page")->where(" page_id = '$page_id' ")->find();
  132. }
  133. if (!$return) {
  134. $return['error_code'] = 10103 ;
  135. $return['error_message'] = 'request fail' ;
  136. }
  137. $this->sendResult($return);
  138. }
  139. //删除页面
  140. public function delete(){
  141. $page_id = I("page_id/d")? I("page_id/d") : 0;
  142. $page = D("Page")->where(" page_id = '$page_id' ")->find();
  143. $login_user = $this->checkLogin();
  144. if (!$this->checkItemCreator($login_user['uid'] , $page['item_id']) && $login_user['uid'] != $page['author_uid']) {
  145. $this->message(L('no_permissions_to_delete_page',array("author_username"=>$page['author_username'])));
  146. return;
  147. }
  148. if ($page) {
  149. $ret = D("Page")->where(" page_id = '$page_id' ")->delete();
  150. //更新项目时间
  151. D("Item")->where(" item_id = '$page[item_id]' ")->save(array("last_update_time"=>time()));
  152. }
  153. if ($ret) {
  154. $this->message(L('delete_succeeded'),U("Home/item/show?item_id={$page['item_id']}"));
  155. }else{
  156. $this->message(L('delete_failed'),U("Home/item/show?item_id={$page['item_id']}"));
  157. }
  158. }
  159. //历史版本
  160. public function history(){
  161. $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
  162. $this->assign("page_id" , $page_id);
  163. $PageHistory = D("PageHistory")->where("page_id = '$page_id' ")->order(" addtime desc")->limit(10)->select();
  164. if ($PageHistory) {
  165. foreach ($PageHistory as $key => &$value) {
  166. $page_content = gzuncompress(base64_decode($value['page_content']));
  167. $value['page_content'] = $page_content ? $page_content : $value['page_content'] ;
  168. $value['addtime'] = date("Y-m-d H:i:s" , $value['addtime']);
  169. }
  170. }
  171. $this->assign("PageHistory" , $PageHistory);
  172. $this->display();
  173. }
  174. //上传图片
  175. public function uploadImg(){
  176. $qiniu_config = C('UPLOAD_SITEIMG_QINIU') ;
  177. if (!empty($qiniu_config['driverConfig']['secrectKey'])) {
  178. //上传到七牛
  179. $Upload = new \Think\Upload(C('UPLOAD_SITEIMG_QINIU'));
  180. $info = $Upload->upload($_FILES);
  181. $url = $info['editormd-image-file']['url'] ;
  182. echo json_encode(array("url"=>$url,"success"=>1));
  183. }else{
  184. $upload = new \Think\Upload();// 实例化上传类
  185. $upload->maxSize = 3145728 ;// 设置附件上传大小
  186. $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
  187. $upload->rootPath = './Public/Uploads/';// 设置附件上传目录
  188. $upload->savePath = '';// 设置附件上传子目录
  189. $info = $upload->upload() ;
  190. if(!$info) {// 上传错误提示错误信息
  191. $this->error($upload->getError());
  192. return;
  193. }else{// 上传成功 获取上传文件信息
  194. $url = get_domain().__ROOT__.substr($upload->rootPath,1).$info['editormd-image-file']['savepath'].$info['editormd-image-file']['savename'] ;
  195. echo json_encode(array("url"=>$url,"success"=>1));
  196. }
  197. }
  198. }
  199. public function diff(){
  200. $login_user = $this->checkLogin();
  201. $page_history_id = I("page_history_id/d");
  202. $page_id = I("page_id/d");
  203. $page = D("Page")->where(" page_id = '$page_id' ")->find();
  204. $cur_page_content = $page['page_content'];
  205. $item_id = $page['item_id'] ?$page['item_id'] :$item_id;
  206. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  207. $this->message(L('no_permissions'));
  208. return;
  209. }
  210. $page = D("PageHistory")->where(" page_history_id = '$page_history_id' ")->find();
  211. $page_content = gzuncompress(base64_decode($page['page_content']));
  212. $history_page_content = $page_content ? $page_content : $page['page_content'] ;
  213. $this->assign("cur_page_content" , $cur_page_content);
  214. $this->assign("history_page_content" , $history_page_content);
  215. $this->display();
  216. }
  217. }