164 lines
5.1 KiB
PHP
164 lines
5.1 KiB
PHP
<?php
|
||
namespace app\common\model;
|
||
|
||
use think\Db;
|
||
use think\Hook;
|
||
use think\Model;
|
||
|
||
class Gifts extends Model
|
||
{
|
||
protected $name = 'gifts';
|
||
protected $autoWriteTimestamp = 'int';
|
||
protected $createTime = 'createtime';
|
||
protected $updateTime = 'updatetime';
|
||
|
||
|
||
protected $append = [
|
||
'createtime_text',
|
||
];
|
||
|
||
|
||
public function getCreatetimeTextAttr($value, $data)
|
||
{
|
||
$value = $value ? $value : $data['createtime'];
|
||
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
|
||
}
|
||
|
||
public function getUpdatetimeTextAttr($value, $data)
|
||
{
|
||
$value = $value ? $value : $data['updatetime'];
|
||
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
|
||
}
|
||
|
||
public static function getList($where = [], $order = 'weigh desc', $field = null, $limit = null)
|
||
{
|
||
$list = self::where($where)->order($order)->field($field)->paginate();
|
||
|
||
// 处理图片 URL,添加 CDN 域名
|
||
$cdnurl = \think\Config::get('upload.cdnurl');
|
||
if ($cdnurl && !empty($list)) {
|
||
foreach ($list as &$item) {
|
||
if (!empty($item['image']) && strpos($item['image'], 'http') !== 0) {
|
||
$item['image'] = $cdnurl . $item['image'];
|
||
}
|
||
}
|
||
}
|
||
|
||
return $list;
|
||
}
|
||
|
||
/*
|
||
* 赠送礼物
|
||
*/
|
||
public static function giveGift($gift_id, $from_user_id, $to_user_id, $num = 1){
|
||
//检测礼物
|
||
$gift = self::get($gift_id);
|
||
if(!$gift){
|
||
return ['code'=>0,'masg'=>'礼物不存在'];
|
||
}
|
||
if($gift['status'] != 1){
|
||
return ['code'=>0,'masg'=>'礼物已下架'];
|
||
}
|
||
//检测用户
|
||
$from_user = User::get($from_user_id);
|
||
if(!$from_user){
|
||
return ['code'=>0,'masg'=>'用户不存在'];
|
||
}
|
||
$to_user = User::get($to_user_id);
|
||
if(!$to_user){
|
||
return ['code'=>0,'masg'=>'用户不存在'];
|
||
}
|
||
$price = $gift['price'] * $num;
|
||
if($from_user['money'] < $price){
|
||
return ['code'=>0,'masg'=>'金币不足'];
|
||
}
|
||
|
||
//判断是不是好友
|
||
$isFriend_1 = FriendRelation::isFriend($from_user_id, $to_user_id);
|
||
$isFriend_2 = FriendRelation::isFriend($to_user_id, $from_user_id);
|
||
if (!$isFriend_1 || !$isFriend_2) {
|
||
return ['code'=>0,'masg'=>'请先添加对方为好友'];
|
||
}
|
||
Db::startTrans();
|
||
try {
|
||
//扣除金币
|
||
User::money($price*-1, $from_user_id, '赠送礼物:'.$gift['name']);
|
||
//增加礼物记录
|
||
$insert_data = [
|
||
'gifts_id'=>$gift_id,
|
||
'from_user_id'=>$from_user_id,
|
||
'to_user_id'=>$to_user_id,
|
||
'nums'=>$num,
|
||
'name'=>$gift['name'],
|
||
'image'=>$gift['image'],
|
||
'intimacy_value'=>$gift['intimacy_value'] * $num,
|
||
'createtime'=>time(),
|
||
];
|
||
|
||
GiftsLog::create($insert_data);
|
||
//增加亲密度
|
||
FriendRelation::addFriendDensity($from_user_id, $to_user_id, $gift['intimacy_value'] * $num,'gifts');
|
||
|
||
//监听亲密度升级问题
|
||
$data=['user_id'=>$from_user_id,'friend_id'=>$to_user_id,'intimacy_value'=>$insert_data['intimacy_value']];
|
||
Hook::listen('intimacy_uplevel', $data);
|
||
Db::commit();
|
||
}catch (\Exception $e){
|
||
Db::rollback();
|
||
return ['code'=>0,'masg'=>$e->getMessage()];
|
||
}
|
||
return ['code'=>1,'masg'=>'赠送成功'];
|
||
|
||
}
|
||
|
||
/*
|
||
* 赠送礼物-给女友
|
||
*/
|
||
public static function giveGiftGirlfriend($gift_id, $from_user_id, $num = 1){
|
||
//检测礼物
|
||
$gift = self::get($gift_id);
|
||
if(!$gift){
|
||
return ['code'=>0,'masg'=>'礼物不存在'];
|
||
}
|
||
if($gift['status'] != 1){
|
||
return ['code'=>0,'masg'=>'礼物已下架'];
|
||
}
|
||
//检测用户
|
||
$from_user = User::get($from_user_id);
|
||
if(!$from_user){
|
||
return ['code'=>0,'masg'=>'用户不存在'];
|
||
}
|
||
|
||
$price = $gift['price'] * $num;
|
||
if($from_user['money'] < $price){
|
||
return ['code'=>0,'masg'=>'金币不足'];
|
||
}
|
||
|
||
Db::startTrans();
|
||
try {
|
||
//扣除金币
|
||
User::money($price*-1, $from_user_id, '赠送礼物:'.$gift['name']);
|
||
//增加礼物记录
|
||
$insert_data = [
|
||
'gifts_id'=>$gift_id,
|
||
'from_user_id'=>$from_user_id,
|
||
'to_user_id'=>0,
|
||
'nums'=>$num,
|
||
'name'=>$gift['name'],
|
||
'image'=>$gift['image'],
|
||
'intimacy_value'=>$gift['intimacy_value'] * $num,
|
||
'createtime'=>time(),
|
||
'type'=>2,
|
||
];
|
||
GiftsLog::create($insert_data);
|
||
$UserBondLog = new UserBondLog();
|
||
$UserBondLog->add_log(3,$gift['intimacy_value'] * $num,$from_user);
|
||
Db::commit();
|
||
}catch (\Exception $e){
|
||
Db::rollback();
|
||
return ['code'=>0,'masg'=>$e->getMessage()];
|
||
}
|
||
|
||
}
|
||
|
||
} |