318 lines
11 KiB
PHP
318 lines
11 KiB
PHP
<?php
|
|
namespace app\common\model;
|
|
|
|
use app\admin\model\Third;
|
|
use think\Hook;
|
|
use think\Model;
|
|
use app\common\model\User ;
|
|
|
|
class FriendRelation extends Model
|
|
{
|
|
|
|
|
|
protected $name = 'friend_relations';
|
|
// 自动写入时间戳字段
|
|
protected $autoWriteTimestamp = 'int';
|
|
// 定义时间戳字段名
|
|
protected $createTime = 'createtime';
|
|
protected $updateTime = 'updatetime';
|
|
|
|
// 定义状态常量
|
|
const STATUS_NORMAL = 1; // 正常
|
|
const STATUS_BLOCKED = 2; // 拉黑
|
|
const STATUS_DELETED = 0; // 删除
|
|
|
|
|
|
protected $type = [
|
|
'id' => 'integer',
|
|
'user_id' => 'integer',
|
|
'friend_id' => 'integer',
|
|
'status' => 'integer',
|
|
'group_id' => 'integer'
|
|
];
|
|
|
|
// 关联用户信息
|
|
public function user()
|
|
{
|
|
return $this->belongsTo('User', 'user_id', 'id');
|
|
}
|
|
|
|
public function friend()
|
|
{
|
|
return $this->belongsTo('User', 'friend_id', 'id');
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
* 获取我的好友列表
|
|
*/
|
|
public static function getFriendList($userId){
|
|
$list = self::where(['user_id'=>$userId,'status'=>self::STATUS_NORMAL])->with(['friend'])->paginate();
|
|
$ThirdModel = new Third();
|
|
foreach ($list as $row) {
|
|
$row->getRelation('friend')->visible(['id','username','nickname','avatar','user_number','open_id']);
|
|
$third_info = $ThirdModel->where(['user_id'=>$row['friend']['id'],'platform'=>'wxapp'])->find();
|
|
$row->friend->open_id = $third_info['openid']??'';
|
|
}
|
|
return $list;
|
|
}
|
|
|
|
/*
|
|
* 搜索用户
|
|
*/
|
|
public static function searchUser($userId, $keyword){
|
|
$lists = User::where(
|
|
['id'=>['>',1],'user_number|nickname'=>['like','%'.$keyword.'%']]
|
|
)->where('id', '<>', $userId)->field('id,nickname,avatar,user_number')->paginate();
|
|
//判断好友状态
|
|
foreach ($lists as $row) {
|
|
|
|
$friend_info = self::where(['user_id'=>$userId,'friend_id'=>$row['id']])->find();
|
|
if (empty($friend_info)){
|
|
$row->is_friend = 0;
|
|
}else{
|
|
$row->is_friend = $friend_info['status'];
|
|
}
|
|
}
|
|
|
|
return $lists;
|
|
}
|
|
|
|
/*
|
|
* 申请添加好友
|
|
*/
|
|
public static function applyFriend($user_id,$friend_id,$message='')
|
|
{
|
|
$is_friend = self::isFriend($user_id,$friend_id);
|
|
if ($is_friend){
|
|
return ['code'=>0,'msg'=>'已经是好友关系了'];
|
|
}
|
|
|
|
//判断是不是已经申请过了
|
|
$friend_requests = FriendRequests::get(['from_user_id'=>$user_id,'to_user_id'=>$friend_id]);
|
|
if (!empty($friend_requests) && $friend_requests['status'] == 0){
|
|
return ['code'=>0,'msg'=>'已经申请请勿重复申请'];
|
|
}
|
|
//添加申请记录
|
|
$insert_data = [
|
|
'from_user_id' => $user_id,
|
|
'to_user_id' => $friend_id,
|
|
'message' => $message,
|
|
'status' => '0',
|
|
'createtime' => time(),
|
|
];
|
|
FriendRequests::create($insert_data);
|
|
//发送消息通知
|
|
$data=[
|
|
'to_user_id'=>$friend_id,
|
|
'from_user_id'=>$user_id,
|
|
'type'=>"1",
|
|
'content'=>$message,
|
|
];
|
|
Hook::listen('add_friend_notifications', $data);
|
|
|
|
return ['code'=>1,'msg'=>'申请成功'];
|
|
}
|
|
|
|
|
|
public static function getApplyList($userId){
|
|
$list = FriendRequests::where(['to_user_id'=>$userId,'status'=>0])->with(['from_user'])->paginate();
|
|
foreach ($list as $row) {
|
|
$row->getRelation('from_user')->visible(['id','nickname','avatar','user_number']);
|
|
}
|
|
return $list;
|
|
}
|
|
|
|
public static function handleApply($userId,$friend_requests_id,$status){
|
|
|
|
//先查询是不是被申请人
|
|
$friend_requests_info = FriendRequests::get($friend_requests_id);
|
|
if (empty($friend_requests_info)) return ['code'=>0,'msg'=>'非法操作'];
|
|
if ($friend_requests_info['to_user_id'] != $userId) return ['code'=>0,'msg'=>'非法操作'];
|
|
if ($friend_requests_info['status'] != '0') return ['code'=>0,'msg'=>'已处理,请勿重复处理'];
|
|
|
|
//经检查是否已经是好友
|
|
$is_friend = self::isFriend($userId, $friend_requests_info['from_user_id']);
|
|
if ($is_friend){
|
|
return ['code'=>0,'msg'=>'已经是好友关系了'];
|
|
}
|
|
|
|
$friend_requests_info->status = $status;
|
|
$friend_requests_info->save();
|
|
//处理会员关系
|
|
if ($status == "1"){//同意
|
|
$result = self::addFriend($userId,$friend_requests_info['from_user_id']);
|
|
}elseif ($status == "2"){//拒绝
|
|
$result = FriendRequests::update(['status'=>2],['id'=>$friend_requests_id]);
|
|
}
|
|
|
|
//发送消息通知
|
|
$data=[
|
|
'to_user_id'=>$friend_requests_info['from_user_id'],
|
|
'from_user_id'=>$userId,
|
|
'type'=>"2",
|
|
'status'=>$status,
|
|
];
|
|
Hook::listen('handle_friend_notifications', $data);
|
|
|
|
|
|
return ['code'=>1,'msg'=>'处理成功'];
|
|
}
|
|
|
|
/**
|
|
* 检查是否为好友关系
|
|
*/
|
|
public static function isFriend($userId, $friendId)
|
|
{
|
|
return self::where(
|
|
[
|
|
'user_id'=> $friendId,
|
|
'friend_id'=> $userId,
|
|
'status'=> self::STATUS_NORMAL
|
|
])->count() > 0;
|
|
}
|
|
|
|
/*
|
|
* 获取信息
|
|
*/
|
|
public static function getLevel($userId, $friendId){
|
|
$info = self::where(
|
|
[
|
|
'user_id'=> $userId,
|
|
'friend_id'=> $friendId,
|
|
'status'=> self::STATUS_NORMAL
|
|
])->find();
|
|
return $info;
|
|
}
|
|
|
|
|
|
/**
|
|
* 添加好友
|
|
*/
|
|
public static function addFriend($userId, $friendId, $remark = '', $groupId = 0)
|
|
{
|
|
// 检查是否已存在好友关系
|
|
$exists = self::where(
|
|
['user_id'=>$userId,'friend_id'=>$friendId]
|
|
)->find();
|
|
|
|
if ($exists) {
|
|
if ($exists->status == self::STATUS_DELETED) {
|
|
// 如果之前删除过,恢复关系
|
|
$exists->save([
|
|
'status' => self::STATUS_NORMAL,
|
|
]);
|
|
}
|
|
return $exists;
|
|
} else {
|
|
// 创建双向好友关系
|
|
$relation1 = self::create([
|
|
'user_id' => $userId,
|
|
'friend_id' => $friendId,
|
|
'status' => self::STATUS_NORMAL,
|
|
]);
|
|
|
|
$relation2 = self::create([
|
|
'user_id' => $friendId,
|
|
'friend_id' => $userId,
|
|
'status' => self::STATUS_NORMAL,
|
|
]);
|
|
|
|
return $relation1;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除好友
|
|
*/
|
|
public static function deleteFriend($userId, $friendId)
|
|
{
|
|
// 删除双向关系
|
|
self::where(
|
|
['user_id'=>$userId,'friend_id'=>$friendId]
|
|
)->delete();
|
|
|
|
self::where(
|
|
['user_id'=>$friendId,'friend_id'=>$userId]
|
|
)->delete();
|
|
}
|
|
|
|
|
|
/*
|
|
* 增加亲密度
|
|
*/
|
|
public static function addFriendDensity($userId,$friendId,$intimacy_value,$send_type){
|
|
|
|
|
|
//如果不是礼物赠送 判断是不是达到每日上限值
|
|
if ($send_type != 'gifts'){
|
|
if (in_array($send_type, ['text','image','file','audio','video'])){
|
|
//判断文本
|
|
$intimacy_day_info = IntimacyDay::get(['type'=>"1"]);
|
|
$intimacy_friend_info = IntimacyFriend::get(['user_id'=>$userId,'friend_id'=>$friendId,'type'=>"1"]);
|
|
if (!empty($intimacy_friend_info)){
|
|
if ($intimacy_friend_info['intimacy'] >= $intimacy_day_info['upper']){
|
|
return ['code'=>0,'msg'=>'亲密度已达到上限'];
|
|
}else{
|
|
IntimacyFriend::where(['user_id'=>$userId,'friend_id'=>$friendId,'type'=>"1",'day'=>date('Y-m-d')])->setInc('intimacy',$intimacy_value);
|
|
IntimacyFriend::where(['user_id'=>$friendId,'friend_id'=>$userId,'type'=>"1",'day'=>date('Y-m-d')])->setInc('intimacy',$intimacy_value);
|
|
}
|
|
}else{
|
|
IntimacyFriend::create(['user_id'=>$userId,'friend_id'=>$friendId,'type'=>"1",'intimacy'=>$intimacy_value,'day'=>date('Y-m-d')]);
|
|
IntimacyFriend::create(['user_id'=>$friendId,'friend_id'=>$userId,'type'=>"1",'intimacy'=>$intimacy_value,'day'=>date('Y-m-d')]);
|
|
}
|
|
}else{
|
|
//判断音视频
|
|
$intimacy_day_info = IntimacyDay::get(['type'=>"2"]);
|
|
$intimacy_friend_info = IntimacyFriend::get(['user_id'=>$userId,'friend_id'=>$friendId,'type'=>"2",'day'=>date('Y-m-d')]);
|
|
if (!empty($intimacy_friend_info)){
|
|
if ($intimacy_friend_info['intimacy'] >= $intimacy_day_info['upper']){
|
|
return ['code'=>0,'msg'=>'亲密度已达到上限'];
|
|
}else{
|
|
IntimacyFriend::where(['user_id'=>$userId,'friend_id'=>$friendId,'type'=>"2",'day'=>date('Y-m-d')])->setInc('intimacy',$intimacy_value);
|
|
IntimacyFriend::where(['user_id'=>$friendId,'friend_id'=>$userId,'type'=>"2",'day'=>date('Y-m-d')])->setInc('intimacy',$intimacy_value);
|
|
}
|
|
}else{
|
|
IntimacyFriend::create(['user_id'=>$userId,'friend_id'=>$friendId,'type'=>"2",'intimacy'=>$intimacy_value,'day'=>date('Y-m-d')]);
|
|
IntimacyFriend::create(['user_id'=>$friendId,'friend_id'=>$userId,'type'=>"2",'intimacy'=>$intimacy_value,'day'=>date('Y-m-d')]);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
self::where(
|
|
['user_id'=>$userId,'friend_id'=>$friendId]
|
|
)->setInc('intimacy',$intimacy_value);
|
|
|
|
self::where(
|
|
['user_id'=>$friendId,'friend_id'=>$userId]
|
|
)->setInc('intimacy',$intimacy_value);
|
|
|
|
}
|
|
/*
|
|
* 监听是否亲密度升级
|
|
*/
|
|
public static function checkIntimacyUpgrade($userId,$friendId,$intimacy_value=0){
|
|
|
|
//查询当前亲密度
|
|
$friend_info = self::where(['user_id'=>$userId,'friend_id'=>$friendId])->find();
|
|
if (!empty($friend_info)){
|
|
//查询等级配置
|
|
$intimacy_config = IntimacyConfig::order("intimacy asc")->select();
|
|
$level = 0;
|
|
foreach ($intimacy_config as $row){
|
|
if ($row['intimacy'] <= $friend_info['intimacy']){
|
|
$level = $row['level'];
|
|
}
|
|
}
|
|
//更新亲密度等级
|
|
self::update(['intimacy_level'=>$level],['user_id'=>$userId,'friend_id'=>$friendId]);
|
|
self::update(['intimacy_level'=>$level],['user_id'=>$friendId,'friend_id'=>$userId]);
|
|
}
|
|
}
|
|
|
|
|
|
}
|