PageController.class.php 13 KB

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