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.

611 lines
20 KiB

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