superbee %!s(int64=7) %!d(string=hai) anos
pai
achega
5ae55a325a

+ 35 - 10
application/newhome/controller/Buddy.php

@@ -50,7 +50,15 @@ class Buddy extends Controller{
 		
 		$this->checkBuddyNum($user, $buddy);
 		
-		$this->buddyModel->saveAllInfo([['user_id'=>$user, 'buddy_id'=>$buddy], ['user_id'=>$buddy, 'buddy_id'=>$user]]);
+		$info = $this->buddyModel->getBuddy($user, $buddy);
+		if(empty($info)){
+			$this->buddyModel->addAndSave(['user_id'=>$user, 'buddy_id'=>$buddy, 'status'=>UserBuddy::Wait]);
+		}
+		
+		$res = $this->buddyModel->getBuddy($buddy, $user);
+		if(empty($res)){
+			$this->buddyModel->addAndSave(['user_id'=>$buddy, 'buddy_id'=>$user]);
+		}
 		
 		return json(['error'=>0]);
 	}
@@ -62,7 +70,7 @@ class Buddy extends Controller{
 	public function agree(){
 		$data = decode($this->request->post());
 		
-		$buddy = $data['b'] = '1705233561389599432';
+		$buddy = $data['b'];
 		$user = $data['u'];
 		
 		$this->checkBuddyNum($user, $buddy);
@@ -96,26 +104,43 @@ class Buddy extends Controller{
 			$this->buddyModel->remove($info->toArray());
 		}
 		
+		$res = $this->buddyModel->getBuddy($buddy, $user);
+		
+		if($res){
+			$this->buddyModel->remove($res->toArray());
+		}
+		
 		return json(['error'=>0]);
 	}
 	
+	public function like(){
+		$data = decode($this->request->post());
+		
+		$user = $data['u'];
+		$nick = $data['n'];
+		
+		$res = $this->userModel->likeUserByNick($user, $nick);
+		
+		return json(['l'=>$res]);
+	}
+	
 	private function checkBuddyNum($user, $buddy){
 		
+		if($user == $buddy) exit(json_encode(['error'=>1020]));
+		
 		$info = $this->userModel->getUserById($user);
-		if(empty($info)) 
-			exit(json_encode(['error'=>1023]));
+		if(empty($info)) exit(json_encode(['error'=>1023]));
+		
 		$count = $this->buddyModel->getBuddyCount($user);
 		// 个人超出好友指定数量则返回错误
-		if($count >= $info['buddy']) 
-			exit(json_encode(['error'=>1011]));
+		if($count >= $info['buddy']) exit(json_encode(['error'=>1011]));
 		
 		$buddy_info = $this->userModel->getUserById($buddy);
-		if(empty($buddy_info)) 
-			exit(json_encode(['error'=>1023]));
+		if(empty($buddy_info)) exit(json_encode(['error'=>1023]));
+		
 		$buddy_count = $this->buddyModel->getBuddyCount($buddy);
 		// 好友数量超出指定数量则返回错误
-		if($buddy_count >= $buddy_info['buddy']) 
-			exit(json_encode(['error'=>1012]));
+		if($buddy_count >= $buddy_info['buddy']) exit(json_encode(['error'=>1012]));
 	}
 	
 }

+ 13 - 0
application/newhome/controller/Comment.php

@@ -26,6 +26,19 @@ class Comment extends Controller {
 		return json(["l"=>$info]);
 	}
 	
+	public function find(){
+		$data = decode($this->request->post());
+		
+		$user = $data['u'];
+		$comment = $data['c'];
+		$page = $data['p'];
+		$type = $data['t'];
+		
+		$info = $this->commentModel->getInfoByComment($user, $comment, $type, $page);
+		
+		return json(["l"=>$info]);
+	}
+	
 	public function comment(){
 		$data = decode($this->request->post());
 		

+ 15 - 3
application/user/model/User.php

@@ -93,12 +93,13 @@ class User extends Model{
 	 */
 	public function loginByUid($uid){
 		$this->data['uid'] = $uid;
-		$info = $this->db()->field('id i, diamond d, coin c, short o, praise p')->where(array("uid"=>$uid))->find();
+		$info = $this->db()->field('id i, diamond d, coin c, short o, praise p, buddy b')->where(array("uid"=>$uid))->find();
 		
 		if(empty($info)) {
     		$info['d'] = 0;
     		$info['c'] = 0;
     		$info['p'] = 0;
+    		$info['b'] = 25;
     		$info['i'] = $this->createUser($uid);
     		$info['o'] = $this->data['short'];
     	}
@@ -241,12 +242,23 @@ class User extends Model{
 	 * @return array
 	 */
 	public function recommendUser($user){
-		$sql = "SELECT id i, nickname n, last_login t FROM `gd_user` WHERE id != $user AND nickname != '' AND robot = 0 AND id NOT IN (SELECT `buddy_id` FROM `gd_user_buddy` WHERE user_id = $user AND `status` = 1);";
+		$sql = "SELECT id i, nickname n, last_login t FROM `gd_user` WHERE id != $user AND nickname != '' AND robot = 0 AND id NOT IN (SELECT `buddy_id` FROM `gd_user_buddy` WHERE user_id = $user AND `status` = 1) LIMIT 10;";
+		
+		$res = $this->db()->query($sql);
+		
+		return $res;
+	}
+	
+	/**
+	 * 根据昵称模糊查找用户
+	 * @param unknown $nick
+	 */
+	public function likeUserByNick($user, $nick){
+		$sql = "SELECT id i, nickname n, last_login t FROM `gd_user` WHERE `nickname` LIKE '%$nick%' AND id != $user AND nickname != '' AND robot = 0 AND id NOT IN (SELECT `buddy_id` FROM `gd_user_buddy` WHERE user_id = $user AND `status` = 1);";
 		
 		$res = $this->db()->query($sql);
 		
 		return $res;
-// 		return $res[rand(0, count($res) - 1)];
 	}
 	
 }

+ 3 - 6
application/user/model/UserBuddy.php

@@ -6,6 +6,7 @@ use think\Model;
 class UserBuddy extends Model {
 	const Friend = 1;
 	const Apply = 0;
+	const Wait = 2;
 	
 	protected $createTime = 'inputtime';			// 创建时间
 	protected $updateTime = 'updatetime';			// 每次更新时间
@@ -19,7 +20,7 @@ class UserBuddy extends Model {
 	public function findUserBuddy($user, $status = UserBuddy::Friend){
 		return $this->db()->alias("b")
 		->field("u.id i, u.nickname n, u.praise p, u.robot r, last_login t")
-		->join("gd_user u", "u.id = b.user_id", "LEFT")
+		->join("gd_user u", "u.id = b.buddy_id", "LEFT")
 		->where(['b.user_id'=>$user, 'b.status'=>$status])
 		->select();
 	}
@@ -53,12 +54,8 @@ class UserBuddy extends Model {
 	}
 	
 	public function addAndSave($data){
+		$this->data = $data;
 		return $this->allowField(true)->isUpdate((isset($data['id']) && $data['id'])?true:false)->save($data);
 	}
 	
-	public function saveAllInfo($data){
-		return $this->saveAll($data);
-	}
-	
-	
 }

+ 11 - 0
application/user/model/UserComment.php

@@ -23,6 +23,17 @@ class UserComment extends Model{
 		->select();
 	}
 	
+	public function getInfoByComment($user, $comment, $type, $page = 0){
+		return $this->db()
+		->alias("c")
+		->field("c.comment_id s, c.inputtime t, c.content c, u.nickname n")
+		->join("gd_user u", "u.id = c.comment_id", "LEFT")
+		->where(['c.user_id'=>$user, "c.comment_id"=>$comment, "c.type"=>$type])
+		->page($page, config('paginate.list_rows'))
+		->order("inputtime DESC")
+		->select();
+	}
+	
 	public function add($data){
 		return $this->save($data);
 	}