User.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace app\user\model;
  3. use think\Model;
  4. use think\Db;
  5. use think\Validate;
  6. use think\Request;
  7. use think\db\Query;
  8. /**
  9. * 用户模型类
  10. * @author Superbee
  11. *
  12. */
  13. class User extends Model{
  14. protected $insert = ['last_login', 'short', 'registertime'];
  15. protected $update = ['last_login', 'last_ip'];
  16. public function initialize(){
  17. parent::initialize();
  18. }
  19. /* 自动填充设置 begin */
  20. protected function setShortAttr(){
  21. return shortCode($this->data['uid']);
  22. }
  23. protected function setLastIpAttr() {
  24. return request()->ip();
  25. }
  26. protected function setLastLoginAttr(){
  27. return getCurrentTime();
  28. }
  29. protected function setRegisterTimeAttr(){
  30. return getCurrentTime();
  31. }
  32. /* 自动填充设置 end */
  33. /**
  34. * 获取数据信息
  35. * @param int $page 页码
  36. * @param array $where 查询条件
  37. */
  38. public function getInfo($page = 0, $where = array()){
  39. return $this->db()
  40. ->where($where)
  41. ->page($page, config('paginate.list_rows'))
  42. ->select();
  43. }
  44. /**
  45. * 获取分页信息
  46. * @param array $where 查询条件
  47. */
  48. public function getPage($where = array()){
  49. $total = $this->db()->count();
  50. $paginate = $this->db()->where($where)->paginate(config('paginate.list_rows'), $total);
  51. return $paginate->render();
  52. }
  53. /**
  54. * 根据id找到对象
  55. * @param int $id
  56. */
  57. public function getUserById($id){
  58. return $this->db()->where(['id'=>$id])->find();
  59. }
  60. /**
  61. * 根据短码找到对象
  62. * @param string $code
  63. * @return array
  64. */
  65. public function getUserByCode($code){
  66. return $this->db()->where(['short'=>$code])->find();
  67. }
  68. /**
  69. * 添加和更新的方法
  70. */
  71. public function addAndSave($data){
  72. return $this->allowField(true)->isUpdate(isset($data['id']) && $data['id']?true:false)->save($data);
  73. }
  74. /**
  75. * 用户登录
  76. * @param string $uid
  77. * @return Ambigous <i:id, c:int, d:int>
  78. */
  79. public function loginByUid($uid){
  80. $this->data['uid'] = $uid;
  81. $info = $this->db()->field('id i, diamond d, coin c, short o, praise p, buddy b')->where(array("uid"=>$uid))->find();
  82. if(empty($info)) {
  83. $info['d'] = 0;
  84. $info['c'] = 0;
  85. $info['p'] = 0;
  86. $info['b'] = 25;
  87. $info['i'] = $this->createUser($uid);
  88. $info['o'] = $this->data['short'];
  89. }
  90. $this->isUpdate(true)->save(['id'=>$info['i']]);
  91. return $info;
  92. }
  93. /**
  94. * 随机出非个人的短码
  95. * @param int $id
  96. */
  97. public function randOtherInfoByShort($short){
  98. $info = $this->db()->field('id, short')
  99. ->where("short", "<>", $short)
  100. ->whereTime('last_login', 'between', $this->getNearMonth())
  101. ->select();
  102. return $this->getUserJsonFile($info);
  103. }
  104. /**
  105. * 随机出非个人的Id
  106. * @param int $id
  107. * @return int
  108. */
  109. public function randOtherInfoById($id){
  110. $info = $this->db()->field('id, short')
  111. ->where("id", "<>", $id)
  112. ->whereTime('last_login', 'between', $this->getNearMonth())
  113. ->select();
  114. return $this->getUserJsonFile($info);
  115. }
  116. /**
  117. * 得到最近30天日期
  118. * @return array
  119. */
  120. private function getNearMonth(){
  121. $back = date('Y-m-d H:i:s');
  122. $front = date('Y-m-d H:i:s',strtotime("-30 day"));
  123. return [$front, $back];
  124. }
  125. /**
  126. * 找到文件路径
  127. * @param array $info
  128. * @param string $id
  129. * @param string $short
  130. * @return string
  131. */
  132. private function getUserJsonFile($info, $id = 'id', $short = 'short'){
  133. $r = rand(0, count($info)-1);
  134. $file_id = $info[$r][$id];
  135. $file_short = $info[$r][$short];
  136. $path_id = "load/$file_id.txt";
  137. $path_short = "load/$file_short.txt";
  138. if(is_file($path_id)){
  139. return $file_id;
  140. } elseif (is_file($path_short)){
  141. return $file_short;
  142. } else {
  143. unset($info[$r]);
  144. return $this->getUserJsonFile(array_values($info));
  145. }
  146. }
  147. /**
  148. * 创建用户
  149. * @param string $uid
  150. * @return 返回用户数据库id
  151. */
  152. public function createUser($uid){
  153. $data = ['id'=>getId(), 'uid'=>$uid];
  154. $this->isUpdate(false)->save($data);
  155. return $data['id'];
  156. }
  157. /**
  158. * 查看生成排行榜
  159. */
  160. public function lookList(){
  161. $day = date("Y-m-d", time());
  162. $path = "list/$day.txt";
  163. $res = $this->db()->field("ordertime")->find();
  164. // 时间相同就读取文件, 不同就生成新文件
  165. if($res['ordertime'] == $day) {
  166. $file = fopen($path, "r") or die(json(['error'=>1009]));
  167. $info = fread($file, filesize($path));
  168. fclose($file);
  169. return $info;
  170. }
  171. $data = $this->db()->field("id, praise")->limit(config('orderlist'))->order("praise DESC")->select();
  172. // 重置信息
  173. $this->db()->query("UPDATE gd_user SET orderlist = 99 , ordertime = '".$day."'");
  174. foreach ($data as $k=>$v){
  175. $temp = $v->toArray();
  176. $temp['orderlist'] = $k + 1;
  177. $this->db()->update($temp);
  178. }
  179. $info = json_encode($data);
  180. $file = fopen($path, 'w') or die(json(['error'=>1009]));
  181. fwrite($file, $info);
  182. fclose($file);
  183. return $info;
  184. }
  185. /**
  186. * 根据昵称找到用户
  187. * @param string $nickname
  188. * @return array
  189. */
  190. public function findUserByName($nickname){
  191. return $this->db()->where(['nickname'=>$nickname])->find();
  192. }
  193. /**
  194. * 获取近期注册机器人id
  195. * @param number $limit
  196. * @return \think\Collection|\think\db\false|PDOStatement|string
  197. */
  198. public function getRobotId($limit = 1){
  199. return $this->db()->field("id i")->where(['robot'=>1])->order('registertime DESC')->limit($limit)->select();
  200. }
  201. /**
  202. * 推荐用户
  203. * @param int $user
  204. * @return array
  205. */
  206. public function recommendUser($user){
  207. $sql = "SELECT id i, nickname n, last_login t FROM `gd_user` WHERE id != $user AND nickname != '' AND robot = 0 AND id NOT IN (SELECT `buddy_id` FROM `gd_user_buddy` WHERE user_id = $user AND `status` = 1) LIMIT 10;";
  208. $res = $this->db()->query($sql);
  209. return $res;
  210. }
  211. /**
  212. * 根据昵称模糊查找用户
  213. * @param unknown $nick
  214. */
  215. public function likeUserByNick($user, $nick){
  216. $sql = "SELECT id i, nickname n, last_login t FROM `gd_user` WHERE `nickname` LIKE '%$nick%' AND id != $user AND nickname != '' AND robot = 0 AND id NOT IN (SELECT `buddy_id` FROM `gd_user_buddy` WHERE user_id = $user AND `status` = 1);";
  217. $res = $this->db()->query($sql);
  218. return $res;
  219. }
  220. }