IbOpenForm.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018/1/16/016
  6. * Time: 16:23
  7. */
  8. namespace backend\models\forms;
  9. use backend\models\MailApi;
  10. use backend\models\OpenApi;
  11. use common\helpers\BankCardHelper2;
  12. use common\helpers\Idcard;
  13. use yii\base\Model;
  14. use yii\web\UploadedFile;
  15. class IbOpenForm extends Model
  16. {
  17. public $id_card;
  18. public $card0;
  19. public $card1;
  20. public $name;
  21. public $address;
  22. public $mobile;
  23. public $email;
  24. public $vcode;
  25. public $rid;
  26. public $ref_id;
  27. public $forex;
  28. public $metal;
  29. public $hk_fifty;
  30. public $btc;
  31. public $gold;
  32. public $silver;
  33. public $cfd;
  34. // public $wy;
  35. public $bank_name;
  36. public $bank_province;
  37. public $bank_city;
  38. public $bank_district;
  39. public $bank_branch;
  40. public $bank_card_no;
  41. public $collect_name;
  42. public $bank_swift_code;
  43. public $agree1;
  44. public $agree2;
  45. /**
  46. * @inheritdoc
  47. */
  48. public function rules()
  49. {
  50. return [
  51. ['id_card', 'required', 'message' => '请填写身份证号码'],
  52. ['id_card', 'checkIdCard'],
  53. ['card0', 'required', 'message' => '请选择身份证正面照片'],
  54. ['card0', 'file'],
  55. ['card1', 'required', 'message' => '请选择身份证反面照片'],
  56. ['card1', 'file'],
  57. ['name', 'required', 'message' => '请填写姓名'],
  58. ['address', 'required', 'message' => '请填写地址'],
  59. //['mobile', 'required', 'message' => '请填写手机号码'],
  60. //['mobile', 'match', 'pattern' => '/^1(3[0-9]|4[57]|5[0-35-9]|7[01678]|8[0-9])\d{8}$/'],
  61. ['email', 'required', 'message' => '请填写电子邮箱'],
  62. ['email', 'email'],
  63. ['rid', 'required', 'message' => '请填写推荐人ID'],
  64. ['ref_id', 'required', 'message' => '请填写上级代理ID'],
  65. ['vcode', 'required', 'message' => '请填写邮件验证码'],
  66. ['vcode', 'checkMailVcode'],
  67. /*
  68. ['bank_name', 'required', 'message' => '请选择银行'],
  69. ['bank_province', 'required', 'message' => '请选择省'],
  70. ['bank_city', 'required', 'message' => '请选择市'],
  71. ['bank_branch', 'required', 'message' => '请填写支行名称'],
  72. ['bank_card_no', 'required', 'message' => '请填写银行卡号'],
  73. ['bank_card_no', 'checkBankCardNo'],
  74. ['bank_swift_code', 'required', 'message' => '请填写银行swift代码'],
  75. */
  76. ['collect_name', 'required', 'message' => '请填写收款人姓名'],
  77. ['rid', 'integer'],
  78. [['agree1', 'agree2'], 'boolean', 'trueValue' => 'on'],
  79. ];
  80. }
  81. /**
  82. * @param string $attribute
  83. * @param array $params
  84. */
  85. public function checkIdCard($attribute, $params = [])
  86. {
  87. if (!$this->hasErrors()) {
  88. if (Idcard::getInstance()->isChinaIDCard($this->id_card) == false) {
  89. $this->addError($attribute, '身份证号码格式错误');
  90. }
  91. }
  92. }
  93. /**
  94. * @param string $attribute
  95. * @param array $params
  96. */
  97. public function checkBankCardNo($attribute, $params = [])
  98. {
  99. if (!$this->hasErrors()) {
  100. if (BankCardHelper2::check_bankCard($this->bank_card_no) == false) {
  101. $this->addError($attribute, '银行卡账号格式错误');
  102. }
  103. }
  104. }
  105. /**
  106. * @param string $attribute
  107. * @param array $params
  108. */
  109. public function checkMailVcode($attribute, $params = [])
  110. {
  111. if (!$this->hasErrors()) {
  112. $api = new MailApi();
  113. $result = $api->getMailCode($this->email);
  114. if ($result['code'] == 1) {
  115. $code = isset($result['data']['code']) ? trim($result['data']['code']) : '';
  116. $created = isset($result['data']['created']) ? trim($result['data']['created']) : 0;
  117. if ($code == $this->vcode && (intval($created / 1000) + 1800) >= time()) {
  118. // 验证码正确 有效
  119. } else {
  120. $this->addError($attribute, '邮件证码错误');
  121. }
  122. }
  123. }
  124. }
  125. /**
  126. * 代理商(ib)开户申请
  127. * @return bool
  128. */
  129. public function complete()
  130. {
  131. if ($this->validate()) {
  132. $open = $openRecord = [];
  133. $openApi = new OpenApi();
  134. $open['open_type'] = 2;
  135. $open['name'] = $this->name;
  136. $open['id_card'] = $this->id_card;
  137. $open['email'] = $this->email;
  138. //$open['mobile'] = $this->mobile;
  139. $webRoot = \Yii::getAlias('@webroot');
  140. if ($this->card0 instanceof UploadedFile) {
  141. $filename = false;
  142. for ($i = 0; $i < 2; $i++) {
  143. $temp = '/upload/' . date('YmdHis_') . mt_rand(10000, 99999) . '.' . $this->card0->getExtension();
  144. if (!is_file($webRoot . $temp)) {
  145. $filename = $temp;
  146. break;
  147. }
  148. }
  149. if ($filename != false && $this->card0->saveAs($webRoot . $filename)) {
  150. $open['id_card_file_path0'] = $filename;
  151. }
  152. }
  153. if ($this->card1 instanceof UploadedFile) {
  154. $filename = false;
  155. for ($i = 0; $i < 2; $i++) {
  156. $temp = '/upload/' . date('YmdHis_') . mt_rand(10000, 99999) . '.' . $this->card1->getExtension();
  157. if (!is_file($webRoot . $temp)) {
  158. $filename = $temp;
  159. break;
  160. }
  161. }
  162. if ($filename != false && $this->card1->saveAs($webRoot . $filename)) {
  163. $open['id_card_file_path1'] = $filename;
  164. }
  165. }
  166. $open['rid'] = $this->rid;
  167. $open['address'] = $this->address;
  168. $rs = $openApi->open($open);
  169. if ($rs['code'] == 1) {
  170. $open_id = $rs['data']['id'];
  171. $openRecord['open_id'] = $open_id;
  172. $openRecord['ref_id'] = $this->ref_id;
  173. $openRecord['collect_name'] = $this->collect_name;
  174. /*
  175. $openRecord['bank_name'] = $this->bank_name;
  176. $openRecord['bank_province'] = $this->bank_province;
  177. $openRecord['bank_city'] = $this->bank_city;
  178. $openRecord['bank_district'] = $this->bank_district;
  179. $openRecord['bank_branch'] = $this->bank_branch;
  180. $openRecord['bank_card_no'] = $this->bank_card_no;
  181. $openRecord['bank_swift_code'] = $this->bank_swift_code;
  182. */
  183. $result = $openApi->openRecord($openRecord);
  184. if ($result['code'] == 1) {
  185. return true;
  186. } else {
  187. if (is_array($result['message'])) {
  188. $this->addErrors($result['message']);
  189. } else {
  190. $this->addError('name', $result['message']);
  191. }
  192. }
  193. } else {
  194. if (is_array($rs['message'])) {
  195. $this->addErrors($rs['message']);
  196. } else {
  197. $this->addError('name', $rs['message']);
  198. }
  199. }
  200. } else {
  201. $this->addErrors($this->getErrors());
  202. }
  203. return !$this->hasErrors();
  204. }
  205. }