351 lines
14 KiB
PHP
351 lines
14 KiB
PHP
<?php
|
||
|
||
namespace app\api\model;
|
||
|
||
use think\Db;
|
||
use think\Model;
|
||
|
||
class UserCp extends Model
|
||
{
|
||
|
||
/*
|
||
* 检测是否赠送的Cp礼物
|
||
* @param $from_user_id 送礼用户ID
|
||
* @param $gift_id 礼物ID
|
||
* @param $to_user_id 接收用户ID
|
||
* @param $room_id 房间ID
|
||
* @return int 0-无,1-等待回应,2-已建立Cp空间
|
||
*/
|
||
public function checkGift($from_user_id, $gift_id, $to_user_id, $room_id, $num)
|
||
{
|
||
//查询是否已经存在Cp空间
|
||
$rees = Db::name('user_cp_zone')->where(['user_id1' => $from_user_id,'user_id2' => $to_user_id,'status' => 1])->find();
|
||
if(!$rees){
|
||
$rees = Db::name('user_cp_zone')->where(['user_id1' => $to_user_id,'user_id2' => $from_user_id,'status' => 1])->find();
|
||
}
|
||
if($rees){
|
||
$this->addCpLevel($from_user_id, $to_user_id, $room_id, $gift_id, $rees['id'], $num);
|
||
return 0;
|
||
}
|
||
|
||
$cp_gift_id = explode(',', get_system_config_value('cp_gift_id'));
|
||
if(!in_array($gift_id, $cp_gift_id)){
|
||
return 0;
|
||
}
|
||
//查询收礼人有没有给送礼人送过cp礼物 status:0-待回应,1-建交成功,2-已取消
|
||
$res = Db::name('user_cp_find')->where(['from_user_id' => $to_user_id, 'to_user_id' => $from_user_id,'status' => 0])->find();
|
||
if($res){
|
||
//创建推送信息1:回应送礼 并创建Cp空间,
|
||
$data = [
|
||
'from_user_id' => $from_user_id,
|
||
'to_user_id' => $to_user_id,
|
||
'gift_id' => $gift_id,
|
||
'createtime' => time(),
|
||
'status' => 1
|
||
];
|
||
$r = Db::name('user_cp_find')->insertGetId($data);
|
||
//修改状态
|
||
Db::name('user_cp_find')->where(['id' => $res['id']])->update(['status' => 1]);
|
||
|
||
//创建Cp空间
|
||
$data = [
|
||
'user_id1' => $from_user_id,
|
||
'user_id2' => $to_user_id,
|
||
'createtime' => time(),
|
||
'status' => 1,//1-已建立Cp空间,2-已取消
|
||
'level' => 1,
|
||
//经验值
|
||
'exp' => 0,
|
||
];
|
||
$re = Db::name('user_cp_zone')->insertGetId($data);
|
||
|
||
//添加一个记录
|
||
$datas = [
|
||
'room_id' => $room_id,
|
||
'from_user_id' => $from_user_id,
|
||
'to_user_id' => $to_user_id,
|
||
'gift_id' => 0,
|
||
'num' => 0,
|
||
'cp_zone_id' => $re,
|
||
'exp' => 0,
|
||
'exp_total' => 0,
|
||
'remark' => '建立了心动空间,让我们相爱每一天吧!',
|
||
'createtime' => time(),
|
||
];
|
||
Db::name('user_cp_gift_log')->insert($datas);
|
||
|
||
// 检查并更新CP空间状态
|
||
$this->updateCpZoneStatus($from_user_id, $re, $res['id'] ?? 0);
|
||
$this->updateCpZoneStatus($to_user_id, $re, $res['id'] ?? 0);
|
||
|
||
// 检查并更新CP查找记录状态
|
||
$this->updateCpFindStatus($to_user_id, $r, $res['id'] ?? 0);
|
||
$this->updateCpFindStatus($from_user_id, $r, $res['id'] ?? 0);
|
||
|
||
|
||
// 给两个用户添加CP装扮
|
||
$pendant_ids = Db::name('user_cp_level')->where(['deletetime' => 0])->order('level asc')->value('pendant_id');
|
||
$pendant_id = Db::name('vs_decorate_price')->where(['id' => $pendant_ids])->value('did');
|
||
if($pendant_id){
|
||
$this->assignCpDecoration([$from_user_id, $to_user_id], 11, $pendant_id); // 11为CP装扮类型
|
||
}
|
||
|
||
// 给两个用户添加CP特效
|
||
$rights_icon_ids = Db::name('user_cp_level')->where(['deletetime' => 0])->order('level asc')->value('rights_icon_id');
|
||
$rights_icon_id = Db::name('vs_decorate_price')->where(['id' => $rights_icon_ids])->value('did');
|
||
if($rights_icon_id){
|
||
$this->assignCpDecoration([$from_user_id, $to_user_id], 10, $rights_icon_id); // 10为CP特效类型
|
||
}
|
||
|
||
//给前端推送
|
||
return 2;
|
||
}else{//创建推送信息2:表达心动信号
|
||
$ress = Db::name('user_cp_find')
|
||
->where(['from_user_id' => $from_user_id,'status' => 0])
|
||
->select();
|
||
if($ress){
|
||
//把状态改为2 status:0-待回应,1-建交成功,2-已取消,
|
||
foreach ($ress as $v){
|
||
Db::name('user_cp_find')->where(['id' => $v['id']])->update(['status' => 2]);
|
||
}
|
||
}
|
||
|
||
$data = [
|
||
'from_user_id' => $from_user_id,
|
||
'to_user_id' => $to_user_id,
|
||
'gift_id' => $gift_id,
|
||
'createtime' => time(),
|
||
];
|
||
Db::name('user_cp_find')->insert($data);
|
||
//给前端推送
|
||
return 1;
|
||
}
|
||
}
|
||
|
||
|
||
/*
|
||
* 更新CP空间状态
|
||
* @param int $user_id 用户ID
|
||
* @param int $current_zone_id 当前创建的CP空间ID
|
||
* @param int $related_find_id 相关的查找记录ID
|
||
*/
|
||
private function updateCpZoneStatus($user_id, $current_zone_id)
|
||
{
|
||
$zone = Db::name('user_cp_zone')
|
||
->where(['status' => 1])
|
||
->where('(user_id1 = ' . $user_id . ' OR user_id2 = ' . $user_id . ')')
|
||
->find();
|
||
|
||
if ($zone && $current_zone_id != $zone['id']) {
|
||
Db::name('user_cp_zone')->where(['id' => $zone['id']])->update(['status' => 2]);
|
||
}
|
||
}
|
||
|
||
/*
|
||
* 更新CP查找记录状态
|
||
* @param int $user_id 用户ID
|
||
* @param int $current_find_id 当前创建的查找记录ID
|
||
* @param int $related_find_id 相关的查找记录ID
|
||
*/
|
||
private function updateCpFindStatus($user_id, $current_find_id, $related_find_id = 0)
|
||
{
|
||
$find_records = Db::name('user_cp_find')
|
||
->where(['status' => 1])
|
||
->where('(from_user_id = ' . $user_id . ' OR to_user_id = ' . $user_id . ')')
|
||
->select();
|
||
|
||
if ($find_records) {
|
||
foreach ($find_records as $record) {
|
||
if ($record['id'] != $current_find_id && $record['id'] != $related_find_id) {
|
||
Db::name('user_cp_find')->where(['id' => $record['id']])->update(['status' => 2]);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
/*
|
||
* 为CP用户分配装扮或特效
|
||
* @param array $user_ids 用户ID数组
|
||
* @param int $type 装扮类型(10-特效, 11-装扮)
|
||
* @param int $did 装扮ID
|
||
*/
|
||
private function assignCpDecoration($user_ids, $type, $did)
|
||
{
|
||
// 先检查用户是否已有该装扮,如果有则设为未使用
|
||
foreach ($user_ids as $user_id) {
|
||
$existing_decoration = Db::name('vs_user_decorate')
|
||
->where([
|
||
'user_id' => $user_id,
|
||
'type' => $type,
|
||
'did' => $did,
|
||
'is_using' => 1
|
||
])
|
||
->find();
|
||
|
||
if ($existing_decoration) {
|
||
Db::name('vs_user_decorate')
|
||
->where(['udid' => $existing_decoration['udid']])
|
||
->update(['is_using' => 2]);
|
||
}
|
||
}
|
||
|
||
// 给用户添加新装扮
|
||
$decorate_data = [
|
||
'type' => $type,
|
||
'did' => $did,
|
||
'is_using' => 1,
|
||
'end_time' => 0,
|
||
'is_perpetual' => 1,
|
||
'createtime' => time(),
|
||
];
|
||
|
||
foreach ($user_ids as $user_id) {
|
||
$insert_data = array_merge(['user_id' => $user_id], $decorate_data);
|
||
Db::name('vs_user_decorate')->insert($insert_data);
|
||
}
|
||
}
|
||
|
||
|
||
|
||
/*
|
||
* 送Cp礼物 增加Cp 经验 判断是否升级
|
||
* @param $from_user_id 送礼用户ID
|
||
* @param $to_user_id 接收用户ID
|
||
* @param $gift_id 礼物ID
|
||
* @param $room_id 房间ID
|
||
* @param $cp_zone_id Cp空间ID
|
||
*/
|
||
public function addCpLevel($from_user_id, $to_user_id, $room_id, $gift_id, $cp_zone_id, $num)
|
||
{
|
||
$jinbi_arr = explode(',', get_system_config_value('cp_exp_rate'));
|
||
$jinbi = $jinbi_arr[0];
|
||
$jinyan = $jinbi_arr[1];
|
||
$exp = round(db::name('vs_gift')->where(['gid' => $gift_id])->value('gift_price') * $num / $jinbi * $jinyan, 2);
|
||
$yuan_exp = Db::name('user_cp_zone')->where(['id' => $cp_zone_id])->value('exp');
|
||
$zone_level = Db::name('user_cp_zone')->where(['id' => $cp_zone_id])->value('level');
|
||
$toUserNickname =db::name('user')->where(['id' => $to_user_id])->value('nickname')??'未知用户';
|
||
$giftName = db::name('vs_gift')->where(['gid' => $gift_id])->value('gift_name')??'未知礼物';
|
||
//添加一个记录
|
||
$datas = [
|
||
'room_id' => $room_id,
|
||
'from_user_id' => $from_user_id,
|
||
'to_user_id' => $to_user_id,
|
||
'gift_id' => $gift_id,
|
||
'num' => $num,
|
||
'cp_zone_id' => $cp_zone_id,
|
||
'exp' => $exp,
|
||
'exp_total' => $yuan_exp + $exp,
|
||
'remark' => '送给'.$toUserNickname.$num.'个'.$giftName.',获得'.$exp.'经验值,总经验值增加至'.($yuan_exp + $exp),
|
||
'createtime' => time(),
|
||
];
|
||
Db::name('user_cp_gift_log')->insert($datas);
|
||
//判断是否升级
|
||
$level = Db::name('user_cp_level')->where(['change_value' => ['<=', $yuan_exp + $exp],'deletetime' => 0])
|
||
->order('change_value desc')->field('level,pendant_id,rights_icon_id')->find();
|
||
if($level['level'] != $zone_level){//登记不一致 (升级)
|
||
//修改Cp空间等级
|
||
$data['level'] = $level['level'];
|
||
//给用户修改Cp装扮
|
||
if($level['pendant_id']){
|
||
//先检查是否已有该装扮
|
||
$pendant_id = Db::name('vs_decorate_price')->where(['id' => $level['pendant_id']])->value('did');
|
||
if($pendant_id){
|
||
$this->assignCpDecoration([$from_user_id, $to_user_id], 11, $pendant_id); // 11为CP装扮类型
|
||
}
|
||
|
||
}else{//当前等级没有装扮挂件把原有的装扮取消掉
|
||
$decorate_data = [
|
||
'type' => 11, // 装扮类型11-cp装扮
|
||
'is_using' => 2, // 1-使用中,2-未使用
|
||
'updatetime' => time(),
|
||
];
|
||
foreach ([$from_user_id, $to_user_id] as $user_id) {
|
||
$insert_data = array_merge(['user_id' => $user_id], $decorate_data);
|
||
Db::name('vs_user_decorate')->where(['type' => 11])->update($insert_data);
|
||
}
|
||
|
||
}
|
||
//给用户修改Cp特效
|
||
if($level['rights_icon_id']){
|
||
//先检查是否已有该特效
|
||
$rights_icon_id = Db::name('vs_decorate_price')->where(['id' => $level['rights_icon_id']])->value('did');
|
||
if($rights_icon_id){
|
||
$this->assignCpDecoration([$from_user_id, $to_user_id], 10, $rights_icon_id); // 10为CP特效类型
|
||
}
|
||
}else{//当前等级没有特效挂件把原有的特效取消掉
|
||
$decorate_data = [
|
||
'type' => 10, // 装扮类型10-cp特效
|
||
'is_using' => 2, // 1-使用中,2-未使用
|
||
'updatetime' => time(),
|
||
];
|
||
foreach ([$from_user_id, $to_user_id] as $user_id) {
|
||
$insert_data = array_merge(['user_id' => $user_id], $decorate_data);
|
||
Db::name('vs_user_decorate')->where(['type' => 10])->update($insert_data);
|
||
}
|
||
}
|
||
|
||
//添加一个记录
|
||
$pendant = db::name('vs_decorate_price')->where('id',$level['pendant_id'])->field('did,day')->find();
|
||
if($pendant){
|
||
$pendant_title = '装扮-'.$pendant['day'].'天-'.db::name('vs_decorate')->where('did',$pendant['did'])->value('title');
|
||
$datas = [
|
||
'room_id' => $room_id,
|
||
'from_user_id' => $from_user_id,
|
||
'to_user_id' => $to_user_id,
|
||
'gift_id' => 0,
|
||
'num' => 0,
|
||
'cp_zone_id' => $cp_zone_id,
|
||
'exp' => 0,
|
||
'exp_total' => 0,
|
||
'remark' => '我们等级达到了'.$level['level'].'级,获得奖励'.$pendant_title,
|
||
'createtime' => time(),
|
||
];
|
||
Db::name('user_cp_gift_log')->insert($datas);
|
||
}
|
||
|
||
}
|
||
$data['exp'] = $yuan_exp + $exp;
|
||
Db::name('user_cp_zone')->where(['id' => $cp_zone_id])->update($data);
|
||
return true;
|
||
}
|
||
|
||
|
||
/*
|
||
* Cp空间
|
||
* @param $user_id 用户ID
|
||
*/
|
||
public function cpZone($user_id, $page, $page_limit)
|
||
{
|
||
$res = Db::name('user_cp_zone')->where(['user_id1' => $user_id,'status' => 1])->find();
|
||
if(!$res){
|
||
$res = Db::name('user_cp_zone')->where(['user_id2' => $user_id,'status' => 1])->find();
|
||
}
|
||
if($res){
|
||
$res['user_info1'] = Db::name('user')->where(['id' => $res['user_id1']])->field('id user_id,nickname,avatar')->find();
|
||
$res['user_info2'] = Db::name('user')->where(['id' => $res['user_id2']])->field('id user_id,nickname,avatar')->find();
|
||
//升下一级所需经验值
|
||
$next_level_exp = Db::name('user_cp_level')->where(['level' => ['>',$res['level']]])->order('level asc')->value('change_value');
|
||
$res['next_level_exp'] = $next_level_exp - $res['exp'];
|
||
if($res['next_level_exp'] < 0){
|
||
$res['next_level_exp'] = 0;
|
||
}
|
||
$res['pendant'] = model('api/Decorate')->user_decorate_detail($user_id, 11);//挂件(装扮)
|
||
//送礼记录
|
||
$res['gift_log'] = Db::name('user_cp_gift_log')->where(['cp_zone_id' => $res['id']])->order('id desc')->page($page,$page_limit)->select();
|
||
foreach ($res['gift_log'] as &$v){
|
||
if($v['gift_id']){
|
||
$v['gift_name'] = Db::name('vs_gift')->where(['gid' => $v['gift_id']])->value('gift_name');
|
||
}
|
||
$v['from_user_info'] = Db::name('user')->where(['id' => $v['from_user_id']])->field('id user_id,nickname,avatar')->find();
|
||
$v['to_user_info'] = Db::name('user')->where(['id' => $v['to_user_id']])->field('id user_id,nickname,avatar')->find();
|
||
}
|
||
return ['code' => 1, 'msg' => '成功', 'data' => $res ];
|
||
}else{
|
||
return ['code' => 0, 'msg' => '暂无Cp空间', 'data' => null];
|
||
}
|
||
}
|
||
|
||
|
||
} |