874 行
33 KiB
PHP
874 行
33 KiB
PHP
<?php
|
||
|
||
namespace RiderApi\Controller;
|
||
|
||
/**
|
||
* 骑手端认证控制器
|
||
*/
|
||
class AuthController extends BaseController
|
||
{
|
||
|
||
/**
|
||
* 骑手登录
|
||
*/
|
||
public function login()
|
||
{
|
||
try {
|
||
// 支持JSON格式和form-data格式的输入
|
||
$phone = '';
|
||
$code = '';
|
||
$password = '';
|
||
$defaultPassword = '';
|
||
|
||
// 检查Content-Type是否为JSON
|
||
$contentType = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '';
|
||
if (strpos($contentType, 'application/json') !== false) {
|
||
// JSON格式输入
|
||
$input = file_get_contents('php://input');
|
||
$data = json_decode($input, true);
|
||
$phone = isset($data['phone']) ? $data['phone'] : '';
|
||
$code = isset($data['code']) ? $data['code'] : '';
|
||
$password = isset($data['password']) ? $data['password'] : '';
|
||
$defaultPassword = isset($data['default_password']) ? $data['default_password'] : '123456';
|
||
} else {
|
||
// 表单格式输入
|
||
$phone = $this->getRequestParam('phone', '');
|
||
$code = $this->getRequestParam('code', '');
|
||
$password = $this->getRequestParam('password', '');
|
||
$defaultPassword = $this->getRequestParam('default_password', '123456');
|
||
}
|
||
|
||
if (empty($phone)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '手机号不能为空'));
|
||
}
|
||
|
||
// 验证手机号格式
|
||
if (!preg_match('/^1[3-9]\d{9}$/', $phone)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '手机号格式不正确'));
|
||
}
|
||
|
||
// 如果提供了验证码,则使用验证码登录
|
||
if (!empty($code)) {
|
||
// 检查是否为调试环境
|
||
$isDebug = C('APP_DEBUG') && C('DEBUG_SMS_ENABLED');
|
||
$debugCode = C('DEBUG_SMS_CODE');
|
||
if (empty($debugCode)) {
|
||
$debugCode = '111111'; // 默认验证码
|
||
}
|
||
|
||
if ($isDebug && $code === $debugCode) {
|
||
// 调试模式:接受配置的默认验证码,跳过session检查
|
||
// 不需要检查session中的验证码
|
||
} else {
|
||
// 非调试模式:验证码登录
|
||
$sessionCode = session('sms_code_' . $phone);
|
||
|
||
if (empty($sessionCode)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '验证码已过期,请重新获取'));
|
||
}
|
||
|
||
if ($code !== $sessionCode) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '验证码错误'));
|
||
}
|
||
|
||
// 验证码正确,清除session中的验证码
|
||
session('sms_code_' . $phone, null);
|
||
}
|
||
} else if (!empty($password)) {
|
||
// 密码登录 - 需要先查询用户验证密码
|
||
$riderModel = M('rider_info');
|
||
$rider = $riderModel->where(array('rider_phone' => $phone, 'status' => 1))->find();
|
||
|
||
if (!$rider) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '用户不存在'));
|
||
}
|
||
|
||
// 验证密码
|
||
if (!$this->verifyPassword($password, $rider['password'], $rider['salt'])) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '密码错误'));
|
||
}
|
||
|
||
// 更新最后登录时间
|
||
$riderModel->where(array('id' => $rider['id']))->save(array(
|
||
'last_login_time' => date('Y-m-d H:i:s')
|
||
));
|
||
} else {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '请提供验证码或密码'));
|
||
}
|
||
|
||
// 如果是验证码登录,需要查询或创建用户
|
||
if (!empty($code)) {
|
||
// 查询骑手信息(包括被禁用的用户)
|
||
$riderModel = M('rider_info');
|
||
$rider = $riderModel->where(array('rider_phone' => $phone))->find();
|
||
|
||
if (!$rider) {
|
||
// 新用户自动注册,设置默认密码
|
||
$salt = $this->generateSalt();
|
||
$encryptedDefaultPassword = $this->encryptPassword($defaultPassword, $salt); // 使用参数传入的默认密码
|
||
|
||
$data = array(
|
||
'rider_phone' => $phone,
|
||
'rider_name' => '骑手' . substr($phone, -4),
|
||
'password' => $encryptedDefaultPassword,
|
||
'salt' => $salt,
|
||
'work_status' => 0,
|
||
'auth_status' => 0,
|
||
'add_time' => date('Y-m-d H:i:s'),
|
||
'last_login_time' => date('Y-m-d H:i:s'),
|
||
'status' => 1
|
||
);
|
||
|
||
$riderId = $riderModel->add($data);
|
||
if (!$riderId) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '注册失败'));
|
||
}
|
||
|
||
$rider = $riderModel->find($riderId);
|
||
} else {
|
||
// 如果用户被禁用,不允许登录
|
||
if ($rider['status'] != 1) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '账户已被禁用,请联系管理员'));
|
||
}
|
||
|
||
// 更新最后登录时间
|
||
$riderModel->where(array('id' => $rider['id']))->save(array(
|
||
'last_login_time' => date('Y-m-d H:i:s')
|
||
));
|
||
}
|
||
}
|
||
|
||
// 生成token
|
||
$token = md5($rider['id'] . $rider['rider_phone'] . time() . rand(1000, 9999));
|
||
|
||
// 保存token到数据库
|
||
$tokenModel = M('rider_token');
|
||
|
||
// 先删除该骑手的旧token(实现单点登录)
|
||
$tokenModel->where(array('rider_id' => $rider['id']))->delete();
|
||
|
||
// 保存新token
|
||
$tokenData = array(
|
||
'rider_id' => $rider['id'],
|
||
'token' => $token,
|
||
'expires_at' => date('Y-m-d H:i:s', time() + 7 * 24 * 3600), // 7天过期
|
||
'device_info' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '',
|
||
'ip_address' => $this->getClientIP()
|
||
// created_at 和 updated_at 字段由 MySQL 自动设置
|
||
);
|
||
|
||
$tokenResult = $tokenModel->add($tokenData);
|
||
if (!$tokenResult) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '登录失败,请重试'));
|
||
}
|
||
|
||
// 返回登录成功信息
|
||
$this->ajaxReturn(array(
|
||
'code' => 1,
|
||
'msg' => '登录成功',
|
||
'data' => array(
|
||
'rider_id' => $rider['id'],
|
||
'rider_name' => $rider['rider_name'],
|
||
'rider_phone' => $rider['rider_phone'],
|
||
'rider_avatar' => $rider['rider_avatar'],
|
||
'work_status' => $rider['work_status'],
|
||
'auth_status' => $rider['auth_status'],
|
||
'balance' => $rider['balance'],
|
||
'rating' => $rider['rating'],
|
||
'token' => $token
|
||
)
|
||
));
|
||
} catch (\Exception $e) {
|
||
// 记录错误日志
|
||
error_log('Login Error: ' . $e->getMessage());
|
||
$this->ajaxReturn(['code' => 0, 'msg' => '登录失败,请稍后再试']);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 发送短信验证码(复用现有阿里云短信模块)
|
||
* @param string $phone 手机号
|
||
* @param string $code 验证码
|
||
* @return array
|
||
*/
|
||
private function sendSmsCode($phone, $code)
|
||
{
|
||
try {
|
||
// 获取系统短信配置
|
||
$smsConfig = M('sms_config')->find();
|
||
|
||
if (empty($smsConfig)) {
|
||
return array('code' => 0, 'msg' => '系统暂不支持发送验证码功能');
|
||
}
|
||
|
||
if (
|
||
empty($smsConfig['accesskeyid']) || empty($smsConfig['accesskeysecret'])
|
||
|| empty($smsConfig['accessname']) || empty($smsConfig['accesscode'])
|
||
) {
|
||
return array('code' => 0, 'msg' => '系统短信配置有误');
|
||
}
|
||
|
||
// 调用阿里云短信发送
|
||
$result = $this->sendAliyunSms($phone, $code, $smsConfig);
|
||
|
||
return $result;
|
||
} catch (\Exception $e) {
|
||
return array('code' => 0, 'msg' => '短信发送异常:' . $e->getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 阿里云短信发送
|
||
* @param string $phone 手机号
|
||
* @param string $code 验证码
|
||
* @param array $config 短信配置
|
||
* @return array
|
||
*/
|
||
private function sendAliyunSms($phone, $code, $config)
|
||
{
|
||
require_once VENDOR_PATH . "Aliyu/SignatureHelper.php";
|
||
|
||
$params = array();
|
||
|
||
// 设置短信参数
|
||
$params["PhoneNumbers"] = $phone;
|
||
$params["SignName"] = $config['accessname'];
|
||
$params["TemplateCode"] = $config['accesscode'];
|
||
$params['TemplateParam'] = array("code" => $code);
|
||
$params['OutId'] = "rider_" . time();
|
||
$params['SmsUpExtendCode'] = "1234567";
|
||
|
||
// 转换模板参数为JSON
|
||
if (!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
|
||
$params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
|
||
}
|
||
|
||
require_once "./SignatureHelper.php";
|
||
|
||
// 初始化SignatureHelper
|
||
$helper = new \SignatureHelper();
|
||
|
||
try {
|
||
$content = $helper->request(
|
||
$config['accesskeyid'],
|
||
$config['accesskeysecret'],
|
||
"dysmsapi.aliyuncs.com",
|
||
array_merge($params, array(
|
||
"RegionId" => "cn-hangzhou",
|
||
"Action" => "SendSms",
|
||
"Version" => "2017-05-25",
|
||
))
|
||
);
|
||
|
||
$content = json_decode(json_encode($content), true);
|
||
|
||
if ($content && $content['Code'] == 'OK') {
|
||
return array('code' => 1, 'msg' => '发送成功', 'data' => $content, 'params' => $params);
|
||
} else {
|
||
$errorMsg = isset($content['Message']) ? $content['Message'] : '短信发送失败';
|
||
return array('code' => 0, 'msg' => '发送失败:' . $errorMsg);
|
||
}
|
||
} catch (\Exception $e) {
|
||
return array('code' => 0, 'msg' => '短信配置异常:' . $e->getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 生成密码盐值
|
||
* @return string
|
||
*/
|
||
private function generateSalt()
|
||
{
|
||
return substr(md5(uniqid(rand(), true)), 0, 10);
|
||
}
|
||
|
||
/**
|
||
* 加密密码
|
||
* @param string $password 原始密码
|
||
* @param string $salt 盐值
|
||
* @return string
|
||
*/
|
||
private function encryptPassword($password, $salt)
|
||
{
|
||
return md5(md5($password) . $salt);
|
||
}
|
||
|
||
/**
|
||
* 验证密码
|
||
* @param string $password 输入的密码
|
||
* @param string $encryptedPassword 数据库中的加密密码
|
||
* @param string $salt 盐值
|
||
* @return bool
|
||
*/
|
||
private function verifyPassword($password, $encryptedPassword, $salt)
|
||
{
|
||
return $this->encryptPassword($password, $salt) === $encryptedPassword;
|
||
}
|
||
|
||
/**
|
||
* 修改密码
|
||
*/
|
||
public function changePassword()
|
||
{
|
||
$riderId = $this->riderId;
|
||
$rider = $this->riderInfo;
|
||
|
||
// 使用统一的参数处理方法
|
||
$oldPassword = trim($this->getRequestParam('old_password', ''));
|
||
$newPassword = trim($this->getRequestParam('new_password', ''));
|
||
|
||
if (empty($oldPassword) || empty($newPassword)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '参数不完整'));
|
||
}
|
||
|
||
if (strlen($newPassword) < 6) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '新密码长度不能少于6位'));
|
||
}
|
||
|
||
if (!$rider) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '用户不存在'));
|
||
}
|
||
|
||
// 验证旧密码
|
||
if (!$this->verifyPassword($oldPassword, $rider['password'], $rider['salt'])) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '原密码错误'));
|
||
}
|
||
|
||
// 生成新的盐值和加密新密码
|
||
$newSalt = $this->generateSalt();
|
||
$newEncryptedPassword = $this->encryptPassword($newPassword, $newSalt);
|
||
|
||
// 更新密码
|
||
$riderModel = M('rider_info');
|
||
$result = $riderModel->where(array('id' => $riderId))->save(array(
|
||
'password' => $newEncryptedPassword,
|
||
'salt' => $newSalt
|
||
));
|
||
|
||
if ($result !== false) {
|
||
$this->ajaxReturn(array('code' => 1, 'msg' => '密码修改成功'));
|
||
} else {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '密码修改失败'));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 发送验证码
|
||
*/
|
||
public function sendCode()
|
||
{
|
||
try {
|
||
// 支持JSON格式和form-data格式的输入
|
||
$phone = '';
|
||
|
||
// 检查Content-Type是否为JSON
|
||
$contentType = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '';
|
||
if (strpos($contentType, 'application/json') !== false) {
|
||
// JSON格式输入
|
||
$input = file_get_contents('php://input');
|
||
$data = json_decode($input, true);
|
||
$phone = isset($data['phone']) ? $data['phone'] : '';
|
||
} else {
|
||
// 表单格式输入
|
||
$phone = $this->getRequestParam('phone', '');
|
||
}
|
||
|
||
if (empty($phone)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '手机号不能为空'));
|
||
}
|
||
|
||
// 验证手机号格式
|
||
if (!preg_match('/^1[3-9]\d{9}$/', $phone)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '手机号格式不正确'));
|
||
}
|
||
|
||
// 检查是否为调试环境
|
||
$isDebug = C('APP_DEBUG') && C('DEBUG_SMS_ENABLED');
|
||
|
||
if ($isDebug) {
|
||
// 调试环境:使用配置的默认验证码
|
||
$code = C('DEBUG_SMS_CODE');
|
||
if (empty($code)) {
|
||
$code = '111111'; // 默认验证码
|
||
}
|
||
|
||
// 将验证码存储到session中,有效期5分钟
|
||
session('sms_code_' . $phone, $code, 300);
|
||
|
||
$this->ajaxReturn(array(
|
||
'code' => 1,
|
||
'msg' => '验证码发送成功(调试模式)',
|
||
'data' => array(
|
||
'debug' => true,
|
||
'code' => $code // 调试模式下返回验证码
|
||
)
|
||
));
|
||
} else {
|
||
// 生产环境:调用阿里云短信服务发送验证码
|
||
$code = sprintf('%06d', rand(100000, 999999));
|
||
|
||
// 调用现有的短信发送服务
|
||
$smsResult = $this->sendSmsCode($phone, $code);
|
||
|
||
if ($smsResult['code'] == 1) {
|
||
// 短信发送成功,将验证码存储到session中,有效期5分钟
|
||
session('sms_code_' . $phone, $code, 300);
|
||
|
||
$this->ajaxReturn(array(
|
||
'code' => 1,
|
||
'msg' => '验证码发送成功',
|
||
'data' => array(
|
||
'debug' => false
|
||
)
|
||
));
|
||
} else {
|
||
// 短信发送失败
|
||
$this->ajaxReturn(array(
|
||
'code' => 0,
|
||
'msg' => $smsResult['msg'] ?: '验证码发送失败,请稍后重试'
|
||
));
|
||
}
|
||
}
|
||
} catch (\Exception $e) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => $e->getMessage()));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 实名认证
|
||
*/
|
||
public function realAuth()
|
||
{
|
||
$riderId = $this->riderId;
|
||
$rider = $this->riderInfo;
|
||
|
||
// 检查Content-Type并解析参数
|
||
$contentType = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '';
|
||
if (strpos($contentType, 'application/json') !== false) {
|
||
$input = json_decode(file_get_contents('php://input'), true);
|
||
$realName = isset($input['real_name']) ? trim($input['real_name']) : '';
|
||
$idCard = isset($input['id_card']) ? trim($input['id_card']) : '';
|
||
$idCardFront = isset($input['id_card_front']) ? trim($input['id_card_front']) : '';
|
||
$idCardBack = isset($input['id_card_back']) ? trim($input['id_card_back']) : '';
|
||
} else {
|
||
$realName = $this->getRequestParam('real_name', '');
|
||
$idCard = $this->getRequestParam('id_card', '');
|
||
$idCardFront = $this->getRequestParam('id_card_front', '');
|
||
$idCardBack = $this->getRequestParam('id_card_back', '');
|
||
}
|
||
|
||
if (empty($realName)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '真实姓名不能为空'));
|
||
}
|
||
|
||
if (empty($idCard)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '身份证号不能为空'));
|
||
}
|
||
|
||
// 验证身份证号格式
|
||
if (!preg_match('/^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/', $idCard)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '身份证号格式不正确'));
|
||
}
|
||
|
||
$riderModel = M('rider_info');
|
||
$rider = $riderModel->find($riderId);
|
||
|
||
if (!$rider) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '骑手不存在'));
|
||
}
|
||
|
||
// 更新认证信息
|
||
$data = array(
|
||
'rider_name' => $realName,
|
||
'id_card' => $idCard,
|
||
'id_card_front' => $idCardFront,
|
||
'id_card_back' => $idCardBack,
|
||
'auth_status' => 1 // 认证中
|
||
);
|
||
|
||
$result = $riderModel->where(array('id' => $riderId))->save($data);
|
||
|
||
if ($result !== false) {
|
||
$this->ajaxReturn(array(
|
||
'code' => 1,
|
||
'msg' => '认证信息提交成功,请等待审核'
|
||
));
|
||
} else {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '提交失败'));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 提交认证信息
|
||
*/
|
||
public function submitAuthentication()
|
||
{
|
||
try {
|
||
$riderId = $this->riderId;
|
||
|
||
// 检查Content-Type并解析参数
|
||
$contentType = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '';
|
||
if (strpos($contentType, 'application/json') !== false) {
|
||
$input = json_decode(file_get_contents('php://input'), true);
|
||
$name = isset($input['name']) ? trim($input['name']) : '';
|
||
$idCard = isset($input['id_card']) ? trim($input['id_card']) : '';
|
||
$idCardFront = isset($input['id_card_front']) ? trim($input['id_card_front']) : '';
|
||
$idCardBack = isset($input['id_card_back']) ? trim($input['id_card_back']) : '';
|
||
$handheldPhoto = isset($input['handheld_photo']) ? trim($input['handheld_photo']) : '';
|
||
$emergencyName = isset($input['emergency_name']) ? trim($input['emergency_name']) : '';
|
||
$emergencyPhone = isset($input['emergency_phone']) ? trim($input['emergency_phone']) : '';
|
||
$emergencyRelation = isset($input['emergency_relation']) ? trim($input['emergency_relation']) : '';
|
||
$healthCert = isset($input['health_cert']) ? trim($input['health_cert']) : '';
|
||
$healthCertExpire = isset($input['health_cert_expire']) ? trim($input['health_cert_expire']) : '';
|
||
} else {
|
||
$name = $this->getRequestParam('name', '');
|
||
$idCard = $this->getRequestParam('id_card', '');
|
||
$idCardFront = $this->getRequestParam('id_card_front', '');
|
||
$idCardBack = $this->getRequestParam('id_card_back', '');
|
||
$handheldPhoto = $this->getRequestParam('handheld_photo', '');
|
||
$emergencyName = $this->getRequestParam('emergency_name', '');
|
||
$emergencyPhone = $this->getRequestParam('emergency_phone', '');
|
||
$emergencyRelation = $this->getRequestParam('emergency_relation', '');
|
||
$healthCert = $this->getRequestParam('health_cert', '');
|
||
$healthCertExpire = $this->getRequestParam('health_cert_expire', '');
|
||
}
|
||
|
||
// 参数验证
|
||
if (empty($name)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '真实姓名不能为空'));
|
||
}
|
||
|
||
if (empty($idCard)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '身份证号不能为空'));
|
||
}
|
||
|
||
if (empty($idCardFront)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '身份证正面照片不能为空'));
|
||
}
|
||
|
||
if (empty($idCardBack)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '身份证反面照片不能为空'));
|
||
}
|
||
|
||
if (empty($handheldPhoto)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '手持身份证照片不能为空'));
|
||
}
|
||
|
||
if (empty($emergencyName)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '紧急联系人姓名不能为空'));
|
||
}
|
||
|
||
if (empty($emergencyPhone)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '紧急联系人电话不能为空'));
|
||
}
|
||
|
||
if (empty($emergencyRelation)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '与紧急联系人关系不能为空'));
|
||
}
|
||
|
||
if (empty($healthCert)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '健康证照片不能为空'));
|
||
}
|
||
|
||
if (empty($healthCertExpire)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '健康证有效期不能为空'));
|
||
}
|
||
|
||
// 验证身份证号格式
|
||
if (!preg_match('/^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/', $idCard)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '身份证号格式不正确'));
|
||
}
|
||
|
||
// 验证紧急联系人电话格式
|
||
if (!preg_match('/^1[3-9]\d{9}$/', $emergencyPhone)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '紧急联系人电话格式不正确'));
|
||
}
|
||
|
||
// 验证健康证有效期格式
|
||
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $healthCertExpire)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '健康证有效期格式不正确,请使用YYYY-MM-DD格式'));
|
||
}
|
||
|
||
// 验证健康证有效期是否为未来日期
|
||
if (strtotime($healthCertExpire) <= time()) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '健康证有效期必须为未来日期'));
|
||
}
|
||
|
||
$riderModel = M('rider_info');
|
||
$rider = $riderModel->find($riderId);
|
||
|
||
if (!$rider) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '骑手不存在'));
|
||
}
|
||
|
||
// 检查是否已经认证通过
|
||
if ($rider['auth_status'] == 2) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '您已通过认证,无需重复提交'));
|
||
}
|
||
|
||
// 检查是否正在审核中
|
||
if ($rider['auth_status'] == 1) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '您的认证正在审核中,请勿重复提交'));
|
||
}
|
||
|
||
// 检查身份证号是否已被其他用户使用
|
||
$existRider = $riderModel->where(array(
|
||
'id_card' => $idCard,
|
||
'id' => array('neq', $riderId),
|
||
'status' => 1
|
||
))->find();
|
||
|
||
if ($existRider) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '该身份证号已被其他用户使用'));
|
||
}
|
||
|
||
// 更新认证信息
|
||
$data = array(
|
||
'rider_name' => $name,
|
||
'id_card' => $idCard,
|
||
'id_card_front' => $idCardFront,
|
||
'id_card_back' => $idCardBack,
|
||
'auth_status' => 1, // 认证中
|
||
'auth_time' => date('Y-m-d H:i:s'),
|
||
'auth_remark' => '' // 清空之前的备注
|
||
);
|
||
|
||
// 如果数据库表有对应字段,则添加这些字段
|
||
$riderModel = M('rider_info');
|
||
$tableFields = $riderModel->getDbFields();
|
||
|
||
if (in_array('handheld_photo', $tableFields)) {
|
||
$data['handheld_photo'] = $handheldPhoto;
|
||
}
|
||
if (in_array('emergency_name', $tableFields)) {
|
||
$data['emergency_name'] = $emergencyName;
|
||
}
|
||
if (in_array('emergency_phone', $tableFields)) {
|
||
$data['emergency_phone'] = $emergencyPhone;
|
||
}
|
||
if (in_array('emergency_relation', $tableFields)) {
|
||
$data['emergency_relation'] = $emergencyRelation;
|
||
}
|
||
if (in_array('health_cert', $tableFields)) {
|
||
$data['health_cert'] = $healthCert;
|
||
}
|
||
if (in_array('health_cert_expire', $tableFields)) {
|
||
$data['health_cert_expire'] = $healthCertExpire;
|
||
}
|
||
|
||
$result = $riderModel->where(array('id' => $riderId))->save($data);
|
||
|
||
if ($result !== false) {
|
||
// 记录认证日志
|
||
$logModel = M('rider_auth_log');
|
||
if ($logModel) {
|
||
$logData = array(
|
||
'rider_id' => $riderId,
|
||
'action' => 'submit',
|
||
'old_status' => $rider['auth_status'],
|
||
'new_status' => 1,
|
||
'remark' => '骑手提交认证信息',
|
||
'ip_address' => $this->getClientIP(),
|
||
'add_time' => date('Y-m-d H:i:s')
|
||
);
|
||
$logModel->add($logData);
|
||
}
|
||
|
||
$this->ajaxReturn(array(
|
||
'code' => 1,
|
||
'msg' => '认证信息提交成功,请等待审核',
|
||
'data' => array(
|
||
'auth_status' => 1,
|
||
'auth_status_text' => '认证中',
|
||
'submit_time' => date('Y-m-d H:i:s')
|
||
)
|
||
));
|
||
} else {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '提交失败,请重试'));
|
||
}
|
||
} catch (\Exception $e) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => $e->getMessage()));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 上传健康证
|
||
*/
|
||
public function uploadHealthCert()
|
||
{
|
||
$riderId = $this->riderId;
|
||
$rider = $this->riderInfo;
|
||
|
||
// 检查Content-Type并解析参数
|
||
$contentType = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '';
|
||
if (strpos($contentType, 'application/json') !== false) {
|
||
$input = json_decode(file_get_contents('php://input'), true);
|
||
$healthCert = isset($input['health_cert']) ? trim($input['health_cert']) : '';
|
||
$healthCertExpire = isset($input['health_cert_expire']) ? trim($input['health_cert_expire']) : '';
|
||
} else {
|
||
$healthCert = $this->getRequestParam('health_cert', '');
|
||
$healthCertExpire = $this->getRequestParam('health_cert_expire', '');
|
||
}
|
||
|
||
if (empty($healthCert)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '健康证照片不能为空'));
|
||
}
|
||
|
||
if (empty($healthCertExpire)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '健康证过期时间不能为空'));
|
||
}
|
||
|
||
$riderModel = M('rider_info');
|
||
$rider = $riderModel->find($riderId);
|
||
|
||
if (!$rider) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '骑手不存在'));
|
||
}
|
||
|
||
// 更新健康证信息
|
||
$data = array(
|
||
'health_cert' => $healthCert,
|
||
'health_cert_expire' => $healthCertExpire
|
||
);
|
||
|
||
$result = $riderModel->where(array('id' => $riderId))->save($data);
|
||
|
||
if ($result !== false) {
|
||
$this->ajaxReturn(array(
|
||
'code' => 1,
|
||
'msg' => '健康证上传成功'
|
||
));
|
||
} else {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '上传失败'));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取骑手信息
|
||
*/
|
||
public function getRiderInfo()
|
||
{
|
||
$rider = $this->riderInfo;
|
||
|
||
if (!$rider) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '获取骑手信息失败'));
|
||
}
|
||
|
||
// 确保通知设置字段存在
|
||
if (!isset($rider['order_notification'])) {
|
||
$rider['order_notification'] = 1; // 默认开启
|
||
}
|
||
if (!isset($rider['voice_broadcast'])) {
|
||
$rider['voice_broadcast'] = 1; // 默认开启
|
||
}
|
||
if (!isset($rider['vibration_alert'])) {
|
||
$rider['vibration_alert'] = 1; // 默认开启
|
||
}
|
||
|
||
$this->ajaxReturn(array(
|
||
'code' => 1,
|
||
'msg' => '获取成功',
|
||
'data' => array_merge($rider, array(
|
||
'online_start_time' => $rider['online_start_time'] ?: null
|
||
))
|
||
));
|
||
}
|
||
|
||
/**
|
||
* 更新工作状态
|
||
*/
|
||
public function updateWorkStatus()
|
||
{
|
||
try {
|
||
$riderId = $this->riderId;
|
||
|
||
// 检查Content-Type并解析参数
|
||
$contentType = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '';
|
||
if (strpos($contentType, 'application/json') !== false) {
|
||
$input = json_decode(file_get_contents('php://input'), true);
|
||
$workStatus = isset($input['work_status']) ? intval($input['work_status']) : -1;
|
||
} else {
|
||
$workStatus = $this->getRequestParam('work_status', -1, 'intval');
|
||
}
|
||
|
||
// 验证工作状态参数
|
||
if ($workStatus < 0 || $workStatus > 2) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '工作状态参数错误'));
|
||
}
|
||
|
||
// 验证状态映射
|
||
$statusMap = array(
|
||
0 => '离线',
|
||
1 => '在线',
|
||
2 => '忙碌'
|
||
);
|
||
|
||
if (!isset($statusMap[$workStatus])) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '无效的工作状态'));
|
||
}
|
||
|
||
$riderModel = M('rider_info');
|
||
|
||
$riderInfo = $riderModel->find($riderId);
|
||
|
||
if (!$riderInfo) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '骑手不存在'));
|
||
}
|
||
|
||
// 检查认证状态,只有已认证的骑手才能上线
|
||
if ($workStatus == 1 && $riderInfo['auth_status'] != 2) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '请先完成实名认证才能上线'));
|
||
}
|
||
|
||
// 更新工作状态
|
||
$data = array(
|
||
'work_status' => $workStatus,
|
||
'status_update_time' => date('Y-m-d H:i:s')
|
||
);
|
||
|
||
// 处理在线开始时间
|
||
if ($workStatus == 1) {
|
||
// 上线时记录在线开始时间
|
||
$data['online_start_time'] = time();
|
||
} elseif ($workStatus == 0) {
|
||
// 下线时清空在线开始时间
|
||
$data['online_start_time'] = null;
|
||
}
|
||
|
||
$result = $riderModel->where(array('id' => $riderId))->save($data);
|
||
|
||
if ($result !== false) {
|
||
$this->ajaxReturn(array(
|
||
'code' => 1,
|
||
'msg' => '工作状态更新成功',
|
||
'data' => array(
|
||
'work_status' => $workStatus,
|
||
'work_status_text' => $statusMap[$workStatus],
|
||
'update_time' => date('Y-m-d H:i:s')
|
||
)
|
||
));
|
||
} else {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '状态更新失败'));
|
||
}
|
||
} catch (\Exception $e) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => $e->getMessage()));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取客户端IP地址
|
||
*/
|
||
private function getClientIP()
|
||
{
|
||
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
||
$ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
|
||
return trim($ips[0]);
|
||
} elseif (isset($_SERVER['HTTP_X_REAL_IP']) && !empty($_SERVER['HTTP_X_REAL_IP'])) {
|
||
return $_SERVER['HTTP_X_REAL_IP'];
|
||
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
|
||
return $_SERVER['REMOTE_ADDR'];
|
||
}
|
||
return '0.0.0.0';
|
||
}
|
||
}
|