|
@@ -1,111 +1,122 @@
|
|
|
-<?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 string $uid
|
|
|
- * @return 返回用户数据库id
|
|
|
- */
|
|
|
- public function createUser($uid){
|
|
|
- $data = ['id'=>getId(), 'uid'=>$uid];
|
|
|
- $this->isUpdate(false)->save($data);
|
|
|
- return $data['id'];
|
|
|
- }
|
|
|
+<?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();
|
|
|
+ $r = rand(0, count($info)-1);
|
|
|
+
|
|
|
+ return $info[$r]['short'];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建用户
|
|
|
+ * @param string $uid
|
|
|
+ * @return 返回用户数据库id
|
|
|
+ */
|
|
|
+ public function createUser($uid){
|
|
|
+ $data = ['id'=>getId(), 'uid'=>$uid];
|
|
|
+ $this->isUpdate(false)->save($data);
|
|
|
+ return $data['id'];
|
|
|
+ }
|
|
|
}
|