User.class.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. $ids = $this->objDUserInfo -> findIdsBy(array());
  67. $userData = $this->objDUserInfo -> gets($ids);
  68. if ($userData) {
  69. foreach ($userData as $k => $v) {
  70. if (empty($v['headimgurl']) || (isset($v['headimgurl']) && !strstr($v['headimgurl'], 'https://'))) {
  71. $result['headimgurl'] = $this->getAvatar();
  72. $this->objDUserInfo->modify (array('headimgurl' => $result['headimgurl']) , array (
  73. 'id' => $v['id']
  74. ) );
  75. }
  76. }
  77. }
  78. }
  79. /**
  80. * 初始化一个新用户 返回ID
  81. */
  82. public function initUser() {
  83. $info = array (
  84. 'is_new' => UserInfo::NEW_USER ,
  85. 'passport_uid' => '-1',
  86. 'headimgurl' => $this->getAvatar(),
  87. );
  88. return $this->objDUserInfo->add ( $info );
  89. }
  90. /**
  91. * 绑定一个新用户
  92. */
  93. public function bindingUser($info, $uid) {
  94. $info ['is_new'] = UserInfo::OLD_USER;
  95. return $this->objDUserInfo->modify ( $info, array (
  96. 'id' => $uid
  97. ) );
  98. }
  99. /**
  100. * 通过本平台的UID 获取用户信息
  101. */
  102. public function get($uid) {
  103. $result = array ();
  104. if (! Verify::unsignedInt ( $uid )) {
  105. return $result;
  106. }
  107. $result = $this->objDUserInfo->gets ( array (
  108. $uid
  109. ) );
  110. if (! $result) {
  111. return array ();
  112. }
  113. $result = array_pop ( $result );
  114. if (empty($result['headimgurl'])) {
  115. $result['headimgurl'] = $this->getAvatar();
  116. $this->objDUserInfo->modify (array('headimgurl' => $result['headimgurl']) , array (
  117. 'id' => $result['id']
  118. ));
  119. }
  120. return $result;
  121. }
  122. /**
  123. * 通过本平台的UIDS 批量获取用户信息
  124. */
  125. public function gets(array $uids) {
  126. $result = array ();
  127. if (! $uids) {
  128. return $result;
  129. }
  130. $result = $this->objDUserInfo->gets ( $uids );
  131. return $result;
  132. }
  133. }