PageController.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class PageController extends BaseController {
  5. //页面详情
  6. public function info(){
  7. $page_id = I("page_id/d");
  8. $page = D("Page")->where(" page_id = '$page_id' ")->find();
  9. if (!$page || $page['is_del'] == 1) {
  10. sleep(1);
  11. $this->sendError(10101);
  12. return false;
  13. }
  14. $login_user = $this->checkLogin(false);
  15. if (!$this->checkItemVisit($login_user['uid'] , $page['item_id'])) {
  16. $this->sendError(10103);
  17. return;
  18. }
  19. $page = $page ? $page : array();
  20. if ($page) {
  21. //unset($page['page_content']);
  22. $page['addtime'] = date("Y-m-d H:i:s",$page['addtime']);
  23. //判断是否包含附件信息
  24. $page['attachment_count'] = D("FilePage")->where("page_id = '$page_id' ")->count();
  25. $singlePage = M("SinglePage")->where(" page_id = '%d' ",array($page_id))->limit(1)->find();
  26. if ($singlePage) {
  27. $page['unique_key'] = $singlePage['unique_key'] ;
  28. }else{
  29. $page['unique_key'] = '' ;
  30. }
  31. }
  32. $this->sendResult($page);
  33. // 埋个点,升级数据库
  34. R("Update/checkDb" , array(false));
  35. }
  36. //删除页面
  37. public function delete(){
  38. $page_id = I("page_id/d")? I("page_id/d") : 0;
  39. $page = D("Page")->where(" page_id = '$page_id' ")->find();
  40. $login_user = $this->checkLogin();
  41. if (!$this->checkItemManage($login_user['uid'] , $page['item_id']) && $login_user['uid'] != $page['author_uid']) {
  42. $this->sendError(10303);
  43. return ;
  44. }
  45. if ($page) {
  46. $ret = D("Page")->softDeletePage($page_id);
  47. //更新项目时间
  48. D("Item")->where(" item_id = '$page[item_id]' ")->save(array("last_update_time"=>time()));
  49. }
  50. if ($ret) {
  51. $this->sendResult(array());
  52. }else{
  53. $this->sendError(10101);
  54. }
  55. }
  56. //保存
  57. public function save(){
  58. $login_user = $this->checkLogin();
  59. $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
  60. $is_urlencode = I("is_urlencode/d") ? I("is_urlencode/d") : 0 ; //页面内容是否经过了转义
  61. $page_title = I("page_title") ?I("page_title") : L("default_title");
  62. $page_comments = I("page_comments") ?I("page_comments") :'';
  63. $page_content = I("page_content");
  64. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  65. $item_id = I("item_id/d")? I("item_id/d") : 0;
  66. $s_number = I("s_number/d")? I("s_number/d") : '';
  67. $login_user = $this->checkLogin();
  68. if (!$this->checkItemEdit($login_user['uid'] , $item_id)) {
  69. $this->sendError(10103);
  70. return;
  71. }
  72. if (!$page_content) {
  73. $this->sendError(10103,"不允许保存空内容,请随便写点什么");
  74. return;
  75. }
  76. if ($is_urlencode) {
  77. $page_content = urldecode($page_content);
  78. }
  79. $data['page_title'] = $page_title ;
  80. $data['page_content'] = $page_content ;
  81. $data['page_comments'] = $page_comments ;
  82. if($s_number)$data['s_number'] = $s_number ;
  83. $data['item_id'] = $item_id ;
  84. $data['cat_id'] = $cat_id ;
  85. $data['addtime'] = time();
  86. $data['author_uid'] = $login_user['uid'] ;
  87. $data['author_username'] = $login_user['username'];
  88. if ($page_id > 0 ) {
  89. // 设置里的历史版本数量
  90. $history_version_count = D("Options")->get("history_version_count" ) ;
  91. if(!$history_version_count){
  92. $history_version_count = 20 ;
  93. D("Options")->set("history_version_count" ,$history_version_count) ;
  94. }
  95. //在保存前先把当前页面的版本存档
  96. $page = D("Page")->where(" page_id = '$page_id' ")->find();
  97. if (!$this->checkItemEdit($login_user['uid'] , $page['item_id'])) {
  98. $this->sendError(10103);
  99. return;
  100. }
  101. $insert_history = array(
  102. 'page_id'=>$page['page_id'],
  103. 'item_id'=>$page['item_id'],
  104. 'cat_id'=>$page['cat_id'],
  105. 'page_title'=>$page['page_title'],
  106. 'page_comments'=>$page['page_comments'],
  107. 'page_content'=>base64_encode( gzcompress($page['page_content'], 9)),
  108. 's_number'=>$page['s_number'],
  109. 'addtime'=>$page['addtime'],
  110. 'author_uid'=>$page['author_uid'],
  111. 'author_username'=>$page['author_username'],
  112. );
  113. D("PageHistory")->add($insert_history);
  114. $ret = D("Page")->where(" page_id = '$page_id' ")->save($data);
  115. //统计该page_id有多少历史版本了
  116. $Count = D("PageHistory")->where(" page_id = '$page_id' ")->Count();
  117. if ($Count > $history_version_count ) {
  118. //每个单页面只保留最多$history_version_count个历史版本
  119. $ret = D("PageHistory")->where(" page_id = '$page_id' ")->limit($history_version_count)->order("page_history_id desc")->select();
  120. D("PageHistory")->where(" page_id = '$page_id' and page_history_id < ".$ret[$history_version_count-1]['page_history_id'] )->delete();
  121. }
  122. //如果是单页项目,则将页面标题设置为项目名
  123. $item_array = D("Item")->where(" item_id = '$item_id' ")->find();
  124. if ($item_array['item_type'] == 2 ) {
  125. D("Item")->where(" item_id = '$item_id' ")->save(array("last_update_time"=>time(),"item_name"=>$page_title));
  126. }else{
  127. D("Item")->where(" item_id = '$item_id' ")->save(array("last_update_time"=>time()));
  128. }
  129. $return = D("Page")->where(" page_id = '$page_id' ")->find();
  130. }else{
  131. $page_id = D("Page")->add($data);
  132. //更新项目时间
  133. D("Item")->where(" item_id = '$item_id' ")->save(array("last_update_time"=>time()));
  134. $return = D("Page")->where(" page_id = '$page_id' ")->find();
  135. }
  136. if (!$return) {
  137. $return['error_code'] = 10103 ;
  138. $return['error_message'] = 'request fail' ;
  139. }
  140. $this->sendResult($return);
  141. }
  142. //历史版本列表
  143. public function history(){
  144. $login_user = $this->checkLogin(false);
  145. $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
  146. $page = M("Page")->where(" page_id = '$page_id' ")->find();
  147. if (!$this->checkItemVisit($login_user['uid'] , $page['item_id'])) {
  148. $this->sendError(10103);
  149. return;
  150. }
  151. $PageHistory = D("PageHistory")->where("page_id = '$page_id' ")->order(" addtime desc")->limit(20)->select();
  152. if ($PageHistory) {
  153. foreach ($PageHistory as $key => &$value) {
  154. $value['addtime'] = date("Y-m-d H:i:s" , $value['addtime']);
  155. $page_content = uncompress_string($value['page_content']);
  156. if (!empty($page_content)) {
  157. $value['page_content'] = htmlspecialchars_decode($page_content) ;
  158. }
  159. }
  160. $this->sendResult($PageHistory);
  161. }else{
  162. $this->sendResult(array());
  163. }
  164. }
  165. // 更新历史备注信息
  166. public function updateHistoryComments(){
  167. $login_user = $this->checkLogin(false);
  168. $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
  169. $page_comments = I("page_comments") ;
  170. $page_history_id = I("page_history_id/d") ? I("page_history_id/d") : 0 ;
  171. $page = M("Page")->where(" page_id = '$page_id' ")->find();
  172. if (!$this->checkItemEdit($login_user['uid'] , $page['item_id'])) {
  173. $this->sendError(10103);
  174. return;
  175. }
  176. $res = D("PageHistory")->where(" page_history_id = '$page_history_id' ")->save(array(
  177. "page_comments"=>$page_comments
  178. ));
  179. $this->sendResult($res);
  180. }
  181. //返回当前页面和历史某个版本的页面以供比较
  182. public function diff(){
  183. $page_id = I("page_id/d");
  184. $page_history_id = I("page_history_id/d");
  185. if (!$page_id) {
  186. return false;
  187. }
  188. $page = M("Page")->where(" page_id = '$page_id' ")->find();
  189. if (!$page) {
  190. sleep(1);
  191. $this->sendError(10101);
  192. return false;
  193. }
  194. $login_user = $this->checkLogin(false);
  195. if (!$this->checkItemVisit($login_user['uid'] , $page['item_id'])) {
  196. $this->sendError(10103);
  197. return;
  198. }
  199. $history_page = D("PageHistory")->where(" page_history_id = '$page_history_id' ")->find();
  200. $page_content = uncompress_string($history_page['page_content']);
  201. $history_page['page_content'] = $page_content ? $page_content : $history_page['page_content'] ;
  202. $this->sendResult(array("page"=>$page,"history_page"=>$history_page));
  203. }
  204. //上传图片
  205. public function uploadImg(){
  206. //重定向控制器和方法
  207. R("Attachment/uploadImg");
  208. }
  209. //上传附件
  210. public function upload(){
  211. //重定向控制器和方法
  212. R("Attachment/attachmentUpload");
  213. }
  214. public function uploadList(){
  215. //重定向控制器和方法
  216. R("Attachment/pageAttachmentUploadList");
  217. }
  218. //删除已上传文件
  219. public function deleteUploadFile(){
  220. //重定向控制器和方法
  221. R("Attachment/deletePageUploadFile");
  222. }
  223. //创建单页
  224. public function createSinglePage(){
  225. $page_id = I("page_id/d");
  226. $isCreateSiglePage = I("isCreateSiglePage");
  227. $page = M("Page")->where(" page_id = '$page_id' ")->find();
  228. if (!$page || $page['is_del'] == 1) {
  229. sleep(1);
  230. $this->sendError(10101);
  231. return false;
  232. }
  233. $login_user = $this->checkLogin(false);
  234. if (!$this->checkItemEdit($login_user['uid'] , $page['item_id'])) {
  235. $this->sendError(10103);
  236. return;
  237. }
  238. D("SinglePage")->where(" page_id = '$page_id' ")->delete();
  239. $unique_key = md5(time().rand()."gbgdhbdgtfgfK3@bv45342regdhbdgtfgftghsdg");
  240. $add = array(
  241. "unique_key" => $unique_key ,
  242. "page_id" => $page_id ,
  243. );
  244. if ($isCreateSiglePage == 'true') { //这里的布尔值被转成字符串了
  245. D("SinglePage")->add($add);
  246. $this->sendResult($add);
  247. }else{
  248. $this->sendResult(array());
  249. }
  250. }
  251. //页面详情
  252. public function infoByKey(){
  253. $unique_key = I("unique_key");
  254. if (!$unique_key) {
  255. return false;
  256. }
  257. $singlePage = M("SinglePage")->where(" unique_key = '%s' ",array($unique_key))->find();
  258. $page_id = $singlePage['page_id'];
  259. $page = M("Page")->where(" page_id = '$page_id' ")->find();
  260. if (!$page || $page['is_del'] == 1) {
  261. sleep(1);
  262. $this->sendError(10101);
  263. return false;
  264. }
  265. $login_user = $this->checkLogin(false);
  266. $page = $page ? $page : array();
  267. if ($page) {
  268. unset($page['item_id']);
  269. unset($page['cat_id']);
  270. $page['addtime'] = date("Y-m-d H:i:s",$page['addtime']);
  271. //判断是否包含附件信息
  272. $page['attachment_count'] = D("FilePage")->where("page_id = '$page_id' ")->count();
  273. }
  274. $this->sendResult($page);
  275. }
  276. //同一个目录下的页面排序
  277. public function sort(){
  278. $pages = I("pages");
  279. $item_id = I("item_id/d");
  280. $login_user = $this->checkLogin();
  281. if (!$this->checkItemEdit($login_user['uid'] , $item_id)) {
  282. $this->sendError(10103);
  283. return ;
  284. }
  285. $ret = '';
  286. $data_array = json_decode(htmlspecialchars_decode($pages) , true) ;
  287. if ($data_array) {
  288. foreach ($data_array as $key => $value) {
  289. $ret = D("Page")->where(" page_id = '%d' and item_id = '%d' ",array($key ,$item_id ) )->save(array(
  290. "s_number" => $value ,
  291. ));
  292. }
  293. }
  294. $this->sendResult(array());
  295. }
  296. //判断页面是否加了编辑锁
  297. public function isLock(){
  298. $page_id = I("page_id/d");
  299. $lock = 0 ;
  300. $now = time() ;
  301. $login_user = $this->checkLogin(false);
  302. $res = D("PageLock")->where(" page_id = '$page_id' and page_id > 0 and lock_to > '{$now}' ")->find() ;
  303. if( $res){
  304. $lock = 1 ;
  305. }
  306. $this->sendResult(array(
  307. "lock" => $lock,
  308. "lock_uid" => $res['lock_uid'] ? $res['lock_uid'] : '',
  309. "lock_username" => $res['lock_username'] ? $res['lock_username'] : '',
  310. "is_cur_user" => $res['lock_uid'] == $login_user['uid'] ? 1 : 0 ,
  311. ));
  312. }
  313. //设置页面加锁时间
  314. public function setLock(){
  315. $page_id = I("page_id/d");
  316. $lock_to = I("lock_to/d") ? I("lock_to/d") :(time() + 5*60*60 ) ;
  317. $item_id = I("item_id/d");
  318. $login_user = $this->checkLogin();
  319. if (!$this->checkItemEdit($login_user['uid'] , $item_id)) {
  320. $this->sendError(10103);
  321. return ;
  322. }
  323. D("PageLock")->where( "page_id = '{$page_id}' ")->delete();
  324. $id = D("PageLock")->add(array(
  325. "page_id" => $page_id ,
  326. "lock_uid" => $login_user['uid'] ,
  327. "lock_username" => $login_user['username'] ,
  328. "lock_to" => $lock_to ,
  329. "addtime" => time() ,
  330. ));
  331. $now = time() ;
  332. D("PageLock")->where( "lock_to < '{$now}' ")->delete();
  333. $this->sendResult(array("id"=>$id));
  334. }
  335. }