OpenForm.php 5.6 KB

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