123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- <?php
- namespace app\user\model;
- use think\Model;
- use think\Db;
- use think\Validate;
- use think\Request;
- use think\db\Query;
- /**
- * 用户模型类
- * @author Superbee
- *
- */
- class User extends Model{
- protected $insert = ['last_login', 'short', 'registertime'];
- protected $update = ['last_login', 'last_ip'];
-
- public function initialize(){
- parent::initialize();
- }
-
- /* 自动填充设置 begin */
- protected function setShortAttr(){
- return shortCode($this->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()->toArray();
- }
-
- /**
- * 根据短码找到对象
- * @param string $code
- * @return array
- */
- public function getUserByCode($code){
- return $this->db()->where(['short'=>$code])->find()->toArray();
- }
-
- /**
- * 添加和更新的方法
- */
- public function addAndSave($data){
- return $this->allowField(true)->isUpdate(isset($data['id']) && $data['id']?true:false)->save($data);
- }
-
- /**
- * 用户登录
- * @param string $uid
- * @return Ambigous <i:id, c:int, d:int>
- */
- public function loginByUid($uid){
- $this->data['uid'] = $uid;
- $info = $this->db()->field('id i, diamond d, coin c, short o, praise p')->where(array("uid"=>$uid))->find();
-
- if(empty($info)) {
- $info['d'] = 0;
- $info['c'] = 0;
- $info['p'] = 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, 'short');
- }
-
- private function getUserJsonFile($info, $field){
- $r = rand(0, count($info)-1);
- $filename = $info[$r][$field];
- $path = "load/$filename.txt";
- if(!file_exists($path)){
- unset($info[$r]);
- $short = $this->getUserJsonFile(array_values($info), $field);
- }
- return $filename;
- }
-
- /**
- * 随机出非个人的Id
- * @param int $id
- * @return int
- */
- public function randOtherId($id){
- $info = $this->db()->field('id')->where("id", "<>", $id)->select();
- return $this->getUserJsonFile($info, 'id');
- }
-
- /**
- * 创建用户
- * @param string $uid
- * @return 返回用户数据库id
- */
- public function createUser($uid){
- $data = ['id'=>getId(), 'uid'=>$uid];
- $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('listorder'))->order("praise")->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();
- }
-
- }
|