WithdrawForm.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace backend\models\forms;
  3. use backend\models\WithdrawApi;
  4. use Yii;
  5. use yii\base\Model;
  6. class WithdrawForm extends Model
  7. {
  8. public $login;
  9. public $amount;
  10. public $true_name;
  11. public $bank_province;
  12. public $bank_city;
  13. public $bank_name;
  14. public $bank_sub_name;
  15. public $bank_card_no;
  16. public $swift;
  17. public $mobile;
  18. /**
  19. * @inheritdoc
  20. */
  21. public function rules()
  22. {
  23. return [
  24. [['login', 'amount', 'true_name', 'bank_province', 'bank_city', 'bank_name', 'bank_sub_name', 'bank_card_no', 'swift', 'mobile'], 'required'],
  25. [['login'], 'integer'],
  26. [['amount'], 'match', 'pattern' => '/^([1-9]\d*|[0])(\.\d{1,2})?$/'],
  27. [['bank_card_no'], 'integer'],
  28. [['mobile'], 'match', 'pattern' => '/^1(3[0-9]|4[57]|5[0-35-9]|7[01678]|8[0-9])\d{8}$/'],
  29. ];
  30. }
  31. /**
  32. * @return bool
  33. */
  34. public function addWithdraw()
  35. {
  36. if (Yii::$app->getUser()->getIsGuest()) {
  37. $this->addError('login', '用户未登录');
  38. Yii::$app->getUser()->loginRequired();
  39. return false;
  40. }
  41. if ($this->validate()) {
  42. $data = [];
  43. $data['member_id'] = Yii::$app->getUser()->getId();
  44. $data['amount'] = $this->amount;
  45. $data['true_name'] = $this->true_name;
  46. $data['bank_name'] = $this->bank_name;
  47. $data['bank_sub_name'] = $this->bank_sub_name;
  48. $data['bank_card_no'] = $this->bank_card_no;
  49. $data['bank_province'] = $this->bank_province;
  50. $data['bank_city'] = $this->bank_city;
  51. $data['mobile'] = $this->mobile;
  52. $data['login'] = $this->login;
  53. $data['swift'] = $this->swift;
  54. $api = new WithdrawApi();
  55. $result = $api->addWithdraw($data);
  56. if ($result['code'] == 0) {
  57. if (is_array($result['message'])) {
  58. $this->addErrors($result['message']);
  59. } else {
  60. $this->addError('receivers', $result['message']);
  61. }
  62. }
  63. }
  64. return !$this->hasErrors();
  65. }
  66. }