| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
- namespace api\modules\v1\controllers\user;
- use api\controllers\OnAuthController;
- use common\enums\AddonsEnum;
- use common\enums\ApiStatusEnum;
- use common\enums\StatusEnum;
- use common\helpers\sms\Sms;
- use common\models\common\UserBankCard;
- use Yii;
- class BankCardController extends OnAuthController
- {
- public $modelClass = '';
- /**
- *
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = [];
- /**
- * @name:
- * @msg: 绑定银行卡,单纯绑定不签约快捷支付
- * @param {*}
- * @return {*}
- */
- public function actionBindCard()
- {
- if (!\Yii::$app->request->isPost) {
- return $this->error('请求方式错误');
- }
- $post = \Yii::$app->request->post();
- //'bank_code', 'card_type', 'card_type_name' 这三个参数由后端处理,前端不提交该参数
- $rules = [
- [['bank_card_no', 'payer_name', 'bank_name'], 'required'],
- ['bank_card_no', 'string', 'max' => 64],
- [['payer_name', 'bank_name','bank_address'], 'string', 'max' => 255],
- [['bank_code', 'card_type', 'card_type_name'], 'string', 'max' => 32],
- [['idcard'], 'string', 'max' => 18, 'min' => 18],
- ];
- $res = \Yii::$app->services->validate->validate($post, $rules);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- // 获取设置
- $setting = \Yii::$app->services->setting->mall->get($this->mall_id, 'other');
- // 开启身份证设置
- if (isset($setting['has_bank_idcard']) && $setting['has_bank_idcard'] && empty($post['idcard'])) {
- return $this->error('身份证不能为空');
- }
- $validate_card = [];
- try {
- $validate_card = UserBankCard::validateCardInfo($post['bank_card_no'],$post['bank_name']);
- }catch (\Exception $e){
- $validate_card['card_type'] = 'dc';
- $validate_card['card_type_name'] = '储蓄卡';
- }
- $post = array_merge($post,$validate_card);
- $bank_card_info = UserBankCard::findONe(['mall_id'=>$this->mall_id,'bank_card_no' => $post['bank_card_no'], 'status' => StatusEnum::ENABLED]);
- if (!empty($bank_card_info)) {
- return $this->error('银行卡已经被绑定');
- }
- $post['user_id'] = $this->user_id;
- $post['card_type'] = strtolower($post['card_type']);
- $post['bank_address'] = isset($post['bank_address']) ? $post['bank_address'] : '';
- $post['idcard'] = isset($post['idcard']) ? $post['idcard'] : '';
- $model = UserBankCard::setData($this->mall_id, $post, 'bind');
- if (false === $model) return $this->error(UserBankCard::getStaticError());
- return $this->success('操作成功', ['card_id' => $model->id]);
- }
- /**
- * @name:
- * @msg: 银行卡详情
- * @param {*}
- * @return {*}
- */
- public function actionCardInfo()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $card_id = \Yii::$app->request->get('card_id');
- if (empty($card_id)) return $this->error('参数错误');
- $card_info = UserBankCard::cardInfo($this->mall_id, $this->user_id, $card_id);
- // dd(UserBankCard::getStaticError());
- if (false === $card_info) {
- return $this->error(UserBankCard::getStaticError());
- }
- return $this->success('success', $card_info, ApiStatusEnum::CODE_SUCCESS, 0);
- }
- /**
- * @name:
- * @msg: 获取已经绑定的银行卡列表
- * @param {*}
- * @return {*}
- */
- public function actionCardList()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $list = UserBankCard::getUserCardList($this->user_id, $this->mall_id);
- return $this->success('success', ['list' => $list], ApiStatusEnum::CODE_SUCCESS, 0);
- }
- /**
- * @name:
- * @msg: 删除银行卡
- * @param {*}
- * @return {*}
- */
- public function actionCardDelete()
- {
- if (!\Yii::$app->request->isPost) {
- return $this->error('请求方式错误');
- }
- $card_id = \Yii::$app->request->post('card_id');
- $vcode = \Yii::$app->request->post('vcode');
- if(empty($this->user['mobile'])) return $this->error('请先登录');
- $mobile = $this->user['mobile'];
- if (empty($card_id) || empty($vcode) || empty($mobile)) return $this->error('参数错误');
- if (!Sms::checkValidateCode($mobile, $vcode,Sms::SMS_TYPE_BANK_CARD_DELETE)) return $this->error('验证码不正确');
- $card_info = UserBankCard::cardInfo($this->mall_id, $this->user_id, $card_id);
- // dd($card_info);
- if (empty($card_info)) {
- return $this->error('银行卡不存在或者已经删除');
- }
- if (1 == $card_info['is_sign']) {
- return $this->error('请先解除快捷支付的签约');
- }
- $params = [
- 'id' => $card_id,
- 'status' => StatusEnum::DELETE,
- ];
- $res = UserBankCard::setData($this->mall_id, $params, 'delete');
- if (false === $res) return $this->error(UserBankCard::getStaticError());
- return $this->success('删除成功');
- }
- public function actionGetUnbindSmsCode(){
- $area_code = '';// 初始化区号
- $gateways = 'domestic';// 默认国内短信
- // 检测是登录并且区号不为空则使用国际短信
- if (!empty($this->user) && !empty($this->user->area_code)) {
- $gateways = 'international';
- $area_code = $this->user->area_code;
- }
- // 判断是否有区号
- if (!empty($params['area_code']) && $params['area_code'] != 86) {
- $gateways = 'international';
- $area_code = $params['area_code'];
- }
- if(empty($this->user['mobile'])) return $this->error('请先登录');
- $params['mobile'] = $this->user['mobile'];
- $sms = new Sms($this->mall_id, $gateways);
- try {
- if ($sms->sendCaptcha($params['mobile'], $area_code,Sms::SMS_TYPE_BANK_CARD_DELETE) === false) return $this->error('发送失败');
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- return $this->success('发送成功');
- }
- public function actionDel()
- {
- try {
- if (\Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['card_id'], 'required','message' => '请输入卡id'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- UserBankCard::delBankCard($this->user_id,$params['card_id'],$this->mall_id);
- return $this->success('删除成功');
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- /**
- * 是否需要填写身份证
- *
- * @return mixed
- */
- public function actionHasIdcard()
- {
- $setting = \Yii::$app->services->setting->mall->get($this->mall_id, 'other');
- return $this->success('ok', [
- 'has_bank_idcard' => isset($setting['has_bank_idcard']) ? $setting['has_bank_idcard'] : 0
- ]);
- }
- }
|