User.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace Cas\Module;
  3. use Cas\Dao\UserInfo;
  4. use KIF\Verify;
  5. use KIF\Cookie;
  6. use KIF\Core\Request;
  7. use KIF\Core\Config;
  8. /**
  9. *
  10. * CAS 用户模型
  11. *
  12. * @author lihuanchun@qq.com
  13. */
  14. class User {
  15. private $objDUserInfo;
  16. public function __construct() {
  17. $this->objDUserInfo = new UserInfo ();
  18. }
  19. /**
  20. * 页面中调用获取 当前用户信息的方法
  21. */
  22. public function getRunTimeUserData() {
  23. $JsonUserData = Cookie::get ( 'CAS_USER_DATA' );
  24. $objDUser = new User ();
  25. if (empty ( $JsonUserData )) {
  26. $info = array ();
  27. $uid = $objDUser->initUser ( $info );
  28. } else {
  29. $userData = json_decode ( $userData );
  30. if ($userData ['key'] != Math::md5_16 ( $userData ['id'] )) {
  31. return false;
  32. }
  33. $uid = $userData ['id'];
  34. }
  35. $userData = $objDUser->get ( $uid );
  36. $userData ['key'] = Math::md5_16 ( $uid );
  37. $userData = json_decode ( $userData );
  38. Cooke::set ( 'CAS_USER_DATA', $userData, 60 * 60 * 24 * 20 ,'.'.$_SERVER['HTTP_HOST']);
  39. return $userData;
  40. }
  41. /**
  42. * 通过$passport_uid 获取用户信息
  43. */
  44. public function getUserDataPasspord($passport_uid){
  45. $condition = array(
  46. 'passport_uid' => $passport_uid
  47. );
  48. $ids = $this->objDUserInfo -> findIdsBy($condition,1);
  49. $userData = $this->objDUserInfo -> gets($ids);
  50. $userData = array_pop($userData);
  51. return $userData;
  52. }
  53. /**
  54. * 获取一个随机头像
  55. */
  56. public function getAvatar() {
  57. $num = 16;
  58. $config = Config::getInstance ()->get ( 'web_cfg' );
  59. $file = $config['cdn'] . '/public/avatar/'.rand(1,$num).'.jpg';
  60. return $file;
  61. }
  62. /**
  63. * 为当前用户分配一个头像
  64. */
  65. public function setAvatar() {
  66. $total = 1000;
  67. $limit = 0;
  68. $state = true;
  69. while($state) {
  70. $state = $this->setAvatarLimit($limit, $total);
  71. $limit = $limit + $total;
  72. }
  73. }
  74. public function setAvatarLimit($limit, $total)
  75. {
  76. $limit = $limit . ',' . $total;
  77. $ids = $this->objDUserInfo -> findIdsBy(array(), $limit);
  78. if (!$ids) {
  79. return false;
  80. }
  81. $userData = $this->objDUserInfo -> gets($ids);
  82. if ($userData) {
  83. foreach ($userData as $k => $v) {
  84. if (empty($v['headimgurl']) || (isset($v['headimgurl']) && !strstr($v['headimgurl'], 'https://'))) {
  85. $result['headimgurl'] = $this->getAvatar();
  86. $this->objDUserInfo->modify (array('headimgurl' => $result['headimgurl']) , array (
  87. 'id' => $v['id']
  88. ) );
  89. }
  90. }
  91. }
  92. return true;
  93. }
  94. /**
  95. * 初始化一个新用户 返回ID
  96. */
  97. public function initUser() {
  98. $info = array (
  99. 'is_new' => UserInfo::NEW_USER ,
  100. 'passport_uid' => '-1',
  101. 'headimgurl' => $this->getAvatar(),
  102. );
  103. return $this->objDUserInfo->add ( $info );
  104. }
  105. /**
  106. * 绑定一个新用户
  107. */
  108. public function bindingUser($info, $uid) {
  109. $info ['is_new'] = UserInfo::OLD_USER;
  110. return $this->objDUserInfo->modify ( $info, array (
  111. 'id' => $uid
  112. ) );
  113. }
  114. /**
  115. * 通过本平台的UID 获取用户信息
  116. */
  117. public function get($uid) {
  118. $result = array ();
  119. if (! Verify::unsignedInt ( $uid )) {
  120. return $result;
  121. }
  122. $result = $this->objDUserInfo->gets ( array (
  123. $uid
  124. ) );
  125. if (! $result) {
  126. return array ();
  127. }
  128. $result = array_pop ( $result );
  129. if (empty($result['headimgurl'])) {
  130. $result['headimgurl'] = $this->getAvatar();
  131. $this->objDUserInfo->modify (array('headimgurl' => $result['headimgurl']) , array (
  132. 'id' => $result['id']
  133. ));
  134. }
  135. return $result;
  136. }
  137. /**
  138. * 通过本平台的UIDS 批量获取用户信息
  139. */
  140. public function gets(array $uids) {
  141. $result = array ();
  142. if (! $uids) {
  143. return $result;
  144. }
  145. $result = $this->objDUserInfo->gets ( $uids );
  146. return $result;
  147. }
  148. }