ItemController.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class ItemController extends BaseController {
  5. //我的项目列表
  6. public function myList(){
  7. $login_user = $this->checkLogin();
  8. $items = D("Item")->field("item_id,item_name,last_update_time,item_description")->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();
  9. //读取需要置顶的项目
  10. $top_items = D("ItemTop")->where("uid = '$login_user[uid]'")->select();
  11. if ($top_items) {
  12. $top_item_ids = array() ;
  13. foreach ($top_items as $key => $value) {
  14. $top_item_ids[] = $value['item_id'];
  15. }
  16. foreach ($items as $key => $value) {
  17. $items[$key]['top'] = 0 ;
  18. if (in_array($value['item_id'], $top_item_ids) ) {
  19. $items[$key]['top'] = 1 ;
  20. $tmp = $items[$key] ;
  21. unset($items[$key]);
  22. array_unshift($items,$tmp) ;
  23. }
  24. }
  25. $items = array_values($items);
  26. }
  27. $items = $items ? $items : array();
  28. $this->sendResult($items);
  29. }
  30. //项目详情
  31. public function detail(){
  32. $login_user = $this->checkLogin();
  33. $item_id = I("item_id/d");
  34. $uid = $login_user['uid'] ;
  35. if(!$this->checkItemCreator($uid , $item_id)){
  36. $this->sendError(10303);
  37. return ;
  38. }
  39. $items = D("Item")->where("item_id = '$item_id' ")->find();
  40. $items = $items ? $items : array();
  41. $this->sendResult($items);
  42. }
  43. //更新项目信息
  44. public function update(){
  45. $login_user = $this->checkLogin();
  46. $item_id = I("item_id/d");
  47. $item_name = I("item_name");
  48. $item_description = I("item_description");
  49. $item_domain = I("item_domain");
  50. $password = I("password");
  51. $uid = $login_user['uid'] ;
  52. if(!$this->checkItemCreator($uid , $item_id)){
  53. $this->sendError(10303);
  54. return ;
  55. }
  56. if ($item_domain) {
  57. if(!ctype_alnum($item_domain) || is_numeric($item_domain) ){
  58. //echo '个性域名只能是字母或数字的组合';exit;
  59. $this->sendError(10305);
  60. return false;
  61. }
  62. $item = D("Item")->where("item_domain = '%s' and item_id !='%s' ",array($item_domain,$item_id))->find();
  63. if ($item) {
  64. //个性域名已经存在
  65. $this->sendError(10304);
  66. return false;
  67. }
  68. }
  69. $save_data = array(
  70. "item_name" => $item_name ,
  71. "item_description" => $item_description ,
  72. "item_domain" => $item_domain ,
  73. "password" => $password ,
  74. );
  75. $items = D("Item")->where("item_id = '$item_id' ")->save($save_data);
  76. $items = $items ? $items : array();
  77. $this->sendResult($items);
  78. }
  79. //转让项目
  80. public function attorn(){
  81. $login_user = $this->checkLogin();
  82. $username = I("username");
  83. $item_id = I("item_id/d");
  84. $password = I("password");
  85. $item = D("Item")->where("item_id = '$item_id' ")->find();
  86. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  87. $this->sendError(10303);
  88. return ;
  89. }
  90. if(! D("User")-> checkLogin($item['username'],$password)){
  91. $this->sendError(10208);
  92. return ;
  93. }
  94. $member = D("User")->where(" username = '%s' ",array($username))->find();
  95. if (!$member) {
  96. $this->sendError(10209);
  97. return ;
  98. }
  99. $data['username'] = $member['username'] ;
  100. $data['uid'] = $member['uid'] ;
  101. $id = D("Item")->where(" item_id = '$item_id' ")->save($data);
  102. $return = D("Item")->where("item_id = '$item_id' ")->find();
  103. if (!$return) {
  104. $this->sendError(10101);
  105. }
  106. $this->sendResult($return);
  107. }
  108. //删除项目
  109. public function delete(){
  110. $login_user = $this->checkLogin();
  111. $item_id = I("item_id/d");
  112. $password = I("password");
  113. $item = D("Item")->where("item_id = '$item_id' ")->find();
  114. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  115. $this->sendError(10303);
  116. return ;
  117. }
  118. if(! D("User")-> checkLogin($item['username'],$password)){
  119. $this->sendError(10208);
  120. return ;
  121. }
  122. D("Page")->where("item_id = '$item_id' ")->delete();
  123. D("Catalog")->where("item_id = '$item_id' ")->delete();
  124. D("PageHistory")->where("item_id = '$item_id' ")->delete();
  125. D("ItemMember")->where("item_id = '$item_id' ")->delete();
  126. $return = D("Item")->where("item_id = '$item_id' ")->delete();
  127. if (!$return) {
  128. $this->sendError(10101);
  129. }else{
  130. }
  131. $this->sendResult($return);
  132. }
  133. //归档项目
  134. public function archive(){
  135. $login_user = $this->checkLogin();
  136. $item_id = I("item_id/d");
  137. $password = I("password");
  138. $item = D("Item")->where("item_id = '$item_id' ")->find();
  139. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  140. $this->sendError(10303);
  141. return ;
  142. }
  143. if(! D("User")-> checkLogin($item['username'],$password)){
  144. $this->sendError(10208);
  145. return ;
  146. }
  147. $return = D("Item")->where("item_id = '$item_id' ")->save(array("is_archived"=>1));
  148. if (!$return) {
  149. $this->sendError(10101);
  150. }else{
  151. $this->sendResult($return);
  152. }
  153. }
  154. public function getKey(){
  155. $login_user = $this->checkLogin();
  156. $item_id = I("item_id/d");
  157. $item = D("Item")->where("item_id = '$item_id' ")->find();
  158. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  159. $this->sendError(10303);
  160. return ;
  161. }
  162. $item_token = D("ItemToken")->getTokenByItemId($item_id);
  163. if (!$item_token) {
  164. $this->sendError(10101);
  165. }
  166. $this->sendResult($item_token);
  167. }
  168. public function resetKey(){
  169. $login_user = $this->checkLogin();
  170. $item_id = I("item_id/d");
  171. $item = D("Item")->where("item_id = '$item_id' ")->find();
  172. if(!$this->checkItemCreator($login_user['uid'] , $item['item_id'])){
  173. $this->sendError(10303);
  174. return ;
  175. }
  176. $ret = D("ItemToken")->where("item_id = '$item_id' ")->delete();
  177. if ($ret) {
  178. $this->getKey();
  179. }else{
  180. $this->sendError(10101);
  181. }
  182. }
  183. public function updateByApi(){
  184. $api_key = I("api_key");
  185. $api_token = I("api_token");
  186. $cat_name = I("cat_name");
  187. $cat_name_sub = I("cat_name_sub");
  188. $page_title = I("page_title");
  189. $page_content = I("page_content");
  190. $s_number = I("s_number") ? I("s_number") : 99;
  191. $ret = D("ItemToken")->getTokenByKey($api_key);
  192. if ($ret && $ret['api_token'] == $api_token) {
  193. $item_id = $ret['item_id'] ;
  194. D("ItemToken")->setLastTime($item_id);
  195. }else{
  196. $this->sendError(10306);
  197. return false;
  198. }
  199. //如果传送了二级目录
  200. if ($cat_name) {
  201. $cat_name_array = D("Catalog")->where(" item_id = '$item_id' and level = 2 and cat_name = '%s' ",array($cat_name))->find();
  202. //如果不存在则新建
  203. if (!$cat_name_array) {
  204. $add_data = array(
  205. "cat_name" => $cat_name,
  206. "item_id" => $item_id,
  207. "addtime" => time(),
  208. "level" => 2
  209. );
  210. D("Catalog")->add($add_data);
  211. $cat_name_array = D("Catalog")->where(" item_id = '$item_id' and level = 2 and cat_name = '%s' ",array($cat_name))->find();
  212. }
  213. }
  214. //如果传送了三级目录
  215. if ($cat_name_sub) {
  216. $cat_name_sub_array = D("Catalog")->where(" item_id = '$item_id' and level = 3 and cat_name = '%s' ",array($cat_name_sub))->find();
  217. //如果不存在则新建
  218. if (!$cat_name_sub_array) {
  219. $add_data = array(
  220. "cat_name" => $cat_name_sub,
  221. "item_id" => $item_id,
  222. "parent_cat_id" => $cat_name_array['cat_id'],
  223. "addtime" => time(),
  224. "level" => 3
  225. );
  226. D("Catalog")->add($add_data);
  227. $cat_name_sub_array = D("Catalog")->where(" item_id = '$item_id' and level = 3 and cat_name = '%s' ",array($cat_name_sub))->find();
  228. }
  229. }
  230. //目录id
  231. $cat_id = 0 ;
  232. if ($cat_name_array && $cat_name_array['cat_id'] > 0 ) {
  233. $cat_id = $cat_name_array['cat_id'] ;
  234. }
  235. if ($cat_name_sub_array && $cat_name_sub_array['cat_id'] > 0 ) {
  236. $cat_id = $cat_name_sub_array['cat_id'] ;
  237. }
  238. if ($page_content) {
  239. $page_array = D("Page")->where(" item_id = '$item_id' and cat_id = '$cat_id' and page_title ='%s' ",array($page_title))->find();
  240. //如果不存在则新建
  241. if (!$page_array) {
  242. $add_data = array(
  243. "author_username" => "from_api",
  244. "item_id" => $item_id,
  245. "cat_id" => $cat_id,
  246. "page_title" => $page_title,
  247. "page_content" => $page_content,
  248. "s_number" => $s_number,
  249. "addtime" => time(),
  250. );
  251. $page_id = D("Page")->add($add_data);
  252. }else{
  253. $page_id = $page_array['page_id'] ;
  254. $update_data = array(
  255. "author_username" => "from_api",
  256. "item_id" => $item_id,
  257. "cat_id" => $cat_id,
  258. "page_title" => $page_title,
  259. "page_content" => $page_content,
  260. "s_number" => $s_number,
  261. );
  262. D("Page")->where(" page_id = '$page_id' ")->save($update_data);
  263. }
  264. }
  265. if ($page_id) {
  266. $ret = D("Page")->where(" page_id = '$page_id' ")->find();
  267. $this->sendResult($ret);
  268. }else{
  269. $this->sendError(10101);
  270. }
  271. }
  272. //置顶项目
  273. public function top(){
  274. $login_user = $this->checkLogin();
  275. $item_id = I("item_id/d");
  276. $action = I("action");
  277. if ($action == 'top') {
  278. $ret = D("ItemTop")->add(array("item_id"=>$item_id,"uid"=>$login_user['uid'],"addtime"=>time()));
  279. }
  280. elseif ($action == 'cancel') {
  281. $ret = D("ItemTop")->where(" uid = '$login_user[uid]' and item_id = '$item_id' ")->delete();
  282. }
  283. if ($ret) {
  284. $this->sendResult(array());
  285. }else{
  286. $this->sendError(10101);
  287. }
  288. }
  289. //验证访问密码
  290. public function pwd(){
  291. $item_id = I("item_id/d");
  292. $password = I("password");
  293. $v_code = I("v_code");
  294. $refer_url = I('refer_url');
  295. //检查用户输错密码的次数。如果超过一定次数,则需要验证 验证码
  296. $key= 'item_pwd_fail_times_'.$item_id;
  297. if(!D("VerifyCode")->_check_times($key,10)){
  298. if (!$v_code || $v_code != session('v_code')) {
  299. $this->sendError(10206,L('verification_code_are_incorrect'));
  300. return;
  301. }
  302. }
  303. $item = D("Item")->where("item_id = '$item_id' ")->find();
  304. if ($item['password'] == $password) {
  305. session("visit_item_".$item_id , 1 );
  306. $this->sendResult(array("refer_url"=>base64_decode($refer_url)));
  307. }else{
  308. D("VerifyCode")->_ins_times($key);//输错密码则设置输错次数
  309. if(D("VerifyCode")->_check_times($key,10)){
  310. $error_code = 10307 ;
  311. }else{
  312. $error_code = 10308 ;
  313. }
  314. $this->sendError($error_code,L('access_password_are_incorrect'));
  315. }
  316. }
  317. public function itemList(){
  318. $login_user = $this->checkLogin();
  319. $items = D("Item")->where("uid = '$login_user[uid]' ")->select();
  320. $items = $items ? $items : array();
  321. $this->sendResult($items);
  322. }
  323. //新建项目
  324. public function add(){
  325. $login_user = $this->checkLogin();
  326. $item_name = I("item_name");
  327. $item_domain = I("item_domain") ? I("item_domain") : '';
  328. $copy_item_id = I("copy_item_id");
  329. $password = I("password");
  330. $item_description = I("item_description");
  331. $item_type = I("item_type");
  332. if ($item_domain) {
  333. if(!ctype_alnum($item_domain) || is_numeric($item_domain) ){
  334. //echo '个性域名只能是字母或数字的组合';exit;
  335. $this->sendError(10305);
  336. return false;
  337. }
  338. $item = D("Item")->where("item_domain = '%s' ",array($item_domain))->find();
  339. if ($item) {
  340. //个性域名已经存在
  341. $this->sendError(10304);
  342. return false;
  343. }
  344. }
  345. //如果是复制项目
  346. if ($copy_item_id > 0) {
  347. if (!$this->checkItemPermn($login_user['uid'] , $copy_item_id)) {
  348. $this->sendError(10103);
  349. return;
  350. }
  351. $ret = D("Item")->copy($copy_item_id,$login_user['uid'],$item_name,$item_description,$password,$item_domain);
  352. if ($ret) {
  353. $this->sendResult(array());
  354. }else{
  355. $this->sendError(10101);
  356. }
  357. return ;
  358. }
  359. $insert = array(
  360. "uid" => $login_user['uid'] ,
  361. "username" => $login_user['username'] ,
  362. "item_name" => $item_name ,
  363. "password" => $password ,
  364. "item_description" => $item_description ,
  365. "item_domain" => $item_domain ,
  366. "item_type" => $item_type ,
  367. "addtime" =>time()
  368. );
  369. $item_id = D("Item")->add($insert);
  370. if ($item_id) {
  371. //如果是单页应用,则新建一个默认页
  372. if ($item_type == 2 ) {
  373. $insert = array(
  374. 'author_uid' => $login_user['uid'] ,
  375. 'author_username' => $login_user['username'],
  376. "page_title" => $item_name ,
  377. "item_id" => $item_id ,
  378. "cat_id" => 0 ,
  379. "page_content" => '欢迎使用showdoc。点击右上方的编辑按钮进行编辑吧!' ,
  380. "addtime" =>time()
  381. );
  382. $page_id = D("Page")->add($insert);
  383. }
  384. $this->sendResult(array());
  385. }else{
  386. $this->sendError(10101);
  387. }
  388. }
  389. }