data['uid']); } protected function setLastIpAttr() { return request()->ip(); } protected function setLastLoginAttr(){ return getCurrentTime(); } protected function setRegisterTimeAttr(){ return getCurrentTime(); } /* 自动填充设置 end */ /** * 获取数据信息 * @param int $page 页码 * @param array $where 查询条件 */ public function getInfo($page = 0, $where = array()){ return $this->db() ->where($where) ->page($page, config('paginate.list_rows')) ->select(); } /** * 获取分页信息 * @param array $where 查询条件 */ public function getPage($where = array()){ $query = getPaginatiorQuery(); $total = $this->db()->where($where)->count(); // 查询条件 $config = ['query'=>$query]; $paginate = $this->db()->where($where)->paginate(config('paginate.list_rows'), $total, $config); return $paginate->render(); } /** * 根据id找到对象 * @param int $id */ public function getUserById($id){ return $this->db()->where(['id'=>$id])->find(); } /** * 根据短码找到对象 * @param string $code * @return array */ public function getUserByCode($code){ return $this->db()->where(['short'=>$code])->find(); } /** * 添加和更新的方法 */ public function addAndSave($data){ return $this->allowField(true)->isUpdate(isset($data['id']) && $data['id']?true:false)->save($data); } /** * 用户登录 * @param string $uid * @return array Ambigous */ public function loginByUid($uid){ $this->data['uid'] = $uid; $info = $this->db()->field('id i, diamond d, coin c, short o, praise p, buddy b')->where(array("uid"=>$uid))->find(); if(empty($info)) { $info['d'] = 0; $info['c'] = 0; $info['p'] = 0; $info['b'] = 25; $info['i'] = $this->createUser($uid); $info['o'] = $this->data['short']; } $this->isUpdate(true)->save(['id'=>$info['i']]); return $info; } public function loginBySdk($ext_id){ $this->data['uid'] = ""; $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(); if(empty($info)) { $info['d'] = 0; $info['c'] = 0; $info['p'] = 0; $info['b'] = 25; $info['i'] = $this->createUserBySdk($ext_id); $info['e'] = $ext_id; $info['o'] = $this->data['short']; $info['t'] = $this->data['last_login']; } $this->isUpdate(true)->save(['id'=>$info['i']]); return $info; } /** * 随机出非个人的短码 * @param int $id */ public function randOtherInfoByShort($short){ $info = $this->db()->field('id, short') ->where("short", "<>", $short) ->whereTime('last_login', 'between', $this->getNearMonth()) ->select(); return $this->getUserJsonFile($info); } /** * 随机出非个人的Id * @param int $id * @return int */ public function randOtherInfoById($id){ $info = $this->db()->field('id, short') ->where("id", "<>", $id) ->whereTime('last_login', 'between', $this->getNearMonth()) ->select(); return $this->getUserJsonFile($info); } /** * 得到最近30天日期 * @return array */ private function getNearMonth(){ $back = date('Y-m-d H:i:s'); $front = date('Y-m-d H:i:s',strtotime("-30 day")); return [$front, $back]; } /** * 找到文件路径 * @param array $info * @param string $id * @param string $short * @return string */ private function getUserJsonFile($info, $id = 'id', $short = 'short'){ $r = rand(0, count($info)-1); $file_id = $info[$r][$id]; $file_short = $info[$r][$short]; $path_id = "load/$file_id.txt"; $path_short = "load/$file_short.txt"; if(is_file($path_id)){ return $file_id; } elseif (is_file($path_short)){ return $file_short; } else { unset($info[$r]); return $this->getUserJsonFile(array_values($info)); } } /** * 创建用户 * @param string $uid * @return int 返回用户数据库id */ public function createUser($uid){ $data = ['id'=>getId(), 'uid'=>$uid]; $this->isUpdate(false)->save($data); return $data['id']; } public function createUserBySdk($ext_id){ $data = ['id'=>getId(), 'ext_id'=>$ext_id]; $this->isUpdate(false)->save($data); return $data['id']; } /** * 查看生成排行榜 */ public function lookList(){ $day = date("Y-m-d", time()); $path = "list/$day.txt"; $res = $this->db()->field("ordertime")->find(); // 时间相同就读取文件, 不同就生成新文件 if($res['ordertime'] == $day) { $file = fopen($path, "r") or die(json(['error'=>1009])); $info = fread($file, filesize($path)); fclose($file); return $info; } $data = $this->db()->field("id, praise")->limit(config('orderlist'))->order("praise DESC")->select(); // 重置信息 $this->db()->query("UPDATE gd_user SET orderlist = 99 , ordertime = '".$day."'"); foreach ($data as $k=>$v){ $temp = $v->toArray(); $temp['orderlist'] = $k + 1; $this->db()->update($temp); } $info = json_encode($data); $file = fopen($path, 'w') or die(json(['error'=>1009])); fwrite($file, $info); fclose($file); return $info; } /** * 根据昵称找到用户 * @param string $nickname * @return array */ public function findUserByName($nickname){ return $this->db()->where(['nickname'=>$nickname])->find(); } /** * 获取近期注册机器人id * @param number $limit * @return \think\Collection|string */ public function getRobotId($limit = 1){ return $this->db()->field("id i")->where(['robot'=>1])->order('registertime DESC')->limit($limit)->select(); } /** * 推荐用户 * @param int $user * @return array */ public function recommendUser($user){ $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;"; $res = $this->db()->query($sql); return $res; } /** * 根据昵称模糊查找用户 * @param string $nick */ public function likeUserByNick($user, $nick){ $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);"; $res = $this->db()->query($sql); return $res; } }