No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

620 líneas
20 KiB

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