public function contract_auth()
 {
     $uid = $this->user['uid'];
     $auth_id = I('auth_id', 0, 'intval');
     $url = I('url') or ajax_error('上传图片异常!');
     $url_watermark = \Common\Util\ImageUtil::qiniu_watermark($url);
     $auth = M('loan_user_contract_auth')->where(['uid' => $uid, 'auth_id' => $auth_id])->find();
     if ($auth) {
         M('loan_user_contract_auth')->where(['uid' => $uid, 'auth_id' => $auth_id])->save(['created' => time(), 'url' => $url, 'url_watermark' => $url_watermark]);
     } else {
         $auth = M('loan_config_auth')->find($auth_id) or $this->error('找不到此认证项资料!');
         M('loan_user_contract_auth')->add(['uid' => $uid, 'auth_id' => $auth_id, 'auth_name' => $auth['name'], 'auth_sort' => $auth['sort'], 'created' => time(), 'url' => $url, 'url_watermark' => $url_watermark]);
     }
     ajax_success();
 }
 public function user_auth()
 {
     $model = D('Loan/user_contract');
     $order = M('loan_order')->field('id,uid')->find(I('get.order_id'));
     if (IS_POST) {
         $order or ajax_error('找不到此订单信息!');
         $uid = $order['uid'];
         if (I('get.type') == 'base') {
             $model->update($uid) or ajax_error($model->getError());
             ajax_success('保存成功!');
         }
         $url = 'http://yszjdx.qiniudn.com/' . I('key');
         $url_watermark = \Common\Util\ImageUtil::qiniu_watermark($url);
         $auth_id = I('get.auth_id', 0, 'intval');
         $auth = M('loan_user_contract_auth')->where(['uid' => $uid, 'auth_id' => $auth_id])->find();
         if ($auth) {
             M('loan_user_contract_auth')->where(['uid' => $uid, 'auth_id' => $auth_id])->save(['created' => time(), 'url' => $url, 'url_watermark' => $url_watermark]);
         } else {
             $auth = M('loan_config_auth')->find($auth_id) or $this->error('找不到此认证项资料!');
             M('loan_user_contract_auth')->add(['uid' => $uid, 'auth_id' => $auth_id, 'auth_name' => $auth['name'], 'auth_sort' => $auth['sort'], 'created' => time(), 'url' => $url, 'url_watermark' => $url_watermark]);
         }
         ajax_success(['url' => $url]);
     }
     $order or $this->error('找不到此订单信息!');
     $uid = $order['uid'];
     // 获取学校类信息
     $info = M('user_info')->find($uid) or $this->error('系统错误:找不到用户信息!');
     $info['university_id'] > 0 or $this->error('请先设置个人学校信息!');
     $campus = M('university_campus')->field('province,city,district')->find($info['campus_id']);
     $campus or $this->error('系统错误:找不到校区信息!');
     $contract = $model->find($uid) ?: $info;
     $this->assign('contract', $contract);
     $this->assign('campus', $campus);
     $auth_data = $model->getAuth($uid);
     // 使用水印
     $auth_data = array_map(function ($item) {
         $item['origin_url'] = $item['url'];
         if ($item['url_watermark']) {
             $item['url'] = $item['url_watermark'];
         }
         return $item;
     }, $auth_data);
     $this->assign('_auth', $auth_data);
     $this->assign('order', $order);
     $this->display();
 }
 function update()
 {
     $uid = I('uid', 0, 'intval');
     $model = M($this->_table);
     // 更新认证材料项
     $auth_id = I('auth_id', 0, 'intval');
     if ($auth_id > 0) {
         $url = I('url') or $this->error('上传图片异常!');
         $url_watermark = \Common\Util\ImageUtil::qiniu_watermark($url);
         $auth = M('loan_user_contract_auth')->where(['uid' => $uid, 'auth_id' => $auth_id])->find();
         if ($auth) {
             M('loan_user_contract_auth')->where(['uid' => $uid, 'auth_id' => $auth_id])->save(['created' => time(), 'url' => $url, 'url_watermark' => $url_watermark]);
         } else {
             $auth = M('loan_config_auth')->find($auth_id) or $this->error('找不到此认证项资料!');
             M('loan_user_contract_auth')->add(['uid' => $uid, 'auth_id' => $auth_id, 'auth_name' => $auth['name'], 'auth_sort' => $auth['sort'], 'created' => time(), 'url' => $url, 'url_watermark' => $url_watermark]);
         }
         $this->success('更新成功!');
         exit;
     }
     $university_id = I('university_id', 0, 'intval');
     $campus_id = I('campus_id', 0, 'intval');
     $college_id = I('college_id', 0, 'intval');
     $major_id = I('major_id', 0, 'intval');
     $_POST['university_name'] = M('university')->where('id=' . $university_id)->getField('name') or $this->error('此学校不存在!');
     $_POST['campus_name'] = M('university_campus')->where('id=' . $campus_id)->getField('name') or $this->error('此校区不存在!');
     $_POST['college_name'] = M('university_college')->where('id=' . $college_id)->getField('name') or $this->error('此学院不存在!');
     $_POST['major_name'] = M('university_college_major')->where('id=' . $major_id)->getField('name') or $this->error('此专业不存在!');
     $data = [];
     $required_field = ['realname', 'id_card', 'study_year', 'start_year', 'university_id', 'university_name', 'campus_id', 'campus_name', 'college_id', 'college_name', 'major_id', 'major_name', 'dormitory'];
     foreach ($required_field as $vo) {
         if (isset($_POST[$vo])) {
             $data[$vo] = I($vo) or $this->error($vo . '不能为空!');
         }
     }
     $field = ['contact_qq', 'contact_wx'];
     foreach ($field as $vo) {
         if (isset($_POST[$vo])) {
             $data[$vo] = I($vo);
         }
     }
     // 验证记录是否存在
     $contract = $model->find($uid) or $this->error('找不到认证信息!');
     // 验证身份证号
     $id_card = I('post.id_card');
     if (!preg_match('/^[\\dX]{18}$/i', $id_card)) {
         return $this->error('请输入合法的身份证号!');
     }
     if ($model->where(['uid' => ['neq', $uid], 'id_card' => $id_card])->count()) {
         $this->error('此身份证号已被其它用户提交认证!');
     }
     // 根据身份证号算出年龄
     $birth_day = substr($id_card, 6, 4) . '-' . substr($id_card, 10, 2) . '-' . substr($id_card, 12, 2);
     if (date('Y-m-d', strtotime($birth_day)) == '1970-01-01') {
         $this->error('身份证号输入错误!');
     }
     $datetime1 = new \DateTime($birth_day);
     $datetime2 = new \DateTime(date('Y-m-d', $contract['created']));
     $data['age'] = $datetime1->diff($datetime2)->format('%y');
     if ($data['age'] <= 10 || $data['age'] >= 50) {
         $this->error('年龄异常!');
     }
     $data['birthday'] = $birth_day;
     // 开启事务
     $model->startTrans();
     // 同步更新真实姓名
     M('user')->where(['uid' => $uid])->setField('realname', $data['realname']);
     M('loan_order')->where(['uid' => $uid])->setField('realname', $data['realname']);
     M('loan_user')->where(['uid' => $uid])->setField('realname', $data['realname']);
     // 同步更新入学年份
     M('user_info')->where(['uid' => $uid])->setField('start_year', $data['start_year']);
     // 更新额度
     $userModel = D('Loan/User');
     $user_limit = $userModel->getLimit($uid, $data['study_year']);
     if ($user_limit === false) {
         $model->rollback();
         $this->error($userModel->getError());
     }
     $data['money_limit'] = $user_limit['money'];
     $data['max_month'] = $user_limit['month'];
     $model->where(['uid' => $uid])->save($data);
     $model->commit();
     // 更新联系人信息
     $contact1_name = I('post.contact1_name');
     $contact1_realname = I('post.contact1_realname');
     $contact1_tel = I('post.contact1_tel');
     $contact2_name = I('post.contact2_name');
     $contact2_realname = I('post.contact2_realname');
     $contact2_tel = I('post.contact2_tel');
     $model_contact = M('loan_user_contract_contact');
     $model_contact->where('uid=' . $uid)->delete();
     $model_contact->add(['name' => $contact1_name, 'realname' => $contact1_realname, 'telephone' => $contact1_tel, 'uid' => $uid]);
     $model_contact->add(['name' => $contact2_name, 'realname' => $contact2_realname, 'telephone' => $contact2_tel, 'uid' => $uid]);
     $this->success();
 }
 public function auth()
 {
     $order_id = I('order_id');
     $auth_id = I('auth_id', 0, 'intval');
     $url = I('url') or $this->error('上传图片异常!');
     $url_watermark = \Common\Util\ImageUtil::qiniu_watermark($url);
     $auth = M('loan_order_auth')->where(['order_id' => $order_id, 'auth_id' => $auth_id])->find();
     if ($auth) {
         M('loan_order_auth')->where(['order_id' => $order_id, 'auth_id' => $auth_id])->save(['created' => time(), 'url' => $url, 'url_watermark' => $url_watermark]);
     } else {
         $auth = M('loan_config_auth')->find($auth_id) or $this->error('找不到此认证项资料!');
         M('loan_order_auth')->add(['order_id' => $order_id, 'auth_id' => $auth_id, 'auth_name' => $auth['name'], 'auth_sort' => $auth['sort'], 'created' => time(), 'url' => $url, 'url_watermark' => $url_watermark]);
     }
     // 更新财务记录
     $auth_id == 11 && M('loan_order_finance')->where(['order_id' => $order_id])->setField('is_upload_receive_pic', 1);
     $this->success('更新成功!');
 }
 public function auth_user()
 {
     $order = $this->_order('id,uid');
     $order_id = $order['id'];
     $uid = $order['uid'];
     if (!IS_POST) {
         // 获取学校类信息
         $info = M('user_info')->find($uid) or ajax_error('找不到用户信息!');
         $info['university_id'] > 0 or ajax_error('尚未设置学校信息!');
         $campus = M('university_campus')->field('province,city,district')->find($info['campus_id']);
         $campus or ajax_error('找不到此用户校区信息!');
         $model = D('Loan/user_contract');
         $field = 'status,realname,id_card,dormitory,start_year,study_year,university_name,college_name,major_name,campus_name,province,city,district,failure_reason remark';
         $contract = $model->where(['uid' => $uid])->field($field)->find($uid) or ajax_error('找不到此学生认证信息!');
         $auth_data = $model->getAuth($uid);
         // 使用水印
         $auth_data = array_map(function ($item) {
             $item['origin_url'] = $item['url'];
             if ($item['url_watermark']) {
                 $item['url'] = $item['url_watermark'];
             }
             return $item;
         }, $auth_data);
         ajax_success(['contract' => $contract, 'auth' => $auth_data, 'is_allow_update' => in_array($contract['status'], [0, 2]) ? 1 : 0]);
     }
     // 判断是否允许更新
     $model = D('Loan/user_contract');
     $contract_status = $model->where(['uid' => $uid])->getField('status');
     in_array($contract_status, [0, 2]) or ajax_error('当前不允许更新认证信息!');
     // 处理上传
     $url = I('url');
     $url_watermark = \Common\Util\ImageUtil::qiniu_watermark($url);
     $auth_id = I('auth_id', 0, 'intval');
     $authModel = M('loan_user_contract_auth');
     if ($authModel->where(['uid' => $uid, 'auth_id' => $auth_id])->save(['created' => time(), 'url' => $url, 'url_watermark' => $url_watermark]) < 1) {
         $auth = M('loan_config_auth')->find($auth_id) or ajax_error('找不到此认证项资料!');
         $authModel->add(['uid' => $uid, 'auth_id' => $auth_id, 'auth_name' => $auth['name'], 'auth_sort' => $auth['sort'], 'created' => time(), 'url' => $url, 'url_watermark' => $url_watermark]);
     }
     ajax_success();
 }