Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

358 Zeilen
12 KiB

vor 3 Jahren
  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 diff(){
  159. $page_id = I("page_id/d");
  160. $page_history_id = I("page_history_id/d");
  161. if (!$page_id) {
  162. return false;
  163. }
  164. $page = M("Page")->where(" page_id = '$page_id' ")->find();
  165. if (!$page) {
  166. sleep(1);
  167. $this->sendError(10101);
  168. return false;
  169. }
  170. $login_user = $this->checkLogin(false);
  171. if (!$this->checkItemVisit($login_user['uid'] , $page['item_id'])) {
  172. $this->sendError(10103);
  173. return;
  174. }
  175. $history_page = D("PageHistory")->where(" page_history_id = '$page_history_id' ")->find();
  176. $page_content = uncompress_string($history_page['page_content']);
  177. $history_page['page_content'] = $page_content ? $page_content : $history_page['page_content'] ;
  178. $this->sendResult(array("page"=>$page,"history_page"=>$history_page));
  179. }
  180. //上传图片
  181. public function uploadImg(){
  182. //重定向控制器和方法
  183. R("Attachment/uploadImg");
  184. }
  185. //上传附件
  186. public function upload(){
  187. //重定向控制器和方法
  188. R("Attachment/attachmentUpload");
  189. }
  190. public function uploadList(){
  191. //重定向控制器和方法
  192. R("Attachment/pageAttachmentUploadList");
  193. }
  194. //删除已上传文件
  195. public function deleteUploadFile(){
  196. //重定向控制器和方法
  197. R("Attachment/deletePageUploadFile");
  198. }
  199. //创建单页
  200. public function createSinglePage(){
  201. $page_id = I("page_id/d");
  202. $isCreateSiglePage = I("isCreateSiglePage");
  203. $page = M("Page")->where(" page_id = '$page_id' ")->find();
  204. if (!$page || $page['is_del'] == 1) {
  205. sleep(1);
  206. $this->sendError(10101);
  207. return false;
  208. }
  209. $login_user = $this->checkLogin(false);
  210. if (!$this->checkItemPermn($login_user['uid'] , $page['item_id'])) {
  211. $this->sendError(10103);
  212. return;
  213. }
  214. D("SinglePage")->where(" page_id = '$page_id' ")->delete();
  215. $unique_key = md5(time().rand()."gbgdhbdgtfgfK3@bv45342regdhbdgtfgftghsdg");
  216. $add = array(
  217. "unique_key" => $unique_key ,
  218. "page_id" => $page_id ,
  219. );
  220. if ($isCreateSiglePage == 'true') { //这里的布尔值被转成字符串了
  221. D("SinglePage")->add($add);
  222. $this->sendResult($add);
  223. }else{
  224. $this->sendResult(array());
  225. }
  226. }
  227. //页面详情
  228. public function infoByKey(){
  229. $unique_key = I("unique_key");
  230. if (!$unique_key) {
  231. return false;
  232. }
  233. $singlePage = M("SinglePage")->where(" unique_key = '%s' ",array($unique_key))->find();
  234. $page_id = $singlePage['page_id'];
  235. $page = M("Page")->where(" page_id = '$page_id' ")->find();
  236. if (!$page || $page['is_del'] == 1) {
  237. sleep(1);
  238. $this->sendError(10101);
  239. return false;
  240. }
  241. $login_user = $this->checkLogin(false);
  242. $page = $page ? $page : array();
  243. if ($page) {
  244. unset($page['item_id']);
  245. unset($page['cat_id']);
  246. $page['addtime'] = date("Y-m-d H:i:s",$page['addtime']);
  247. //判断是否包含附件信息
  248. $page['attachment_count'] = D("FilePage")->where("page_id = '$page_id' ")->count();
  249. }
  250. $this->sendResult($page);
  251. }
  252. //同一个目录下的页面排序
  253. public function sort(){
  254. $pages = I("pages");
  255. $item_id = I("item_id/d");
  256. $login_user = $this->checkLogin();
  257. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  258. $this->sendError(10103);
  259. return ;
  260. }
  261. $ret = '';
  262. $data_array = json_decode(htmlspecialchars_decode($pages) , true) ;
  263. if ($data_array) {
  264. foreach ($data_array as $key => $value) {
  265. $ret = D("Page")->where(" page_id = '$key' and item_id = '$item_id' ")->save(array(
  266. "s_number" => $value ,
  267. ));
  268. }
  269. }
  270. $this->sendResult(array());
  271. }
  272. //判断页面是否加了编辑锁
  273. public function isLock(){
  274. $page_id = I("page_id/d");
  275. $lock = 0 ;
  276. $now = time() ;
  277. $login_user = $this->checkLogin(false);
  278. $res = D("PageLock")->where(" page_id = '$page_id' and page_id > 0 and lock_to > '{$now}' ")->find() ;
  279. if( $res){
  280. $lock = 1 ;
  281. }
  282. $this->sendResult(array(
  283. "lock" => $lock,
  284. "lock_uid" => $res['lock_uid'] ? $res['lock_uid'] : '',
  285. "lock_username" => $res['lock_username'] ? $res['lock_username'] : '',
  286. "is_cur_user" => $res['lock_uid'] == $login_user['uid'] ? 1 : 0 ,
  287. ));
  288. }
  289. //设置页面加锁时间
  290. public function setLock(){
  291. $page_id = I("page_id/d");
  292. $lock_to = I("lock_to/d") ? I("lock_to/d") :(time() + 5*60*60 ) ;
  293. $item_id = I("item_id/d");
  294. $login_user = $this->checkLogin();
  295. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  296. $this->sendError(10103);
  297. return ;
  298. }
  299. D("PageLock")->where( "page_id = '{$page_id}' ")->delete();
  300. $id = D("PageLock")->add(array(
  301. "page_id" => $page_id ,
  302. "lock_uid" => $login_user['uid'] ,
  303. "lock_username" => $login_user['username'] ,
  304. "lock_to" => $lock_to ,
  305. "addtime" => time() ,
  306. ));
  307. $now = time() ;
  308. D("PageLock")->where( "lock_to < '{$now}' ")->delete();
  309. $this->sendResult(array("id"=>$id));
  310. }
  311. }