Browse Source

add buddy controller and model

superbee 7 năm trước cách đây
mục cha
commit
2e300a6ad1

+ 99 - 0
application/newhome/controller/Buddy.php

@@ -0,0 +1,99 @@
+<?php
+namespace app\newhome\controller;
+
+use think\Controller;
+use app\user\model\UserBuddy;
+use app\user\model\User;
+
+class Buddy extends Controller{
+	private $buddyModel, $userModel;
+	
+	public function _initialize(){
+		parent::_initialize();
+		
+		$this->buddyModel = new UserBuddy();
+		$this->userModel = new User();
+	}
+	
+	public function index(){
+		$data = decode($this->request->post());
+		$user = $data['u'];
+		
+		$res = $this->buddyModel->findUserBuddy($user);
+		
+		return json(['l'=>$res]);
+	}
+	
+	public function apply(){
+		$data = decode($this->request->post());
+		
+		$buddy = $data['b'];
+		$user = $data['u'];
+		
+		$this->checkBuddyNum($user, $buddy);
+		
+		$this->buddyModel->saveAllInfo([['user_id'=>$user, 'buddy_id'=>$buddy], ['user_id'=>$buddy, 'buddy_id'=>$user]]);
+		
+		return json(['error'=>0]);
+	}
+	
+	public function agree(){
+		$data = decode($this->request->post());
+		
+		$buddy = $data['b'];
+		$user = $data['u'];
+		
+		$this->checkBuddyNum($user, $buddy);
+		
+		$info = $this->buddyModel->getBuddy($user, $buddy);
+		if($info){
+			$this->buddyModel->update(['status'=>UserBuddy::Friend, 'updatetime'=>getCurrentTime()], ['id'=>$info['id']]);
+		}else{
+			$this->buddyModel->save(['user_id'=>$user, 'buddy_id'=>$buddy, 'status'=>UserBuddy::Friend, 'inputtime'=>getCurrentTime(), 'updatetime'=>getCurrentTime()]);
+		}
+		
+		$res = $this->buddyModel->getBuddy($buddy, $user);
+		if($res){
+			$this->buddyModel->update(['status'=>UserBuddy::Friend, 'updatetime'=>getCurrentTime()], ['id'=>$res['id']]);
+		}else{
+			$this->buddyModel->save(['user_id'=>$buddy, 'buddy_id'=>$user, 'status'=>UserBuddy::Friend, 'inputtime'=>getCurrentTime(), 'updatetime'=>getCurrentTime()]);
+		}
+		
+		return json(['error'=>0]);
+	}
+	
+	public function remove(){
+		$data = decode($this->request->post());
+		
+		$buddy = $data['b'];
+		$user = $data['u'];
+		
+		$info = $this->buddyModel->getBuddy($user, $buddy);
+		
+		if($info){
+			$this->buddyModel->remove($info->toArray());
+		}
+		
+		return json(['error'=>0]);
+	}
+	
+	private function checkBuddyNum($user, $buddy){
+		
+		$info = $this->userModel->getUserById($user);
+		if(empty($info)) 
+			exit(json_encode(['error'=>1023]));
+		$count = $this->buddyModel->getBuddyCount($user);
+		// 个人超出好友指定数量则返回错误
+		if($count >= $info['buddy']) 
+			exit(json_encode(['error'=>1011]));
+		
+		$buddy_info = $this->userModel->getUserById($buddy);
+		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]));
+	}
+	
+}

+ 60 - 0
application/user/model/UserBuddy.php

@@ -0,0 +1,60 @@
+<?php
+namespace app\user\model;
+
+use think\Model;
+
+class UserBuddy extends Model {
+	const Friend = 1;
+	const Apply = 0;
+	
+	/**
+	 * 查找好友列表
+	 * @param int $user
+	 * @return array|\think\db\false|PDOStatement|string|\think\Model
+	 */
+	public function findUserBuddy($user){
+		return $this->db()->alias("b")
+		->field("u.id i, u.nickname n, u.praise p, u.robot r")
+		->join("gd_user u", "u.id = b.user_id", "LEFT")
+		->where(['b.user_id'=>$user, 'b.status'=>Friend])
+		->select();
+	}
+	
+	/**
+	 * 找到好友
+	 * @param int $user
+	 * @param int $buddy
+	 * @return array|\think\db\false|PDOStatement|string|\think\Model
+	 */
+	public function getBuddy($user, $buddy){
+		return $this->db()->where(['user_id'=>$user, 'buddy_id'=>$buddy])->find();
+// 		return $this->db()->alias("b")
+// 		->field("u.id, u.nickname, u.praise, u.robot")
+// 		->join("gd_user u", "u.id = b.user_id", "LEFT")
+// 		->where(['user_id'=>$user, 'buddy_id'=>$buddy])
+// 		->find();
+	}
+	
+	/**
+	 * 好友列表数量
+	 * @param int $user
+	 * @return number|string
+	 */
+	public function getBuddyCount($user){
+		return $this->db()->where(['user_id'=>$user, 'status'=>UserBuddy::Friend])->count();
+	}
+	
+	public function remove($data){
+		return $this->db()->delete($data);
+	}
+	
+	public function addAndSave($data){
+		return $this->allowField(true)->isUpdate((isset($data['id']) && $data['id'])?true:false)->save($data);
+	}
+	
+	public function saveAllInfo($data){
+		return $this->saveAll($data);
+	}
+	
+	
+}