Files
zzp-server/app/api/logic/MallLogic.php
2026-01-19 14:19:22 +08:00

235 lines
7.3 KiB
PHP

<?php
namespace app\api\logic;
use app\common\logic\BaseLogic;
use app\common\service\{UtilsService,ConfigService,FileService};
use app\common\model\finance\{UserFinance};
use app\common\model\user\{User};
use app\common\model\mall\{MallGoods,MallGoodsRecord};
use app\common\model\setting\Language;
use think\facade\Config;
use think\facade\{Db};
/**
* 项目逻辑
* Class MallLogic
* @package app\api\logic
*/
class MallLogic extends BaseLogic
{
/**
* @notes 首页数据
* @param $params
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author BD
* @date 2024/02/22 10:54
*/
public static function index(array $params)
{
$user = User::where(['id' => $params['user_id']])->findOrEmpty();
$user_res['point'] = $user['user_point'];
$goods_lists = MallGoods::field('id,title,image,price,num,langs')
->where(['is_show' => 1,'type' => 1])
->order(['sort' => 'desc', 'id' => 'desc'])
->select()
->toArray();
foreach ($goods_lists as &$goods) {
//多语言替换
$data = UtilsService::get_langs_data($goods['langs'],$params['lang']);
$data_title = '';
if(count($data) > 0){
$data_title = $data['title'];
}
$goods['title'] = $data_title;
$goods['image'] = FileService::getFileUrl($goods['image']);
unset($goods['langs']);
}
//查询初始交易密码
$need_set_pwd = 0;
$pwd_pay = ConfigService::get('login', 'password_pay');
$passwordSalt = Config::get('project.unique_identification');
if ($user['password_pay'] == create_password($pwd_pay, $passwordSalt)) {
$need_set_pwd = 1;
}
return [
'lists' => $goods_lists,
'user' => $user_res,
'need_set_pwd' => $need_set_pwd
];
}
/**
* @notes 兑换
* @param array $params
* @return array|false
* @author BD
* @date 2024/02/22 10:54
*/
public static function buy(array $params)
{
Db::startTrans();
try {
$goods = MallGoods::where(['is_show' => 1])->findOrEmpty($params['id']);
$user_id = $params['user_id'];
$data = [
'sn' => generate_sn(MallGoodsRecord::class, 'sn'),
'user_id' => $user_id,
'm_goods_id' => $goods['id'],
'm_goods_title' => $goods['title'],
'm_goods_image' => FileService::setFileUrl($goods['image']),
'm_goods_langs' => $goods['langs'],
'price' => $goods['price'],
'money' => $goods['money'],
'point' => $goods['point'],
'type' => $goods['type'],
'type2' => $goods['type2'],
'status' => 1,//状态0进行中1已完成
];
$order = MallGoodsRecord::create($data);
//剩余数量-1
MallGoods::update([
'id' => $goods['id'],
'num' => $goods['num'] - 1
]);
//扣除积分
if($goods['price'] > 0){
//用户积分修改
UtilsService::user_money_change($user_id, 2, $goods['price'],'user_point');
}
//类型1现金
switch ($goods['type2']) {
case 1:
if($goods['money'] > 0.01){
//记录日志
UtilsService::user_finance_add(
$user_id,
23,
1,
$goods['money'],
$order['sn'],
'',
1//冻结
);
//用户资金修改
UtilsService::user_money_change($user_id, 1, $goods['money'],'user_money');
}
break;
case 2:
break;
case 3:
break;
}
Db::commit();
return [];
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 抽奖数据
* @param $params
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author BD
* @date 2024/02/22 10:54
*/
public static function drawIndex(array $params)
{
$user = User::where(['id' => $params['user_id']])->findOrEmpty();
$user_res['point'] = $user['user_point'];
//单次抽奖消耗积分
$config = ConfigService::get('website', 'trade');
$config_res['point'] = $config['draw_point'];
$goods_lists = MallGoods::field('id,title as name,image as img,langs')
->where(['is_show' => 1,'type' => 2])
->order(['sort' => 'desc', 'id' => 'desc'])
->select()
->toArray();
foreach ($goods_lists as &$goods) {
//多语言替换
$data = UtilsService::get_langs_data($goods['langs'],$params['lang']);
$data_title = '';
if(count($data) > 0){
$data_title = $data['title'];
}
$goods['name'] = $data_title;
$goods['img'] = FileService::getFileUrl($goods['img']);
unset($goods['langs']);
}
return [
'prizeList' => $goods_lists,
'user' => $user_res,
'config' => $config_res,
];
}
/**
* @notes 抽奖
* @param array $params
* @return array|false
* @author BD
* @date 2024/02/22 10:54
*/
public static function draw(array $params)
{
Db::startTrans();
try {
//单次抽奖消耗积分
$config = ConfigService::get('website', 'trade');
$draw_point = $config['draw_point'];
$user = User::where(['id' => $params['user_id']])->findOrEmpty();
$goods_lists = MallGoods::where(['is_show' => 1,'type' => 2])
->order(['sort' => 'desc', 'id' => 'desc'])
->select()
->toArray();
//概率算法
$lists = [];
foreach($goods_lists as $index=>$item) {
$lists[$index] = $item['win_rate'];
}
$prizeIndex = UtilsService::get_draw_rand($goods_lists,$lists,$params['user_id'],$draw_point);
Db::commit();
return [
'index' => $prizeIndex
];
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
}