You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

526 lines
23 KiB

<?php
/**
* 用户
*/
namespace App\Http\Controllers\Mobile;
use App\Helpers\ResponseCode;
use App\Helpers\StarterResponseCode;
use App\Jobs\SendAppointCar;
use App\Jobs\SendCourseCar;
use App\Models\Appointment;
use App\Models\Config;
use App\Models\CourseSign;
use App\Models\RelatedModel;
use App\Models\ScoreLog;
use App\Models\ThirdAppointmentLog;
use App\Models\User;
use App\Repositories\DoorRepository;
use App\Repositories\YuanheRepository;
use EasyWeChat\Factory;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Overtrue\Pinyin\Pinyin;
class UserController extends CommonController
{
/**
* @OA\Get(
* path="/api/mobile/user/applet-login",
* tags={"小程序-用户管理"},
* summary="小程序静默登陆",
* @OA\Parameter(name="code", in="query", @OA\Schema(type="string"), required=false, description="code"),
* @OA\Parameter(name="pid", in="query", @OA\Schema(type="string"), required=false, description="上级id"),
* @OA\Response(
* response=200,
* description="操作成功"
* )
* )
*/
public function appletLogin()
{
$all = \request()->all();
$messages = [
'code.required' => 'code必填',
];
$validator = Validator::make($all, [
'code' => 'required',
], $messages);
if ($validator->fails()) {
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
$pid = request('pid', 0);
// 获取配置信息
$appid = \config('app.applet_appid');
$appSecret = \config('app.applet_secret');
$url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $appSecret . "&js_code={$all['code']}&grant_type=authorization_code";
$userInfo = json_decode(file_get_contents($url), true);
if (!isset($userInfo['openid'])) {
return $this->fail([ResponseCode::ERROR_PARAMETER, 'code异常']);
}
$user = User::where('openid', $userInfo['openid'])->first();
if (empty($user)) {
$user = User::create([
'openid' => $userInfo['openid'],
'pid' => $pid,
'code' => randStr(8)
]);
if (!empty($pid)) {
// 给上级奖励
$score = Config::getValueByKey('share_score');
ScoreLog::add($pid, $score, '分享获得');
}
}
$token = $user->createToken("mobile-token")->plainTextToken;
return $this->success(compact('token'));
}
/**
* @OA\Get(
* path="/api/mobile/user/account-login",
* tags={"小程序-用户管理"},
* summary="账号密码登陆",
* @OA\Parameter(name="mobile", in="query", @OA\Schema(type="string"), required=false, description="mobile"),
* @OA\Parameter(name="password", in="query", @OA\Schema(type="string"), required=false, description="password"),
* @OA\Response(
* response=200,
* description="操作成功"
* )
* )
*/
public function accountLogin()
{
$all = \request()->all();
$messages = [
'mobile.required' => '手机号必填',
'password.required' => '密码必填',
];
$validator = Validator::make($all, [
'mobile' => 'required',
'password' => 'required',
], $messages);
if ($validator->fails()) {
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
$user = User::where('mobile', $all['mobile'])->where('id', $this->getUserId())->first();
if (!$user || !Hash::check($all['password'], $user->password)) {
return $this->fail([ResponseCode::ERROR_PARAMETER, '账号密码不正确']);
}
return $this->success("验证通过");
}
/**
* @OA\Post(
* path="/api/mobile/user/update-user",
* tags={"小程序-用户管理"},
* summary="更新用户信息",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="认证token"),
* @OA\Parameter(name="openid", in="query", @OA\Schema(type="string"), description="用户的openid"),
* @OA\Parameter(name="sex", in="query", @OA\Schema(type="string"), description="性别男/女"),
* @OA\Parameter(name="nickname", in="query", @OA\Schema(type="string"), description="昵称"),
* @OA\Parameter(name="mobile", in="query", @OA\Schema(type="string"), description="手机号"),
* @OA\Parameter(name="country", in="query", @OA\Schema(type="string"), description="国家"),
* @OA\Parameter(name="province", in="query", @OA\Schema(type="string"), description="省份"),
* @OA\Parameter(name="city", in="query", @OA\Schema(type="string"), description="城市"),
* @OA\Parameter(name="headimgurl", in="query", @OA\Schema(type="string"), description="头像url"),
* @OA\Parameter(name="username", in="query", @OA\Schema(type="string"), description="用户名"),
* @OA\Parameter(name="old_password", in="query", @OA\Schema(type="string"), description="旧密码"),
* @OA\Parameter(name="password", in="query", @OA\Schema(type="string"), description="密码"),
* @OA\Parameter(name="name", in="query", @OA\Schema(type="string"), description="名字"),
* @OA\Parameter(name="birthday", in="query", @OA\Schema(type="string"), description="生日"),
* @OA\Parameter(name="email", in="query", @OA\Schema(type="string"), description="邮箱"),
* @OA\Parameter(name="education", in="query", @OA\Schema(type="integer"), description="学历"),
* @OA\Parameter(name="school", in="query", @OA\Schema(type="string"), description="学校"),
* @OA\Parameter(name="speciality", in="query", @OA\Schema(type="string"), description="专业"),
* @OA\Parameter(name="honour", in="query", @OA\Schema(type="string"), description="荣誉"),
* @OA\Parameter(name="introduce", in="query", @OA\Schema(type="string"), description="介绍"),
* @OA\Parameter(name="company_name", in="query", @OA\Schema(type="string"), description="公司名称"),
* @OA\Parameter(name="company_position", in="query", @OA\Schema(type="string"), description="个人职务"),
* @OA\Parameter(name="company_has_share", in="query", @OA\Schema(type="string"), description="是否有股份"),
* @OA\Parameter(name="company_build_date", in="query", @OA\Schema(type="string"), description="公司成立日期"),
* @OA\Parameter(name="company_area", in="query", @OA\Schema(type="string"), description="公司区域"),
* @OA\Parameter(name="company_type", in="query", @OA\Schema(type="string"), description="公司性质"),
* @OA\Parameter(name="company_industry", in="query", @OA\Schema(type="string"), description="公司所属行业"),
* @OA\Parameter(name="company_business", in="query", @OA\Schema(type="string"), description="公司主营业务"),
* @OA\Parameter(name="company_fund", in="query", @OA\Schema(type="string"), description="公司融资情况"),
* @OA\Parameter(name="company_need_fund", in="query", @OA\Schema(type="boolean"), description="公司是否需要融资"),
* @OA\Parameter(name="sign_from", in="query", @OA\Schema(type="string"), description="报名信息来源"),
* @OA\Parameter(name="remark", in="query", @OA\Schema(type="string"), description="备注"),
* @OA\Parameter(name="idcard", in="query", @OA\Schema(type="string"), description="身份证号码"),
* @OA\Parameter(name="plate", in="query", @OA\Schema(type="string"), description="车牌号多个英文逗号分隔"),
* @OA\Parameter(name="type", in="query", @OA\Schema(type="string"), description="人才类型"),
* @OA\Response(
* response=200,
* description="操作成功"
* )
* )
*/
public function updateUser()
{
$all = \request()->all();
$model = User::find($this->getUserId());
if (isset($all['password'])) {
// 判断旧密码是否正确
if (!Hash::check($all['old_password'], $model->password)) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '旧密码不正确']);
}
$model->password = Hash::make($all['password']);
}
$all['name'] = $all['username'];
if (isset($all['name']) && !empty($all['name'])) {
$all['letter'] = strtoupper(Pinyin::abbr(mb_substr($all['name'], 0, 1))[0]);
}
// 如果有公司信息,就更新一下公司
if (isset($all['company_name']) && !empty($all['company_name']) && $model->company_name != $all['company_name']) {
// 调用命令行更新
Artisan::call("update_company --user_id={$model->id}");
}
$model->fill($all);
$model->save();
// 判断下,如果用户新加入车牌号,并且有未开始或者进行中的预约,则直接预约车牌号
$appointmentModel = Appointment::where('user_id', $this->getUserId())
->where('status', 1)
->where('end_time', '>', date('Y-m-d H:i:s'))
->get();
// 有预约数据并且车牌号不为空
if ($appointmentModel->isNotEmpty() && $model->plate) {
foreach ($appointmentModel as $appointment) {
$appointment->plate = $model->plate;
$appointment->save();
$plateArray = explode(',', $model->plate);
foreach ($plateArray as $plate) {
// 判断是否已预约
$has = ThirdAppointmentLog::where('appointment_id', $appointment->id)
->where('plate', $plate)
->where('plate_status', 1)
->first();
if ($has) continue;
// 车辆预约
dispatch((new SendAppointCar($appointment, $plate)));
}
}
}
return $this->success('更新成功');
}
/**
* @OA\Get(
* path="/api/mobile/user/get-user-info",
* tags={"小程序-用户管理"},
* summary="获取用户信息",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=false, description="token"),
* @OA\Response(
* response=200,
* description="操作成功"
* )
* )
*/
public function getUserInfo()
{
$user = User::with('appointments')
->withCount(['appointments as pass_appointments' => function ($query) {
$query->whereIn('status', [0, 1]);
}])->with(['courseSigns' => function ($query) {
$query->whereHas('course')->with('course.typeDetail')->where('status', 1)->where('fee_status', 1);
}])->find($this->getUserId());
$doorRepository = new DoorRepository();
$door_appointments = Appointment::where('user_id', $this->getUserId())
->where('status', 1)
->orderBy('id', 'desc')
->first();
if ($door_appointments) {
$door_appointments->qrcode = $doorRepository->getEmpQrCode($door_appointments, $out);
}
// 进行中的课程
$course_signs = CourseSign::where('user_id', $this->getUserId())
->with('course')->where('status', 1)
->whereHas('course', function ($query) {
$nowDate = date('Y-m-d');
$query->where('start_date', '<=', $nowDate)->where('end_date', '>=', $nowDate);
})->first();
if ($course_signs) {
// todo::上线解开注释
//$course_signs->qrcode = $doorRepository->getEmpQrCodeByCourse($course_signs, $out);
$course_signs->qrcode = '';
}
// 是否有资格进入校友库
$enter_schoolmate = User::whereHas('courseSigns', function ($query) {
$query->where('fee_status', 1)->where('status', 1);
})->where('id', $this->getUserId())->count();
// 是否生日
$is_birthday = 0;
if ($user->birthday == date('Y-m-d')) {
$is_birthday = 1;
}
return $this->success(compact('user', 'door_appointments', 'course_signs', 'enter_schoolmate', 'is_birthday'));
}
/**
* @OA\Get(
* path="/api/mobile/user/mobile",
* tags={"小程序-用户管理"},
* summary="获取微信授权手机号",
* description="",
* @OA\Parameter(name="code", in="query", @OA\Schema(type="string"), required=false, description="code"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description=""
* )
* )
*/
public function mobile()
{
$all = \request()->all();
$messages = [
'code.required' => 'code必填',
];
$validator = Validator::make($all, [
'code' => 'required',
], $messages);
if ($validator->fails()) {
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
$config = [
'app_id' => \config('app.applet_appid'),
'secret' => \config('app.applet_secret')
];
$app = Factory::miniProgram($config);
$result = $app->phone_number->getUserPhoneNumber($all['code']);
$mobile = $result['phone_info']['purePhoneNumber'] ?? '';
// 判断手机号是否存在
$hasMobile = User::where('mobile', $mobile)->first();
if ($hasMobile) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '手机号已存在']);
}
$hasMobile->mobile = $mobile;
$hasMobile->save();
return $this->success($hasMobile);
}
/**
* @OA\Get(
* path="/api/mobile/user/bind-mobile",
* tags={"手机端-用户管理"},
* summary="验证码绑定手机号",
* description="",
* @OA\Parameter(name="mobile", in="query", @OA\Schema(type="string"), required=true, description="手机号"),
* @OA\Parameter(name="code", in="query", @OA\Schema(type="string"), required=true, description="验证码"),
* @OA\Parameter(name="is_bind", in="query", @OA\Schema(type="string"), required=true, description="是否绑定手机号"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description=""
* )
* )
*/
public function bindMobile()
{
$all = \request()->all();
$messages = [
'mobile.required' => '手机号必填',
'mobile.numeric' => '手机号格式错误',
'code' => '验证码必填',
'is_bind' => '是否绑定必填',
];
$validator = Validator::make($all, [
'mobile' => 'required|numeric',
'code' => 'required',
'is_bind' => 'required',
], $messages);
if ($validator->fails()) {
return $this->fail([StarterResponseCode::START_ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
$key = 'sms_' . $all['mobile'];
$check = Cache::get($key);
if (empty($check)) return $this->fail([ResponseCode::ERROR_BUSINESS, '请先发送验证码']);
if ($check['code'] != $all['code']) return $this->fail([ResponseCode::ERROR_BUSINESS, '验证码错误']);
// 判断手机号是否存在
$hasMobile = User::where('mobile', $all['mobile'])->where('id', '!=', $this->getUserId())->first();
if ($hasMobile) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '手机号已存在']);
}
$model = User::find($this->getUserId());
if ($all['is_bind']) {
$model->mobile = $all['mobile'];
$model->save();
}
return $this->success($model);
}
/**
* @OA\Get(
* path="/api/mobile/user/check-mobile",
* tags={"手机端-用户管理"},
* summary="检测手机号",
* description="",
* @OA\Parameter(name="mobile", in="query", @OA\Schema(type="string"), required=true, description="手机号"),
* @OA\Parameter(name="code", in="query", @OA\Schema(type="string"), required=true, description="验证码"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description=""
* )
* )
*/
public function checkMobile()
{
$all = \request()->all();
$messages = [
'mobile.required' => '手机号必填',
'mobile.numeric' => '手机号格式错误',
'code' => '验证码必填',
];
$validator = Validator::make($all, [
'mobile' => 'required|numeric',
'code' => 'required',
], $messages);
if ($validator->fails()) {
return $this->fail([StarterResponseCode::START_ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
$key = 'sms_' . $all['mobile'];
$check = Cache::get($key);
if (empty($check)) return $this->fail([ResponseCode::ERROR_BUSINESS, '请先发送验证码']);
if ($check['code'] != $all['code']) return $this->fail([ResponseCode::ERROR_BUSINESS, '验证码错误']);
// 判断手机号是否存在
$hasMobile = User::where('mobile', $all['mobile'])->first();
if ($hasMobile) {
if ($hasMobile->id != $this->getUserId()) {
$openid = $this->getUser()->openid;
$code = $this->getUser()->code ?? randStr(8);
// 当前用户绑定的相关报名,修改成新用户
CourseSign::where('user_id', $this->getUserId())->update(['user_id' => $hasMobile->id]);
// 删除当前用户
User::where('id', $this->getUserId())->delete();
// 旧用户绑定新用户
$hasMobile->openid = $openid;
$hasMobile->code = $code;
$hasMobile->save();
}
$token = $hasMobile->createToken("mobile-token")->plainTextToken;
return $this->success(compact('token'));
}
return $this->fail([ResponseCode::ERROR_BUSINESS, '校友库中还没有您的信息,请先注册']);
}
/**
* @OA\Get (
* path="/api/mobile/user/send-sms",
* tags={"手机端-用户管理"},
* summary="短信发送",
* description="",
* @OA\Parameter(name="mobile", in="query", @OA\Schema(type="string"), required=true, description="手机号"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description=""
* )
* )
*/
public function sendSms()
{
$all = \request()->all();
$messages = [
'mobile.required' => '手机号必填',
'mobile.numeric' => '手机号格式错误',
];
$validator = Validator::make($all, [
'mobile' => 'required|numeric',
], $messages);
if ($validator->fails()) {
return $this->fail([StarterResponseCode::START_ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
$key = 'sms_' . $all['mobile'];
$check = Cache::get($key);
if (isset($check) && time() - $check['time'] <= 60) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '请勿频繁发送']);
}
$code = rand(1000, 9999);
$smsSign = Config::getValueByKey('sms_sign');
$content = "{$smsSign}您的验证码是:{$code},验证码五分钟内有效,如非本人操作,请忽略。";
$result = ymSms($all['mobile'], $content);
if ($result) {
// 缓存
Cache::put($key, ['code' => $code, 'time' => time()], 300);
return $this->success("发送成功");
}
return $this->fail([StarterResponseCode::START_ERROR_PARAMETER, "发送失败"]);
}
/**
* @OA\Post (
* path="/api/mobile/user/update-donates",
* tags={"手机端-用户管理"},
* summary="新增校友捐赠",
* description="",
* @OA\Parameter(name="xxx", in="query", @OA\Schema(type="string"), required=true, description="字段"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description=""
* )
* )
*/
public function updateDonates()
{
$all = request()->all();
$model = (new RelatedModel())->setTable('donates');
$model->fill($all);
$model->save();
// 短信通知审核人
$smsSign = Config::getValueByKey('sms_sign');
$appointment_mobile = Config::getValueByKey('appointment_mobile');
$appointment_mobile = explode(',', $appointment_mobile);
$content = "{$smsSign}您收到一个新捐赠信息,请您登陆管理后台进行查看。";
foreach ($appointment_mobile as $mobile) {
ymSms($mobile, $content);
}
return $this->success("新增成功");
}
public function h5Show()
{
$code = request('code');
if (empty($code)) {
return '编码不存在';
}
$appointment = Appointment::where('code', $code)->first();
if (empty($appointment)) {
return '预约不存在';
}
if ($appointment->status != 1) {
return '预约状态异常';
}
$appointment->start_time = date('Y/m/d', strtotime($appointment->start_time));
$appointment->end_time = date('Y/m/d', strtotime($appointment->end_time));
return view('h5_show', compact('appointment'));
}
/**
* 刷新二维码
*/
public function qrcodeRefresh()
{
$code = request('code');
if (empty($code)) {
return '编码不存在';
}
$appointment = Appointment::where('code', $code)->first();
if (empty($appointment)) {
return '预约不存在';
}
// 获取二维码
$doorRepository = new DoorRepository();
// 获取门禁二维码
$qrcode = $doorRepository->getEmpQrCode($appointment, $out);
return $this->success(compact('qrcode'));
}
}