User.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. $query = getPaginatiorQuery();
  50. $total = $this->db()->where($where)->count();
  51. // 查询条件
  52. $config = ['query'=>$query];
  53. $paginate = $this->db()->where($where)->paginate(config('paginate.list_rows'), $total, $config);
  54. return $paginate->render();
  55. }
  56. /**
  57. * 根据id找到对象
  58. * @param int $id
  59. */
  60. public function getUserById($id){
  61. return $this->db()->where(['id'=>$id])->find();
  62. }
  63. /**
  64. * 根据短码找到对象
  65. * @param string $code
  66. * @return array
  67. */
  68. public function getUserByCode($code){
  69. return $this->db()->where(['short'=>$code])->find();
  70. }
  71. /**
  72. * 添加和更新的方法
  73. */
  74. public function addAndSave($data){
  75. return $this->allowField(true)->isUpdate(isset($data['id']) && $data['id']?true:false)->save($data);
  76. }
  77. /**
  78. * 用户登录
  79. * @param string $uid
  80. * @return array Ambigous <i:id, c:int, d:int>
  81. */
  82. public function loginByUid($uid){
  83. $this->data['uid'] = $uid;
  84. $info = $this->db()->field('id i, diamond d, coin c, short o, praise p, buddy b')->where(array("uid"=>$uid))->find();
  85. if(empty($info)) {
  86. $info['d'] = 0;
  87. $info['c'] = 0;
  88. $info['p'] = 0;
  89. $info['b'] = 25;
  90. $info['i'] = $this->createUser($uid);
  91. $info['o'] = $this->data['short'];
  92. }
  93. $this->isUpdate(true)->save(['id'=>$info['i']]);
  94. return $info;
  95. }
  96. public function loginBySdk($ext_id){
  97. $this->data['uid'] = "";
  98. $info = $this->db()->field('id i, ext_id e, diamond d, mobile m, coin c, short o, praise p, buddy b, last_login t')->where(array("ext_id"=>$ext_id))->find();
  99. if(empty($info)) {
  100. $info['d'] = 0;
  101. $info['c'] = 0;
  102. $info['p'] = 0;
  103. $info['b'] = 25;
  104. $info['i'] = $this->createUserBySdk($ext_id);
  105. $info['e'] = $ext_id;
  106. $info['o'] = $this->data['short'];
  107. $info['t'] = $this->data['last_login'];
  108. }
  109. $this->isUpdate(true)->save(['id'=>$info['i']]);
  110. return $info;
  111. }
  112. /**
  113. * 随机出非个人的短码
  114. * @param int $id
  115. */
  116. public function randOtherInfoByShort($short){
  117. $info = $this->db()->field('id, short')
  118. ->where("short", "<>", $short)
  119. ->whereTime('last_login', 'between', $this->getNearMonth())
  120. ->select();
  121. return $this->getUserJsonFile($info);
  122. }
  123. /**
  124. * 随机出非个人的Id
  125. * @param int $id
  126. * @return int
  127. */
  128. public function randOtherInfoById($id){
  129. $info = $this->db()->field('id, short')
  130. ->where("id", "<>", $id)
  131. ->whereTime('last_login', 'between', $this->getNearMonth())
  132. ->select();
  133. return $this->getUserJsonFile($info);
  134. }
  135. /**
  136. * 得到最近30天日期
  137. * @return array
  138. */
  139. private function getNearMonth(){
  140. $back = date('Y-m-d H:i:s');
  141. $front = date('Y-m-d H:i:s',strtotime("-30 day"));
  142. return [$front, $back];
  143. }
  144. /**
  145. * 找到文件路径
  146. * @param array $info
  147. * @param string $id
  148. * @param string $short
  149. * @return string
  150. */
  151. private function getUserJsonFile($info, $id = 'id', $short = 'short'){
  152. $r = rand(0, count($info)-1);
  153. $file_id = $info[$r][$id];
  154. $file_short = $info[$r][$short];
  155. $path_id = "load/$file_id.txt";
  156. $path_short = "load/$file_short.txt";
  157. if(is_file($path_id)){
  158. return $file_id;
  159. } elseif (is_file($path_short)){
  160. return $file_short;
  161. } else {
  162. unset($info[$r]);
  163. return $this->getUserJsonFile(array_values($info));
  164. }
  165. }
  166. /**
  167. * 创建用户
  168. * @param string $uid
  169. * @return int 返回用户数据库id
  170. */
  171. public function createUser($uid){
  172. $data = ['id'=>getId(), 'uid'=>$uid];
  173. $this->isUpdate(false)->save($data);
  174. return $data['id'];
  175. }
  176. public function createUserBySdk($ext_id){
  177. $data = ['id'=>getId(), 'ext_id'=>$ext_id];
  178. $this->isUpdate(false)->save($data);
  179. return $data['id'];
  180. }
  181. /**
  182. * 查看生成排行榜
  183. */
  184. public function lookList(){
  185. $day = date("Y-m-d", time());
  186. $path = "list/$day.txt";
  187. $res = $this->db()->field("ordertime")->find();
  188. // 时间相同就读取文件, 不同就生成新文件
  189. if($res['ordertime'] == $day) {
  190. $file = fopen($path, "r") or die(json(['error'=>1009]));
  191. $info = fread($file, filesize($path));
  192. fclose($file);
  193. return $info;
  194. }
  195. $data = $this->db()->field("id, praise")->limit(config('orderlist'))->order("praise DESC")->select();
  196. // 重置信息
  197. $this->db()->query("UPDATE gd_user SET orderlist = 99 , ordertime = '".$day."'");
  198. foreach ($data as $k=>$v){
  199. $temp = $v->toArray();
  200. $temp['orderlist'] = $k + 1;
  201. $this->db()->update($temp);
  202. }
  203. $info = json_encode($data);
  204. $file = fopen($path, 'w') or die(json(['error'=>1009]));
  205. fwrite($file, $info);
  206. fclose($file);
  207. return $info;
  208. }
  209. /**
  210. * 根据昵称找到用户
  211. * @param string $nickname
  212. * @return array
  213. */
  214. public function findUserByName($nickname){
  215. return $this->db()->where(['nickname'=>$nickname])->find();
  216. }
  217. /**
  218. * 获取近期注册机器人id
  219. * @param number $limit
  220. * @return \think\Collection|string
  221. */
  222. public function getRobotId($limit = 1){
  223. return $this->db()->field("id i")->where(['robot'=>1])->order('registertime DESC')->limit($limit)->select();
  224. }
  225. /**
  226. * 推荐用户
  227. * @param int $user
  228. * @return array
  229. */
  230. public function recommendUser($user){
  231. $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;";
  232. $res = $this->db()->query($sql);
  233. return $res;
  234. }
  235. /**
  236. * 根据昵称模糊查找用户
  237. * @param string $nick
  238. */
  239. public function likeUserByNick($user, $nick){
  240. $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);";
  241. $res = $this->db()->query($sql);
  242. return $res;
  243. }
  244. }