Prechádzať zdrojové kódy

commit code User Model and Controller

superbee 8 rokov pred
rodič
commit
8742118710

+ 0 - 8
2222.htaccess

@@ -1,8 +0,0 @@
-<IfModule mod_rewrite.c>
-  Options +FollowSymlinks -Multiviews
-  RewriteEngine On
-
-  RewriteCond %{REQUEST_FILENAME} !-d
-  RewriteCond %{REQUEST_FILENAME} !-f
-  RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
-</IfModule>

+ 15 - 1
application/home/controller/User.php

@@ -10,12 +10,13 @@ class User extends Controller {
 	
 	public function _initialize(){
 		$this->skillModel = new UserSkill();
+		$this->userModel = new \app\user\model\User(); 
+		
 	}
 	
 	public function login()
 	{
 		$data = decode($this->request->post());
-		$this->userModel = new \app\user\model\User(); 
 		
 		$info = $this->userModel->loginByUid($data['u']);
 		$info['s'] = $this->skillModel->getUserSkill($info['i']);
@@ -62,4 +63,17 @@ class User extends Controller {
 		return json(['error'=>0]);
 	}
 	
+	public function rand(){
+		$data = decode($this->request->post());
+		
+		$short = $this->userModel->randOtherCode($data['i']);
+		
+		$path = "load/$short.txt";
+		$file = fopen($path, "r") or die(json(['error'=>1009]));
+		$info = fread($file, filesize($path));
+		fclose($file);
+		
+		return json(['l'=>$info]);
+	}
+	
 }

+ 121 - 110
application/user/model/User.php

@@ -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'];
+	}
 }

+ 1 - 0
load/.htaccess

@@ -0,0 +1 @@
+deny from all