420 lines
17 KiB
PHP
420 lines
17 KiB
PHP
<?php
|
||
|
||
namespace app\api\model;
|
||
|
||
use think\Db;
|
||
use think\Model;
|
||
|
||
class Decorate extends Model
|
||
{
|
||
//装扮列表
|
||
// public function get_decorate_list($type, $page, $page_limit)
|
||
// {
|
||
// $page = intval($page);
|
||
// $page_limit = $page_limit < 30 ? $page_limit : 30;
|
||
|
||
// $map = [];
|
||
// $map[] = ['type', '=', $type];
|
||
// $map[] = ['is_buy', '=', 1];
|
||
// $map[] = ['show_status', '=', 1];
|
||
// $map[] = ['is_delete', '=', 1];
|
||
// $list = db::name('decorate')->field('did,title,type,base_image,play_image,price,day')->where($map)->order('price asc')->page($page, $page_limit)->select();
|
||
// foreach ($list as $k => &$v) {
|
||
// $v['base_image'] = localpath_to_netpath($v['base_image']);
|
||
// $v['play_image'] = localpath_to_netpath($v['play_image']);
|
||
// }
|
||
// return ['code' => 200, 'msg' => '获取成功', 'data' => $list];
|
||
// }
|
||
public function get_decorate_list($type, $page, $page_limit)
|
||
{
|
||
$page = intval($page);
|
||
$page_limit = $page_limit < 30 ? $page_limit : 30;
|
||
|
||
$map = [];
|
||
$map[] = ['a.type', '=', $type];
|
||
$map[] = ['a.show_status', '=', 1];
|
||
$map[] = ['a.is_buy', '=', 1];
|
||
$map[] = ['a.is_delete', '=', 1];
|
||
$map[] = ['b.is_delete', '=', 1];
|
||
$map[] = ['a.is_user_buy', '=', 2];
|
||
$list = db::name('decorate')->alias('a')->join('yy_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')->where($map)->group('b.did')->order('price asc')->page($page, $page_limit)->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' => 200, 'msg' => '获取成功', 'data' => $list];
|
||
}
|
||
|
||
//装扮详情
|
||
public function get_decorate_info($did){
|
||
$decorate_info = db::name('decorate')->field('did,title,type,base_image,play_image,special_num')->find($did);
|
||
|
||
$decorate_info['base_image'] = localpath_to_netpath($decorate_info['base_image']);
|
||
$decorate_info['play_image'] = localpath_to_netpath($decorate_info['play_image']);
|
||
$decorate_info['decorate_price'] = db::name('decorate_price')->where('did', $decorate_info['did'])->where('is_delete', 1)->field('price,day')->select();
|
||
foreach ($decorate_info['decorate_price'] as $k => &$v){
|
||
$v['price'] = (int)$v['price'];
|
||
}
|
||
|
||
return ['code' => 200, 'msg' => '获取成功', 'data' => $decorate_info];
|
||
}
|
||
|
||
|
||
//购买装扮
|
||
// public function pay_decorate($uid, $did, $num){
|
||
// $user_info = db::name('user')->find($uid);
|
||
// if(!$user_info){
|
||
// return ['code' => 201, 'msg' => '参数错误', 'data' => null];
|
||
// }
|
||
|
||
// if($user_info['is_teenager'] == 1){
|
||
// return ['code' => 201, 'msg' => '已开启青少年模式', 'data' => null];
|
||
// }
|
||
|
||
// //该装扮是否存在
|
||
// $map = [];
|
||
// $map[] = ['did', '=', $did];
|
||
// $map[] = ['is_buy', '=', 1];
|
||
// $map[] = ['show_status', '=', 1];
|
||
// $map[] = ['is_delete', '=', 1];
|
||
// $decorate_info = db::name('decorate')->where($map)->find();
|
||
// if(!$decorate_info){
|
||
// return ['code' => 201, 'msg' => '该装扮不存在', 'data' => null];
|
||
// }
|
||
|
||
// if($num < 1){
|
||
// return ['code' => 201, 'msg' => '购买数量不能小于1', 'data' => null];
|
||
// }
|
||
|
||
// $now_time = time();
|
||
// Db::startTrans();
|
||
// try {
|
||
// $pay_price = $decorate_info['price'] * $num;
|
||
// $day = $decorate_info['day'] * $num;
|
||
|
||
// //金币是否足够
|
||
// $reslut = model('admin/User')->change_user_money_by_user_info($user_info, -$pay_price, 2, 21, "购买装扮", $user_info['uid'], 0);
|
||
// if ($reslut['code'] != 200) {
|
||
// Db::rollback();
|
||
// return ['code' => 201, 'msg' => $reslut['msg'], 'data' => null];
|
||
// }
|
||
|
||
// //该用户是否有该装扮
|
||
// $map = [];
|
||
// $map[] = ['uid', '=', $uid];
|
||
// $map[] = ['did', '=', $did];
|
||
// $user_decorate_data = db::name('user_decorate')->where($map)->find();
|
||
// if(empty($user_decorate_data)){
|
||
// $data = [];
|
||
// $data['uid'] = $uid;
|
||
// $data['did'] = $did;
|
||
// $data['type'] = $decorate_info['type'];
|
||
// $data['is_using'] = 2;
|
||
// $data['add_time'] = $now_time;
|
||
// $data['end_time'] = $now_time + $day * 24 * 3600;
|
||
// $result = db::name('user_decorate')->insert($data);
|
||
// $start_time = $now_time;
|
||
// $change_time = $day * 24 * 3600;
|
||
// }else{
|
||
// if($user_decorate_data['end_time'] < $now_time){
|
||
// $start_time = $now_time;
|
||
// $change_time = $day * 24 * 3600;
|
||
// $update = [];
|
||
// $update['add_time'] = $now_time;
|
||
// $update['end_time'] = $now_time + $day * 24 * 3600;
|
||
// $result = db::name('user_decorate')->where('udid', $user_decorate_data['udid'])->update($update);
|
||
// }else{
|
||
// $start_time = $user_decorate_data['end_time'];
|
||
// if($start_time >= 1893465753){
|
||
// $change_time = 0;
|
||
// $result = 1;
|
||
// }else{
|
||
// $change_time = $day * 24 * 3600;
|
||
// $result = db::name('user_decorate')->where('udid', $user_decorate_data['udid'])->setInc('end_time', $change_time);
|
||
// }
|
||
// }
|
||
// }
|
||
|
||
// if(!$result){
|
||
// Db::rollback();
|
||
// return ['code' => 201, 'msg' => '操作失败', 'data' => null];
|
||
// }
|
||
|
||
// //记录日志
|
||
// $insert_data = [];
|
||
// $insert_data['uid'] = $uid;
|
||
// $insert_data['type'] = $decorate_info['type'];
|
||
// $insert_data['did'] = $did;
|
||
// $insert_data['add_time'] = $now_time;
|
||
// $insert_data['remark'] = '购买';
|
||
// $insert_data['from_type'] = 2;
|
||
// $insert_data['start_time'] = $start_time;
|
||
// $insert_data['end_time'] = $start_time + $change_time;
|
||
// $insert_data['day_num'] = $day;
|
||
// $insert_data['pay_price'] = $pay_price;
|
||
// $result = db::name('user_decorate_log')->insert($insert_data);
|
||
// if(!$result){
|
||
// Db::rollback();
|
||
// return ['code' => 201, 'msg' => '操作失败', 'data' => null];
|
||
// }
|
||
// // 提交事务
|
||
// Db::commit();
|
||
// return ['code' => 200, 'msg' => "购买成功", 'data' => null];
|
||
// } catch (\Exception $e) {
|
||
// dump($e);
|
||
// // 回滚事务
|
||
// Db::rollback();
|
||
// return ['code' => 201, 'msg' => "请重试", 'data' => null];
|
||
// }
|
||
|
||
// }
|
||
|
||
//购买装扮
|
||
public function pay_decorate($uid, $did, $day){
|
||
$user_info = db::name('user')->find($uid);
|
||
if(!$user_info){
|
||
return ['code' => 201, 'msg' => '参数错误', 'data' => null];
|
||
}
|
||
|
||
//该装扮是否存在
|
||
$map = [];
|
||
$map[] = ['did', '=', $did];
|
||
$map[] = ['is_buy', '=', 1];
|
||
$map[] = ['show_status', '=', 1];
|
||
$map[] = ['is_delete', '=', 1];
|
||
$decorate_info = db::name('decorate')->where($map)->find();
|
||
if(!$decorate_info){
|
||
return ['code' => 201, 'msg' => '该装扮不存在', 'data' => null];
|
||
}
|
||
|
||
//该天数是否存在
|
||
$map = [];
|
||
$map[] = ['did', '=', $did];
|
||
$map[] = ['day', '=', $day];
|
||
$map[] = ['is_delete', '=', 1];
|
||
$decorate_price_info = db::name('decorate_price')->where($map)->find();
|
||
if(!$decorate_price_info){
|
||
return ['code' => 201, 'msg' => '该装扮天数不存在', 'data' => null];
|
||
}
|
||
|
||
if($decorate_info['type'] == 6 && $decorate_info['is_user_buy'] == 1){
|
||
return ['code' => 201, 'msg' => '该个人靓号已被购买', 'data' => null];
|
||
}
|
||
if($decorate_info['type'] == 7 && $decorate_info['is_user_buy'] == 1){
|
||
return ['code' => 201, 'msg' => '该房间靓号已被购买', 'data' => null];
|
||
}
|
||
$now_time = time();
|
||
Db::startTrans();
|
||
try {
|
||
$pay_price = $decorate_price_info['price'];
|
||
|
||
//金币是否足够
|
||
$reslut = model('admin/User')->change_user_money_by_user_info($user_info, -$pay_price, 2, 21, "购买装扮", $user_info['uid'], 0);
|
||
if ($reslut['code'] != 200) {
|
||
Db::rollback();
|
||
return ['code' => 201, 'msg' => $reslut['msg'], 'data' => null];
|
||
}
|
||
|
||
//该用户是否有该装扮
|
||
$map = [];
|
||
$map[] = ['uid', '=', $uid];
|
||
$map[] = ['did', '=', $did];
|
||
$user_decorate_data = db::name('user_decorate')->where($map)->find();
|
||
if(empty($user_decorate_data)){
|
||
$data = [];
|
||
$data['uid'] = $uid;
|
||
$data['did'] = $did;
|
||
$data['type'] = $decorate_info['type'];
|
||
$data['is_using'] = 2;
|
||
$data['add_time'] = $now_time;
|
||
$data['end_time'] = $now_time + $day * 24 * 3600;
|
||
$data['special_num'] = $decorate_info['special_num'];
|
||
$result = db::name('user_decorate')->insert($data);
|
||
$start_time = $now_time;
|
||
$change_time = $day * 24 * 3600;
|
||
}else{
|
||
if($user_decorate_data['end_time'] < $now_time){
|
||
$start_time = $now_time;
|
||
$change_time = $day * 24 * 3600;
|
||
$update = [];
|
||
$update['add_time'] = $now_time;
|
||
$update['end_time'] = $now_time + $day * 24 * 3600;
|
||
$result = db::name('user_decorate')->where('udid', $user_decorate_data['udid'])->update($update);
|
||
}else{
|
||
$start_time = $user_decorate_data['end_time'];
|
||
if($start_time >= 1893465753){
|
||
$change_time = 0;
|
||
$result = 1;
|
||
}else{
|
||
$change_time = $day * 24 * 3600;
|
||
$result = db::name('user_decorate')->where('udid', $user_decorate_data['udid'])->setInc('end_time', $change_time);
|
||
}
|
||
}
|
||
}
|
||
|
||
if(!$result){
|
||
Db::rollback();
|
||
return ['code' => 201, 'msg' => '操作失败', 'data' => null];
|
||
}
|
||
|
||
//记录日志
|
||
$insert_data = [];
|
||
$insert_data['uid'] = $uid;
|
||
$insert_data['type'] = $decorate_info['type'];
|
||
$insert_data['did'] = $did;
|
||
$insert_data['add_time'] = $now_time;
|
||
$insert_data['remark'] = '购买';
|
||
$insert_data['from_type'] = 2;
|
||
$insert_data['start_time'] = $start_time;
|
||
$insert_data['end_time'] = $start_time + $change_time;
|
||
$insert_data['day_num'] = $day;
|
||
$insert_data['pay_price'] = $pay_price;
|
||
$insert_data['special_num'] = $decorate_info['special_num'];
|
||
$result = db::name('user_decorate_log')->insert($insert_data);
|
||
if(!$result){
|
||
Db::rollback();
|
||
return ['code' => 201, 'msg' => '操作失败', 'data' => null];
|
||
}
|
||
//如果购买个人或房间靓号
|
||
if($decorate_info['type'] == 6 || $decorate_info['type'] == 7) {
|
||
Db::name('decorate')->where('did', $decorate_info['did'])->update(['is_user_buy' => 1, 'update_time' => time()]);
|
||
}
|
||
// 提交事务
|
||
Db::commit();
|
||
return ['code' => 200, 'msg' => "购买成功", 'data' => null];
|
||
} catch (\Exception $e) {
|
||
dump($e);
|
||
// 回滚事务
|
||
Db::rollback();
|
||
return ['code' => 201, 'msg' => "请重试", 'data' => null];
|
||
}
|
||
|
||
}
|
||
|
||
//奖励装扮 is_perpetual 是否永久,1是2否
|
||
public function draw_user_decorate($uid, $did, $day, $remake, $is_perpetual = 2, $cp_id = 0){
|
||
$user_info = db::name('user')->field('uid')->find($uid);
|
||
if(!$user_info){
|
||
return ['code' => 201, 'msg' => '参数错误', 'data' => null];
|
||
}
|
||
|
||
$decorate_info = db::name('decorate')->where('did', $did)->find();
|
||
if(!$decorate_info){
|
||
return ['code' => 201, 'msg' => '参数错误', 'data' => null];
|
||
}
|
||
|
||
$now_time = time();
|
||
Db::startTrans();
|
||
try {
|
||
//是否有该装扮
|
||
$map = [];
|
||
$map[] = ['uid', '=', $uid];
|
||
$map[] = ['did', '=', $did];
|
||
$map[] = ['cp_id', '=', $cp_id];
|
||
$map[] = ['is_perpetual', '=', $is_perpetual];
|
||
$user_decorate_data = db::name('user_decorate')->where($map)->find();
|
||
if(empty($user_decorate_data)){
|
||
$data = [];
|
||
$data['uid'] = $uid;
|
||
$data['did'] = $did;
|
||
$data['type'] = $decorate_info['type'];
|
||
$data['is_using'] = 2;
|
||
$data['add_time'] = $now_time;
|
||
$data['end_time'] = $now_time + $day * 24 * 3600;
|
||
$data['cp_id'] = $cp_id;
|
||
$data['is_perpetual'] = $is_perpetual;
|
||
$result = db::name('user_decorate')->insert($data);
|
||
$start_time = $now_time;
|
||
$change_time = $day * 24 * 3600;
|
||
}else{
|
||
if($user_decorate_data['end_time'] < $now_time){
|
||
$start_time = $now_time;
|
||
$change_time = $day * 24 * 3600;
|
||
$update = [];
|
||
$update['add_time'] = $now_time;
|
||
$update['end_time'] = $now_time + $day * 24 * 3600;
|
||
$result = db::name('user_decorate')->where('udid', $user_decorate_data['udid'])->update($update);
|
||
}else{
|
||
$start_time = $user_decorate_data['end_time'];
|
||
if($start_time >= 1893465753){
|
||
$change_time = 0;
|
||
$result = 1;
|
||
}else{
|
||
$change_time = $day * 24 * 3600;
|
||
$result = db::name('user_decorate')->where('udid', $user_decorate_data['udid'])->setInc('end_time', $change_time);
|
||
}
|
||
}
|
||
}
|
||
|
||
if(!$result){
|
||
Db::rollback();
|
||
return ['code' => 201, 'msg' => '操作失败', 'data' => null];
|
||
}
|
||
|
||
//记录日志
|
||
$insert_data = [];
|
||
$insert_data['uid'] = $uid;
|
||
$insert_data['type'] = $decorate_info['type'];
|
||
$insert_data['did'] = $did;
|
||
$insert_data['add_time'] = $now_time;
|
||
$insert_data['remark'] = $remake;
|
||
$insert_data['from_type'] = 4;
|
||
$insert_data['start_time'] = $start_time;
|
||
$insert_data['end_time'] = $start_time + $change_time;
|
||
$insert_data['day_num'] = $day;
|
||
$result = db::name('user_decorate_log')->insert($insert_data);
|
||
if(!$result){
|
||
Db::rollback();
|
||
return ['code' => 201, 'msg' => '操作失败', 'data' => null];
|
||
}
|
||
|
||
// 提交事务
|
||
Db::commit();
|
||
return ['code' => 200, 'msg' => "成功", 'data' => null];
|
||
} catch (\Exception $e) {
|
||
dump($e);
|
||
// 回滚事务
|
||
Db::rollback();
|
||
return ['code' => 201, 'msg' => "请重试", 'data' => null];
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|
||
public function get_cp_decorate_list($uid,$type){
|
||
if(!in_array($type,[1,9])){
|
||
return ['code' => 201, 'msg' => "头像框非法!", 'data' => null];
|
||
}
|
||
$user_coupling = db::name('user_coupling')->where([["uid|receive_uid",'=',$uid],['is_delete','=',1]])->order(['id'=>'desc'])->find();
|
||
if(empty($user_coupling)){
|
||
return ['code' => 201, 'msg' => "您尚未建立cp!", 'data' => null];
|
||
}
|
||
$cp_id = $user_coupling['id'];
|
||
//查询我的装扮
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
}
|