ItemController.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class ItemController extends BaseController {
  5. //单个项目信息
  6. public function info(){
  7. $this->checkLogin(false);
  8. $item_id = I("item_id");
  9. $item_domain = I("item_domain/s");
  10. $current_page_id = I("page_id/d");
  11. if (! is_numeric($item_id)) {
  12. $item_domain = $item_id ;
  13. }
  14. //判断个性域名
  15. if ($item_domain) {
  16. $item = D("Item")->where("item_domain = '%s'",array($item_domain))->find();
  17. if ($item['item_id']) {
  18. $item_id = $item['item_id'] ;
  19. }
  20. }
  21. $login_user = session("login_user");
  22. $uid = $login_user['uid'] ? $login_user['uid'] : 0 ;
  23. if(!$this->checkItemVisit($uid , $item_id)){
  24. $this->sendError(10303);
  25. return ;
  26. }
  27. $item = D("Item")->where("item_id = '$item_id' ")->find();
  28. if (!$item || $item['is_del'] == 1) {
  29. sleep(1);
  30. $this->sendError(10101,'项目不存在或者已删除');
  31. return false;
  32. }
  33. if ($item['item_type'] == 1 ) {
  34. $this->_show_regular_item($item);
  35. }
  36. elseif ($item['item_type'] == 2 ) {
  37. $this->_show_single_page_item($item);
  38. }else{
  39. $this->_show_regular_item($item);
  40. }
  41. }
  42. //展示常规项目
  43. private function _show_regular_item($item){
  44. $item_id = $item['item_id'];
  45. $default_page_id = I("default_page_id/d");
  46. $keyword = I("keyword");
  47. $default_cat_id2 = $default_cat_id3 = 0 ;
  48. $login_user = session("login_user");
  49. $uid = $login_user['uid'] ? $login_user['uid'] : 0 ;
  50. $is_login = $uid > 0 ? true :false;
  51. $menu = array(
  52. "pages" =>array(),
  53. "catalogs" =>array(),
  54. );
  55. //是否有搜索词
  56. if ($keyword) {
  57. $keyword = \SQLite3::escapeString($keyword) ;
  58. $pages = D("Page")->where("item_id = '$item_id' and is_del = 0 and ( page_title like '%{$keyword}%' or page_content like '%{$keyword}%' ) ")->order(" `s_number` asc ")->field("page_id,author_uid,cat_id,page_title,addtime")->select();
  59. $menu['pages'] = $pages ? $pages : array();
  60. }else{
  61. $menu = D("Item")->getMemu($item_id) ;
  62. }
  63. $domain = $item['item_domain'] ? $item['item_domain'] : $item['item_id'];
  64. $share_url = get_domain().__APP__.'/'.$domain;
  65. $ItemPermn = $this->checkItemPermn($uid , $item_id) ;
  66. $ItemCreator = $this->checkItemCreator($uid , $item_id);
  67. //如果带了默认展开的页面id,则获取该页面所在的二级目录/三级目录/四级目录
  68. if ($default_page_id) {
  69. $page = D("Page")->where(" page_id = '$default_page_id' ")->find();
  70. if ($page) {
  71. $default_cat_id4 = $page['cat_id'] ;
  72. $cat1 = D("Catalog")->where(" cat_id = '$default_cat_id4' and parent_cat_id > 0 ")->find();
  73. if ($cat1) {
  74. $default_cat_id3 = $cat1['parent_cat_id'];
  75. }else{
  76. $default_cat_id3 = $default_cat_id4;
  77. $default_cat_id4 = 0 ;
  78. }
  79. $cat2 = D("Catalog")->where(" cat_id = '$default_cat_id3' and parent_cat_id > 0 ")->find();
  80. if ($cat2) {
  81. $default_cat_id2 = $cat2['parent_cat_id'];
  82. }else{
  83. $default_cat_id2 = $default_cat_id3;
  84. $default_cat_id3 = 0 ;
  85. }
  86. }
  87. }
  88. if (LANG_SET == 'en-us') {
  89. $help_url = "https://www.showdoc.cc/help-en";
  90. }
  91. else{
  92. $help_url = "https://www.showdoc.cc/help";
  93. }
  94. $return = array(
  95. "item_id"=>$item_id ,
  96. "item_domain"=>$item['item_domain'] ,
  97. "is_archived"=>$item['is_archived'] ,
  98. "item_name"=>$item['item_name'] ,
  99. "default_page_id"=>(string)$default_page_id ,
  100. "default_cat_id2"=>$default_cat_id2 ,
  101. "default_cat_id3"=>$default_cat_id3 ,
  102. "default_cat_id4"=>$default_cat_id4 ,
  103. "unread_count"=>$unread_count ,
  104. "item_type"=>1 ,
  105. "menu"=>$menu ,
  106. "is_login"=>$is_login,
  107. "ItemPermn"=>$ItemPermn ,
  108. "ItemCreator"=>$ItemCreator ,
  109. );
  110. $this->sendResult($return);
  111. }
  112. //展示单页项目
  113. private function _show_single_page_item($item){
  114. $item_id = $item['item_id'];
  115. $current_page_id = I("page_id/d");
  116. $login_user = session("login_user");
  117. $uid = $login_user['uid'] ? $login_user['uid'] : 0 ;
  118. $is_login = $uid > 0 ? true :false;
  119. //获取页面
  120. $page = D("Page")->where(" item_id = '$item_id' ")->find();
  121. $domain = $item['item_domain'] ? $item['item_domain'] : $item['item_id'];
  122. $share_url = get_domain().__APP__.'/'.$domain;
  123. $ItemPermn = $this->checkItemPermn($uid , $item_id) ;
  124. $ItemCreator = $this->checkItemCreator($uid , $item_id);
  125. $menu = array() ;
  126. $menu['pages'] = $page ;
  127. $return = array(
  128. "item_id"=>$item_id ,
  129. "item_domain"=>$item['item_domain'] ,
  130. "is_archived"=>$item['is_archived'] ,
  131. "item_name"=>$item['item_name'] ,
  132. "current_page_id"=>$current_page_id ,
  133. "unread_count"=>$unread_count ,
  134. "item_type"=>2 ,
  135. "menu"=>$menu ,
  136. "is_login"=>$is_login,
  137. "ItemPermn"=>$ItemPermn ,
  138. "ItemCreator"=>$ItemCreator ,
  139. );
  140. $this->sendResult($return);
  141. }
  142. //我的项目列表
  143. public function myList(){
  144. $login_user = $this->checkLogin();
  145. $items = D("Item")->field("item_id,item_name,last_update_time,item_description,is_del")->where("uid = '$login_user[uid]' or item_id in ( select item_id from ".C('DB_PREFIX')."item_member where uid = '$login_user[uid]' ) ")->order("item_id asc")->select();
  146. foreach ($items as $key => $value) {
  147. //如果项目已标识为删除
  148. if ($value['is_del'] == 1) {
  149. unset($items[$key]);
  150. }
  151. }
  152. $items = array_values($items);
  153. //读取需要置顶的项目
  154. $top_items = D("ItemTop")->where("uid = '$login_user[uid]'")->select();
  155. if ($top_items) {
  156. $top_item_ids = array() ;
  157. foreach ($top_items as $key => $value) {
  158. $top_item_ids[] = $value['item_id'];
  159. }
  160. foreach ($items as $key => $value) {
  161. $items[$key]['top'] = 0 ;
  162. if (in_array($value['item_id'], $top_item_ids) ) {
  163. $items[$key]['top'] = 1 ;
  164. $tmp = $items[$key] ;
  165. unset($items[$key]);
  166. array_unshift($items,$tmp) ;
  167. }
  168. }
  169. }
  170. $items = $items ? array_values($items) : array();
  171. $this->sendResult($items);
  172. }
  173. //项目详情
  174. public function detail(){
  175. $login_user = $this->checkLogin();
  176. $item_id = I("item_id/d");
  177. $uid = $login_user['uid'] ;
  178. if(!$this->checkItemCreator($uid , $item_id)){
  179. $this->sendError(10303);
  180. return ;
  181. }
  182. $items = D("Item")->where("item_id = '$item_id' ")->find();
  183. $items = $items ? $items : array();
  184. $this->sendResult($items);
  185. }
  186. //更新项目信息
  187. public function update(){
  188. $login_user = $this->checkLogin();
  189. $item_id = I("item_id/d");
  190. $item_name = I("item_name");
  191. $item_description = I("item_description");
  192. $item_domain = I("item_domain");
  193. $password = I("password");
  194. $uid = $login_user['uid'] ;
  195. if(!$this->checkItemCreator($uid , $item_id)){
  196. $this->sendError(10303);
  197. return ;
  198. }
  199. if ($item_domain) {
  200. if(!ctype_alnum($item_domain) || is_numeric($item_domain) ){
  201. //echo '个性域名只能是字母或数字的组合';exit;
  202. $this->sendError(10305);
  203. return false;
  204. }
  205. $item = D("Item")->where("item_domain = '%s' and item_id !='%s' ",array($item_domain,$item_id))->find();
  206. if ($item) {
  207. //个性域名已经存在
  208. $this->sendError(10304);
  209. return false;
  210. }
  211. }
  212. $save_data = array(
  213. "item_name" => $item_name ,
  214. "item_description" => $item_description ,
  215. "item_domain" => $item_domain ,
  216. "password" => $password ,
  217. );
  218. $items = D("Item")->where("item_id = '$item_id' ")->save($save_data);
  219. $items = $items ? $items : array();
  220. $this->sendResult($items);
  221. }
  222. //转让项目
  223. public function attorn(){
  224. $login_user = $this->checkLogin();
  225. $username = I("username");
  226. $item_id = I("item_id/d");
  227. $password = I("password");
  228. $item = D("Item")->where("item_id = '$item_id' ")->find();
  229. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  230. $this->sendError(10303);
  231. return ;
  232. }
  233. if(! D("User")-> checkLogin($item['username'],$password)){
  234. $this->sendError(10208);
  235. return ;
  236. }
  237. $member = D("User")->where(" username = '%s' ",array($username))->find();
  238. if (!$member) {
  239. $this->sendError(10209);
  240. return ;
  241. }
  242. $data['username'] = $member['username'] ;
  243. $data['uid'] = $member['uid'] ;
  244. $id = D("Item")->where(" item_id = '$item_id' ")->save($data);
  245. $return = D("Item")->where("item_id = '$item_id' ")->find();
  246. if (!$return) {
  247. $this->sendError(10101);
  248. }
  249. $this->sendResult($return);
  250. }
  251. //删除项目
  252. public function delete(){
  253. $login_user = $this->checkLogin();
  254. $item_id = I("item_id/d");
  255. $password = I("password");
  256. $item = D("Item")->where("item_id = '$item_id' ")->find();
  257. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  258. $this->sendError(10303);
  259. return ;
  260. }
  261. if(! D("User")-> checkLogin($item['username'],$password)){
  262. $this->sendError(10208);
  263. return ;
  264. }
  265. $return = D("Item")->soft_delete_item($item_id);
  266. if (!$return) {
  267. $this->sendError(10101);
  268. }else{
  269. }
  270. $this->sendResult($return);
  271. }
  272. //归档项目
  273. public function archive(){
  274. $login_user = $this->checkLogin();
  275. $item_id = I("item_id/d");
  276. $password = I("password");
  277. $item = D("Item")->where("item_id = '$item_id' ")->find();
  278. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  279. $this->sendError(10303);
  280. return ;
  281. }
  282. if(! D("User")-> checkLogin($item['username'],$password)){
  283. $this->sendError(10208);
  284. return ;
  285. }
  286. $return = D("Item")->where("item_id = '$item_id' ")->save(array("is_archived"=>1));
  287. if (!$return) {
  288. $this->sendError(10101);
  289. }else{
  290. $this->sendResult($return);
  291. }
  292. }
  293. public function getKey(){
  294. $login_user = $this->checkLogin();
  295. $item_id = I("item_id/d");
  296. $item = D("Item")->where("item_id = '$item_id' ")->find();
  297. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  298. $this->sendError(10303);
  299. return ;
  300. }
  301. $item_token = D("ItemToken")->getTokenByItemId($item_id);
  302. if (!$item_token) {
  303. $this->sendError(10101);
  304. }
  305. $this->sendResult($item_token);
  306. }
  307. public function resetKey(){
  308. $login_user = $this->checkLogin();
  309. $item_id = I("item_id/d");
  310. $item = D("Item")->where("item_id = '$item_id' ")->find();
  311. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  312. $this->sendError(10303);
  313. return ;
  314. }
  315. $ret = D("ItemToken")->where("item_id = '$item_id' ")->delete();
  316. if ($ret) {
  317. $this->getKey();
  318. }else{
  319. $this->sendError(10101);
  320. }
  321. }
  322. public function updateByApi(){
  323. //转到Open控制器的updateItem方法
  324. R('Open/updateItem');
  325. }
  326. //置顶项目
  327. public function top(){
  328. $login_user = $this->checkLogin();
  329. $item_id = I("item_id/d");
  330. $action = I("action");
  331. if ($action == 'top') {
  332. $ret = D("ItemTop")->add(array("item_id"=>$item_id,"uid"=>$login_user['uid'],"addtime"=>time()));
  333. }
  334. elseif ($action == 'cancel') {
  335. $ret = D("ItemTop")->where(" uid = '$login_user[uid]' and item_id = '$item_id' ")->delete();
  336. }
  337. if ($ret) {
  338. $this->sendResult(array());
  339. }else{
  340. $this->sendError(10101);
  341. }
  342. }
  343. //验证访问密码
  344. public function pwd(){
  345. $item_id = I("item_id/d");
  346. $password = I("password");
  347. $v_code = I("v_code");
  348. $refer_url = I('refer_url');
  349. //检查用户输错密码的次数。如果超过一定次数,则需要验证 验证码
  350. $key= 'item_pwd_fail_times_'.$item_id;
  351. if(!D("VerifyCode")->_check_times($key,10)){
  352. if (!$v_code || $v_code != session('v_code')) {
  353. $this->sendError(10206,L('verification_code_are_incorrect'));
  354. return;
  355. }
  356. }
  357. session('v_code',null) ;
  358. $item = D("Item")->where("item_id = '$item_id' ")->find();
  359. if ($item['password'] == $password) {
  360. session("visit_item_".$item_id , 1 );
  361. $this->sendResult(array("refer_url"=>base64_decode($refer_url)));
  362. }else{
  363. D("VerifyCode")->_ins_times($key);//输错密码则设置输错次数
  364. if(D("VerifyCode")->_check_times($key,10)){
  365. $error_code = 10307 ;
  366. }else{
  367. $error_code = 10308 ;
  368. }
  369. $this->sendError($error_code,L('access_password_are_incorrect'));
  370. }
  371. }
  372. public function itemList(){
  373. $login_user = $this->checkLogin();
  374. $items = D("Item")->where("uid = '$login_user[uid]' ")->select();
  375. $items = $items ? $items : array();
  376. $this->sendResult($items);
  377. }
  378. //新建项目
  379. public function add(){
  380. $login_user = $this->checkLogin();
  381. $item_name = I("item_name");
  382. $item_domain = I("item_domain") ? I("item_domain") : '';
  383. $copy_item_id = I("copy_item_id");
  384. $password = I("password");
  385. $item_description = I("item_description");
  386. $item_type = I("item_type");
  387. if ($item_domain) {
  388. if(!ctype_alnum($item_domain) || is_numeric($item_domain) ){
  389. //echo '个性域名只能是字母或数字的组合';exit;
  390. $this->sendError(10305);
  391. return false;
  392. }
  393. $item = D("Item")->where("item_domain = '%s' ",array($item_domain))->find();
  394. if ($item) {
  395. //个性域名已经存在
  396. $this->sendError(10304);
  397. return false;
  398. }
  399. }
  400. //如果是复制项目
  401. if ($copy_item_id > 0) {
  402. if (!$this->checkItemPermn($login_user['uid'] , $copy_item_id)) {
  403. $this->sendError(10103);
  404. return;
  405. }
  406. $ret = D("Item")->copy($copy_item_id,$login_user['uid'],$item_name,$item_description,$password,$item_domain);
  407. if ($ret) {
  408. $this->sendResult(array());
  409. }else{
  410. $this->sendError(10101);
  411. }
  412. return ;
  413. }
  414. $insert = array(
  415. "uid" => $login_user['uid'] ,
  416. "username" => $login_user['username'] ,
  417. "item_name" => $item_name ,
  418. "password" => $password ,
  419. "item_description" => $item_description ,
  420. "item_domain" => $item_domain ,
  421. "item_type" => $item_type ,
  422. "addtime" =>time()
  423. );
  424. $item_id = D("Item")->add($insert);
  425. if ($item_id) {
  426. //如果是单页应用,则新建一个默认页
  427. if ($item_type == 2 ) {
  428. $insert = array(
  429. 'author_uid' => $login_user['uid'] ,
  430. 'author_username' => $login_user['username'],
  431. "page_title" => $item_name ,
  432. "item_id" => $item_id ,
  433. "cat_id" => 0 ,
  434. "page_content" => '欢迎使用showdoc。点击右上方的编辑按钮进行编辑吧!' ,
  435. "addtime" =>time()
  436. );
  437. $page_id = D("Page")->add($insert);
  438. }
  439. $this->sendResult(array());
  440. }else{
  441. $this->sendError(10101);
  442. }
  443. }
  444. }