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()){ $total = $this->db()->count(); $paginate = $this->db()->where($where)->paginate(config('paginate.list_rows'), $total); return $paginate->render(); } /** * 根据id找到对象 * @param int $id */ public function getUserById($id){ return $this->db()->where(['id'=>$id])->find(); } /** * 添加和更新的方法 */ public function addAndSave(){ $data = request()->post(); return $this->allowField(true)->isUpdate(isset($data['id']) && $data['id']?true:false)->save($data); } /** * 用户登录 * @param string $uid * @return Ambigous */ public function loginByUid($uid){ $this->data['uid'] = $uid; $info = $this->db()->field('id i, diamond d, coin c, short o')->where(array("uid"=>$uid))->find(); if(empty($info)) { $info['d'] = 0; $info['c'] = 0; $info['i'] = $this->createUser($uid); $info['o'] = $this->data['short']; } $this->isUpdate(true)->save(['id'=>$info['i']]); return $info; } /** * 随机出非个人的短码 * @param int $id */ public function randOtherCode($id){ $info = $this->db()->field('short')->where("id", "<>", $id)->select(); return $this->getUserJsonFile($info); } private function getUserJsonFile($info){ $r = rand(0, count($info)-1); $short = $info[$r]['short']; $path = "load/$short.txt"; if(!file_exists($path)){ unset($info[$r]); $short = $this->getUserJsonFile(array_values($info)); } return $short; } /** * 创建用户 * @param string $uid * @return 返回用户数据库id */ public function createUser($uid){ $data = ['id'=>getId(), 'uid'=>$uid]; $this->isUpdate(false)->save($data); return $data['id']; } }