You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

392 lines
13 KiB

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