OpenForm.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace backend\models\forms;
  3. use backend\models\OpenApi;
  4. use backend\models\SmsApi;
  5. use common\helpers\Idcard;
  6. use Yii;
  7. use yii\base\Model;
  8. use yii\web\UploadedFile;
  9. class OpenForm extends Model
  10. {
  11. public $card0;
  12. public $card1;
  13. public $name;
  14. public $id_card;
  15. public $email;
  16. public $mobile;
  17. public $vcode;
  18. public $level;
  19. public $rid;
  20. public $address;
  21. public $agree1;
  22. public $agree2;
  23. public $agree3;
  24. public $agree4;
  25. public $agree5;
  26. public $agree6;
  27. /**
  28. * @inheritdoc
  29. */
  30. public function rules()
  31. {
  32. return [
  33. ['card0', 'required', 'message' => '请选择身份证正面照片'],
  34. ['card0', 'file'],
  35. ['card1', 'required', 'message' => '请选择身份证反面照片'],
  36. ['card1', 'file'],
  37. ['name', 'required', 'message' => '请填写姓名'],
  38. ['id_card', 'required', 'message' => '身份证号码格式错误'],
  39. ['id_card', 'checkIdCard'],
  40. ['email', 'required', 'message' => '请填写电子邮箱'],
  41. ['email', 'email'],
  42. ['mobile', 'required', 'message' => '请填写手机号码'],
  43. ['mobile', 'match', 'pattern' => '/^1(3[0-9]|4[57]|5[0-35-9]|7[01678]|8[0-9])\d{8}$/'],
  44. ['vcode', 'required', 'message' => '请填写手机验证码'],
  45. ['vcode', 'checkVcode'],
  46. ['level', 'required'],
  47. ['level', 'in', 'range' => [100, 200]],
  48. ['rid', 'integer'],
  49. ['address', 'required', 'message' => '请填写地址'],
  50. [['agree1', 'agree2', 'agree3', 'agree4', 'agree5', 'agree6'], 'boolean', 'trueValue' => 'on'],
  51. ];
  52. }
  53. /**
  54. * @param string $attribute
  55. * @param array $params
  56. */
  57. public function checkIdCard($attribute, $params = [])
  58. {
  59. if (!$this->hasErrors()) {
  60. if (Idcard::getInstance()->isChinaIDCard($this->id_card) == false) {
  61. //$this->addError($attribute, '身份证号码格式错误');
  62. }
  63. }
  64. }
  65. /**
  66. * @param string $attribute
  67. * @param array $params
  68. */
  69. public function checkVcode($attribute, $params = [])
  70. {
  71. if (!$this->hasErrors()) {
  72. $api = new SmsApi();
  73. $result = $api->getCode($this->mobile);
  74. if ($result['code'] == 1) {
  75. $code = isset($result['data']['code']) ? trim($result['data']['code']) : '';
  76. $created = isset($result['data']['created']) ? trim($result['data']['created']) : 0;
  77. if ($code == $this->vcode && (intval($created / 1000) + 1800) >= time()) {
  78. // 验证码正确 有效
  79. } else {
  80. $this->addError($attribute, '手机验证码错误');
  81. }
  82. }
  83. }
  84. }
  85. /**
  86. * @return bool
  87. */
  88. public function open()
  89. {
  90. if ($this->validate()) {
  91. $api = new OpenApi();
  92. $data = [];
  93. $data['name'] = $this->name;
  94. $data['id_card'] = $this->id_card;
  95. $data['email'] = $this->email;
  96. $data['mobile'] = $this->mobile;
  97. $webRoot = Yii::getAlias('@webroot');
  98. if ($this->card0 instanceof UploadedFile) {
  99. $filename = false;
  100. for ($i = 0; $i < 2; $i++) {
  101. $temp = '/upload/' . date('YmdHis_') . mt_rand(10000, 99999) . '.' . $this->card0->getExtension();
  102. if (!is_file($webRoot . $temp)) {
  103. $filename = $temp;
  104. break;
  105. }
  106. }
  107. if ($filename != false && $this->card0->saveAs($webRoot . $filename)) {
  108. $data['id_card_file_path0'] = $filename;
  109. }
  110. }
  111. if ($this->card1 instanceof UploadedFile) {
  112. $filename = false;
  113. for ($i = 0; $i < 2; $i++) {
  114. $temp = '/upload/' . date('YmdHis_') . mt_rand(10000, 99999) . '.' . $this->card1->getExtension();
  115. if (!is_file($webRoot . $temp)) {
  116. $filename = $temp;
  117. break;
  118. }
  119. }
  120. if ($filename != false && $this->card1->saveAs($webRoot . $filename)) {
  121. $data['id_card_file_path1'] = $filename;
  122. }
  123. }
  124. $data['rid'] = $this->rid;
  125. $data['address'] = $this->address;
  126. $data['level'] = $this->level;
  127. $result = $api->open($data);
  128. if ($result['code'] == 1) {
  129. return true;
  130. } else {
  131. if (is_array($result['message'])) {
  132. $this->addErrors($result['message']);
  133. } else {
  134. $this->addError('name', $result['message']);
  135. }
  136. }
  137. }
  138. return !$this->hasErrors();
  139. }
  140. }