| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2018/1/16/016
- * Time: 16:23
- */
- namespace backend\models\forms;
- use backend\models\MailApi;
- use backend\models\OpenApi;
- use common\helpers\BankCardHelper2;
- use common\helpers\Idcard;
- use yii\base\Model;
- use yii\web\UploadedFile;
- class IbOpenForm extends Model
- {
- public $id_card;
- public $card0;
- public $card1;
- public $name;
- public $address;
- public $mobile;
- public $email;
- public $vcode;
- public $rid;
- public $ref_id;
- public $forex;
- public $metal;
- public $hk_fifty;
- public $btc;
- public $gold;
- public $silver;
- public $cfd;
- // public $wy;
- public $bank_name;
- public $bank_province;
- public $bank_city;
- public $bank_district;
- public $bank_branch;
- public $bank_card_no;
- public $collect_name;
- public $bank_swift_code;
- public $agree1;
- public $agree2;
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- ['id_card', 'required', 'message' => '请填写身份证号码'],
- ['id_card', 'checkIdCard'],
- ['card0', 'required', 'message' => '请选择身份证正面照片'],
- ['card0', 'file'],
- ['card1', 'required', 'message' => '请选择身份证反面照片'],
- ['card1', 'file'],
- ['name', 'required', 'message' => '请填写姓名'],
- ['address', 'required', 'message' => '请填写地址'],
- //['mobile', 'required', 'message' => '请填写手机号码'],
- //['mobile', 'match', 'pattern' => '/^1(3[0-9]|4[57]|5[0-35-9]|7[01678]|8[0-9])\d{8}$/'],
- ['email', 'required', 'message' => '请填写电子邮箱'],
- ['email', 'email'],
- ['rid', 'required', 'message' => '请填写推荐人ID'],
- ['ref_id', 'required', 'message' => '请填写上级代理ID'],
- ['vcode', 'required', 'message' => '请填写邮件验证码'],
- ['vcode', 'checkMailVcode'],
- /*
- ['bank_name', 'required', 'message' => '请选择银行'],
- ['bank_province', 'required', 'message' => '请选择省'],
- ['bank_city', 'required', 'message' => '请选择市'],
- ['bank_branch', 'required', 'message' => '请填写支行名称'],
- ['bank_card_no', 'required', 'message' => '请填写银行卡号'],
- ['bank_card_no', 'checkBankCardNo'],
- ['bank_swift_code', 'required', 'message' => '请填写银行swift代码'],
- */
- ['collect_name', 'required', 'message' => '请填写收款人姓名'],
- ['rid', 'integer'],
- [['agree1', 'agree2'], 'boolean', 'trueValue' => 'on'],
- ];
- }
- /**
- * @param string $attribute
- * @param array $params
- */
- public function checkIdCard($attribute, $params = [])
- {
- if (!$this->hasErrors()) {
- if (Idcard::getInstance()->isChinaIDCard($this->id_card) == false) {
- $this->addError($attribute, '身份证号码格式错误');
- }
- }
- }
- /**
- * @param string $attribute
- * @param array $params
- */
- public function checkBankCardNo($attribute, $params = [])
- {
- if (!$this->hasErrors()) {
- if (BankCardHelper2::check_bankCard($this->bank_card_no) == false) {
- $this->addError($attribute, '银行卡账号格式错误');
- }
- }
- }
- /**
- * @param string $attribute
- * @param array $params
- */
- public function checkMailVcode($attribute, $params = [])
- {
- if (!$this->hasErrors()) {
- $api = new MailApi();
- $result = $api->getMailCode($this->email);
- if ($result['code'] == 1) {
- $code = isset($result['data']['code']) ? trim($result['data']['code']) : '';
- $created = isset($result['data']['created']) ? trim($result['data']['created']) : 0;
- if ($code == $this->vcode && (intval($created / 1000) + 1800) >= time()) {
- // 验证码正确 有效
- } else {
- $this->addError($attribute, '邮件证码错误');
- }
- }
- }
- }
- /**
- * 代理商(ib)开户申请
- * @return bool
- */
- public function complete()
- {
- if ($this->validate()) {
- $open = $openRecord = [];
- $openApi = new OpenApi();
- $open['open_type'] = 2;
- $open['name'] = $this->name;
- $open['id_card'] = $this->id_card;
- $open['email'] = $this->email;
- //$open['mobile'] = $this->mobile;
- $webRoot = \Yii::getAlias('@webroot');
- if ($this->card0 instanceof UploadedFile) {
- $filename = false;
- for ($i = 0; $i < 2; $i++) {
- $temp = '/upload/' . date('YmdHis_') . mt_rand(10000, 99999) . '.' . $this->card0->getExtension();
- if (!is_file($webRoot . $temp)) {
- $filename = $temp;
- break;
- }
- }
- if ($filename != false && $this->card0->saveAs($webRoot . $filename)) {
- $open['id_card_file_path0'] = $filename;
- }
- }
- if ($this->card1 instanceof UploadedFile) {
- $filename = false;
- for ($i = 0; $i < 2; $i++) {
- $temp = '/upload/' . date('YmdHis_') . mt_rand(10000, 99999) . '.' . $this->card1->getExtension();
- if (!is_file($webRoot . $temp)) {
- $filename = $temp;
- break;
- }
- }
- if ($filename != false && $this->card1->saveAs($webRoot . $filename)) {
- $open['id_card_file_path1'] = $filename;
- }
- }
- $open['rid'] = $this->rid;
- $open['address'] = $this->address;
- $rs = $openApi->open($open);
- if ($rs['code'] == 1) {
- $open_id = $rs['data']['id'];
- $openRecord['open_id'] = $open_id;
- $openRecord['ref_id'] = $this->ref_id;
- $openRecord['collect_name'] = $this->collect_name;
- /*
- $openRecord['bank_name'] = $this->bank_name;
- $openRecord['bank_province'] = $this->bank_province;
- $openRecord['bank_city'] = $this->bank_city;
- $openRecord['bank_district'] = $this->bank_district;
- $openRecord['bank_branch'] = $this->bank_branch;
- $openRecord['bank_card_no'] = $this->bank_card_no;
- $openRecord['bank_swift_code'] = $this->bank_swift_code;
- */
- $result = $openApi->openRecord($openRecord);
- if ($result['code'] == 1) {
- return true;
- } else {
- if (is_array($result['message'])) {
- $this->addErrors($result['message']);
- } else {
- $this->addError('name', $result['message']);
- }
- }
- } else {
- if (is_array($rs['message'])) {
- $this->addErrors($rs['message']);
- } else {
- $this->addError('name', $rs['message']);
- }
- }
- } else {
- $this->addErrors($this->getErrors());
- }
- return !$this->hasErrors();
- }
- }
|