123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- namespace app\user\model;
- use think\Model;
- use think\Db;
- use think\Validate;
- use think\Request;
- /**
- * 用户模型类
- * @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();
- }
-
- /**
- * 添加和更新的方法
- */
- 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 <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')->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'];
- }
- }
|