multi-store/app/admin/logic/user/UserLogic.php
mkm 40ec3e5ee0 feat: 修改了订单相关的API,优化了支付逻辑;
fix: 修复了用户地址、商品库存等错误;
refactor: 重构了登录逻辑,提高了代码可读性;
style: 调整了代码格式,使其更加规范;
test: 增加了订单支付的测试用例;
docs: 更新了相关文档;
build: 更新了依赖;
ops: 优化了服务器性能;
chore: 更新了.gitignore文件;
2024-08-27 11:56:48 +08:00

335 lines
12 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
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台PHP版
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用可去除界面版权logo
// | gitee下载https://gitee.com/likeshop_gitee/likeadmin
// | github下载https://github.com/likeshop-github/likeadmin
// | 访问官网https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\admin\logic\user;
use app\common\enum\OrderEnum;
use app\common\enum\user\UserTerminalEnum;
use app\common\enum\YesNoEnum;
use app\common\logic\BaseLogic;
use app\common\model\finance\CapitalFlow;
use app\common\model\store_finance_flow\StoreFinanceFlow;
use app\common\model\store_order\StoreOrder;
use app\common\model\store_product_gift\StoreProductGift;
use app\common\model\user\User;
use app\common\model\user\UserAddress;
use app\common\model\user\UserRecharge;
use app\common\model\user_create_log\UserCreateLog;
use app\common\model\user_label\UserLabel;
use app\common\model\user_ship\UserShip;
use app\common\model\user_sign\UserSign;
use app\common\model\vip_flow\VipFlow;
use think\facade\Db;
use app\common\service\FileService;
use support\exception\BusinessException;
use Webman\Config;
/**
* 用户逻辑层
* Class UserLogic
* @package app\admin\logic\user
*/
class UserLogic extends BaseLogic
{
/**
* @notes 添加用户列表
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/04/25 10:20
*/
public static function add(array $params)
{
$passwordSalt = Config::get('project.unique_identification');
$password = create_password($params['password'], $passwordSalt);
$defaultAvatar = config('project.default_image.admin_avatar');
$avatar = !empty($params['avatar']) ? FileService::setFileUrl($params['avatar']) : $defaultAvatar;
Db::startTrans();
try {
$res=User::create([
'avatar' => $avatar,
'real_name' => $params['real_name'],
'nickname' => $params['nickname'],
'account' => $params['account'],
'password' => $password,
'mobile' => $params['mobile'],
'sex' => $params['sex'],
'is_disable' => $params['is_disable'],
]);
Db::commit();
return $res;
} catch (\Throwable $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
}
}
public static function checkAddress(array $params)
{
$user_ship=$params['user_ship']??0;
if($user_ship==2){
if(!isset($params['village'])){
throw new BusinessException('请设置村参数');
}
$arr=User::where('user_ship',$user_ship)->alias('user')->join('user_address address','user.id=address.uid and village='.$params['village'])->find();
if ($arr) {
throw new BusinessException('该区域已有村长请重新选择');
}
}elseif($user_ship==3){
if(!isset($params['brigade'])){
throw new BusinessException('请设置队参数');
}
$arr=User::where('user_ship',$user_ship)->alias('user')->join('user_address address','user.id=address.uid and village='.$params['village'] .' and brigade='.$params['brigade'])->find();
if($arr){
throw new BusinessException('该区域已有队长请重新选择');
}
}
return true;
}
public static function StoreAdd(array $params)
{
self::checkAddress($params);
$passwordSalt = Config::get('project.unique_identification');
$password = create_password(123456, $passwordSalt);
$defaultAvatar = config('project.default_image.admin_avatar');
$avatar = !empty($params['avatar']) ? FileService::setFileUrl($params['avatar']) : $defaultAvatar;
Db::startTrans();
try {
$data=[
'avatar' => $avatar,
'real_name' => $params['real_name']??"",
'nickname' => '用户'.time(),
'account' => $params['mobile'],
'password' => $password,
'mobile' => $params['mobile'],
'label_id' => $params['label_id']??0,
'store_id' => $params['store_id']??0,
];
if(isset($params['user_ship']) &&$params['user_ship']==4){
$data['user_ship']=4;
}
$res=User::create($data);
UserCreateLog::create([
'uid' => $res['id'],
'create_uid' => $params['create_uid']??0,
'store_id' => $params['store_id']??0,
'staff_id' => $params['staff_id']??0,
'user_ship' => $data['user_ship']??0,
]);
UserAddress::create([
'uid' => $res['id'],
'real_name' => $params['real_name']??"",
'mobile' => $params['mobile']??'',
'province' => $params['province']??'',
'city' => $params['city']??'',
'area' => $params['area']??'',
'street' => $params['street']??'',
'village' => $params['village']??'',
'brigade' => $params['brigade']??'',
'is_default' => 1,
]);
Db::commit();
return $res;
} catch (\Exception $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
}
}
/**
* @notes 编辑用户列表
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/04/25 10:20
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
User::where('id', $params['id'])->update([
// 'avatar' => $params['avatar'] ?? '',
// 'real_name' => $params['real_name'],
// 'nickname' => $params['nickname'],
// 'account' => $params['account'],
// 'password' => $params['password'] ?? '',
// 'mobile' => $params['mobile'] ?? '',
// 'sex' => $params['sex'] ?? 0,
// 'is_disable' => $params['is_disable'] ?? 0,
'label_id'=>$params['label_id']
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
}
}
/**
* @notes 用户详情
* @param int $userId
* @return array
* @author 乔峰
* @date 2022/9/22 16:32
*/
public static function detail(int $userId): array
{
$field = [
'id', 'account', 'nickname', 'avatar', 'real_name','integral','label_id','user_ship',
'sex', 'mobile', 'create_time', 'login_time', 'channel','now_money','purchase_funds'
];
$user = User::where(['id' => $userId])->field($field)
->findOrEmpty();
$user['channel'] = UserTerminalEnum::getTermInalDesc($user['channel']);
$user->sex = $user->getData('sex');
$user['number']=StoreFinanceFlow::where('other_uid',$userId)->where(['status'=>0,'financial_pm'=>1,'type'=>1])->sum('number');
$user['user_ship_name']=$user->user_ship>0?UserShip::where('id',$user->user_ship)->value('title'):"一般用户";
return $user->toArray();
}
/**
* @notes 更新用户信息
* @param array $params
* @return User
* @author 乔峰
* @date 2022/9/22 16:38
*/
public static function setUserInfo(array $params)
{
return User::update([
'id' => $params['id'],
$params['field'] => $params['value']
]);
}
public static function dealDetails($params)
{
switch ($params['type']){
case 1:
//采购款明细
$categories = ['user_balance_recharge', 'user_order_purchase_pay','system_purchase_add','user_balance_recharge_refund'];
$query = CapitalFlow::where('uid', $params['id'])
->whereIn('category', $categories);
$count = $query->count();
$data = $query
->page($params['page_no'],$params['page_size'])
->order('id','desc')
->select()->toArray();
foreach ($data as &$value){
if($value['category'] == 'user_order_purchase_pay'){
$value['order_sn'] = StoreOrder::where('id',$value['link_id'])->value('order_id');
}elseif($value['category'] == 'user_balance_recharge'){
$value['order_sn'] = UserRecharge::where('id',$value['link_id'])->value('order_id');
}
}
break;
case 2:
//余额明细
$category = ['system_balance_add','user_order_balance_pay'];
$query = CapitalFlow::where('uid', $params['id'])
->whereIn('category', $category);
$count = $query->count();
$data = $query
->page($params['page_no'],$params['page_size'])
->order('id','desc')
->select()->toArray();
foreach ($data as &$value){
$value['order_sn'] = StoreOrder::where('id',$value['link_id'])->value('order_id');
}
break;
case 3:
//礼品券明细
$query = UserSign::where(['uid'=>$params['id']]);
$count = $query->count();
$data =$query
->page($params['page_no'],$params['page_size'])
->order('id','desc')
->select()->toArray();
break;
case 4:
//返还金明细 -todo back
$query = VipFlow::with('store')->where(['user_id'=>$params['id']]);
$count = $query->count();
$data = $query
->page($params['page_no'],$params['page_size'])
->order('id','desc')
->select()->toArray();
foreach ($data as &$value){
if($value['status'] == 0){
$value['title'] = "购买商品".$value['all']."元获得".$value['number']."元返还金";
}else{
//退回到余额、微信、采购款
$back = self::dealTitleCate($value['pay_type']);
$value['title'] = "返还金解冻".$value['number']."元退回到".$back;
}
}
break;
default:
$data = [];
$count = 0;
}
return [
'lists' => $data,
'count' => $count
];
}
public static function giftList($uid,$params)
{
$query = StoreProductGift::with(['store','user','goodsName'])->where('uid',$uid);
$count = $query->count();
$list = $query->page($params['page_no'],$params['page_size'])
->order('id','desc')
->select()->toArray();
return [
'lists' => $list,
'count' => $count
];
}
public static function dealTitleCate($pay_type)
{
switch ($pay_type){
case 18:
$title = "采购款";
break;
case 17:
$title = "现金";
break;
case 3:
$title ="余额";
break;
case 7:
case 9:
$title ="微信";
break;
case 13:
$title ="支付宝";
break;
default:
$title ="默认";
}
return $title;
}
}