BaseController.class.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. class BaseController extends Controller {
  5. public function message($msg , $redirect = ''){
  6. $this->assign("msg" , $msg);
  7. $this->assign("redirect" , $redirect);
  8. $this->display ("Common/message");
  9. }
  10. public function checkLogin($redirect = true){
  11. if (strtolower(C("DB_TYPE")) == 'mysql' ) {
  12. echo 'ShowDoc does not support mysql any more . http://api.pkbigdata.com/help?page_id=31990 ';
  13. clear_runtime();
  14. exit();
  15. }
  16. if ( ! session("login_user")) {
  17. $cookie_token = cookie('cookie_token');
  18. if ($cookie_token) {
  19. $ret = D("UserToken")->getToken($cookie_token);
  20. if ($ret && $ret['token_expire'] > time() ) {
  21. $login_user = D("User")->where("uid = $ret[uid]")->find();
  22. unset($ret['password']);
  23. session("login_user" , $login_user);
  24. return $login_user ;
  25. }
  26. }
  27. if ($redirect) {
  28. $this->message("你尚未登录!",U('Home/User/login'));
  29. exit();
  30. }
  31. }else{
  32. return session("login_user") ;
  33. }
  34. }
  35. /**
  36. * 返回json数据
  37. */
  38. public function sendResult($array){
  39. if (isset($array['error_code'])) {
  40. $result['error_code'] = $array['error_code'] ;
  41. $result['error_message'] = $array['error_message'] ;
  42. }
  43. else{
  44. $result['error_code'] = 0 ;
  45. $result['data'] = $array ;
  46. }
  47. echo json_encode($result);
  48. }
  49. //判断某用户是否有项目管理权限(项目成员member_group_id为1,以及 项目创建者)
  50. protected function checkItemPermn($uid , $item_id){
  51. if (!$uid) {
  52. return false;
  53. }
  54. if (session("mamage_item_".$item_id)) {
  55. return true;
  56. }
  57. $item = D("Item")->where("item_id = '%d' ",array($item_id))->find();
  58. if ($item['uid'] && $item['uid'] == $uid) {
  59. session("mamage_item_".$item_id , 1 );
  60. return true;
  61. }
  62. $ItemMember = D("ItemMember")->where("item_id = '%d' and uid = '%d' and member_group_id = 1 ",array($item_id,$uid))->find();
  63. if ($ItemMember) {
  64. session("mamage_item_".$item_id , 1 );
  65. return true;
  66. }
  67. return false;
  68. }
  69. //判断某用户是否为项目创建者
  70. protected function checkItemCreator($uid , $item_id){
  71. if (!$uid) {
  72. return false;
  73. }
  74. if (session("creat_item_".$item_id)) {
  75. return true;
  76. }
  77. $item = D("Item")->where("item_id = '%d' ",array($item_id))->find();
  78. if ($item['uid'] && $item['uid'] == $uid) {
  79. session("creat_item_".$item_id , 1 );
  80. return true;
  81. }
  82. return false;
  83. }
  84. //判断某用户是否有项目访问权限(公开项目的话所有人可访问,私有项目则项目成员、项目创建者和访问密码输入者可访问)
  85. protected function checkItemVisit($uid , $item_id, $refer_url= ''){
  86. if (session("visit_item_".$item_id)) {
  87. return true;
  88. }
  89. if ($this->checkItemCreator($uid , $item_id)) {
  90. session("visit_item_".$item_id , 1 );
  91. return true;
  92. }
  93. $ItemMember = D("ItemMember")->where("item_id = '%d' and uid = '%d' ",array($item_id,$uid))->find();
  94. if ($ItemMember) {
  95. session("visit_item_".$item_id , 1 );
  96. return true;
  97. }
  98. $item = D("Item")->where("item_id = '%d' ",array($item_id))->find();
  99. if ($item['password']) {
  100. //跳转到输入访问密码框
  101. header("location:".U("Home/item/pwd",array("item_id"=>$item_id,"refer_url"=>base64_encode($refer_url))));
  102. }else{
  103. session("visit_item_".$item_id , 1 );
  104. return true;
  105. }
  106. }
  107. }