ZUser.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. class ZUser extends ZComponent
  3. {
  4. /**
  5. * 返回用户的昵称
  6. */
  7. public function getUserNames($user_ids){
  8. $fields = array('name' => 1);
  9. if (is_array($user_ids)){
  10. $where = array('_id' => array('$in' => $user_ids));
  11. $cursor = RUser::model()->getCollection()->find($where, $fields);
  12. $user_names = array();
  13. foreach ($cursor as $v){
  14. $user_names[(string)$v['_id']] = $v['name'];
  15. }
  16. } else {
  17. $user_names = '';
  18. $where = array('_id' => $user_ids);
  19. $cursor = RUser::model()->getCollection()->findOne($where, $fields);
  20. if ($cursor){
  21. $user_names = $cursor['name'];
  22. }
  23. }
  24. return $user_names;
  25. }
  26. public function getUserNames2($user_ids){
  27. $criteria = new EMongoCriteria();
  28. $criteria->_id('in', $user_ids);
  29. $cursor = RUser::model()->findAll($criteria);
  30. $user_names = array();
  31. $i=0;
  32. foreach ($cursor as $v){
  33. $user_names[$i]['user_name'] = $v->name;
  34. $user_names[$i]['user_id'] = (string)$v->_id;
  35. $i++;
  36. }
  37. return $user_names;
  38. }
  39. /**
  40. * 根据用户ID返回用户
  41. */
  42. public function get($user_id){
  43. $criteria = new EMongoCriteria();
  44. $criteria->_id('==', $user_id);
  45. $user = RUser::model()->find($criteria);
  46. return $user;
  47. }
  48. /**
  49. * 根据用户id返回用户名称
  50. *
  51. * 如果存在用户则返回名称,不存在则返回默认字符串
  52. * @param $user_id 用户id号
  53. * @return string 用户名
  54. */
  55. public function getUserName($user_id) {
  56. $user = $this->get($user_id);
  57. if (isset($user) && $user) {
  58. return $user->name;
  59. }
  60. return '未知用户';
  61. }
  62. /**
  63. * 根据用户name返回用户
  64. */
  65. public function getUserByName($user_name){
  66. $criteria = new EMongoCriteria();
  67. $criteria->user_name('==', $user_name);
  68. $user = RUser::model()->find($criteria);
  69. return $user;
  70. }
  71. /**
  72. * 根据用户email返回用户
  73. */
  74. public function getUserByEmail($email){
  75. $criteria = new EMongoCriteria();
  76. $criteria->email('==', $email);
  77. $user = RUser::model()->find($criteria);
  78. return $user;
  79. }
  80. /**
  81. * 获取用户信息
  82. */
  83. public function getUserInfo($user_id, $fields=array(), $default=''){
  84. if (is_array($user_id)){
  85. $where = array('_id' => array('$in' => $user_id));
  86. $items = $this->getMultipleModelInfo(RUser::model(), $where, $fields);
  87. $user_info = array();
  88. if (is_array($fields) && count($fields) == 1){
  89. $keys = array_keys($fields);
  90. $key = $keys[0];
  91. foreach ($items as $v){
  92. $user_info[] = $v[$key];
  93. }
  94. } else {
  95. foreach ($items as $v){
  96. $user_info[] = $v;
  97. }
  98. }
  99. } else {
  100. //var_dump(RUser::model());exit;
  101. $where = array('_id' => $user_id);
  102. $user_info = $this->getSingleModelInfo(RUser::model(), $where, $fields, $default);
  103. }
  104. return $user_info;
  105. }
  106. /**
  107. * 检查用户id的数组是否合法
  108. */
  109. public function checkUsers($para,&$ret){
  110. $result = true;
  111. $users = array();
  112. if(is_string($para)){
  113. if($para != ''){
  114. $users = explode(",",$para);
  115. }
  116. }elseif(is_array($para)){
  117. $users = $para;
  118. }else{
  119. $result = false;
  120. return $result;
  121. }
  122. if(!empty($users)){
  123. foreach($users as $user){
  124. if(count($user) != 1){
  125. $result = false;
  126. }else{
  127. if(!CommonFn::isMongoId($user)){
  128. $result = false;
  129. }else{
  130. $_user = $this->get(new MongoId($user));
  131. if(empty($_user)){
  132. $result = false;
  133. }else{
  134. $ret[] = new MongoId($user);
  135. }
  136. }
  137. }
  138. }
  139. }
  140. return $result;
  141. }
  142. public function validate_user_name(&$user_name){
  143. $user_name = preg_replace(array('/\s/','/@/','/^/'),'',$user_name);
  144. $ban_words = Yii::app()->params['user_name_ban_words'];
  145. preg_match_all("/$ban_words/i",$user_name,$matchs);
  146. if(isset($matchs[0])){
  147. foreach ($matchs[0] as $value) {
  148. $user_name = str_replace($value,str_repeat('*',mb_strlen($value,'utf-8')),$user_name);
  149. }
  150. }
  151. if(mb_strlen($user_name, 'utf-8') < 1 ){
  152. $user_name = 'wz_'.dechex(time());
  153. return true;
  154. }
  155. }
  156. }