OpenForm.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. return true;
  61. if (!$this->hasErrors()) {
  62. if (Idcard::getInstance()->isChinaIDCard($this->id_card) == false) {
  63. $this->addError($attribute, '身份证号码格式错误');
  64. }
  65. }
  66. }
  67. /**
  68. * @param string $attribute
  69. * @param array $params
  70. */
  71. public function checkVcode($attribute, $params = [])
  72. {
  73. if (!$this->hasErrors()) {
  74. $api = new SmsApi();
  75. $result = $api->getCode($this->mobile);
  76. if ($result['code'] == 1) {
  77. $code = isset($result['data']['code']) ? trim($result['data']['code']) : '';
  78. $created = isset($result['data']['created']) ? trim($result['data']['created']) : 0;
  79. if ($code == $this->vcode && (intval($created / 1000) + 1800) >= time()) {
  80. // 验证码正确 有效
  81. } else {
  82. $this->addError($attribute, '手机验证码错误');
  83. }
  84. }
  85. }
  86. }
  87. /**
  88. * @param string $attribute
  89. * @param array $params
  90. */
  91. public function checkMailVcode($attribute, $params = [])
  92. {
  93. if (!$this->hasErrors()) {
  94. $api = new MailApi();
  95. $result = $api->getMailCode($this->email);
  96. if ($result['code'] == 1) {
  97. $code = isset($result['data']['code']) ? trim($result['data']['code']) : '';
  98. $created = isset($result['data']['created']) ? trim($result['data']['created']) : 0;
  99. if ($code == $this->vcode && (intval($created / 1000) + 1800) >= time()) {
  100. // 验证码正确 有效
  101. } else {
  102. $this->addError($attribute, '邮件证码错误');
  103. }
  104. }
  105. }
  106. }
  107. /**
  108. * @return bool
  109. */
  110. public function open()
  111. {
  112. if ($this->validate()) {
  113. $api = new OpenApi();
  114. $data = [];
  115. $data['name'] = $this->name;
  116. $data['id_card'] = $this->id_card;
  117. $data['email'] = $this->email;
  118. $webRoot = Yii::getAlias('@webroot');
  119. if ($this->card0 instanceof UploadedFile) {
  120. $filename = false;
  121. for ($i = 0; $i < 2; $i++) {
  122. $temp = '/upload/' . date('YmdHis_') . mt_rand(10000, 99999) . '.' . $this->card0->getExtension();
  123. if (!is_file($webRoot . $temp)) {
  124. $filename = $temp;
  125. break;
  126. }
  127. }
  128. if ($filename != false && $this->card0->saveAs($webRoot . $filename)) {
  129. $data['id_card_file_path0'] = $filename;
  130. }
  131. }
  132. if ($this->card1 instanceof UploadedFile) {
  133. $filename = false;
  134. for ($i = 0; $i < 2; $i++) {
  135. $temp = '/upload/' . date('YmdHis_') . mt_rand(10000, 99999) . '.' . $this->card1->getExtension();
  136. if (!is_file($webRoot . $temp)) {
  137. $filename = $temp;
  138. break;
  139. }
  140. }
  141. if ($filename != false && $this->card1->saveAs($webRoot . $filename)) {
  142. $data['id_card_file_path1'] = $filename;
  143. }
  144. }
  145. $data['rid'] = $this->rid;
  146. $data['address'] = $this->address;
  147. $data['level'] = $this->level;
  148. $result = $api->open($data);
  149. if ($result['code'] == 1) {
  150. return true;
  151. } else {
  152. if (is_array($result['message'])) {
  153. $this->addErrors($result['message']);
  154. } else {
  155. $this->addError('name', $result['message']);
  156. }
  157. }
  158. }
  159. return !$this->hasErrors();
  160. }
  161. }