/**
  * 项目服务器 -> 抓取系统服务器
  *
  * @param unknown $url 相对地址
  * @param unknown $params POST提交参数
  * @return mixed
  */
 protected function _query($url, $params = null, $return = false)
 {
     $timeout = 30;
     $curl_conf = array(CURLOPT_URL => $url, CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER => true, CURLOPT_CONNECTTIMEOUT => $timeout, CURLOPT_TIMEOUT => $timeout, CURLOPT_ENCODING => 'gzip');
     if ($params) {
         $curl_conf[CURLOPT_POST] = true;
         $curl_conf[CURLOPT_POSTFIELDS] = is_array($params) ? http_build_query($params) : $params;
     }
     $ch = curl_init();
     curl_setopt_array($ch, $curl_conf);
     $output = curl_exec($ch);
     $info = curl_getinfo($ch);
     if ($output === false || $info['http_code'] != 200) {
         $error = '连接服务器[' . $url . ']出错,返回状态码[' . $info['http_code'] . '],返回内容:' . $output;
         if (curl_error($ch)) {
             $error .= ",原因:" . curl_error($ch);
         }
         system_warn($error);
         curl_close($ch);
         ajax_error('服务器繁忙,请稍后重试!');
     }
     curl_close($ch);
     if ($return) {
         return $output;
     }
     echo $output;
     exit;
 }
Beispiel #2
0
 public function _send($mobile, $text)
 {
     $apikey = $this->config['key'];
     $url = "http://yunpian.com/v1/sms/send.json";
     $post_string = "apikey={$apikey}&mobile={$mobile}&text=" . $text;
     $ret = $this->_sock_post($url, $post_string);
     if ($ret['code'] == 0) {
         return true;
     } else {
         system_warn('发送短信失败:' . $mobile . ' => ' . $ret['msg']);
         return $ret['msg'];
     }
 }
 /**
  * 支付通知处理结果:输出结果并退出
  *
  * @param int $id 通知记录
  * @param mixed $result 操作结果,true表示成功,其它表示失败原因
  */
 protected function _alipay_notify_finish($id, $result)
 {
     static $_model = null;
     if ($_model === null) {
         $_model = M('user_money_pay_alipay_notify');
     }
     if ($result === TRUE) {
         $_model->save(['id' => $id, 'is_success' => 1]);
         echo 'success';
     } else {
         $_model->save(['id' => $id, 'is_success' => 0, 'remark' => $result]);
         system_warn('检测到支付处理失败:' . $result);
         echo 'fail';
     }
     exit;
 }
 public function index()
 {
     if (I('id') > 0) {
         $this->_action();
         return;
     }
     if (I('get.type') == 'check') {
         $notificationModel = new \Common\Model\SystemNotificationModel();
         $notificationModel->updateStatus() or system_warn($notificationModel->getError());
         $where = $this->_getWhere();
         $unread = M($this->_table)->where($where)->count();
         $last_id = session('SYSTEM_NOTIFICATION_LAST_ID') + 0;
         $where['id'] = ['gt', $last_id];
         $unread_new = M($this->_table)->where($where)->count();
         session('SYSTEM_NOTIFICATION_LAST_ID', max($last_id, M($this->_table)->where($where)->max('id')));
         ajax_success(['unread' => $unread, 'new' => $unread_new]);
     }
     parent::index();
 }
Beispiel #5
0
 /**
  * 增加积分
  *
  * @param int $uid 用户编号
  * @param int $type_name 操作类型
  * @param int $credit 积分值
  * @param string $remark 备注
  * @return boolean
  */
 protected function _update($uid, $type, $credit, $remark = '')
 {
     $user = get_user($uid);
     if (!$user) {
         system_warn('未知的用户编号:' . $uid);
         return false;
     }
     if (!isset($this->_type[$type])) {
         system_warn('未知的积分操作:' . $type);
         return false;
     }
     $this->startTrans();
     if (!$this->where('uid=' . $uid)->setInc('credit', $credit)) {
         $this->rollback();
         return false;
     }
     if (!M('CreditLog')->add(['uid' => $uid, 'created' => time(), 'type' => $type, 'credit' => $credit, 'ip' => get_client_ip(1), 'remark' => $remark])) {
         $this->rollback();
         return false;
     }
     $ret = $this->commit();
     return $ret;
 }
Beispiel #6
0
 public function send($mobile, $message)
 {
     $this->_sensitive($message);
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, "http://sms-api.luosimao.com/v1/send.json");
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
     curl_setopt($ch, CURLOPT_HEADER, FALSE);
     curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
     curl_setopt($ch, CURLOPT_USERPWD, 'api:key-' . $this->key);
     curl_setopt($ch, CURLOPT_POST, TRUE);
     curl_setopt($ch, CURLOPT_POSTFIELDS, array('mobile' => $mobile, 'message' => $message));
     $res = curl_exec($ch);
     curl_close($ch);
     $ret = json_decode($res, true);
     if (!$ret) {
         system_warn('发送短信失败:' . $mobile . ' => 未知返回值:' . $res);
         return;
     }
     if ($ret['error'] != 0) {
         $ignore_msg = ['WRONG_MOBILE'];
         in_array($ret['msg'], $ignore_msg) or system_warn('发送短信失败:' . $mobile . ' => ' . $ret['msg'] . ',发送内容:' . $message);
     }
 }
Beispiel #7
0
 /**
  * 设置设备的标签,例如设置App标签,学校信息标签,业务标签
  *
  * @param string $token 设备token
  * @param string $method 方法,det/del
  * @param string $del_other_pre 相斥前缀
  */
 public function updateTag($token, $tag, $method = 'set', $del_other_pre = '')
 {
     $batch_add = $batch_del = [];
     $model = M('common_push_device');
     $row = $model->where(['app' => $this->_app, 'token' => $token])->field('os,tags')->find();
     if (!$row) {
         system_warn('找不到设备token记录:' . $token);
         return false;
     }
     $os = $row['os'];
     $tags = explode(',', $row['tags']);
     // 新增或删除标签
     $new_tags = is_array($tag) ? $tag : [$tag];
     foreach ($new_tags as $tag) {
         if ($method == 'set') {
             if (in_array($tag, $tags)) {
                 continue;
             }
             $tags[] = $tag;
             $batch_add[] = $tag;
         } else {
             if (!in_array($tag, $tags)) {
                 continue;
             }
             unset($tags[array_search($tag, $tags)]);
             $batch_del[] = $tag;
         }
     }
     // 若是新增,则判断是否要删除相斥的旧标签
     if ($del_other_pre && $method == 'set') {
         $del_tags = [];
         $del_tags_text = [];
         foreach ($tags as $tag) {
             if (strpos($tag, $del_other_pre) === 0 && !in_array($tag, $new_tags)) {
                 $batch_del[] = $tag;
                 unset($tags[array_search($tag, $tags)]);
             }
         }
     }
     // 没有任何变化
     if (!$batch_add || !$batch_del) {
         return true;
     }
     // 发生了变化,保存最新的tag到数据库
     $tags = array_unique(array_filter($tags));
     $model->save(['app' => $this->_app, 'token' => $token, 'tags' => implode(',', $tags)]);
     // 同步到信鸽服务器
     $push = $this->_getInstance($os);
     if ($batch_add) {
         debug('新增标签:' . implode(',', $batch_add));
         $tagToken = [];
         foreach ($batch_add as $tag) {
             $tagToken[] = new TagTokenPair($tag, $token);
         }
         $ret = $push->BatchSetTag($tagToken);
     }
     if ($batch_del) {
         debug('删除标签:' . implode(',', $batch_del));
         $tagToken = [];
         foreach ($batch_del as $tag) {
             $tagToken[] = new TagTokenPair($tag, $token);
         }
         $ret = $push->BatchDelTag($tagToken);
     }
     debug($ret);
     return true;
 }
 /**
  * @title 更新学校信息
  *
  * @param int $majorId 专业编号 '' 为空或跟原来值一样时,则表示不更新
  * @param int $campusId 校区编号 '' 校区可更新无限次,其它只能更新2次
  * @param int $startYear 入学年份 '' 为空或跟原来值一样时,则表示不更新
  * @method post
  */
 public function updateUniversity()
 {
     $user = get_user() or ajax_error('USER_NOT_LOGIN', '登录超时,请重新登录!');
     $majorId = I('post.majorId');
     $startYear = I('post.startYear');
     $campusId = I('post.campusId');
     $infoModel = M('userInfo');
     $userInfo = $infoModel->field('university_id,update_university_num,campus_id,major_id,start_year')->find($user['uid']);
     $userInfo or ajax_error('USER_NOT_FOUND', '找不到此用户信息!');
     $change_campus_only = !$majorId && !$startYear || $userInfo['major_id'] == $majorId && $userInfo['start_year'] == $startYear;
     if (!$change_campus_only) {
         // 学校和入学年份仅允许更新2此,而校区则不限
         $update_university_num = $userInfo['update_university_num'];
         APP_STATUS != 'product' or $update_university_num < 2 or ajax_error('专业和学年仅允许修改一次!');
         // 根据专业编号,查询大学、学院编号
         $majorModel = M('university_college_major');
         $major = $majorModel->where(['id' => $majorId, 'status' => 1])->find() or ajax_error('MAJOR_NOT_FOUND', '找不到此专业信息!');
         // 获取学院信息
         $collegeModel = M('university_college');
         $college = $collegeModel->where(['id' => $major['college_id'], 'university_id' => $major['university_id'], 'status' => 1])->find() or ajax_error('COLLEGE_NOT_FOUND', '找不到此学院信息!');
         // 获取大学信息
         $universityModel = M('university');
         $university = $universityModel->where(['id' => $major['university_id'], 'status' => 1])->find() or ajax_error('MAJOR_NOT_FOUND', '找不到此学院信息!');
         // 获取校区信息
         $campusModel = M('university_campus');
         $campus = $campusModel->where(['id' => $campusId, 'university_id' => $major['university_id']])->find();
         if (!$campus) {
             system_warn('更新用户学校信息失败:找不到校区信息(编号:' . $campusId . ')');
             ajax_error('MAJOR_NOT_FOUND', '找不到此校区信息!');
         }
         // 确认更新
         $infoModel->where(['uid' => $user['uid']])->save(['university_id' => $university['id'], 'university_name' => $university['name'], 'college_id' => $college['id'], 'college_name' => $college['name'], 'major_id' => $major['id'], 'major_name' => $major['name'], 'campus_id' => $campus['id'], 'campus_name' => $campus['name'], 'start_year' => $startYear, 'update_university_num' => $update_university_num + 1]) === FALSE and ajax_error('SYSTEM_ERROR', '系统错误,请联系系统管理员处理!');
     } else {
         // 获取校区信息
         $campusModel = M('university_campus');
         $campus = $campusModel->where(['id' => $campusId, 'university_id' => $userInfo['university_id']])->find();
         if (!$campus) {
             system_warn('更新用户学校信息失败:找不到校区信息(编号:' . $campusId . ')');
             ajax_error('MAJOR_NOT_FOUND', '找不到此校区信息!');
         }
         // 确认更新
         $infoModel->where(['uid' => $user['uid']])->save(['campus_id' => $campus['id'], 'campus_name' => $campus['name']]) === FALSE and ajax_error('SYSTEM_ERROR', '系统错误,请联系系统管理员处理!');
     }
     // 更新学校注册统计
     D('User')->updateStat($user['uid'], $userInfo['university_id'], $userInfo['campus_id']);
     // 更新此用户的学校标签
     $model = new \Common\Model\CommonPushModel();
     $model->updateUniversityTag($user['uid']) or system_warn($model->getError());
     ajax_success();
 }
Beispiel #9
0
 /**
  * 自动还款(从个人钱包扣款)
  *
  * @param int $uid 用户编号
  * @param number $money 最大还款金额
  *       
  * @return 实际还款金额
  */
 public function autoRepay($uid, $money)
 {
     if ($money <= 0) {
         return true;
     }
     $billModel = new \Loan\Model\BillModel();
     // 获取当前总共欠下的还款金额
     $total_need_repay_money = $billModel->autoRepay($uid);
     $pay_money = min($money, $total_need_repay_money);
     if ($money <= 0) {
         return true;
     }
     // 为扣款做好准备
     $trade_no = date('YmdHis') . str_pad(mt_rand(0, 9999), 4, '0', STR_PAD_LEFT);
     $pay_id = M('loan_pay_wallet')->add(['trade_no' => $trade_no, 'created' => time(), 'uid' => $uid, 'money' => $pay_money, 'is_paid' => 0, 'is_success' => 0]);
     // 执行扣款
     $userMoneyModel = new \Common\Model\UserMoneyModel();
     $ret = $userMoneyModel->pay($userMoneyModel::SOURCE_LOAN, $trade_no, $uid, $pay_money, '兼职换购还款');
     if (!$ret) {
         system_warn('自动还款失败:' . $userMoneyModel->getError());
         return $userMoneyModel->getError();
     }
     M('loan_pay_wallet')->where('id=' . $pay_id)->setField('is_paid', 1);
     // 执行还款
     $total_pay_money = $billModel->autoRepay($uid, $pay_money, $trade_no);
     if (!$ret) {
         system_warn('自动还款失败:' . $billModel->getError());
         return $billModel->getError();
     }
     M('loan_pay_wallet')->where('id=' . $pay_id)->setField('is_success', 1);
     return true;
 }
 public function download()
 {
     // 批量下载
     $config = C('UPLOAD_TYPE_CONFIG');
     $config['domain'] = '7xlf9w.com2.z0.glb.qiniucdn.com';
     $config['bucket'] = 'zjfq-contract';
     $qiniu = new \Common\Util\Qiniu($config);
     // 查询打包状态
     $pid = I('pid');
     if ($pid) {
         $ret = $qiniu->status($pid) or ajax_error('无法获取打包状态!');
         if ($ret['code'] == 1 || $ret['code'] == 2) {
             ajax_success();
         }
         if ($ret['code'] == 3) {
             ajax_error($ret['items'][0]['error']);
         } elseif ($ret['code'] == 0) {
             // 打包完成
             $row = M('loan_order_contract_download')->where(['qiniu_persistent_id' => $pid])->find();
             // 标识已下载
             M('loan_order_contract')->where(['order_id' => ['in', $row['order_ids']]])->save(['is_download' => 1, 'use_time' => time() - $row['created']]);
             // 标识所用时间
             M('loan_order_contract_download')->save(['id' => $row['id'], 'use_time' => time() - $row['created']]);
             // 返回下载地址
             $url = $qiniu->downLink($row['zip_file']);
             $url = $url . '?e=' . (time() + 60);
             $url .= '&token=' . $qiniu::sign($qiniu->sk, $qiniu->ak, $url);
             ajax_success($url);
         } else {
             system_warn('未知返回结果:' . print_r($ret, true));
             ajax_error('未知状态!');
         }
     }
     $ids = explode(',', I('get.ids')) or $this->error('参数错误!');
     $model = new \Loan\Model\OrderContractModel();
     if (count($ids) <= 1) {
         $id = $ids[0];
         $url = $model->pdfUrl(I('id')) or $this->error($model->getError());
         M('loan_order_contract')->save(['order_id' => $id, 'is_download' => 1]);
         header('Location: ' . $url);
         exit;
     }
     $orders = M('loan_order_contract')->where(['order_id' => ['in', $ids]])->field('order_id,url')->select() or $this->error('找不到此电子合同记录!');
     $urls = [];
     foreach ($orders as $key => $row) {
         $url = $row['url'] or $this->error('找不到订单的合同地址:' . $row['order_id']);
         $url = $url . '?e=' . (time() + 60);
         $urls[] = $url . '&token=' . $qiniu::sign($qiniu->sk, $qiniu->ak, $url);
     }
     $ret = $qiniu->mkzip($urls) or $this->error('无法请求七牛打包接口!');
     M('loan_order_contract_download')->add(['created' => time(), 'zip_file' => $ret['zip_file'], 'qiniu_persistent_id' => $ret['persistent_id'], 'order_ids' => implode(',', $ids), 'order_count' => count($ids)]);
     $this->success($ret['persistent_id']);
 }
 public function uploadToken()
 {
     $expires = 600;
     $ext = pathinfo(I('filename'), PATHINFO_EXTENSION);
     if (!in_array(strtolower($ext), ['png', 'jpg', 'jpeg', 'gif'])) {
         system_warn('检测到错误的上传图片格式:' . I('filename') . ',提交参数:' . print_r($_REQUEST, true));
         ajax_error(strpos(I('filename'), '.') > 0 ? '仅限上传png/jpg/gif图片' : '服务器无法读取图片格式!');
     }
     $file_name = (I('type') ?: 'loan') . '_auth_' . date('YmdH') . '_' . uniqid() . '.' . $ext;
     $policy['scope'] = 'yszjdx:' . $file_name;
     $policy['deadline'] = time() + $expires;
     $policy['fsizeLimit'] = 5 * 1024 * 1024;
     $policy = json_encode($policy);
     function base64_urlSafeEncode($data)
     {
         $find = array('+', '/');
         $replace = array('-', '_');
         return str_replace($find, $replace, base64_encode($data));
     }
     $policy = base64_urlSafeEncode($policy);
     $qiniu = C('UPLOAD_TYPE_CONFIG');
     $sign = hash_hmac('sha1', $policy, $qiniu['secrectKey'], true);
     $token = $qiniu['accessKey'] . ':' . base64_urlSafeEncode($sign) . ':' . $policy;
     ajax_success(['file_name' => $file_name, 'token' => $token]);
 }
 /**
  * 结算冻结的资金
  *
  * @param int $source_id 业务来源编号
  * @param string $trade_no 交易编号
  * @param string $title 标题
  * @param string $remark 备注
  * @param float $total_money 总金额
  * @param float $from_uid 资金来源所属用户编号
  * @param float $back_money 返回原主金额
  * @param float $to_uid 支付给对方用户编号
  * @param float $to_money 支付给对方金额
  * @param float $service_money 支付给公司金额
  */
 public function checkout($params)
 {
     // 解析参数
     $source_id = $params['source_id'];
     $trade_no = $params['trade_no'];
     $title = $params['title'];
     $remark = $params['remark'];
     $total_money = $params['total_money'];
     $from_uid = $params['from_uid'];
     $back_money = $params['back_money'];
     $back_title = $params['back_title'];
     $to_uid = $params['to_uid'];
     $to_money = $params['to_money'];
     $to_title = $params['to_title'];
     $service_money = $params['service_money'];
     $service_title = $params['service_title'];
     // 记录请求
     $this->_tradeLog($source_id, $trade_no, self::TYPE_FROZEN, $from_uid, $total_money);
     // 对请求进行基本验证
     if (bccomp($total_money, $back_money + $to_money + $service_money, 2) != 0) {
         return $this->_error('结算金额不一致!');
     }
     // 进一步对参数进行验证
     if (!$this->_checkTradeParam($source_id, $trade_no)) {
         return false;
     }
     // 获取欲结算的冻结记录
     $tradeModel = M('user_money_trade');
     $where = ['source_id' => $source_id, 'trade_no' => $trade_no];
     $trade_row = $tradeModel->where($where)->find();
     if (!$trade_row) {
         return $this->_error('找不到此交易单号的冻结记录:' . $trade_no);
     }
     if ($trade_row['uid'] != $from_uid || bccomp($total_money, $trade_row['money'], 2) != 0) {
         return $this->_error('请求参数与冻结记录不符!');
     }
     // 已经成功结算,直接返回true
     if ($trade_row['is_closed'] > 0) {
         return true;
     }
     // 验证服务费金额
     if ($back_money < 0 || $to_money < 0 || $service_money < 0) {
         return $this->_error('金额异常!');
     }
     // 验证来源用户及返回金额
     $from_account = $this->_checkParam($from_uid);
     if (!$from_account) {
         return false;
     }
     // 验证收款账号
     if ($to_uid > 0) {
         $to_account = $this->_checkParam($to_uid);
         if (!$to_account) {
             return false;
         }
     } else {
         if ($to_money > 0) {
             return $this->_error('收款账户编号为空,收款金额却大于0!');
         }
     }
     // 验证数据是否正常
     $checkoutModel = M('user_money_trade_checkout');
     if ($checkoutModel->where($where)->count() > 0) {
         system_warn('检测到异常的结算数据!');
         return $this->_error('系统异常,请稍后重试!');
     }
     $this->startTrans();
     $now = time();
     if (!$tradeModel->where(['source_id' => $source_id, 'trade_no' => $trade_no, 'is_closed' => 0])->setField('is_closed', 1)) {
         return $this->_error('更新交易记录失败!', true);
     }
     $checkoutModel->add(['created' => $now, 'source_id' => $source_id, 'trade_no' => $trade_no, 'back_uid' => $from_uid, 'back_money' => $back_money, 'to_uid' => $to_uid, 'to_money' => $to_money, 'service_money' => $service_money, 'remark' => $remark]);
     // 验证冻结金额
     if (bccomp($from_account['money_frozen'], $total_money, 2) < 0) {
         return $this->_error('用户冻结余额不足!', true);
     }
     $source_frozen = M('user_money_frozen')->where(['source_id' => $source_id, 'uid' => $from_uid])->getField('money');
     if (bccomp($source_frozen, $total_money, 2) < 0) {
         return $this->_error('此业务的用户冻结余额不足!', true);
     }
     // 更新冻结金额
     M('user_money_frozen')->where(['source_id' => $source_id, 'uid' => $from_uid])->setDec('money', $total_money);
     $this->where(['uid' => $from_uid])->setDec('money_frozen', $total_money);
     // 更新来源账户
     if ($back_money > 0) {
         $this->where(['uid' => $from_uid])->save(['money_avaliable' => ['exp', 'money_avaliable+' . $back_money]]);
         M('user_money_log')->add(['uid' => $from_uid, 'created' => $now, 'type' => self::TYPE_CHECKOUT, 'money' => $back_money, 'title' => $back_title, 'remark' => $remark, 'trade_no' => $trade_no]);
     }
     // 更新收款方账户
     if ($to_money > 0) {
         $this->where(['uid' => $to_uid])->save(['money_avaliable' => ['exp', 'money_avaliable+' . $to_money]]);
         M('user_money_log')->add(['uid' => $to_uid, 'created' => $now, 'type' => self::TYPE_CHECKOUT, 'money' => $to_money, 'title' => $to_title, 'remark' => $remark, 'trade_no' => $trade_no]);
     }
     // 更新公司账户
     if ($service_money > 0) {
         $this->where(['uid' => 0])->save(['money_avaliable' => ['exp', 'money_avaliable+' . $service_money]]);
         M('user_money_log')->add(['uid' => 0, 'created' => $now, 'type' => self::TYPE_CHECKOUT, 'title' => $service_title, 'money' => $service_money, 'remark' => $remark, 'trade_no' => $trade_no]);
     }
     $this->commit();
     return true;
 }
 /**
  * 记录操作日志
  */
 protected function _log()
 {
     $menuModel = M('system_menu');
     // 操作对象
     $rule = strtolower(CONTROLLER_NAME . '/' . ACTION_NAME);
     $method = $menuModel->where(['url' => $rule])->field('id,pid,title,url')->find();
     if (!$method) {
         // 找不到则说明有固定参数
         $menuList = $menuModel->where(['url' => ['like', $rule . '%']])->field('id,pid,title,url')->select();
         $REQUEST = unserialize(strtolower(serialize($_REQUEST)));
         foreach ($menuList as $menu) {
             $query = preg_replace('/^.+\\?/U', '', $menu['url']);
             parse_str($query, $param);
             if (array_intersect_assoc($REQUEST, $param) == $param) {
                 $method = $menu;
                 break;
             }
         }
         // 若找不到,则需要人工介入
         if (!$method) {
             system_warn('记录管理员后台操作失败:无法自动识别当前URL!');
             return;
         }
     }
     if ($method['pid'] > 0) {
         $parent = $menuModel->find($method['pid']) or $this->error('系统菜单出错,ID:' . $method['id']);
         $target = $parent['title'];
         $method = $method['title'];
     } else {
         $target = $method['title'];
         $method = '-';
     }
     action_log($target, $method);
 }
Beispiel #14
0
 /**
  * 判断日志级别,发送警告信息
  *
  * @static
  *
  * @param string $level
  * @param string $message
  * @return void
  */
 static function report($level, $message)
 {
     if ($level == self::ERR) {
         system_warn('PHP发生严重错误:<br />' . $message);
     }
 }
Beispiel #15
0
 /**
  * 发送或验证手机验证码
  *
  * @param string $mobile 手机号
  * @param string $code 默认为null,表示发送,否则进行验证操作
  */
 public function verifyCode($mobile, $code = null)
 {
     $now = time();
     $verfiyCodeExpire = 1800;
     $verifyModel = M('user_verify');
     $mobile = trim($mobile);
     if (empty($mobile)) {
         $this->error = '请输入手机号!';
         return false;
     }
     if (strlen($mobile) != 11) {
         $this->error = '请输入11位数手机号!';
         return false;
     }
     if (!preg_match('/^1[3-8]\\d{9}$/', $mobile)) {
         $this->error = '手机号不正确!';
         system_warn('检测到异常手机号!');
         return false;
     }
     if ($code !== null) {
         // 查询最后一次发送的验证码
         $rowVerify = $verifyModel->field('id,code,retry_num')->where(['account_type' => 2, 'account' => $mobile, 'created' => array('egt', time() - $verfiyCodeExpire), 'status' => 1])->order('id DESC')->find();
         if (!$rowVerify) {
             $this->error = '验证码超时,请重新发送!';
             return false;
         }
         // 更新重试次数
         $retry_num = $rowVerify['retry_num'] + 1;
         $update = ['retry_num' => $retry_num];
         if ($retry_num >= 5) {
             $update['status'] = 0;
         }
         $verifyModel->where('id=' . $rowVerify['id'])->save($update);
         // 判断验证码是否正确
         if ($code != $rowVerify['code']) {
             $this->error = '验证码不正确!';
             return false;
         }
         // 正确,关闭此验证码
         $verifyModel->where('id=' . $rowVerify['id'])->setField('status', 0);
         return true;
     }
     // 判断短信发送时间间隔
     $last_send_time = $verifyModel->where(['account' => $mobile])->max('last_send_time');
     if ($last_send_time && $last_send_time + 120 > $now) {
         $this->error = '操作过快,请等待' . ($last_send_time + 120 - $now) . '秒。';
         return false;
     }
     // 获取当前手机号有效的历史验证码
     $history = $verifyModel->field('id,code')->where(['account_type' => 2, 'account' => $mobile, 'created' => array('egt', time() - $verfiyCodeExpire), 'status' => 1])->find();
     if (!$history) {
         $user = get_user();
         $uid = $user ? $user['uid'] : 0;
         $code = str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT);
         $verifyModel->add(['uid' => $uid, 'account_type' => 2, 'account' => $mobile, 'code' => $code, 'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '', 'ip' => get_client_ip(1), 'created' => $now, 'last_send_time' => $now, 'status' => 1]);
     } else {
         $code = $history['code'];
         $verifyModel->where('id=' . $history['id'])->setField('last_send_time', $now);
     }
     return $code;
 }
 /**
  * @title 测算用户额度及最大分期数
  *
  * @param int $university_id 学校编号
  * @param int $start_year 入学年份
  * @param int $study_year 学年制
  * @method get
  */
 public function get_user_limit($university_id, $start_year, $study_year)
 {
     $university_id = intval($university_id);
     $university_config = M('loan_config_university')->find($university_id);
     if (!$university_config) {
         system_warn('暂不支持此学校!');
         ajax_error('暂不支持此学校!');
     }
     // 几年级?
     $grade = 0;
     $now_year = date('Y');
     $start_year = intval($start_year);
     $start_year >= 2000 && $start_year <= $now_year or ajax_error('入学年份有误!');
     if ($start_year >= $now_year) {
         $grade = 1;
     } else {
         $grade = date('Y') - $start_year + 1;
         if (date('m') < 7) {
             $grade -= 1;
         }
     }
     // 读取配置值
     $config = M('loan_config_university_money')->where(['study_year' => $study_year, 'current_year' => $grade, 'education' => $university_config['education']])->field('money,month')->find();
     if (!$config) {
         ajax_error('不支持此学年制!');
     }
     // 距离毕业剩余月份
     $restMonth = 0;
     $start_date = time();
     $end_day = $start_year + intval($study_year) . '-07-01';
     if (date('Y-m-d') < $end_day) {
         for ($m = 1; $m <= 12 * 6; $m++) {
             if (date('Y-m-d', strtotime('+ ' . $m . ' month', $start_date)) >= $end_day) {
                 $restMonth = $m - 1;
                 break;
             }
         }
     }
     ajax_success(['max_money' => number_format($config['money'], 2, '.', ''), 'max_month' => min(24, max(0, $restMonth - 2), $config['month'])]);
 }
 public static function isPromotion($goods)
 {
     // 判断促销
     $now_date = date('Y-m-d');
     $now_time = date('H:i:00');
     $is_promotion = $goods['is_promotion'] > 0;
     if ($is_promotion) {
         $promotion_limit_type = $goods['promotion_limit_type'];
         $is_promotion_date = $now_date >= $goods['promotion_date_start'] && $now_date <= $goods['promotion_date_end'];
         $is_promotion_time = $now_time >= $goods['promotion_time1_start'] && $now_time <= $goods['promotion_time1_end'];
         $is_promotion_time = $is_promotion_time || $now_time >= $goods['promotion_time2_start'] && $now_time <= $goods['promotion_time2_end'];
         $is_promotion_time = $is_promotion_time || $now_time >= $goods['promotion_time3_start'] && $now_time <= $goods['promotion_time3_end'];
         if ($promotion_limit_type == 1) {
             return $is_promotion_date;
         } elseif ($promotion_limit_type == 2) {
             return $is_promotion_time;
         } elseif ($promotion_limit_type == 3) {
             return $is_promotion_date && $is_promotion_time;
         } elseif ($promotion_limit_type == 0) {
             return true;
         } else {
             system_warn('未知的促销限制类型:' . $promotion_limit_type);
             return false;
         }
     }
 }
 public function edit($uid = 0)
 {
     $uid = intval($uid);
     if (IS_POST) {
         $userModel = D('User');
         $password = I('post.password');
         $repassword = I('post.repassword');
         /* 检测密码 */
         if (check_auth('User/User/editAdv')) {
             if ($password) {
                 if ($password != $repassword) {
                     $this->error('密码和重复密码不一致!');
                 }
                 $user_salt = $userModel->salt($uid) or $this->error('找不到此记录信息!');
                 $_POST['password'] = $userModel->password($password, $user_salt);
             } else {
                 unset($_POST['password']);
             }
         } else {
             unset($_POST['password']);
         }
         // 禁止修改手机号
         unset($_POST['mobile']);
         $this->_relaField(true);
         $infoModel = M('user_info');
         if ($userModel->create() && $userModel->save() !== false && $infoModel->create() && $infoModel->save() !== false) {
             // 更新此用户的学校标签
             $model = new \Common\Model\CommonPushModel();
             $model->updateUniversityTag(I('uid')) or system_warn($model->getError());
             $this->success('编辑成功!', U('index'));
         } else {
             $error = $userModel->getError();
             $error = $error ? $error : $userModel->getDbError();
             $this->error(empty($error) ? '未知错误!' : $error);
         }
     } else {
         $this->_initForm();
         $userModel = M()->table('zj_user u')->join('zj_user_info i on i.uid=u.uid');
         $info = $userModel->where('u.uid=' . $uid)->find() or $this->error('找不到此记录信息!');
         $this->assign('info', $info);
         $this->meta_title = '编辑用户';
         $this->display();
     }
 }
Beispiel #19
0
 /**
  * 自动还款(从个人钱包扣款)
  *
  * @param int $uid 用户编号
  * @param number $money 最大还款金额,默认为null,表示返回总共所需还款金额
  *       
  * @return 实际还款金额
  */
 public function autoRepay($uid, $money = null, $trade_no = '')
 {
     $orderModel = new \Loan\Model\OrderModel();
     $where_order_status = implode(',', $orderModel->allowRepayStatus(true));
     $where_order = 'is_support_job=1 and uid =' . intval($uid) . ' and status in (' . $where_order_status . ')';
     $bills = $this->where(['order_id' => ['exp', ' in ( select id from zj_loan_order where ' . $where_order . ')'], 'issue' => ['gt', 0], 'status' => 0])->order('return_limit_time ASC')->select();
     // 返回总共所需的金额
     if ($money === null) {
         $money = 0;
         foreach ($bills as $bill) {
             $money += $bill['money_total'] - $bill['return_money'];
         }
         return $money;
     }
     $total_pay_money = 0;
     $money_remain = $money;
     // 处理订单
     foreach ($bills as $bill) {
         if ($money_remain <= 0) {
             break;
         }
         // 验证统计字段是否正常
         if (bccomp($bill['money_total'], $bill['money_principal'] + $bill['money_fee'] + $bill['money_delay'], 2) != 0) {
             system_warn('检测到账单money_total字段异常!');
             continue;
         }
         $pay_money = min($money_remain, $bill['money_total'] - $bill['return_money']);
         $ret = $this->repay($bill['order_id'], $bill['issue'], $pay_money, 3, $trade_no);
         if (!$ret) {
             system_warn('自动还款失败:' . $this->error);
             continue;
         }
         $money_remain -= $pay_money;
         $total_pay_money += $pay_money;
     }
     return $total_pay_money;
 }
Beispiel #20
0
 /**
  * 结算学生诚意金
  * 将学生的诚意金结算给三方
  *
  * @param int $join_id 参与编号
  * @param number $money_student 退还给学生的金额
  * @param number $money_company 支付给商家的金额
  * @param number $money_service 支付给公司的金额
  */
 public function checkoutStudent($join_id, $money_student, $money_company, $money_service)
 {
     // 获取参与记录
     $join = M('job_join')->find($join_id);
     if (!$join) {
         return $this->_error('找不到此兼职参与信息!');
     }
     if ($join['money_from_student'] <= 0) {
         return $this->_error('此参与无可用诚意金!');
     }
     // 获取兼职记录
     $job_id = $join['job_id'];
     $job = M('job')->find($job_id);
     if (!$job) {
         return $this->_error('找不到此兼职信息!');
     }
     // 格式化所有金额
     $total_money = number_format($join['money_from_student'], 2, '.', '');
     $money_company = number_format($money_company, 2, '.', '');
     $money_student = number_format($money_student, 2, '.', '');
     $money_service = number_format($money_service, 2, '.', '');
     // 验证金额是否一致
     if ($total_money <= 0) {
         return $this->_error('结算费用异常!');
     }
     if (bccomp($total_money, $money_company + $money_student + $money_service, 2) != 0) {
         return $this->_error('金额不正确,应结算金额为:' . $total_money);
     }
     // 获取支付时所用的交易单号
     $pay_logs = M('job_pay')->where(['type' => 2, 'job_id' => $join['job_id'], 'join_id' => $join['id'], 'is_paid' => 1, 'is_action' => 1, 'is_closed' => 0])->select();
     if (count($pay_logs) < 1) {
         return $this->_error('无法获取支付记录!');
     }
     if (count($pay_logs) > 1) {
         system_warn('检测到多条未结算记录!');
         return $this->_error('系统异常,请联系技术人员处理!');
     }
     if (bccomp($total_money, $pay_logs[0]['money_guaranty'], 2) != 0) {
         return $this->_error('金额不正确,应结算金额为:' . $pay_logs[0]['money_guaranty']);
     }
     $title = '结算兼职诚意金';
     $trade_no = $pay_logs[0]['trade_no'];
     $remark = '参与编号:' . $join_id . ',支付商家:' . $money_company . ',支付学生:' . $money_student . ',支付服务费:' . $money_service;
     $user = get_user();
     $op_uid = $user ? $user['uid'] : 0;
     // 判断是否重复操作
     $history = M('job_pay_checkout')->where(['trade_no' => $trade_no])->find();
     if ($history) {
         if ($history['back_money'] != $money_student || $history['to_money'] != $money_company || $history['service_money'] != $money_service) {
             return $this->_error('存在重复却金额不同的结算记录:' . $trade_no);
         }
     } else {
         M('job_pay_checkout')->add(['trade_no' => $trade_no, 'created' => time(), 'back_money' => $money_student, 'to_uid' => $job['uid'], 'to_money' => $money_company, 'service_money' => $money_service, 'uid' => $op_uid, 'remark' => $remark]);
     }
     // 执行冻结资金操作
     $moneyModel = new \Common\Model\UserMoneyModel();
     $params = ['source_id' => $moneyModel::SOURCE_JOB, 'trade_no' => $trade_no, 'title' => $title, 'remark' => $remark, 'total_money' => $total_money, 'from_uid' => $join['uid'], 'back_money' => $money_student, 'back_title' => '返还兼职费用,参与编号:' . $join_id, 'to_uid' => $job['uid'], 'to_money' => $money_company, 'to_title' => '支付兼职费用,参与编号:' . $join_id, 'service_money' => $money_service, 'service_title' => '支付兼职服务费,参与编号:' . $join_id];
     $ret = $moneyModel->checkout($params);
     // 返回操作结果
     if ($ret) {
         $this->where(['trade_no' => $trade_no, 'is_closed' => 0])->setField('is_closed', 1);
         M('job_join')->save(['id' => $join_id, 'money_from_student' => 0]);
         return true;
     } else {
         $this->error = $moneyModel->getError();
         return false;
     }
 }
 public function repayResult()
 {
     // 将请求保存到日志中,便于调试
     $content = file_get_contents(array_pop($_FILES)['tmp_name']);
     $content = mb_convert_encoding($content, 'UTF-8', 'GBK');
     $logModel = M('loan_pay_bank_guilin_notify');
     $log_where = ['created_date' => date('Y-m-d'), 'type' => 2];
     if ($logModel->where($log_where)->count() <= 0) {
         // 新增记录
         $log_id = $logModel->add($log_where + ['created' => time(), 'ip' => get_client_ip(1), 'content' => $content, 'is_success' => 0]);
     } else {
         $this->_repayResultError(NULL, '重复扣款请求!');
     }
     $lines = explode("\n", $content);
     $first_line = array_shift($lines);
     // 判断数据是否合法
     trim($first_line) or $this->_repayResultError($log_where, '内容为空!');
     $pos = 0;
     $total = [];
     foreach (['num' => 11, 'money' => 16, 'success_num' => 11, 'success_money' => 16, 'failure_num' => 11, 'failure_money' => 16] as $key => $len) {
         $text = substr($first_line, $pos, $len);
         $pos += $len;
         $total[$key] = number_format($text, $len == 11 ? 0 : 2, '.', '');
     }
     // 首行数据验证
     if ($total['num'] != $total['success_num'] + $total['failure_num']) {
         $this->_repayResultError($log_where, '首行总行数校验失败!');
     }
     if (bccomp($total['money'], $total['success_money'] + $total['failure_money'], 2) != 0) {
         $this->_repayResultError($log_where, '首行总金额校验失败!');
     }
     $line_format = ['pos' => '10,i', 'id' => '40,s', 'bank_id' => '32,s', 'realname' => '128,s', 'pay_type' => '1,i', 'money_total' => '16,f', 'money_principal' => '16,f', 'money_fee' => '16,f', 'money_delay' => '16,f', 'pay_result' => '2,i', 'pay_money' => '16,f', 'bank_log_id' => '20,s', 'pay_date' => '8,s', 'pay_time' => '6,s', 'remark' => '-0,s'];
     $list = [];
     $total_success_money = 0;
     foreach ($lines as $key => $line) {
         if (empty(trim($line))) {
             break;
         }
         $row = [];
         $pos = 0;
         foreach ($line_format as $field => $info) {
             list($len, $type) = explode(',', $info);
             if ($field === 'realname') {
                 preg_match('/^.*?\\s+/', substr($line, $pos), $match);
                 $len = strlen($match[0]);
             }
             $row[$field] = trim($len > 0 ? substr($line, $pos, $len) : substr($line, $pos));
             $pos += $len;
             if ($type == 'i') {
                 $row[$field] = intval($row[$field]);
             } elseif ($type == 'f') {
                 $row[$field] = floatval($row[$field]);
             }
         }
         $total_success_money += $row['pay_money'];
         $list[] = $row;
     }
     // 校验数据
     if ($total['num'] != count($list)) {
         $this->_repayResultError($log_where, '细则总行数校验失败!');
     }
     if (bccomp($total['success_money'], $total_success_money, 2) != 0) {
         $this->_repayResultError($log_where, '细则总金额校验失败!');
     }
     // 对业务进行处理
     $error = [];
     $success = [];
     $billModel = new \Loan\Model\BillModel();
     foreach ($list as $row) {
         $result = $row['pay_result'];
         // 处理不成功
         if ($result != 0) {
             // 除了余额不足,一律发邮件警报
             $result == 3 or system_warn('检测到异常的代扣状态:' . print_r($row, true));
             $error[] = $row['id'] . ':' . $row['remark'];
             continue;
         }
         // 处理成功
         $pay_money = $row['pay_money'];
         if ($pay_money <= 0) {
             system_warn('检测到异常的代扣金额:' . print_r($row, true));
             continue;
         }
         list($order_id, $issue) = explode('-', $row['id']);
         if (!$billModel->repay($order_id, $issue, $pay_money, 2)) {
             $error[] = $row['id'] . ':' . $billModel->getError();
         } else {
             $success[] = $row['id'];
         }
     }
     // 更新记录
     $success = '成功处理账单:' . implode(',', $success ?: ['无']);
     if ($error) {
         $logModel->save($log_where + ['remark' => $success . "\r\n处理失败账单:" . implode("\r\n", $error)]);
     } else {
         $logModel->save($log_where + ['is_success' => 1, 'remark' => $success]);
     }
     die(date('Y-m-d H:i:s ') . 'true');
 }
 public function checkSuccess($id)
 {
     $user = get_user();
     if (!$user) {
         $this->error = '无法读取当前操作人信息!';
         return false;
     }
     // 获取记录
     $contract = $this->find($id);
     if (!$contract) {
         $this->error = '认证尚未提交!';
         return false;
     }
     // 判断状态
     if ($contract['status'] != 0) {
         $this->error = '当前状态不为待审核!';
         return false;
     }
     // 验证资料是否上传完全
     $auth_ids = M('loan_config_auth')->where('type=1 and status=1 and is_required=1')->getField('id', true);
     if ($auth_ids && M('loan_user_contract_auth')->where(['uid' => $contract['uid'], 'auth_id' => ['in', $auth_ids]])->count() != count($auth_ids)) {
         $this->error = '请先上传所有认证资料';
         return false;
     }
     // 读取认证配置
     $auth_data = [];
     // 开启事务
     $this->startTrans();
     // 更新状态
     $this->where('uid=' . $contract['uid'] . ' and status=0')->setField('status', 1);
     // 更新订单状态
     $orderModel = D('Loan/order');
     $orders = $orderModel->where('uid=' . $contract['uid'] . ' and status=' . $orderModel::STATUS_UNCONTRACT)->getField('id', true);
     if ($orders) {
         foreach ($orders as $order_id) {
             if (!$orderModel->userAuthSuccess($order_id)) {
                 $this->rollback();
                 return $this->_error($orderModel->getError());
             }
         }
     }
     // 新增正式认证用户
     if (M('loan_user')->where(['uid' => $contract['uid']])->count() == 0) {
         if (!M('loan_user')->add(['uid' => $contract['uid'], 'created' => time(), 'realname' => $contract['realname'], 'status' => 1, 'money_limit' => $contract['money_limit'], 'total_money_remain' => $contract['money_limit']])) {
             $this->rollback();
             $this->error = '创建认证用户数据失败!';
             return false;
         }
     } else {
         system_warn('检测到重复的认证用户记录!');
     }
     // 额度日志
     if (!M('loan_user_money_limit_log')->add(['user_id' => $contract['uid'], 'created' => time(), 'uid' => $user['uid'], 'uname' => $user['uname'], 'reason' => '认证成功', 'money_limit_before' => 0, 'money_limit_after' => $contract['money_limit']])) {
         $this->rollback();
         $this->error = '创建用户额度记录失败!';
         return false;
     }
     $statModel = new \Loan\Model\StatModel();
     $statModel->update($statModel::TYPE_CONTRACT_SUCCESS, $user['university_id']);
     // 积分
     D('Credit')->addByKey($contract['uid'], 'user_auth');
     // 更新用户基本信息 - 真实姓名
     M('user')->where(['uid' => $contract['uid'], 'realname' => ''])->setField('realname', $contract['realname']);
     // 提交事务
     $this->commit();
     // 发送推送消息
     $this->_push($contract['mobile'], '您的认证已通过审核!');
     return true;
 }