User.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace app\user\model;
  3. use think\Model;
  4. use think\Db;
  5. use think\Validate;
  6. use think\Request;
  7. /**
  8. * 用户模型类
  9. * @author Superbee
  10. *
  11. */
  12. class User extends Model{
  13. protected $insert = ['last_login', 'short', 'registertime'];
  14. protected $update = ['last_login', 'last_ip'];
  15. public function initialize(){
  16. parent::initialize();
  17. }
  18. /* 自动填充设置 begin */
  19. protected function setShortAttr(){
  20. return shortCode($this->data['uid']);
  21. }
  22. protected function setLastIpAttr() {
  23. return request()->ip();
  24. }
  25. protected function setLastLoginAttr(){
  26. return getCurrentTime();
  27. }
  28. protected function setRegisterTimeAttr(){
  29. return getCurrentTime();
  30. }
  31. /* 自动填充设置 end */
  32. /**
  33. * 获取数据信息
  34. * @param int $page 页码
  35. * @param array $where 查询条件
  36. */
  37. public function getInfo($page = 0, $where = array()){
  38. return $this->db()
  39. ->where($where)
  40. ->page($page, config('paginate.list_rows'))
  41. ->select();
  42. }
  43. /**
  44. * 获取分页信息
  45. * @param array $where 查询条件
  46. */
  47. public function getPage($where = array()){
  48. $total = $this->db()->count();
  49. $paginate = $this->db()->where($where)->paginate(config('paginate.list_rows'), $total);
  50. return $paginate->render();
  51. }
  52. /**
  53. * 根据id找到对象
  54. * @param int $id
  55. */
  56. public function getUserById($id){
  57. return $this->db()->where(['id'=>$id])->find();
  58. }
  59. /**
  60. * 添加和更新的方法
  61. */
  62. public function addAndSave(){
  63. $data = request()->post();
  64. return $this->allowField(true)->isUpdate(isset($data['id']) && $data['id']?true:false)->save($data);
  65. }
  66. /**
  67. * 用户登录
  68. * @param string $uid
  69. * @return Ambigous <i:id, c:int, d:int>
  70. */
  71. public function loginByUid($uid){
  72. $this->data['uid'] = $uid;
  73. $info = $this->db()->field('id i, diamond d, coin c, short o')->where(array("uid"=>$uid))->find();
  74. if(empty($info)) {
  75. $info['d'] = 0;
  76. $info['c'] = 0;
  77. $info['i'] = $this->createUser($uid);
  78. $info['o'] = $this->data['short'];
  79. }
  80. $this->isUpdate(true)->save(['id'=>$info['i']]);
  81. return $info;
  82. }
  83. /**
  84. * 随机出非个人的短码
  85. * @param int $id
  86. */
  87. public function randOtherCode($id){
  88. $info = $this->db()->field('short')->where("id", "<>", $id)->select();
  89. return $this->getUserJsonFile($info);
  90. }
  91. private function getUserJsonFile($info){
  92. $r = rand(0, count($info)-1);
  93. $short = $info[$r]['short'];
  94. $path = "load/$short.txt";
  95. if(!file_exists($path)){
  96. unset($info[$r]);
  97. $short = $this->getUserJsonFile(array_values($info));
  98. }
  99. return $short;
  100. }
  101. /**
  102. * 创建用户
  103. * @param string $uid
  104. * @return 返回用户数据库id
  105. */
  106. public function createUser($uid){
  107. $data = ['id'=>getId(), 'uid'=>$uid];
  108. $this->isUpdate(false)->save($data);
  109. return $data['id'];
  110. }
  111. }