Pārlūkot izejas kodu

commit user function and model

superbee 7 gadi atpakaļ
vecāks
revīzija
22a4a8b628

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

@@ -81,15 +81,20 @@ class User extends Controller {
 	public function rand(){
 		$data = decode($this->request->post());
 		
-		$short = $this->userModel->randOtherCode($data['i']);
-		$res = $this->userModel->getUserByCode($short);
+		$id = $this->userModel->randOtherInfoByShort($data['i']);
 		
-		$path = "load/$short.txt";
+		if(strlen($id) > 10){
+			$res = $this->userModel->getUserById($id);
+		}else{
+			$res = $this->userModel->getUserByCode($id);
+		}
+		
+		$path = "load/$id.txt";
 		$file = fopen($path, "r") or die(json(['error'=>1009]));
 		$info = fread($file, filesize($path));
 		fclose($file);
 		
-		return json(['l'=>$info, 'p'=>$res['praise']]);
+		return json(['l'=>$info, 'p'=>$res['praise'], 'i'=>$res['id']]);
 	}
 	
 	public function delete(){

+ 7 - 4
application/newhome/controller/User.php

@@ -3,7 +3,6 @@ namespace app\newhome\controller;
 
 use think\Controller;
 use app\user\model\UserSkill;
-use think\Config;
 
 class User extends Controller {
 	
@@ -89,15 +88,19 @@ class User extends Controller {
 	public function rand(){
 		$data = decode($this->request->post());
 		
-		$id = $this->userModel->randOtherId($data['i']);
-		$res = $this->userModel->getUserById($id);
+		$id = $this->userModel->randOtherInfoById($data['i']);
+		if(strlen($id) > 10){
+			$res = $this->userModel->getUserById($id);
+		}else{
+			$res = $this->userModel->getUserByCode($id);
+		}
 		
 		$path = "load/$id.txt";
 		$file = fopen($path, "r") or die(json(['error'=>1009]));
 		$info = fread($file, filesize($path));
 		fclose($file);
 		
-		return json(['l'=>$info, 'p'=>$res['praise']]);
+		return json(['l'=>$info, 'p'=>$res['praise'], 'i'=>$res['id']]);
 	}
 	
 	public function delete(){

+ 33 - 19
application/user/model/User.php

@@ -67,7 +67,7 @@ class User extends Model{
 	 * @param int $id
 	 */
 	public function getUserById($id){
-		return $this->db()->where(['id'=>$id])->find()->toArray();
+		return $this->db()->where(['id'=>$id])->find();
 	}
 	
 	/**
@@ -76,7 +76,7 @@ class User extends Model{
 	 * @return array
 	 */
 	public function getUserByCode($code){
-		return $this->db()->where(['short'=>$code])->find()->toArray();
+		return $this->db()->where(['short'=>$code])->find();
 	}
 	
 	/**
@@ -112,20 +112,9 @@ class User extends Model{
 	 * 随机出非个人的短码
 	 * @param int $id
 	 */
-	public function randOtherCode($id){
-		$info = $this->db()->field('short')->where("id", "<>", $id)->select();
-		return $this->getUserJsonFile($info, 'short');
-	}
-	
-	private function getUserJsonFile($info, $field){
-		$r = rand(0, count($info)-1);
-		$filename = $info[$r][$field];
-		$path = "load/$filename.txt";
-		if(!file_exists($path)){
-			unset($info[$r]);
-			$short = $this->getUserJsonFile(array_values($info), $field);
-		}
-		return $filename;
+	public function randOtherInfoByShort($short){
+		$info = $this->db()->field('id, short')->where("short", "<>", $short)->select();
+		return $this->getUserJsonFile($info);
 	}
 	
 	/**
@@ -133,9 +122,34 @@ class User extends Model{
 	 * @param int $id
 	 * @return int
 	 */
-	public function randOtherId($id){
-		$info = $this->db()->field('id')->where("id", "<>", $id)->select();
-		return $this->getUserJsonFile($info, 'id');
+	public function randOtherInfoById($id){
+		$info = $this->db()->field('id, short')->where("id", "<>", $id)->select();
+		return $this->getUserJsonFile($info);
+	}
+	
+	/**
+	 * 找到文件路径
+	 * @param unknown $info
+	 * @param string $id
+	 * @param string $short
+	 * @return string
+	 */
+	private function getUserJsonFile($info, $id = 'id', $short = 'short'){
+		$r = rand(0, count($info)-1);
+		$file_id = $info[$r][$id];
+		$file_short = $info[$r][$short];
+		
+		$path_id = "load/$file_id.txt";
+		$path_short = "load/$file_short.txt";
+		
+		if(is_file($path_id)){
+			return $file_id;
+		} elseif (is_file($path_short)){
+			return $file_short;
+		} else {
+			unset($info[$r]);
+			return $this->getUserJsonFile(array_values($info));
+		}
 	}
 	
 	/**