| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- namespace addons\Store\api\modules\v2\controllers;
- use addons\Store\common\models\Store;
- use addons\Store\common\models\StoreAdmin;
- use addons\Store\common\models\StoreBank;
- use addons\Store\common\models\StoreSettings;
- use addons\Store\common\models\StoreWithdrawLog;
- use addons\Store\common\enums\StoreEnum;
- use addons\Store\common\services\FinanceService;
- use common\enums\ApiStatusEnum;
- use common\enums\StatusEnum;
- use common\models\user\User;
- use common\globals\GlobalInvoke;
- use common\enums\WithdrawWayEnum;
- use common\models\common\PlatformSetting;
- use Yii;
- use api\controllers\OnAuthController;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\Store\api\modules\v2\controllers
- */
- class FinanceController extends BaseController
- {
- public $modelClass = '';
- /**
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = ['withdrawal','edit-store-bank','finance'];
- /**
- * 提现申请
- * @Author bing
- * @DateTime 2022-04-26 18:04:50
- * @return void
- * @copyright: Copyright (c) 2022 广东七件事集团
- */
- public function actionWithdrawal()
- {
- if (\Yii::$app->request->isGet) {
- $store = Store::getOne([['id' => $this->store_id]], true, [], false, 'id,user_id,balance');
- $user = User::getOne([['id' => $store['user_id']]], true, [], false, 'id,nickname,avatar_url,mobile');
- $store_banks = StoreBank::lists(['where' => [['store_id' => $store['id']], ['status' => StatusEnum::ENABLED]], 'id,mall_id,store_id,username,bank,card_no']);
- $balance_cash_service_fee = \Yii::$app->services->setting->mall->get($this->mall_id, 'withdraw_balance.balance_cash_service_fee');
- $store['balance_cash_service_fee'] = $balance_cash_service_fee;
- if(!empty($store_banks)){
- foreach ($store_banks as &$item){
- $item['bank_name'] = $item['bank'];
- $item['bank_card_no'] = $item['card_no'];
- }
- }
- return $this->success('ok', ['user' => $user, 'store' => $store, 'store_banks' => $store_banks], ApiStatusEnum::CODE_SUCCESS, 0);
- } else {
- $params = \Yii::$app->request->post();
- if (empty($params['withdrawal_money'])) return $this->error('请输入金额');
- if ($params['withdrawal_money'] < 0) return $this->error('输入金额必须大于0');
- $store = Store::getOne([['id' => $this->store_id]], false, [], false, 'id,mall_id,user_id,balance');
- $balance_cash_service_fee = \Yii::$app->services->setting->mall->get($this->mall_id, 'withdraw_balance.balance_cash_service_fee');
- if ($params['withdrawal_money'] > $store['balance']) return $this->error('输入金额大于可提现金额');
- $params['mall_id'] = $this->mall_id;
- $params['store_id'] = $this->store_id;
- $params['balance_cash_service_fee'] = $balance_cash_service_fee;
- if (empty($params['remark'])) $params['remark'] = '';
- $res = StoreWithdrawLog::withdrawApply($params);
- if ($res == false) return $this->error(StoreWithdrawLog::getStaticError());
- return $this->success('操作成功');
- }
- }
- //编辑银行卡
- public function actionEditStoreBank()
- {
- if (\Yii::$app->request->isPost) {
- $params = \Yii::$app->request->post();
- $params['mall_id'] = $this->mall_id;
- $params['store_id'] = $this->store_id;
- $res = StoreBank::setData($params);
- if ($res == false) $this->error('操作失败');
- return $this->success();
- } else {
- $params = \Yii::$app->request->get();
- $store_bank_info = StoreBank::getOne([['id' => $params['id']], ['mall_id' => $this->mall_id], ['store_id' => $this->store_id]], true);
- return $this->success('ok', ['store_bank_info' => $store_bank_info],ApiStatusEnum::CODE_SUCCESS,0);
- }
- }
- /**
- * 资产
- * @return mixed|void
- */
- public function actionFinance()
- {
- $store = Store::findOne(['id' => $this->store_id]);
- // $balance_cash_service_fee = \Yii::$app->services->setting->mall->get($this->mall_id, 'withdraw_balance.balance_cash_service_fee');
- $user = User::getOne([['id' => $store->user_id]], true, [], false, 'id,nickname,avatar_url,mobile');
- $finance = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'finance');
- $storeSetting = StoreSettings::findOne(['store_id' => $this->store_id]);
- $freeze_is_enable = 0;
- $finance_name = '资产';
- if(!empty($finance)){
- if(!empty($finance['freeze_is_enable'])){
- $freeze_is_enable = $finance['freeze_is_enable'];
- }
- if(!empty($finance['finance_name'])){
- $finance_name = $finance['finance_name'];
- }
- }
- //提现条件
- if(isset($finance['balance_cash_type'])){
- $cashType = json_decode($finance['balance_cash_type'],true);
- }else{
- //默认银行卡/余额提现
- $cashType = ['balance','bank'];
- }
- $balance_cash_service_fee = 0;
- $balance_cash_service_fee_limit = 0;
- $balance_cash_service_fee_month = 0;
- if (!empty($storeSetting->is_enable_withdraw_setting)) {
- $finance['balance_cash_service_fee_limit'] = $storeSetting->balance_cash_service_fee_limit;
- $finance['balance_cash_service_fee_month'] = $storeSetting->balance_cash_service_fee_month;
- $finance['balance_cash_service_fee_diff'] = $storeSetting->balance_cash_service_fee_diff;
- $finance['balance_cash_service_fee'] = $storeSetting->balance_cash_service_fee;
- }
- if(!empty($finance['balance_cash_service_fee_limit'])){
- $balance_cash_service_fee_limit = $finance['balance_cash_service_fee_limit'];
- $balance_cash_service_fee_month = $finance['balance_cash_service_fee_month'];
- $balance_cash_service_fee = $finance['balance_cash_service_fee_diff'] ?? 0;
- }else{
- if(isset($finance['balance_cash_service_fee'])){
- $balance_cash_service_fee = $finance['balance_cash_service_fee'] ?? 0;
- }else{
- $balance_cash_service_fee = \Yii::$app->services->setting->mall->get($this->mall_id, 'withdraw_balance.balance_cash_service_fee');
- }
- }
- if (!$cashType) {
- return $this->error('系统未设置提现方式');
- }
- $cash__type_name = [];
- $get_system_setting = PlatformSetting::getConfByGroup('system', true);
- if (!empty($cashType)) {
- $cash_type = $cashType;
- $cash__type_name = [];
- $default_withdraw_way = WithdrawWayEnum::getDefaultWithDrawWayMap(WithdrawWayEnum::WITHDRAW_TYPE_2);
- $extension_withdraw_way = GlobalInvoke::exec(GlobalInvoke::INVOKE_GET_WITHDRAW_EXTENSION_WAY,$this->mall_id,['mall_id'=>$this->mall_id]);
- $all_withdraw = array_merge($default_withdraw_way,$extension_withdraw_way);
- $cash_type_keys = array_keys($all_withdraw);
- if (in_array('bank', $cash_type_keys) || in_array('joinpay', $cash_type_keys)) {
- $bank_card_list = StoreBank::lists(['where' => [['store_id' => $this->store_id], ['status' => StatusEnum::ENABLED]], 'select' => 'id,mall_id,store_id,username as payer_name,bank as bank_name,card_no as bank_card_no']);
- }
- foreach ($cash_type as $i => $item) {
- if (isset($all_withdraw[$item])) {
- $taxFee = 0;
- $name = '';
- if (isset($all_withdraw[$item])) {
- if (is_array($all_withdraw[$item])) {
- $name = $all_withdraw[$item]['name'] ?? '';
- $taxFee = $all_withdraw[$item]['taxFee'] ?? 0;
- } else {
- $name = $all_withdraw[$item];
- }
- }
- $cash__type_name[$i]['type'] = $item;
- $cash__type_name[$i]['taxFee'] = $taxFee;
- $cash__type_name[$i]['name'] = $name;
- $cash__type_name[$i]['img'] = $get_system_setting['api_url']['value'] . '/resources/img/payment/' . $item . '.png';
- if ('bank' == $item || 'joinpay' == $item) {
- $cash__type_name[$i]['bank_card_list'] = $bank_card_list;
- }
- }
- }
- }
- $return = [
- 'balance' => $store->balance, //可提现金额
- 'unsettled_amount' => $store->unsettled_amount,//待结算金额
- // 'store_banks' => $store_banks,
- 'balance_cash_service_fee' => $balance_cash_service_fee,
- 'balance_cash_service_fee_limit' => $balance_cash_service_fee_limit,
- 'balance_cash_service_fee_month' => $balance_cash_service_fee_month,
- 'freeze_is_enable' => $freeze_is_enable,
- 'finance_name' => $finance_name,
- 'user' => $user,
- 'cash_type_name' => $cash__type_name,
- ];
- return $this->success('操作成功', $return,ApiStatusEnum::CODE_SUCCESS,JSON_UNESCAPED_UNICODE);
- }
- /**
- * 添加银行卡
- * @return void
- */
- public function actionAddBankCard()
- {
- try {
- $params = \Yii::$app->request->post();
- $res = \Yii::$app->services->validate->validate($params, [
- [['username', 'bank', 'card_no'], 'required'],
- ['card_no', 'string', 'max' => 64],
- [['username'], 'string', 'max' => 50],
- [['bank'], 'string', 'max' => 255],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $bank_card_info = StoreBank::findONe(['mall_id' => $this->mall_id, 'card_no' => $params['card_no'], 'store_id' => $this->store_id, 'status' => StatusEnum::ENABLED]);
- if (!empty($bank_card_info)) {
- throw new \Exception('银行卡已经被绑定');
- }
- $params['mall_id'] = $this->mall_id;
- $params['store_id'] = $this->store_id;
- $res = StoreBank::setData($params);
- if (!$res) throw new \Exception(StoreBank::getStaticError());
- return $this->success('添加成功');
- } catch (\Exception $exception) {
- return $this->error('操作失败:' . $exception->getMessage());
- }
- }
- /**
- * 提现
- * @throws \Exception
- */
- public function actionWithdraw()
- {
- $params = \Yii::$app->request->post();
- $need_auth = GlobalInvoke::exec(GlobalInvoke::REAL_AUTH, $this->mall_id, ['user_id' => $this->user_id,'mall_id' => $this->mall_id,'type' => 'cash']);
- if($need_auth === true){
- //需要先进行实名认证
- return $this->error('请先进行实名认证',[],ApiStatusEnum::CODE_ERROR_NEED_REAL_AUTH);
- }
- $form = new StoreWithdrawLog();
- $params['mall_id'] = $this->mall_id;
- $params['user_id'] = $this->user_id;
- $params['store_id'] = $this->store_id;
- $form->attributes = $params;
- $data = $form->saveRecord($params);
- if ($data['state']) {
- return $this->success('操作成功', $data['data']);
- } else {
- return $this->error($data['msg']);
- }
- }
- /**
- * 获取提现列表
- *
- * @return void
- */
- public function actionGetWithdrawList()
- {
- $service = new FinanceService();
- $service->mall_id = $this->mall_id;
- $service->store_id = $this->store_id;
- $this->success(
- '操作成功',
- $service->getWithdrawList(
- $this->request->get('type', 0),
- $this->request->get('limit', 20),
- $this->platform
- )
- );
- }
- }
|