123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681 |
- <?php
- namespace Api\Controller;
- use Think\Controller;
- class ItemController extends BaseController {
- //单个项目信息
- public function info(){
- $this->checkLogin(false);
- $item_id = I("item_id/s");
- $item_domain = I("item_domain/s");
- $current_page_id = I("page_id/d");
- if (! is_numeric($item_id)) {
- $item_domain = $item_id ;
- }
- //判断个性域名
- if ($item_domain) {
- $item = D("Item")->where("item_domain = '%s'",array($item_domain))->find();
- if ($item['item_id']) {
- $item_id = $item['item_id'] ;
- }
- }
- $login_user = session("login_user");
- $uid = $login_user['uid'] ? $login_user['uid'] : 0 ;
-
- if(!$this->checkItemVisit($uid , $item_id)){
- $this->sendError(10303);
- return ;
- }
- $item = D("Item")->where("item_id = '%d' ",array($item_id))->find();
- if (!$item || $item['is_del'] == 1) {
- sleep(1);
- $this->sendError(10101,'项目不存在或者已删除');
- return false;
- }
- //从2020.7.5开始,常规项目和单页项目合并在一起返回
- $this->_show_regular_item($item);
- }
- //展示常规项目
- private function _show_regular_item($item){
- $item_id = $item['item_id'];
- $default_page_id = I("default_page_id/d");
- $current_page_id = I("page_id/d");
- $keyword = I("keyword");
- $default_cat_id2 = $default_cat_id3 = 0 ;
- $login_user = session("login_user");
- $uid = $login_user['uid'] ? $login_user['uid'] : 0 ;
- $is_login = $uid > 0 ? true :false;
- $menu = array(
- "pages" =>array(),
- "catalogs" =>array(),
- );
- //是否有搜索词
- if ($keyword) {
- $keyword = strtolower ($keyword) ;
- $keyword = \SQLite3::escapeString($keyword) ;
- $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();
- $menu['pages'] = $pages ? $pages : array();
- }else{
- $menu = D("Item")->getMemu($item_id) ;
- if($uid > 0 ){
- $menu = D("Item")->filteMemberItem($uid , $item_id , $menu);
- }
- }
- $domain = $item['item_domain'] ? $item['item_domain'] : $item['item_id'];
- $share_url = get_domain().__APP__.'/'.$domain;
- $item_edit = $this->checkItemEdit($uid , $item_id) ;
- $item_manage = $this->checkItemManage($uid , $item_id);
- //如果带了默认展开的页面id,则获取该页面所在的二级目录/三级目录/四级目录
- if ($default_page_id) {
- $page = D("Page")->where(" page_id = '$default_page_id' ")->find();
- if ($page) {
- $default_cat_id4 = $page['cat_id'] ;
- $cat1 = D("Catalog")->where(" cat_id = '$default_cat_id4' and parent_cat_id > 0 ")->find();
- if ($cat1) {
- $default_cat_id3 = $cat1['parent_cat_id'];
- }else{
- $default_cat_id3 = $default_cat_id4;
- $default_cat_id4 = 0 ;
- }
- $cat2 = D("Catalog")->where(" cat_id = '$default_cat_id3' and parent_cat_id > 0 ")->find();
- if ($cat2) {
- $default_cat_id2 = $cat2['parent_cat_id'];
- }else{
- $default_cat_id2 = $default_cat_id3;
- $default_cat_id3 = 0 ;
- }
- }
- }
- if (LANG_SET == 'en-us') {
- $help_url = "https://www.showdoc.cc/help-en";
- }
- else{
- $help_url = "https://www.showdoc.cc/help";
- }
- //当已经归档了,则去掉编辑权限
- if($item['is_archived']){
- $item_edit = $item_manage = false;
- }
- //如果项目类型为runapi,则获取看看有没有全局参数
- $global_param = array() ;
- if($item['item_type'] == 3){
- $global_param = D("Runapi")->getGlobalParam($item_id);
- }
-
- $return = array(
- "item_id"=>$item_id ,
- "item_domain"=>$item['item_domain'] ,
- "is_archived"=>$item['is_archived'] ,
- "item_name"=>$item['item_name'] ,
- "default_page_id"=>(string)$default_page_id ,
- "default_cat_id2"=>$default_cat_id2 ,
- "default_cat_id3"=>$default_cat_id3 ,
- "default_cat_id4"=>$default_cat_id4 ,
- "unread_count"=>$unread_count ,
- "item_type"=>$item['item_type'] ,
- "menu"=>$menu ,
- "is_login"=>$is_login,
- "item_edit"=>$item_edit ,
- "item_manage"=>$item_manage ,
- "ItemPermn"=>$item_edit , // ItemPermn 和 ItemCreator这两个字段是为了兼容历史。确保各大客户端(web/手机/runapi)改用字段后可以去掉
- "ItemCreator"=>$item_manage ,
- "current_page_id"=>$current_page_id ,
- "global_param"=>$global_param ,
- );
- $this->sendResult($return);
- }
- //我的项目列表
- public function myList(){
- $login_user = $this->checkLogin();
- $original = I("original/d") ? I("original/d") : 0; //1:只返回自己原创的项目;默认是0
- $item_group_id = I("item_group_id/d") ? I("item_group_id/d") : 0; //项目分组id。默认是0
- $where = "uid = '$login_user[uid]' " ;
- $member_item_ids = array(-1) ;
- $item_members = D("ItemMember")->where("uid = '$login_user[uid]'")->select();
- if ($item_members) {
- foreach ($item_members as $key => $value) {
- $member_item_ids[] = $value['item_id'] ;
- }
- }
- $team_item_members = D("TeamItemMember")->where("member_uid = '$login_user[uid]'")->select();
- if ($team_item_members) {
- foreach ($team_item_members as $key => $value) {
- $member_item_ids[] = $value['item_id'] ;
- }
- }
- $where .= " or item_id in ( ".implode(",", $member_item_ids)." ) ";
- if($item_group_id){
- $res = D("ItemGroup")->where(" id = '$item_group_id' ")->find();
- if($res){
- $where = " ({$where}) and item_id in ({$res['item_ids']}) ";
- }
- }
- $items = D("Item")->field("item_id,uid,item_name,item_domain,item_type,last_update_time,item_description,is_del,password")->where($where)->order("item_id asc")->select();
-
-
- foreach ($items as $key => $value) {
- if ($value['uid'] == $login_user['uid']) {
- $items[$key]['creator'] = 1 ;
- }else{
- $items[$key]['creator'] = 0 ;
- unset($items[$key]['password']);
- }
- //判断是否为私密项目
- if ($value['password']) {
- $items[$key]['is_private'] = 1 ;
- }else{
- $items[$key]['is_private'] = 0 ;
- }
-
- //如果项目已标识为删除
- if ($value['is_del'] == 1) {
- unset($items[$key]);
- }
- //如果有参数指定了只返回原创项目
- if($original > 0 && $value['uid'] != $login_user['uid']){
- unset($items[$key]);
- }
- }
- $items = array_values($items);
- //读取需要置顶的项目
- $top_items = D("ItemTop")->where("uid = '$login_user[uid]'")->select();
- if ($top_items) {
- $top_item_ids = array() ;
- foreach ($top_items as $key => $value) {
- $top_item_ids[] = $value['item_id'];
- }
- foreach ($items as $key => $value) {
- $items[$key]['top'] = 0 ;
- if (in_array($value['item_id'], $top_item_ids) ) {
- $items[$key]['top'] = 1 ;
- $tmp = $items[$key] ;
- unset($items[$key]);
- array_unshift($items,$tmp) ;
- }
- }
- }
- //读取项目顺序
- $item_sort = D("ItemSort")->where("uid = '$login_user[uid]' and item_group_id = '$item_group_id' ")->find();
- if ($item_sort) {
- $item_sort_data = json_decode(htmlspecialchars_decode($item_sort['item_sort_data']) , true) ;
- //var_dump($item_sort_data);
- foreach ($items as $key => &$value) {
- //如果item_id有设置了序号,则赋值序号。没有则默认填上0
- if ($item_sort_data[$value['item_id']]) {
- $value['s_number'] = $item_sort_data[$value['item_id']] ;
- }else{
- $value['s_number'] = 0 ;
- }
- }
- $items = $this->_sort_by_key($items , 's_number' ) ;
- }
- $items = $items ? array_values($items) : array();
- $this->sendResult($items);
- // 埋个点,升级数据库
- R("Update/checkDb" , array(false));
- }
- private function _sort_by_key($array , $mykey){
- for ($i=0; $i < count($array) ; $i++) {
- for ($j = $i + 1 ; $j < count($array) ; $j++) {
- if ($array[$i][$mykey] > $array[$j][$mykey] ) {
- $tmp = $array[$i] ;
- $array[$i] = $array[$j] ;
- $array[$j] = $tmp ;
- }
- }
- }
- return $array;
- }
- //项目详情
- public function detail(){
- $login_user = $this->checkLogin();
- $item_id = I("item_id/d");
- $uid = $login_user['uid'] ;
- if(!$this->checkItemManage($uid , $item_id)){
- $this->sendError(10303);
- return ;
- }
- $items = D("Item")->where("item_id = '$item_id' ")->find();
- $items = $items ? $items : array();
- $this->sendResult($items);
- }
- //更新项目信息
- public function update(){
- $login_user = $this->checkLogin();
- $item_id = I("item_id/d");
- $item_name = I("item_name");
- $item_description = I("item_description");
- $item_domain = I("item_domain");
- $password = I("password");
- $uid = $login_user['uid'] ;
- if(!$this->checkItemManage($uid , $item_id)){
- $this->sendError(10303);
- return ;
- }
- if ($item_domain) {
-
- if(!ctype_alnum($item_domain) || is_numeric($item_domain) ){
- //echo '个性域名只能是字母或数字的组合';exit;
- $this->sendError(10305);
- return false;
- }
- $item = D("Item")->where("item_domain = '%s' and item_id !='%s' ",array($item_domain,$item_id))->find();
- if ($item) {
- //个性域名已经存在
- $this->sendError(10304);
- return false;
- }
- }
- $save_data = array(
- "item_name" => $item_name ,
- "item_description" => $item_description ,
- "item_domain" => $item_domain ,
- "password" => $password ,
- );
- $items = D("Item")->where("item_id = '$item_id' ")->save($save_data);
- $items = $items ? $items : array();
- $this->sendResult($items);
- }
- //转让项目
- public function attorn(){
- $login_user = $this->checkLogin();
- $username = I("username");
- $item_id = I("item_id/d");
- $password = I("password");
- $item = D("Item")->where("item_id = '$item_id' ")->find();
- if(!$this->checkItemManage($login_user['uid'] , $item['item_id'])){
- $this->sendError(10303);
- return ;
- }
- if(! D("User")-> checkLogin($item['username'],$password)){
- $this->sendError(10208);
- return ;
- }
- $member = D("User")->where(" username = '%s' ",array($username))->find();
- if (!$member) {
- $this->sendError(10209);
- return ;
- }
- $data['username'] = $member['username'] ;
- $data['uid'] = $member['uid'] ;
-
- $id = D("Item")->where(" item_id = '$item_id' ")->save($data);
- $return = D("Item")->where("item_id = '$item_id' ")->find();
- if (!$return) {
- $this->sendError(10101);
- }
- $this->sendResult($return);
- }
- //删除项目
- public function delete(){
- $login_user = $this->checkLogin();
- $item_id = I("item_id/d");
- $password = I("password");
- $item = D("Item")->where("item_id = '$item_id' ")->find();
- if(!$this->checkItemManage($login_user['uid'] , $item['item_id'])){
- $this->sendError(10303);
- return ;
- }
- if(! D("User")-> checkLogin($item['username'],$password)){
- $this->sendError(10208);
- return ;
- }
- $return = D("Item")->soft_delete_item($item_id);
- if (!$return) {
- $this->sendError(10101);
- }else{
- }
- $this->sendResult($return);
- }
- //归档项目
- public function archive(){
- $login_user = $this->checkLogin();
- $item_id = I("item_id/d");
- $password = I("password");
- $item = D("Item")->where("item_id = '$item_id' ")->find();
- if(!$this->checkItemManage($login_user['uid'] , $item['item_id'])){
- $this->sendError(10303);
- return ;
- }
- if(! D("User")-> checkLogin($item['username'],$password)){
- $this->sendError(10208);
- return ;
- }
- $return = D("Item")->where("item_id = '$item_id' ")->save(array("is_archived"=>1));
- if (!$return) {
- $this->sendError(10101);
- }else{
- $this->sendResult($return);
- }
-
- }
- public function getKey(){
- $login_user = $this->checkLogin();
- $item_id = I("item_id/d");
- $item = D("Item")->where("item_id = '$item_id' ")->find();
- if(!$this->checkItemManage($login_user['uid'] , $item['item_id'])){
- $this->sendError(10303);
- return ;
- }
- $item_token = D("ItemToken")->getTokenByItemId($item_id);
- if (!$item_token) {
- $this->sendError(10101);
- }
- $this->sendResult($item_token);
- }
- public function resetKey(){
- $login_user = $this->checkLogin();
- $item_id = I("item_id/d");
- $item = D("Item")->where("item_id = '$item_id' ")->find();
- if(!$this->checkItemManage($login_user['uid'] , $item['item_id'])){
- $this->sendError(10303);
- return ;
- }
- $ret = D("ItemToken")->where("item_id = '$item_id' ")->delete();
- if ($ret) {
- $this->getKey();
- }else{
- $this->sendError(10101);
- }
- }
- public function updateByApi(){
- //转到Open控制器的updateItem方法
- R('Open/updateItem');
- }
- //置顶项目
- public function top(){
- $login_user = $this->checkLogin();
- $item_id = I("item_id/d");
- $action = I("action");
- if ($action == 'top') {
- $ret = D("ItemTop")->add(array("item_id"=>$item_id,"uid"=>$login_user['uid'],"addtime"=>time()));
- }
- elseif ($action == 'cancel') {
- $ret = D("ItemTop")->where(" uid = '$login_user[uid]' and item_id = '$item_id' ")->delete();
- }
- if ($ret) {
- $this->sendResult(array());
- }else{
- $this->sendError(10101);
- }
- }
-
- //验证访问密码
- public function pwd(){
- $item_id = I("item_id/d");
- $password = I("password");
- $v_code = I("v_code");
- $refer_url = I('refer_url');
- //检查用户输错密码的次数。如果超过一定次数,则需要验证 验证码
- $key= 'item_pwd_fail_times_'.$item_id;
- if(!D("VerifyCode")->_check_times($key,10)){
- if (!$v_code || $v_code != session('v_code')) {
- $this->sendError(10206,L('verification_code_are_incorrect'));
- return;
- }
- }
- session('v_code',null) ;
- $item = D("Item")->where("item_id = '$item_id' ")->find();
- if ($item['password'] == $password) {
- session("visit_item_".$item_id , 1 );
- $this->sendResult(array("refer_url"=>base64_decode($refer_url)));
- }else{
- D("VerifyCode")->_ins_times($key);//输错密码则设置输错次数
-
- if(D("VerifyCode")->_check_times($key,10)){
- $error_code = 10307 ;
- }else{
- $error_code = 10308 ;
- }
- $this->sendError($error_code,L('access_password_are_incorrect'));
- }
- }
- public function itemList(){
- $login_user = $this->checkLogin();
- $items = D("Item")->where("uid = '$login_user[uid]' ")->select();
- $items = $items ? $items : array();
- $this->sendResult($items);
- }
- //新建项目
- public function add(){
- $login_user = $this->checkLogin();
- $item_name = I("item_name");
- $item_domain = I("item_domain") ? I("item_domain") : '';
- $copy_item_id = I("copy_item_id");
- $password = I("password");
- $item_description = I("item_description");
- $item_type = I("item_type") ? I("item_type") : 1 ;
- if ($item_domain) {
-
- if(!ctype_alnum($item_domain) || is_numeric($item_domain) ){
- //echo '个性域名只能是字母或数字的组合';exit;
- $this->sendError(10305);
- return false;
- }
- $item = D("Item")->where("item_domain = '%s' ",array($item_domain))->find();
- if ($item) {
- //个性域名已经存在
- $this->sendError(10304);
- return false;
- }
- }
-
- //如果是复制项目
- if ($copy_item_id > 0) {
- if (!$this->checkItemEdit($login_user['uid'] , $copy_item_id)) {
- $this->sendError(10103);
- return;
- }
- $item_id = D("Item")->copy($copy_item_id,$login_user['uid'],$item_name,$item_description,$password,$item_domain);
- if ($item_id) {
- $this->sendResult(array("item_id"=>$item_id));
- }else{
- $this->sendError(10101);
- }
- return ;
- }
-
- $insert = array(
- "uid" => $login_user['uid'] ,
- "username" => $login_user['username'] ,
- "item_name" => $item_name ,
- "password" => $password ,
- "item_description" => $item_description ,
- "item_domain" => $item_domain ,
- "item_type" => $item_type ,
- "addtime" =>time()
- );
- $item_id = D("Item")->add($insert);
- if ($item_id) {
- //如果是单页应用,则新建一个默认页
- if ($item_type == 2 ) {
- $insert = array(
- 'author_uid' => $login_user['uid'] ,
- 'author_username' => $login_user['username'],
- "page_title" => $item_name ,
- "item_id" => $item_id ,
- "cat_id" => 0 ,
- "page_content" => '欢迎使用showdoc。点击右上方的编辑按钮进行编辑吧!' ,
- "addtime" =>time()
- );
- $page_id = D("Page")->add($insert);
- }
- //如果是表格应用,则新建一个默认页
- if ($item_type == 4 ) {
- $insert = array(
- 'author_uid' => $login_user['uid'] ,
- 'author_username' => $login_user['username'],
- "page_title" => $item_name ,
- "item_id" => $item_id ,
- "cat_id" => 0 ,
- "page_content" => '' ,
- "addtime" =>time()
- );
- $page_id = D("Page")->add($insert);
- }
- $this->sendResult(array("item_id"=>$item_id));
- }else{
- $this->sendError(10101);
- }
-
- }
- //保存项目排序
- public function sort(){
- $login_user = $this->checkLogin();
- $data = I("data");
- $item_group_id = I("item_group_id/d");
- $res = D("ItemSort")->where(" uid ='$login_user[uid]' and item_group_id = $item_group_id ")->find() ;
- if($res){
- $ret = D("ItemSort")->where(" uid ='$login_user[uid]' and item_group_id = $item_group_id ")->save(array("item_sort_data"=>$data,"addtime"=>time()));
- }else{
- $ret = D("ItemSort")->add(array("item_sort_data"=>$data,"item_group_id"=>$item_group_id,"uid"=>$login_user['uid'],"addtime"=>time()));
- }
- if ($ret) {
- $this->sendResult(array());
- }else{
- $this->sendError(10101);
- }
- }
- public function exitItem(){
- $login_user = $this->checkLogin();
- $item_id = I("item_id/d");
- $ret = D("ItemMember")->where("item_id = '$item_id' and uid ='$login_user[uid]' ")->delete();
- $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();
- if ($row) {
- $ret = D("TeamItemMember")->where(" member_uid = '$login_user[uid]' and team_id = '$row[team_id]' ")->delete();
- $ret = D("TeamMember")->where(" member_uid = '$login_user[uid]' and team_id = '$row[team_id]' ")->delete();
- }
-
- if ($ret) {
- $this->sendResult(array());
- }else{
- $this->sendError(10101);
- }
- }
- // 在某个项目中根据内容搜索
- public function search(){
- $keyword = I("keyword");
- $item_id = I("item_id/d");
- $login_user = $this->checkLogin();
- $uid = $login_user['uid'] ;
- if(!$this->checkItemVisit($uid , $item_id)){
- $this->sendError(10303,"没有权限");
- return ;
- }
- $item = D("Item")->where("item_id = '%d' and is_del = 0 ",array($item_id))->find();
- $keyword = \SQLite3::escapeString($keyword) ;
- $pages = D("Page")->search($item_id,$keyword) ;
- if($pages){
- foreach ($pages as $key => $value) {
- $page_content = htmlspecialchars_decode($value['page_content']) ;
- $pos = mb_strpos($page_content,$keyword) ;
- $len = mb_strlen($keyword) ;
- $start = ( $pos - 100 ) > 0 ? ( $pos - 100 ) : 0 ;
- $pages[$key]['search_content'] = '...'.mb_substr($page_content,$start , ($len + 200 ) ).'...' ;
- unset($pages[$key]['page_content']) ;
- $pages[$key]['item_id'] = $item['item_id'] ;
- $pages[$key]['item_name'] = $item['item_name'] ;
- }
- }
- $return = array(
- "item_id"=> $item_id ,
- "item_name"=> $item['item_name'] ,
- "pages"=>$pages
- );
- $this->sendResult($return);
- }
-
- }
|