PageController.class.php 13 KB

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