2024-05-30 21:37:55 +08:00
< ? 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 ;
2024-06-20 11:41:14 +08:00
use app\common\enum\OrderEnum ;
2024-05-30 21:37:55 +08:00
use app\common\enum\user\UserTerminalEnum ;
2024-06-20 11:41:14 +08:00
use app\common\enum\YesNoEnum ;
2024-05-30 21:37:55 +08:00
use app\common\logic\BaseLogic ;
2024-10-29 12:00:25 +08:00
use app\common\logic\CapitalFlowLogic ;
2024-06-20 11:41:14 +08:00
use app\common\model\finance\CapitalFlow ;
2024-06-15 16:07:50 +08:00
use app\common\model\store_finance_flow\StoreFinanceFlow ;
2024-06-20 15:37:53 +08:00
use app\common\model\store_order\StoreOrder ;
2024-07-03 15:43:02 +08:00
use app\common\model\store_product_gift\StoreProductGift ;
2024-05-30 21:37:55 +08:00
use app\common\model\user\User ;
2024-06-14 16:05:48 +08:00
use app\common\model\user\UserAddress ;
2024-06-15 13:44:32 +08:00
use app\common\model\user\UserRecharge ;
2024-06-22 17:04:28 +08:00
use app\common\model\user_create_log\UserCreateLog ;
2024-06-18 15:01:36 +08:00
use app\common\model\user_label\UserLabel ;
2024-07-01 14:34:35 +08:00
use app\common\model\user_ship\UserShip ;
2024-06-20 11:41:14 +08:00
use app\common\model\user_sign\UserSign ;
use app\common\model\vip_flow\VipFlow ;
2024-05-30 21:37:55 +08:00
use think\facade\Db ;
use app\common\service\FileService ;
2024-08-27 11:56:48 +08:00
use support\exception\BusinessException ;
2024-05-30 21:37:55 +08:00
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 {
2024-10-29 12:00:25 +08:00
$res = User :: create ([
2024-05-30 21:37:55 +08:00
'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 ;
2024-08-27 11:56:48 +08:00
} catch ( \Throwable $e ) {
2024-05-30 21:37:55 +08:00
Db :: rollback ();
2024-08-27 11:56:48 +08:00
throw new BusinessException ( $e -> getMessage ());
2024-06-14 15:53:47 +08:00
}
}
2024-07-06 16:01:39 +08:00
public static function checkAddress ( array $params )
2024-06-14 15:53:47 +08:00
{
2024-10-29 12:00:25 +08:00
$user_ship = $params [ 'user_ship' ] ? ? 0 ;
if ( $user_ship == 2 ) {
if ( ! isset ( $params [ 'village' ])) {
2024-08-27 11:56:48 +08:00
throw new BusinessException ( '请设置村参数' );
2024-07-04 09:21:14 +08:00
}
2024-10-29 12:00:25 +08:00
$arr = User :: where ( 'user_ship' , $user_ship ) -> alias ( 'user' ) -> join ( 'user_address address' , 'user.id=address.uid and village=' . $params [ 'village' ]) -> find ();
2024-07-13 11:22:12 +08:00
if ( $arr ) {
2024-08-27 11:56:48 +08:00
throw new BusinessException ( '该区域已有村长请重新选择' );
2024-07-04 09:21:14 +08:00
}
2024-10-29 12:00:25 +08:00
} elseif ( $user_ship == 3 ) {
if ( ! isset ( $params [ 'brigade' ])) {
2024-08-27 11:56:48 +08:00
throw new BusinessException ( '请设置队参数' );
2024-07-04 09:21:14 +08:00
}
2024-10-29 12:00:25 +08:00
$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 ) {
2024-08-27 11:56:48 +08:00
throw new BusinessException ( '该区域已有队长请重新选择' );
2024-07-04 09:21:14 +08:00
}
}
2024-07-06 16:01:39 +08:00
return true ;
}
public static function StoreAdd ( array $params )
{
self :: checkAddress ( $params );
2024-06-14 15:53:47 +08:00
$passwordSalt = Config :: get ( 'project.unique_identification' );
2024-06-15 09:44:23 +08:00
$password = create_password ( 123456 , $passwordSalt );
2024-06-14 15:53:47 +08:00
$defaultAvatar = config ( 'project.default_image.admin_avatar' );
$avatar = ! empty ( $params [ 'avatar' ]) ? FileService :: setFileUrl ( $params [ 'avatar' ]) : $defaultAvatar ;
Db :: startTrans ();
try {
2024-10-29 12:00:25 +08:00
$data = [
2024-06-14 15:53:47 +08:00
'avatar' => $avatar ,
2024-10-29 12:00:25 +08:00
'real_name' => $params [ 'real_name' ] ? ? " " ,
'nickname' => '用户' . time (),
2024-06-14 15:53:47 +08:00
'account' => $params [ 'mobile' ],
'password' => $password ,
'mobile' => $params [ 'mobile' ],
2024-10-29 12:00:25 +08:00
'label_id' => $params [ 'label_id' ] ? ? 0 ,
'store_id' => $params [ 'store_id' ] ? ? 0 ,
2024-06-14 16:05:48 +08:00
];
2024-10-29 12:00:25 +08:00
if ( isset ( $params [ 'user_ship' ]) && $params [ 'user_ship' ] == 4 ) {
$data [ 'user_ship' ] = 4 ;
2024-07-01 16:00:14 +08:00
}
2024-10-29 12:00:25 +08:00
$res = User :: create ( $data );
2024-06-22 17:04:28 +08:00
UserCreateLog :: create ([
'uid' => $res [ 'id' ],
2024-10-29 12:00:25 +08:00
'create_uid' => $params [ 'create_uid' ] ? ? 0 ,
'store_id' => $params [ 'store_id' ] ? ? 0 ,
'staff_id' => $params [ 'staff_id' ] ? ? 0 ,
'user_ship' => $data [ 'user_ship' ] ? ? 0 ,
2024-06-22 17:04:28 +08:00
]);
2024-06-14 16:05:48 +08:00
UserAddress :: create ([
'uid' => $res [ 'id' ],
2024-10-29 12:00:25 +08:00
'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' ] ? ? '' ,
2024-06-14 16:05:48 +08:00
'is_default' => 1 ,
2024-06-14 15:53:47 +08:00
]);
Db :: commit ();
return $res ;
} catch ( \Exception $e ) {
Db :: rollback ();
2024-08-27 11:56:48 +08:00
throw new BusinessException ( $e -> getMessage ());
2024-05-30 21:37:55 +08:00
}
}
/**
* @ 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 ([
2024-06-17 18:00:40 +08:00
// '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,
2024-10-29 12:00:25 +08:00
'label_id' => $params [ 'label_id' ]
2024-05-30 21:37:55 +08:00
]);
Db :: commit ();
return true ;
} catch ( \Exception $e ) {
Db :: rollback ();
2024-08-27 11:56:48 +08:00
throw new BusinessException ( $e -> getMessage ());
2024-05-30 21:37:55 +08:00
}
}
/**
* @ notes 用户详情
* @ param int $userId
* @ return array
* @ author 乔峰
* @ date 2022 / 9 / 22 16 : 32
*/
public static function detail ( int $userId ) : array
{
$field = [
2024-10-29 12:00:25 +08:00
'id' ,
'account' ,
'nickname' ,
'avatar' ,
'real_name' ,
'integral' ,
'label_id' ,
'user_ship' ,
'sex' ,
'mobile' ,
'create_time' ,
'login_time' ,
'channel' ,
'now_money' ,
'purchase_funds'
2024-05-30 21:37:55 +08:00
];
$user = User :: where ([ 'id' => $userId ]) -> field ( $field )
-> findOrEmpty ();
$user [ 'channel' ] = UserTerminalEnum :: getTermInalDesc ( $user [ 'channel' ]);
$user -> sex = $user -> getData ( 'sex' );
2024-10-29 12:00:25 +08:00
$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' ) : " 一般用户 " ;
2024-05-30 21:37:55 +08:00
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' ]
]);
}
2024-06-20 11:41:14 +08:00
2024-10-29 12:00:25 +08:00
/**
* 更新采购款
*/
public static function PurchaseFunds ( array $params )
{
$find = User :: where ([ 'id' => $params [ 'id' ]]) -> find ();
Db :: startTrans ();
try {
$capitalFlowDao = new CapitalFlowLogic ( $find , 'user' );
if ( $params [ 'type' ] == 1 ) {
2024-10-31 15:15:18 +08:00
$capitalFlowDao -> userIncome ( 'system_purchase_add' , 'system' , 0 , $params [ 'purchase_funds' ], $params [ 'mark' ] ? ? '' , 1 );
2025-02-06 16:01:37 +08:00
User :: where ([ 'id' => $params [ 'id' ]]) -> update ([ 'purchase_funds' => bcadd ( $params [ 'purchase_funds' ], $find [ 'purchase_funds' ], 2 )]);
2024-10-29 12:00:25 +08:00
} else {
2024-10-31 15:15:18 +08:00
$capitalFlowDao -> userExpense ( 'system_purchase_dec' , 'system' , 0 , $params [ 'purchase_funds' ], $params [ 'mark' ] ? ? '' );
2025-02-06 16:01:37 +08:00
User :: where ([ 'id' => $params [ 'id' ]]) -> update ([ 'purchase_funds' => bcsub ( $find [ 'purchase_funds' ], $params [ 'purchase_funds' ], 2 )]);
2024-10-29 12:00:25 +08:00
}
Db :: commit ();
return true ;
} catch ( \Exception $e ) {
Db :: rollback ();
throw new BusinessException ( $e -> getMessage ());
}
}
/**
* 更新余额
*/
public static function nowMoney ( array $params )
{
$find = User :: where ([ 'id' => $params [ 'id' ]]) -> find ();
Db :: startTrans ();
try {
$capitalFlowDao = new CapitalFlowLogic ( $find , 'user' );
if ( $params [ 'type' ] == 1 ) {
2024-10-31 15:15:18 +08:00
$capitalFlowDao -> userIncome ( 'system_balance_add' , 'system' , 0 , $params [ 'now_money' ], $params [ 'mark' ] ? ? '' );
2025-02-06 16:01:37 +08:00
User :: where ([ 'id' => $params [ 'id' ]]) -> update ([ 'now_money' => bcadd ( $params [ 'now_money' ], $find [ 'now_money' ], 2 )]);
2024-10-29 12:00:25 +08:00
} else {
2024-10-31 15:15:18 +08:00
$capitalFlowDao -> userExpense ( 'system_balance_reduce' , 'system' , 0 , $params [ 'now_money' ], $params [ 'mark' ] ? ? '' );
2025-02-06 16:01:37 +08:00
User :: where ([ 'id' => $params [ 'id' ]]) -> update ([ 'now_money' => bcsub ( $find [ 'now_money' ], $params [ 'now_money' ], 2 )]);
2024-10-29 12:00:25 +08:00
}
Db :: commit ();
return true ;
} catch ( \Exception $e ) {
Db :: rollback ();
throw new BusinessException ( $e -> getMessage ());
}
}
2024-06-20 11:41:14 +08:00
public static function dealDetails ( $params )
{
2024-10-29 12:00:25 +08:00
switch ( $params [ 'type' ]) {
2024-06-20 11:41:14 +08:00
case 1 :
//采购款明细
2024-11-19 10:37:22 +08:00
$categories = [ 'user_balance_recharge' , 'user_order_purchase_pay' , 'system_purchase_add' , 'user_balance_recharge_refund' , 'purchase_refund' , 'system_purchase_dec' ];
2024-06-20 15:37:53 +08:00
$query = CapitalFlow :: where ( 'uid' , $params [ 'id' ])
-> whereIn ( 'category' , $categories );
2024-06-20 11:41:14 +08:00
$count = $query -> count ();
$data = $query
2024-10-29 12:00:25 +08:00
-> page ( $params [ 'page_no' ], $params [ 'page_size' ])
-> order ( 'id' , 'desc' )
2024-06-20 11:41:14 +08:00
-> select () -> toArray ();
2024-10-29 12:00:25 +08:00
foreach ( $data as & $value ) {
2024-11-21 16:47:20 +08:00
if ( in_array ( $value [ 'category' ],[ 'user_order_purchase_pay' , 'purchase_refund' ])) {
2024-10-29 12:00:25 +08:00
$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' );
2024-06-20 15:37:53 +08:00
}
}
2024-06-20 11:41:14 +08:00
break ;
case 2 :
//余额明细
2024-11-19 10:37:22 +08:00
$category = [ 'system_balance_add' , 'user_order_balance_pay' , 'now_money_refund' , 'user_withdrawal' ];
2024-06-20 15:37:53 +08:00
$query = CapitalFlow :: where ( 'uid' , $params [ 'id' ])
2024-06-22 15:36:48 +08:00
-> whereIn ( 'category' , $category );
2024-06-20 11:41:14 +08:00
$count = $query -> count ();
$data = $query
2024-10-29 12:00:25 +08:00
-> page ( $params [ 'page_no' ], $params [ 'page_size' ])
-> order ( 'id' , 'desc' )
2024-06-20 11:41:14 +08:00
-> select () -> toArray ();
2024-10-29 12:00:25 +08:00
foreach ( $data as & $value ) {
$value [ 'order_sn' ] = StoreOrder :: where ( 'id' , $value [ 'link_id' ]) -> value ( 'order_id' );
2024-06-20 15:37:53 +08:00
}
2024-06-20 11:41:14 +08:00
break ;
case 3 :
//礼品券明细
2024-10-29 12:00:25 +08:00
$query = UserSign :: where ([ 'uid' => $params [ 'id' ]]);
2024-06-20 11:41:14 +08:00
$count = $query -> count ();
2024-10-29 12:00:25 +08:00
$data = $query
-> page ( $params [ 'page_no' ], $params [ 'page_size' ])
-> order ( 'id' , 'desc' )
2024-06-20 11:41:14 +08:00
-> select () -> toArray ();
break ;
case 4 :
//返还金明细 -todo back
2024-10-29 12:00:25 +08:00
$query = VipFlow :: with ( 'store' ) -> where ([ 'user_id' => $params [ 'id' ]]);
2024-06-20 11:41:14 +08:00
$count = $query -> count ();
$data = $query
2024-10-29 12:00:25 +08:00
-> page ( $params [ 'page_no' ], $params [ 'page_size' ])
-> order ( 'id' , 'desc' )
2024-06-20 11:41:14 +08:00
-> select () -> toArray ();
2024-10-29 12:00:25 +08:00
foreach ( $data as & $value ) {
if ( $value [ 'status' ] == 0 ) {
$value [ 'title' ] = " 购买商品 " . $value [ 'all' ] . " 元获得 " . $value [ 'number' ] . " 元返还金 " ;
} else {
2024-07-08 15:15:15 +08:00
//退回到余额、微信、采购款
$back = self :: dealTitleCate ( $value [ 'pay_type' ]);
2024-10-29 12:00:25 +08:00
$value [ 'title' ] = " 返还金解冻 " . $value [ 'number' ] . " 元退回到 " . $back ;
2024-07-08 15:15:15 +08:00
}
}
2024-06-20 11:41:14 +08:00
break ;
default :
$data = [];
$count = 0 ;
}
return [
2024-06-20 13:46:20 +08:00
'lists' => $data ,
2024-06-20 11:41:14 +08:00
'count' => $count
];
}
2024-07-03 15:43:02 +08:00
2024-10-29 12:00:25 +08:00
public static function giftList ( $uid , $params )
2024-07-03 15:43:02 +08:00
{
2024-10-29 12:00:25 +08:00
$query = StoreProductGift :: with ([ 'store' , 'user' , 'goodsName' ]) -> where ( 'uid' , $uid );
2024-07-03 15:43:02 +08:00
$count = $query -> count ();
2024-10-29 12:00:25 +08:00
$list = $query -> page ( $params [ 'page_no' ], $params [ 'page_size' ])
-> order ( 'id' , 'desc' )
2024-07-03 15:43:02 +08:00
-> select () -> toArray ();
return [
'lists' => $list ,
'count' => $count
];
}
2024-07-08 15:15:15 +08:00
public static function dealTitleCate ( $pay_type )
{
2024-10-29 12:00:25 +08:00
switch ( $pay_type ) {
2024-07-08 15:15:15 +08:00
case 18 :
$title = " 采购款 " ;
break ;
case 17 :
$title = " 现金 " ;
break ;
case 3 :
2024-10-29 12:00:25 +08:00
$title = " 余额 " ;
2024-07-08 15:15:15 +08:00
break ;
case 7 :
case 9 :
2024-10-29 12:00:25 +08:00
$title = " 微信 " ;
2024-07-08 15:15:15 +08:00
break ;
case 13 :
2024-10-29 12:00:25 +08:00
$title = " 支付宝 " ;
2024-07-08 15:15:15 +08:00
break ;
default :
2024-10-29 12:00:25 +08:00
$title = " 默认 " ;
2024-07-08 15:15:15 +08:00
}
return $title ;
}
2024-05-30 21:37:55 +08:00
}