BankCardController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace api\modules\v1\controllers\user;
  3. use api\controllers\OnAuthController;
  4. use common\enums\AddonsEnum;
  5. use common\enums\ApiStatusEnum;
  6. use common\enums\StatusEnum;
  7. use common\helpers\sms\Sms;
  8. use common\models\common\UserBankCard;
  9. use Yii;
  10. class BankCardController extends OnAuthController
  11. {
  12. public $modelClass = '';
  13. /**
  14. *
  15. * 不用进行登录验证的方法
  16. *
  17. * 例如: ['index', 'update', 'create', 'view', 'delete']
  18. * 默认全部需要验证
  19. *
  20. * @var array
  21. */
  22. protected $authOptional = [];
  23. /**
  24. * @name:
  25. * @msg: 绑定银行卡,单纯绑定不签约快捷支付
  26. * @param {*}
  27. * @return {*}
  28. */
  29. public function actionBindCard()
  30. {
  31. if (!\Yii::$app->request->isPost) {
  32. return $this->error('请求方式错误');
  33. }
  34. $post = \Yii::$app->request->post();
  35. //'bank_code', 'card_type', 'card_type_name' 这三个参数由后端处理,前端不提交该参数
  36. $rules = [
  37. [['bank_card_no', 'payer_name', 'bank_name'], 'required'],
  38. ['bank_card_no', 'string', 'max' => 64],
  39. [['payer_name', 'bank_name','bank_address'], 'string', 'max' => 255],
  40. [['bank_code', 'card_type', 'card_type_name'], 'string', 'max' => 32],
  41. [['idcard'], 'string', 'max' => 18, 'min' => 18],
  42. ];
  43. $res = \Yii::$app->services->validate->validate($post, $rules);
  44. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  45. // 获取设置
  46. $setting = \Yii::$app->services->setting->mall->get($this->mall_id, 'other');
  47. // 开启身份证设置
  48. if (isset($setting['has_bank_idcard']) && $setting['has_bank_idcard'] && empty($post['idcard'])) {
  49. return $this->error('身份证不能为空');
  50. }
  51. $validate_card = [];
  52. try {
  53. $validate_card = UserBankCard::validateCardInfo($post['bank_card_no'],$post['bank_name']);
  54. }catch (\Exception $e){
  55. $validate_card['card_type'] = 'dc';
  56. $validate_card['card_type_name'] = '储蓄卡';
  57. }
  58. $post = array_merge($post,$validate_card);
  59. $bank_card_info = UserBankCard::findONe(['mall_id'=>$this->mall_id,'bank_card_no' => $post['bank_card_no'], 'status' => StatusEnum::ENABLED]);
  60. if (!empty($bank_card_info)) {
  61. return $this->error('银行卡已经被绑定');
  62. }
  63. $post['user_id'] = $this->user_id;
  64. $post['card_type'] = strtolower($post['card_type']);
  65. $post['bank_address'] = isset($post['bank_address']) ? $post['bank_address'] : '';
  66. $post['idcard'] = isset($post['idcard']) ? $post['idcard'] : '';
  67. $model = UserBankCard::setData($this->mall_id, $post, 'bind');
  68. if (false === $model) return $this->error(UserBankCard::getStaticError());
  69. return $this->success('操作成功', ['card_id' => $model->id]);
  70. }
  71. /**
  72. * @name:
  73. * @msg: 银行卡详情
  74. * @param {*}
  75. * @return {*}
  76. */
  77. public function actionCardInfo()
  78. {
  79. if (!\Yii::$app->request->isGet) {
  80. return $this->error('请求方式错误');
  81. }
  82. $card_id = \Yii::$app->request->get('card_id');
  83. if (empty($card_id)) return $this->error('参数错误');
  84. $card_info = UserBankCard::cardInfo($this->mall_id, $this->user_id, $card_id);
  85. // dd(UserBankCard::getStaticError());
  86. if (false === $card_info) {
  87. return $this->error(UserBankCard::getStaticError());
  88. }
  89. return $this->success('success', $card_info, ApiStatusEnum::CODE_SUCCESS, 0);
  90. }
  91. /**
  92. * @name:
  93. * @msg: 获取已经绑定的银行卡列表
  94. * @param {*}
  95. * @return {*}
  96. */
  97. public function actionCardList()
  98. {
  99. if (!\Yii::$app->request->isGet) {
  100. return $this->error('请求方式错误');
  101. }
  102. $list = UserBankCard::getUserCardList($this->user_id, $this->mall_id);
  103. return $this->success('success', ['list' => $list], ApiStatusEnum::CODE_SUCCESS, 0);
  104. }
  105. /**
  106. * @name:
  107. * @msg: 删除银行卡
  108. * @param {*}
  109. * @return {*}
  110. */
  111. public function actionCardDelete()
  112. {
  113. if (!\Yii::$app->request->isPost) {
  114. return $this->error('请求方式错误');
  115. }
  116. $card_id = \Yii::$app->request->post('card_id');
  117. $vcode = \Yii::$app->request->post('vcode');
  118. if(empty($this->user['mobile'])) return $this->error('请先登录');
  119. $mobile = $this->user['mobile'];
  120. if (empty($card_id) || empty($vcode) || empty($mobile)) return $this->error('参数错误');
  121. if (!Sms::checkValidateCode($mobile, $vcode,Sms::SMS_TYPE_BANK_CARD_DELETE)) return $this->error('验证码不正确');
  122. $card_info = UserBankCard::cardInfo($this->mall_id, $this->user_id, $card_id);
  123. // dd($card_info);
  124. if (empty($card_info)) {
  125. return $this->error('银行卡不存在或者已经删除');
  126. }
  127. if (1 == $card_info['is_sign']) {
  128. return $this->error('请先解除快捷支付的签约');
  129. }
  130. $params = [
  131. 'id' => $card_id,
  132. 'status' => StatusEnum::DELETE,
  133. ];
  134. $res = UserBankCard::setData($this->mall_id, $params, 'delete');
  135. if (false === $res) return $this->error(UserBankCard::getStaticError());
  136. return $this->success('删除成功');
  137. }
  138. public function actionGetUnbindSmsCode(){
  139. $area_code = '';// 初始化区号
  140. $gateways = 'domestic';// 默认国内短信
  141. // 检测是登录并且区号不为空则使用国际短信
  142. if (!empty($this->user) && !empty($this->user->area_code)) {
  143. $gateways = 'international';
  144. $area_code = $this->user->area_code;
  145. }
  146. // 判断是否有区号
  147. if (!empty($params['area_code']) && $params['area_code'] != 86) {
  148. $gateways = 'international';
  149. $area_code = $params['area_code'];
  150. }
  151. if(empty($this->user['mobile'])) return $this->error('请先登录');
  152. $params['mobile'] = $this->user['mobile'];
  153. $sms = new Sms($this->mall_id, $gateways);
  154. try {
  155. if ($sms->sendCaptcha($params['mobile'], $area_code,Sms::SMS_TYPE_BANK_CARD_DELETE) === false) return $this->error('发送失败');
  156. }catch (\Exception $e){
  157. return $this->error($e->getMessage());
  158. }
  159. return $this->success('发送成功');
  160. }
  161. public function actionDel()
  162. {
  163. try {
  164. if (\Yii::$app->request->isPost) {
  165. $params = Yii::$app->request->post();
  166. $res = Yii::$app->services->validate->validate($params, [
  167. [['card_id'], 'required','message' => '请输入卡id'],
  168. ]);
  169. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  170. UserBankCard::delBankCard($this->user_id,$params['card_id'],$this->mall_id);
  171. return $this->success('删除成功');
  172. }
  173. } catch (\Exception $e) {
  174. return $this->error($e->getMessage());
  175. }
  176. }
  177. /**
  178. * 是否需要填写身份证
  179. *
  180. * @return mixed
  181. */
  182. public function actionHasIdcard()
  183. {
  184. $setting = \Yii::$app->services->setting->mall->get($this->mall_id, 'other');
  185. return $this->success('ok', [
  186. 'has_bank_idcard' => isset($setting['has_bank_idcard']) ? $setting['has_bank_idcard'] : 0
  187. ]);
  188. }
  189. }