Files
midi-php/application/api/model/Decorate.php

420 lines
18 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\api\model;
use think\Model;
use think\Db;
use think\Session;
/**
* 装饰模型
*/
class Decorate extends Model
{
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = true;
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
// 定义字段类型 1头像框 2坐骑 3麦圈 6个人靓号 7房间靓号 8公会靓号
public $TypeArray = [1=>'头像框',2=>'坐骑',3=>'麦圈',6=>'个人靓号',7=>'房间靓号',8=>'公会靓号'];
protected $FromType = [1=>'购买',2=>'后台赠送',3=>'礼盒开奖',4=>'好友赠送',5=>'首充赠送',6=>'天降好礼获得',7=>'财富等级特权赠送',8=>'新人充值好礼'];
public function __construct($data = [])
{
parent::__construct($data);
$this->table = 'vs_decorate';
}
//获取装扮类型
public function get_type_list($have_hot = 0)
{
$label = $this->TypeArray;
if ($have_hot) {
$label = [100 => '热门'] + array_filter($this->TypeArray, fn($key) => $key != 100, ARRAY_FILTER_USE_KEY);
}
$data = [];
$i = 0;
foreach ($label as $k => $v) {
$data[$i]['id'] = $k;
$data[$i]['name'] = $v;
++$i;
}
return ['code' => 1, 'msg' => '获取成功', 'data' => $data];
}
//拉去装扮列表
public function get_decorate_list($type=100)
{
$map = [];
if ($type == 100) {
$map = [
'a.delete_time' => 0,
'a.show_status' => 1,
'a.is_buy' => 1,
'a.is_hot' => 1,
'b.is_delete' => 1,
];
}elseif (in_array($type, [6,7,8])) {
$map = [
'a.delete_time' => 0,
'a.show_status' => 1,
'a.is_buy' => 1,
'b.is_delete' => 1,
'a.type' => $type,
'a.is_user_buy' => 2
];
}else{
$map = [
'a.delete_time' => 0,
'a.show_status' => 1,
'a.is_buy' => 1,
'b.is_delete' => 1,
'a.type' => $type,
];
}
$list = DB::name($this->table)->alias('a')->join('fa_vs_decorate_price b', 'a.did = b.did')->field('a.did,a.title,a.type,a.base_image,a.play_image,min(b.price) as price,special_num')->where($map)->group('b.did')->order('price asc')->select();
foreach ($list as $k => &$v) {
$v['base_image'] = localpath_to_netpath($v['base_image']);
$v['play_image'] = localpath_to_netpath($v['play_image']);
$v['price'] = (int)$v['price'];
}
return ['code' => 1, 'msg' => '获取成功', 'data' => $list];
}
// 获取礼物详情
public function get_decorate_detail($did)
{
$decorate = DB::name($this->table)->where(['did' => $did, 'delete_time' => 0, 'show_status' => 1])->find();
if (!$decorate) {
return ['code' => 0, 'msg' => '参数错误'];
}
$decorate_price = DB::name('vs_decorate_price')->where(['did' => $did, 'is_delete' => 1])->order('day asc')->select();
if (!$decorate_price) {
return ['code' => 0, 'msg' => '参数错误'];
}
$result['title'] = $decorate['title'];
foreach ($decorate_price as $k => $v) {
$result['price_list'][$k]['price'] = (int)$v['price'];
$result['price_list'][$k]['discount'] = $v['discount'];
$result['price_list'][$k]['day'] = $v['day'];
//月
$result['price_list'][$k]['month'] = $v['day']/30;
//有效期至
$result['price_list'][$k]['end_time'] = date('Y-m-d',strtotime("+".$v['day']." day"));
}
return ['code' => 1, 'msg' => '获取成功', 'data' => $result];
}
//购买/赠送 装扮
/*
* @param $uid (接受)用户id
* @param $did 装扮id
* @param $day 天数
* @param $give_uid 赠送用户id 0 为系统赠送
* @param $from_type 购买来源 1购买 2后台赠送 3礼盒开奖 4好友赠送 7财富等级特权赠送
* @param $log_remark 日志备注
*
*/
public function pay_decorate($uid, $did, $day,$from_type=1,$give_uid=0,$log_remark=""){
if(empty($log_remark)){
$log_remark = $this->FromType[$from_type];
}
$user_info = db::name('user')->find($uid);
if(!$user_info){
return ['code' => 0, 'msg' => '参数错误', 'data' => null];
}
//该装扮是否存在
$map = [];
$map = [
'did' => $did,
'delete_time' => 0,
];
$decorate_info = DB::name($this->table)->where($map)->find();
if(!$decorate_info){
return ['code' => 0, 'msg' => '该装扮不存在'.$did, 'data' => null];
}
//该天数是否存在
$map = [];
$map = [
'did' => $did,
'day' => $day,
'is_delete' => 1,
];
$decorate_price_info = db::name('vs_decorate_price')->where($map)->find();
if(!$decorate_price_info){
return ['code' => 0, 'msg' => '该装扮天数不存在', 'data' => null];
}
if($decorate_info['type'] == 6 && $decorate_info['is_user_buy'] == 1){
return ['code' => 0, 'msg' => '该个人靓号已被购买', 'data' => null];
}
if($decorate_info['type'] == 7 && $decorate_info['is_user_buy'] == 1){
return ['code' => 0, 'msg' => '该房间靓号已被购买', 'data' => null];
}
if($decorate_info['type'] == 8 && $decorate_info['is_user_buy'] == 1){
return ['code' => 0, 'msg' => '该公会靓号已被购买', 'data' => null];
}
$start_time = $now_time = time();
Db::startTrans();
try {
$pay_price = $decorate_price_info['price'];
if($from_type ==1){
$log_remark = "购买";
//购买操作用户资金 金币是否足够
$reslut = model('common/UserWallet')->change_user_money($user_info['id'], $pay_price, 1, model('common/UserWallet')::OPERATION_DECORATION,model('common/UserWallet')::ChangeTypeLable(model('common/UserWallet')::OPERATION_DECORATION));
if ($reslut['code'] != 1) {
Db::rollback();
return ['code' => 0, 'msg' => $reslut['msg'], 'data' => null];
}
} else if($from_type == 4 && $give_uid){
//购买操作用户资金 金币是否足够
$give_user_info = db::name('user')->find($give_uid);
if(!$give_user_info){
return ['code' => 0, 'msg' => '赠送用户不存在', 'data' => null];
}
$log_remark = $give_user_info['nickname']." 赠送";
$reslut = model('common/UserWallet')->change_user_money($give_uid, $pay_price, 1, model('common/UserWallet')::OPERATION_DECORATION,model('common/UserWallet')::ChangeTypeLable(model('common/UserWallet')::OPERATION_DECORATION));
if ($reslut['code'] != 1) {
Db::rollback();
return ['code' => 0, 'msg' => $reslut['msg'], 'data' => null];
}
}
//该用户是否有该装扮
$map = [];
$map = [
'user_id' => $user_info['id'],
'did' => $did,
];
$user_decorate_data = db::name('vs_user_decorate')->where($map)->find();
$change_time = 0;
if(empty($user_decorate_data)){
$data = [];
$data['user_id'] = $uid;
$data['did'] = $did;
$data['type'] = $decorate_info['type'];
if($decorate_price_info['day'] == 0){//如果礼物购买配置的天数为0则永久有效
$data['is_using'] = 1;
$data['end_time'] = 0;
}else{
$change_time = $day * 24 * 3600;
$data['is_using'] = 2;
$data['end_time'] = $now_time + $change_time;
}
$data['createtime'] = $now_time;
$data['special_num'] = $decorate_info['special_num'];
$result = db::name('vs_user_decorate')->insert($data);
}else{
if($decorate_price_info['day'] > 0){//是不是永久礼物
if($decorate_price_info['price']<=0 && $user_decorate_data['end_time'] > $now_time + $change_time){
return ['code' => 0, 'msg' => '您已购买过此装扮,请勿重复购买', 'data' => null];
}
$change_time = $day * 24 * 3600;
$update['is_using'] = 2;
$update['end_time'] = $user_decorate_data['end_time'] + $change_time;
$result = db::name('vs_user_decorate')->where('udid', $user_decorate_data['udid'])->update($update);
$start_time = $update['end_time'];
}
}
if(!$result){
Db::rollback();
return ['code' => 0, 'msg' => '操作失败', 'data' => null];
}
//记录日志
$insert_data = [];
$insert_data['user_id'] = $uid;
$insert_data['type'] = $decorate_info['type'];
$insert_data['did'] = $did;
$insert_data['createtime'] = $now_time;
$insert_data['remark'] = $log_remark;
$insert_data['from_type'] = $from_type;
$insert_data['start_time'] = $start_time;
$insert_data['end_time'] = $start_time + $change_time;
if($decorate_price_info['day'] ==0){
$insert_data['end_time'] = 0; //永久礼物
}
$insert_data['day_num'] = $day;
$insert_data['pay_price'] = $pay_price ;
$insert_data['special_num'] = $decorate_info['special_num'];
$insert_data['give_uid'] = $give_uid;
$result = db::name('vs_user_decorate_log')->insert($insert_data);
if(!$result){
Db::rollback();
return ['code' => 0, 'msg' => '操作失败', 'data' => null];
}
//如果购买个人或房间靓号
if($decorate_info['type'] == 6 || $decorate_info['type'] == 7 || $decorate_info['type'] == 8) {
Db::name('vs_decorate')->where('did', $decorate_info['did'])->update(['is_user_buy' => 1, 'updatetime' => time()]);
}
// 提交事务
Db::commit();
return ['code' => 1, 'msg' => "购买成功", 'data' => null];
}catch (\Exception $e) {
// 回滚事务
Db::rollback();
return ['code' => 0, 'msg' => "请重试", 'data' => null];
}
}
//用户装扮列表
public function user_decorate($uid, $type,$page=1, $limit=10){
$reslut = db::name('vs_user_decorate')
->alias('ud')->join('vs_decorate d', 'ud.did = d.did')
->field('ud.udid,ud.user_id,ud.is_using,ud.end_time,ud.is_perpetual,ud.special_num,d.title,d.base_image,d.play_image,d.type')
->where('ud.user_id', $uid)
->where('ud.type', $type)
->where(['ud.end_time'=>[">=",time()]])
->page($page, $limit)
->select();
foreach($reslut as $k => &$v){
$remaining_day = ceil(($v['end_time'] - time())/86400);
if($remaining_day <= 0){
$remaining_day = 0;
}
//剩余天数 取整
$v['remaining_day'] = $v['is_perpetual']==1 ? '永久' :$remaining_day;
$v['end_time'] = date('Y-m-d H:i:s', $v['end_time']);
}
return ['code' => 1, 'msg' => "获取成功", 'data' => $reslut];
}
// 设置用户装修
public function set_user_decorate($uid,$udid)
{
$map = [];
$map = [
'user_id' => $uid,
'udid' => $udid
];
$info = db::name('vs_user_decorate')->where($map)->find();
if (empty($info)) {
return ['code' => 0, 'msg' => '装扮不存在', 'data' => null];
}
if ($info['end_time'] < time()) {
return ['code' => 0, 'msg' => '装扮已过期', 'data' => null];
}
Db::startTrans();
try {
//清理该类型装扮使用状态
$map = [];
$map = [
'user_id' => $uid,
'type' => $info['type']
];
$data = [];
$data['is_using'] = 2;
$data['updatetime'] = time();
$reslut = Db::name('vs_user_decorate')->where($map)->update($data);
if (!$reslut) {
Db::rollback();
return ['code' => 0, 'msg' => "设置失败", 'data' => null];
}
//设置使用中状态
$map = [];
$map = [
'udid' => $info['udid']
];
$data = [];
$data['is_using'] = 1;
$data['updatetime'] = time();
$reslut = Db::name('vs_user_decorate')->where($map)->update($data);
if (!$reslut) {
Db::rollback();
return ['code' => 0, 'msg' => "设置失败", 'data' => null];
}
// 提交事务
Db::commit();
//推送信息去所在房间
//查询当前所在房间
$room_id = db::name('vs_room_visitor')->where(['user_id' => $uid])->order('id' , 'desc')->value('room_id');
if($room_id){
//当前用户信息
//推送信息
$text['jia_jia'] = model('Decorate')->user_decorate_detail($uid,2);
$text['FromUserInfo'] = db::name('user')->where('id',$uid)->field('id as user_id,nickname,avatar,sex')->find();
$text['FromUserInfo']['dress'] = model('Decorate')->user_decorate_detail($uid,1);
$text['FromUserInfo']['mic_dress'] = model('Decorate')->user_decorate_detail($uid,4);
$text['FromUserInfo']['chat_dress'] = model('Decorate')->user_decorate_detail($uid,5);
$text['text'] = '用户 ' . $text['FromUserInfo']['nickname'] .' 修改了信息';
model('Chat')->sendMsg(1035,$room_id,$text,$uid);
}
return ['code' => 1, 'msg' => "设置成功", 'data' => null];
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
return ['code' => 0, 'msg' => "设置失败", 'data' => null];
}
}
//取消装扮
public function cancel_user_decorate($uid, $type)
{
//查询当前用户装扮
$user_decorate = db::name('vs_user_decorate')->where(['user_id' => $uid, 'type' => $type,'is_using'=>1,'end_time'=>['>',time()]])->find();
if (!$user_decorate) {
return ['code' => 0, 'msg' => "取消装扮失败,当前用户还未装扮", 'data' => null];
}
$data = [];
$data['is_using'] = 2;
$data['updatetime'] = time();
$reslut = Db::name('vs_user_decorate')->where('udid',$user_decorate['udid'])->update($data);
if (!$reslut) {
return ['code' => 0, 'msg' => "取消装扮失败", 'data' => null];
}
return ['code' => 1, 'msg' => "取消成功", 'data' => null];
}
//用户装扮详情
/*
* @param $id 对象id
* @param $type 装扮类型 1头像框 2坐骑 3麦圈 6个人靓号 7房间靓号 8公会靓号
*/
public function user_decorate_detail($id, $type){
//根据$type 组装查询条件
$reslut = "";
if($type == 7){//7房间靓号 8工会靓号
$room = db::name('vs_room')->where('id', $id)->field('user_id,room_number')->find();
if(empty($room)){
return $reslut;
}
$map = [
'user_id' => $room['user_id']??0,
'type' => $type,
'is_using' => 1
];
$reslut = db::name('vs_user_decorate')->where($map)->where('end_time',['>=',time()],'or')->value('special_num') ?? $room['room_number'];
}elseif($type == 8){
$guild = db::name('vs_guild')->where('id', $id)->field('user_id,guild_special_id')->find();
$map = [
'user_id' => $guild['user_id']??0,
'type' => $type,
'is_using' => 1
];
$reslut = db::name('vs_user_decorate')->where($map)->where('end_time',['>=',time()],'or')->value('special_num') ?? $guild['guild_special_id'];
}elseif($type == 6){
$map = [
'user_id' => $id??0,
'type' => $type,
'is_using' => 1
];
$user_code = db::name('user')->where('id', $id)->value('user_code');
$reslut = db::name('vs_user_decorate')->where($map)->where('end_time',['>=',time()],'or')->value('special_num') ?? $user_code;
}else{
$map = [
'user_id' => $id??0,
'type' => $type,
'is_using' => 1
];
$did = db::name('vs_user_decorate')->where($map)->where('end_time',['>=',time()],'or')->value('did');
if(empty($did)){
$reslut = '';//返回数据
}else{
$reslut = db::name('vs_decorate')->where('did', $did)->value('play_image');
}
}
return $reslut;
}
}