WXController.class.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace KIF\Core;
  3. use Cas\Module\Passport;
  4. use KIF\Data\ResultWrapper;
  5. use KIF\String\String;
  6. /**
  7. *
  8. * 继承自Controller,封装了微信用户登录状态、用户信息等一些方法
  9. * @author lishumingoo@gmail.com
  10. */
  11. class WXController extends \KIF\Core\Controller {
  12. /**
  13. *
  14. * 判断是否登录
  15. * @return boolean
  16. */
  17. public function isLogin() {
  18. $user = self::getUser();
  19. if (!$user) {
  20. return false;
  21. }
  22. if ($user['loginType'] == Passport::LOGIN_TYPE_NOT_AUTH) {
  23. return false;
  24. }
  25. return true;
  26. }
  27. /**
  28. *
  29. * 获取用户id
  30. * @return boolean | int
  31. */
  32. public function getUid() {
  33. $user = $this->getUser();
  34. if (!$user) {
  35. return false;
  36. }
  37. return $user['uid'];
  38. }
  39. /**
  40. *
  41. * 获取用户登陆类型
  42. * 授权、未授权登陆
  43. * @return boolean | int Passport::LOGIN_TYPE_NOT_AUTH Passport::LOGIN_TYPE_HAS_AUTH
  44. */
  45. public function getLoginType() {
  46. $user = $this->getUser();
  47. if (!$user) {
  48. return false;
  49. }
  50. return $user['loginType'];
  51. }
  52. /**
  53. *
  54. * 获取登陆用户openid
  55. * @return boolean | string
  56. */
  57. public function getOpenid() {
  58. $user = $this->getUser();
  59. if (!$user) {
  60. return false;
  61. }
  62. return $user['openid'];
  63. }
  64. /**
  65. *
  66. * 获取用户微信昵称
  67. * @return boolean | string
  68. */
  69. public function getNickname() {
  70. $user = $this->getUser();
  71. if (!$user) {
  72. return false;
  73. }
  74. return $user['nickname'];
  75. }
  76. /**
  77. *
  78. * 获取用户微信头像
  79. * @return boolean | string
  80. */
  81. public function getHeadimgurl() {
  82. $user = $this->getUser();
  83. if (!$user) {
  84. return false;
  85. }
  86. return $user['headimgurl'];
  87. }
  88. /**
  89. *
  90. * 从Cookie中获取用户的登陆信息
  91. * @return boolean | array
  92. */
  93. public function getUser() {
  94. static $user = null;
  95. if ($user) {
  96. return $user;
  97. }
  98. $objPassport = new Passport();
  99. $tmpResult = $objPassport->getLoginInfo();
  100. if ($tmpResult->isSuccess()) {
  101. $user = $tmpResult->getData();
  102. }
  103. return $user;
  104. }
  105. /**
  106. * 判断登陆状态是否满足活动要求,满足时返回true,不满足返回授权地址
  107. * @param Boolean $isAuth 是否需要授权。true时需要loginType = Passport::LOGIN_TYPE_HAS_AUTH; false时loginType至少属于Passport::LOGIN_TYPE_NOT_AUTH;
  108. * @return ResultWrapper success 满足条件; fail 不满足条件,需要通过返回的授权url进行授权
  109. */
  110. public function checkLoginStatus($isAuth = false, $from) {
  111. $authUrl = Request::schemeDomain() . '/connect/';
  112. if (!$isAuth) {
  113. if (!self::getUser()) {
  114. $state = 'base';
  115. }
  116. } else {
  117. if (!self::isLogin()) {
  118. $state = 'userinfo';
  119. }
  120. }
  121. if ($state) {
  122. return ResultWrapper::fail(String::jointUrl($authUrl, array('from' => $from, 'state' => $state)));
  123. }
  124. return ResultWrapper::success();
  125. }
  126. public function __construct() {
  127. if (self::isLogin()) {
  128. define('IS_LOGIN', 1);
  129. define('UID', self::getUid());
  130. define('NICKNAME', self::getNickname());
  131. define('AVATAR', self::getHeadimgurl());
  132. } else {
  133. // $tmpResult = self::checkLoginStatus(false, Request::url());
  134. // if (!$tmpResult->isSuccess()) {
  135. // self::redirect($tmpResult->getData());
  136. // }
  137. define('IS_LOGIN', 0);
  138. }
  139. }
  140. public function run() {
  141. $action = $this->action;
  142. $this->$action();
  143. }
  144. }