User.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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()->toArray();
  59. }
  60. /**
  61. * 根据短码找到对象
  62. * @param string $code
  63. * @return array
  64. */
  65. public function getUserByCode($code){
  66. return $this->db()->where(['short'=>$code])->find()->toArray();
  67. }
  68. /**
  69. * 添加和更新的方法
  70. */
  71. public function addAndSave(){
  72. $data = request()->post();
  73. return $this->allowField(true)->isUpdate(isset($data['id']) && $data['id']?true:false)->save($data);
  74. }
  75. /**
  76. * 用户登录
  77. * @param string $uid
  78. * @return Ambigous <i:id, c:int, d:int>
  79. */
  80. public function loginByUid($uid){
  81. $this->data['uid'] = $uid;
  82. $info = $this->db()->field('id i, diamond d, coin c, short o, praise p')->where(array("uid"=>$uid))->find();
  83. if(empty($info)) {
  84. $info['d'] = 0;
  85. $info['c'] = 0;
  86. $info['p'] = 0;
  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 randOtherCode($id){
  98. $info = $this->db()->field('short')->where("id", "<>", $id)->select();
  99. return $this->getUserJsonFile($info);
  100. }
  101. private function getUserJsonFile($info){
  102. $r = rand(0, count($info)-1);
  103. $short = $info[$r]['short'];
  104. $path = "load/$short.txt";
  105. if(!file_exists($path)){
  106. unset($info[$r]);
  107. $short = $this->getUserJsonFile(array_values($info));
  108. }
  109. return $short;
  110. }
  111. /**
  112. * 创建用户
  113. * @param string $uid
  114. * @return 返回用户数据库id
  115. */
  116. public function createUser($uid){
  117. $data = ['id'=>getId(), 'uid'=>$uid];
  118. $this->isUpdate(false)->save($data);
  119. return $data['id'];
  120. }
  121. /**
  122. * 查看生成排行榜
  123. */
  124. public function lookList(){
  125. $day = date("Y-m-d", time());
  126. $path = "list/$day.txt";
  127. $res = $this->db()->field("ordertime")->find();
  128. // 时间相同就读取文件, 不同就生成新文件
  129. if($res['ordertime'] == $day) {
  130. $file = fopen($path, "r") or die(json(['error'=>1009]));
  131. $info = fread($file, filesize($path));
  132. fclose($file);
  133. return $info;
  134. }
  135. $data = $this->db()->field("id, praise")->limit(config('listorder'))->order("praise")->select();
  136. // 重置信息
  137. $this->db()->query("UPDATE gd_user SET orderlist = 99 , ordertime = '".$day."'");
  138. foreach ($data as $k=>$v){
  139. $temp = $v->toArray();
  140. $temp['orderlist'] = $k + 1;
  141. $this->db()->update($temp);
  142. }
  143. $info = json_encode($data);
  144. $file = fopen($path, 'w') or die(json(['error'=>1009]));
  145. fwrite($file, $info);
  146. fclose($file);
  147. return $info;
  148. }
  149. }