2024-06-01 14:56:45 +08:00
< ? php
namespace app\api\controller\store ;
2024-06-17 10:02:16 +08:00
use app\admin\logic\user\UserLogic as UserUserLogic ;
2024-06-01 14:56:45 +08:00
use app\api\lists\store\SystemStoreLists ;
use app\api\controller\BaseApiController ;
2024-06-22 17:04:28 +08:00
use app\api\lists\user_create_log\UserCreateLogLists ;
2024-06-13 10:27:56 +08:00
use app\api\logic\store\StoreLogic ;
2024-06-17 10:02:16 +08:00
use app\api\logic\user\UserLogic ;
use app\api\validate\UserValidate ;
use app\common\enum\PayEnum ;
use app\common\logic\PaymentLogic ;
use app\common\logic\PayNotifyLogic ;
2024-06-24 18:04:33 +08:00
use app\common\model\Config ;
2024-09-27 18:16:07 +08:00
use app\common\model\store_finance_flow\StoreFinanceFlow ;
use app\common\model\store_order\StoreOrder ;
use app\common\model\system_store\SystemStore ;
use app\common\model\system_store\SystemStoreStaff ;
2024-06-17 10:02:16 +08:00
use app\common\model\user\User ;
2024-07-04 09:33:45 +08:00
use app\common\model\user\UserAddress ;
2024-06-22 17:11:22 +08:00
use app\common\model\user_create_log\UserCreateLog ;
2024-06-17 10:02:16 +08:00
use app\common\model\user_recharge\UserRecharge ;
2024-09-27 18:16:07 +08:00
use app\common\model\warehouse_product\WarehouseProduct ;
2024-08-07 21:26:13 +08:00
use app\common\service\Curl ;
use Exception ;
2024-06-25 10:39:37 +08:00
use support\Cache ;
2024-08-07 21:26:13 +08:00
use support\Log ;
2024-06-17 10:02:16 +08:00
use Webman\RedisQueue\Redis ;
2024-06-01 14:56:45 +08:00
class StoreController extends BaseApiController
{
2024-06-13 10:27:56 +08:00
public $notNeedLogin = [ 'detail' ];
2024-06-01 14:56:45 +08:00
public function lists ()
{
return $this -> dataLists ( new SystemStoreLists ());
}
2024-06-22 17:04:28 +08:00
/**
* 创建用户记录列表
*/
public function create_lists ()
{
return $this -> dataLists ( new UserCreateLogLists ());
}
2024-06-01 14:56:45 +08:00
2024-06-17 16:44:32 +08:00
/**
* 门店信息
*/
2024-06-13 10:27:56 +08:00
public function detail ()
{
$store_id = ( int ) $this -> request -> get ( 'store_id' );
2024-06-17 16:44:32 +08:00
$phone = ( int ) $this -> request -> get ( 'phone' );
$where = [];
if ( $phone ){
$where [ 'phone' ] = $phone ;
}
if ( $store_id ){
$where [ 'id' ] = $store_id ;
}
2024-06-13 10:27:56 +08:00
$info = StoreLogic :: search ( $where );
if ( $info ) {
2024-06-25 09:46:16 +08:00
return $this -> success ( 'ok' , $info );
2024-06-13 10:27:56 +08:00
} else {
return $this -> fail ( '店铺不存在' );
}
}
2024-06-17 10:02:16 +08:00
/**
* 门店会员充值
*/
public function recharge ()
{
$params = ( new UserValidate ()) -> post () -> goCheck ( 'rechargeStoreMoney' );
$auth_code = $this -> request -> post ( 'auth_code' ); //微信支付条码
2024-06-22 16:21:46 +08:00
$recharge_type = $this -> request -> post ( 'recharge_type' , '' ); //微信支付条码
2024-07-13 11:22:12 +08:00
// $code = $this->request->post('code','');//验证码
// $phone = $params['mobile'];
2024-06-28 09:59:05 +08:00
// if($code && $phone){
// $remark = $phone.'_reporting';
// $codeCache = Cache::get($remark);
// if(empty($codeCache)){
// return $this->fail('验证码不存在');
// }
// if ($codeCache != $code) {
// return $this->fail('验证码错误');
// }
// }
2024-06-27 16:38:00 +08:00
if ( $params [ 'price' ] > 10000 ){
return $this -> fail ( '充值金额不能大于10000' );
}
2024-06-27 20:57:48 +08:00
$find = User :: where ( 'account|mobile' , $params [ 'mobile' ]) -> find ();
2024-06-17 10:02:16 +08:00
if ( ! $find ){
2024-06-22 17:04:28 +08:00
$params [ 'create_uid' ] = $this -> userId ;
2024-07-13 11:22:12 +08:00
if ( isset ( $params [ 'user_ship' ]) && in_array ( $params [ 'user_ship' ],[ 2 , 3 ])){
UserUserLogic :: checkAddress ( $params );
}
2024-07-05 17:39:36 +08:00
$find = UserUserLogic :: StoreAdd ( $params );
2024-06-17 17:45:34 +08:00
} else {
2024-07-13 11:22:12 +08:00
if ( $find [ 'user_ship' ] != $params [ 'user_ship' ] && in_array ( $params [ 'user_ship' ],[ 2 , 3 ])){
2024-07-06 18:01:38 +08:00
UserUserLogic :: checkAddress ( $params );
2024-07-06 16:01:39 +08:00
}
2024-06-17 17:45:34 +08:00
$find [ 'real_name' ] = $params [ 'real_name' ];
2024-06-26 19:41:27 +08:00
$find [ 'label_id' ] = $params [ 'label_id' ] ? ? 0 ;
2024-06-17 17:45:34 +08:00
$find -> save ();
2024-07-04 09:33:45 +08:00
$adds = UserAddress :: where ( 'uid' , $find [ 'id' ]) -> find ();
$adds_data = [
'uid' => $find [ '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 ,
];
if ( $adds ){
$adds -> save ( $adds_data );
} else {
UserAddress :: create ( $adds_data );
}
2024-06-17 10:02:16 +08:00
}
2024-06-22 16:21:46 +08:00
if ( $recharge_type != 'INDUSTRYMEMBERS' ){
return $this -> success ( '添加用户成功' );
}
2024-06-17 10:02:16 +08:00
$data = [
'store_id' => $params [ 'store_id' ],
2024-06-17 13:39:56 +08:00
'uid' => $find [ 'id' ],
2024-06-25 13:34:15 +08:00
'other_uid' => $this -> userId ,
2024-06-17 10:02:16 +08:00
'staff_id' => 0 ,
'order_id' => getNewOrderId ( 'CZ' ),
2024-06-27 14:26:39 +08:00
'price' => $params [ 'price' ] ? ? 1000 ,
2024-06-17 14:19:54 +08:00
'recharge_type' => 'INDUSTRYMEMBERS' ,
2024-06-27 20:47:16 +08:00
'user_ship' => $params [ 'user_ship' ] ? ? 0 ,
2024-06-17 10:02:16 +08:00
];
$order = UserRecharge :: create ( $data );
2024-06-13 10:27:56 +08:00
2024-06-17 10:02:16 +08:00
$order [ 'pay_price' ] = $order [ 'price' ];
2024-07-06 17:32:51 +08:00
$order [ 'attach' ] = 'recharge' ;
2024-06-17 11:04:23 +08:00
$result = PaymentLogic :: codepay ( $auth_code , $order , '条码支付' );
2024-06-17 10:02:16 +08:00
if ( isset ( $result [ 'trade_state_desc' ]) && $result [ 'trade_state_desc' ] == '支付成功' ) {
2024-07-06 17:31:43 +08:00
Cache :: set ( 'trade_state' . time (), json_encode ( $result ));
2024-06-17 10:02:16 +08:00
PayNotifyLogic :: handle ( 'recharge' , $result [ 'out_trade_no' ], $result );
} else {
Redis :: send ( 'send-code-pay' , [ 'order_id' => $order [ 'order_id' ], 'pay_type' => 'recharge' ]);
2024-08-07 21:26:13 +08:00
try {
$arr = [ " msgtype " => " markdown " , " markdown " => [ " content " => " 有新的条码支付充值订单 " ]];
$url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=b538e44b-940b-445f-afe0-97320942d959' ;
( new Curl ()) -> postJson ( $url , json_encode ( $arr ));
} catch ( Exception $e ){
Log :: error ( '推送订单失败' );
}
2024-06-17 10:02:16 +08:00
return $this -> success ( '用户支付中' );
}
return $this -> success ( '支付成功' , [ 'out_trade_no' => $result [ 'out_trade_no' ], 'pay_type' => PayEnum :: WECHAT_PAY_BARCODE , 'transaction_id' => $result [ 'transaction_id' ]]);
}
2024-06-17 14:19:54 +08:00
2024-06-24 16:53:53 +08:00
/**
* 重新充值会员
*/
public function again_recharge ()
{
$auth_code = $this -> request -> post ( 'auth_code' ); //微信支付条码
$id = $this -> request -> post ( 'id' , 0 ); //id
$order = UserRecharge :: where ( 'id' , $id ) -> where ( 'paid' , 0 ) -> find ();
if ( ! $order ){
return $this -> fail ( '订单不存在' );
}
2024-06-24 17:21:32 +08:00
$order_id = getNewOrderId ( 'CZ' );
UserRecharge :: where ( 'id' , $id ) -> update ([ 'order_id' => $order_id ]);
$order [ 'order_id' ] = $order_id ;
2024-06-24 16:53:53 +08:00
$order [ 'pay_price' ] = $order [ 'price' ];
2024-07-06 17:32:51 +08:00
$order [ 'attach' ] = 'recharge' ;
2024-06-24 16:53:53 +08:00
$result = PaymentLogic :: codepay ( $auth_code , $order , '条码支付' );
if ( isset ( $result [ 'trade_state_desc' ]) && $result [ 'trade_state_desc' ] == '支付成功' ) {
PayNotifyLogic :: handle ( 'recharge' , $result [ 'out_trade_no' ], $result );
} else {
Redis :: send ( 'send-code-pay' , [ 'order_id' => $order [ 'order_id' ], 'pay_type' => 'recharge' ]);
return $this -> success ( '用户支付中' );
}
return $this -> success ( '支付成功' , [ 'out_trade_no' => $result [ 'out_trade_no' ], 'pay_type' => PayEnum :: WECHAT_PAY_BARCODE , 'transaction_id' => $result [ 'transaction_id' ]]);
}
2024-06-17 14:19:54 +08:00
/**
* 门店会员充值数量
*/
public function recharge_count ()
{
$store_id = $this -> request -> get ( 'store_id' , 0 );
$count = 0 ;
2024-06-22 17:11:22 +08:00
$createLog = 0 ;
2024-06-17 14:19:54 +08:00
if ( $store_id ){
$count = UserRecharge :: where ([ 'store_id' => $store_id , 'recharge_type' => 'INDUSTRYMEMBERS' , 'paid' => 1 ]) -> count ();
2024-06-22 17:11:22 +08:00
$createLog = UserCreateLog :: where ([ 'store_id' => $store_id ]) -> count ();
2024-06-17 14:19:54 +08:00
}
2024-06-22 17:11:22 +08:00
return $this -> success ( 'ok' ,[ 'count' => $count , 'create_log' => $createLog ]);
2024-06-17 14:19:54 +08:00
}
2024-09-23 15:14:09 +08:00
/**
* 小程序导航
*/
public function menu_list (){
$menu = [
2024-10-08 19:28:43 +08:00
// ['name'=>'每日配送统计','url'=>'/pagesOrder/deliveryOrder/index'],
// ['name'=>'累计配送统计','url'=>'/pagesOrder/deliveryOrder/count'],
2024-09-23 18:45:57 +08:00
[ 'name' => '每日销量统计' , 'url' => '/pagesOrder/productSales/index' ],
[ 'name' => '累计销量统计' , 'url' => '/pagesOrder/productSales/count' ],
[ 'name' => '商品库存' , 'url' => '/pagesOrder/store_product/index' ],
[ 'name' => '订单列表' , 'url' => '/pagesOrder/delivery/index' ],
2024-10-08 19:28:43 +08:00
// ['name'=>'店铺收支','url'=>'/pagesOrder/statistics/index'],
2024-09-23 15:14:09 +08:00
];
return $this -> success ( 'ok' ,[ 'menu' => $menu ]);
}
2024-09-27 18:16:07 +08:00
/**
* 店铺收支
*/
public function store_statistics (){
$store_id = SystemStoreStaff :: where ( 'uid' , $this -> userId ) -> value ( 'store_id' );
if ( ! $store_id ){
return $this -> fail ( '请先绑定店铺' );
}
$total_price = WarehouseProduct :: where ( 'store_id' , $store_id ) -> where ( 'financial_pm' , 0 ) -> sum ( 'total_price' );
$order = StoreOrder :: where ( 'store_id' , $store_id ) -> where ( 'refund_status' , 0 ) -> field ( 'sum(refund_price) as refund_price,sum(pay_price) as pay_price' ) -> find ();
$pay_price = bcsub ( $order [ 'pay_price' ], $order [ 'refund_price' ], 2 );
$commission = StoreFinanceFlow :: where ( 'store_id' , $store_id ) -> where ( 'financial_pm' , 0 ) -> where ( 'financial_type' , 3 ) -> sum ( 'number' );
$bond = StoreFinanceFlow :: where ( 'store_id' , $store_id ) -> where ( 'financial_pm' , 0 ) -> where ( 'financial_type' , 11 ) -> sum ( 'number' );
$vip = StoreFinanceFlow :: where ( 'store_id' , $store_id ) -> where ( 'financial_pm' , 0 ) -> where ( 'financial_type' , 12 ) -> sum ( 'number' );
$village = StoreFinanceFlow :: where ( 'store_id' , $store_id ) -> where ( 'financial_pm' , 0 ) -> where ( 'financial_type' , 14 ) -> sum ( 'number' );
$brigade = StoreFinanceFlow :: where ( 'store_id' , $store_id ) -> where ( 'financial_pm' , 0 ) -> where ( 'financial_type' , 15 ) -> sum ( 'number' );
$attrition = StoreFinanceFlow :: where ( 'store_id' , $store_id ) -> where ( 'financial_pm' , 0 ) -> where ( 'financial_type' , 16 ) -> sum ( 'number' );
$money = bcadd ( $commission , $bond , 2 );
$money = bcadd ( $money , $vip , 2 );
$money = bcadd ( $money , $village , 2 );
$money = bcadd ( $money , $brigade , 2 );
$expenditure = bcadd ( $money , $attrition , 2 );
$store_money = SystemStore :: where ( 'id' , $store_id ) -> value ( 'store_money' );
$list = [
[ 'name' => '手续费' , 'data' => [ $commission ]],
[ 'name' => '保证金' , 'data' => [ $bond ]],
[ 'name' => '厨师' , 'data' => [ $vip ]],
[ 'name' => '村长' , 'data' => [ $village ]],
[ 'name' => '队长' , 'data' => [ $brigade ]],
];
return $this -> success ( 'ok' ,[ 'total_price' => $total_price , 'pay_price' => $pay_price , 'list' => $list , 'attrition' => $attrition , 'store_money' => $store_money , 'expenditure' => $expenditure ]);
}
2024-06-04 16:51:26 +08:00
}