Procházet zdrojové kódy

commit user model and controller

superbee před 8 roky
rodič
revize
d728c8f5f9

+ 13 - 4
application/home/controller/User.php

@@ -42,14 +42,14 @@ class User extends Controller {
 	public function other(){
 		$data = decode($this->request->post());
 		
-		$info = $this->userModel->getUserById($data['u']);
+		$res = $this->userModel->getUserById($data['u']);
 		$path = "load/".$info['short'].'.txt';
 		
 		$file = fopen($path, "r") or die(json(['error'=>1009]));
 		$info = fread($file, filesize($path));
 		fclose($file);
 		
-		return json(['l'=>$info]);
+		return json(['l'=>$info, 'p'=>$res['praise']]);
 	}
 	
 	public function load(){
@@ -57,11 +57,13 @@ class User extends Controller {
 		$id = $data['u'];
 		$path = "load/$id.txt";
 		
+		$res = $this->userModel->getUserByCode($id);
+		
 		$file = fopen($path, "r") or die(json(['error'=>1009]));
 		$info = fread($file, filesize($path));
 		fclose($file);
 		
-		return json(['l'=>$info]);
+		return json(['l'=>$info, 'p'=>$res['praise']]);
 	}
 	
 	public function save(){
@@ -80,13 +82,14 @@ class User extends Controller {
 		$data = decode($this->request->post());
 		
 		$short = $this->userModel->randOtherCode($data['i']);
+		$res = $this->userModel->getUserByCode($short);
 		
 		$path = "load/$short.txt";
 		$file = fopen($path, "r") or die(json(['error'=>1009]));
 		$info = fread($file, filesize($path));
 		fclose($file);
 		
-		return json(['l'=>$info]);
+		return json(['l'=>$info, 'p'=>$res['praise']]);
 	}
 	
 	public function delete(){
@@ -100,4 +103,10 @@ class User extends Controller {
 		return json(['r'=>(($res)?1:0)]);
 	}
 	
+	public function look(){
+		$res = $this->userModel->lookList();
+		
+		return json(['l'=>$res]);
+	}
+	
 }

+ 49 - 1
application/user/model/User.php

@@ -5,6 +5,7 @@ use think\Model;
 use think\Db;
 use think\Validate;
 use think\Request;
+use think\db\Query;
 
 /**
  * 用户模型类
@@ -66,7 +67,16 @@ class User extends Model{
 	 * @param int $id
 	 */
 	public function getUserById($id){
-		return $this->db()->where(['id'=>$id])->find();
+		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();
 	}
 	
 	/**
@@ -129,4 +139,42 @@ class User extends Model{
 		$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;		
+	}
+	
+	
 }

+ 4 - 1
config/config.php

@@ -144,7 +144,7 @@ return [
     // 视图输出字符串内容替换
     'view_replace_str'       => [
     		'__PUBLIC__'=> 'public/',
-    		'__CSS__'	=> 'public/static/css',
+    		'__CSS__'	=> 'public/static/css',
     		'__JS__'	=> 'public/static/js',
     ],
     // 默认跳转页面对应的模板文件
@@ -262,4 +262,7 @@ return [
         'var_page'  => 'page',
         'list_rows' => 15,
     ],
+		
+	// 排行榜数量	
+	'orderlist' 			 => 20, 	
 ];