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.

273 lines
10 KiB

3 years ago
<?php
namespace App\Http\Controllers\Mobile;
use App\Helpers\ResponseCode;
use App\Helpers\StarterResponseCode;
3 years ago
use App\Models\Config;
3 years ago
use App\Models\User;
1 month ago
use App\Models\VipCustomer;
3 years ago
use App\Models\Visit;
use EasyWeChat\Factory;
5 days ago
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
3 years ago
use Illuminate\Support\Facades\Validator;
class UserController extends CommonController
{
/**
* @OA\Get(
* path="/api/mobile/user/login",
* tags={"小程序-用户管理"},
* summary="登陆",
* description="",
* @OA\Parameter(name="code", in="query", @OA\Schema(type="string"), required=true, description="code"),
* @OA\Response(
* response="200",
* description="小程序登陆"
* )
* )
*/
public function login()
{
$all = \request()->all();
$messages = [
'code.required' => 'code必填',
];
$validator = Validator::make($all, [
'code' => 'required',
], $messages);
if ($validator->fails()) {
return $this->fail([StarterResponseCode::START_ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
// 获取配置信息
$appid = \config('app.wechat_appid');
$appSecret = \config('app.wechat_appsecret');
5 days ago
$url = "https://api.weixin.qq.com/sns/jscode2session";
$userInfo = null;
try {
$response = Http::timeout(10)->get($url, [
'appid' => $appid,
'secret' => $appSecret,
'js_code' => $all['code'],
'grant_type' => 'authorization_code',
]);
$userInfo = $response->json();
} catch (\Throwable $e) {
Log::error('miniapp login jscode2session request failed', [
'message' => $e->getMessage(),
'appid' => $appid,
]);
return $this->fail([ResponseCode::ERROR_BUSINESS, '微信登录服务不可用']);
}
3 years ago
if (!isset($userInfo['openid'])) {
5 days ago
Log::warning('miniapp login jscode2session invalid response', [
'appid' => $appid,
'wx_errcode' => $userInfo['errcode'] ?? null,
'wx_errmsg' => $userInfo['errmsg'] ?? null,
'response' => $userInfo,
]);
$msg = $userInfo['errmsg'] ?? 'code异常';
return $this->fail([ResponseCode::ERROR_BUSINESS, "code异常:{$msg}"]);
3 years ago
}
$openid = $userInfo['openid'];
//判断该用户是否在数据库
$user = User::where('openid', $openid)->first();
if (empty($user)) {
// 增加用户记录
$user = (new User())->fill(['openid' => $openid])->save();
}
$token = $user->createToken('mobile-token')->plainTextToken;
return $this->success(['token' => $token]);
}
/**
* @OA\Post(
* path="/api/mobile/user/save",
* tags={"小程序-用户管理"},
* summary="更新用户信息",
* description="",
* @OA\Parameter(name="nickname", in="query", @OA\Schema(type="string"), required=false, description="昵称"),
* @OA\Parameter(name="sex", in="query", @OA\Schema(type="int"), required=false, description="性别"),
* @OA\Parameter(name="mobile", in="query", @OA\Schema(type="string"), required=false, description="手机号"),
* @OA\Parameter(name="country", in="query", @OA\Schema(type="string"), required=false, description="国家"),
* @OA\Parameter(name="province", in="query", @OA\Schema(type="string"), required=false, description="省市"),
* @OA\Parameter(name="city", in="query", @OA\Schema(type="string"), required=false, description="城市"),
* @OA\Parameter(name="headimgurl", in="query", @OA\Schema(type="string"), required=false, description="头像地址"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description=""
* )
* )
*/
public function save()
{
$all = \request()->all();
$model = User::find($this->getUserId());
$model->fill($all);
$model->save();
return $this->success("更新成功");
}
/**
* @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.wechat_appid'),
'secret' => \config('app.wechat_appsecret')
];
$app = Factory::miniProgram($config);
$result = $app->phone_number->getUserPhoneNumber($all['code']);
$mobile = $result['phone_info']['purePhoneNumber'] ?? '';
User::where('id', $this->getUserId())->update(['mobile' => $mobile]);
return $this->success(['mobile' => $mobile]);
}
/**
* @OA\Get(
* path="/api/mobile/user/show",
* tags={"小程序-用户管理"},
* summary="获取用户信息",
* description="",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description=""
* )
* )
*/
public function show()
{
1 month ago
$user = $this->guard()->user();
$isVip = false;
if (!empty($user->mobile)) {
$isVip = VipCustomer::where('mobile', $user->mobile)->where('status', 1)->exists();
}
return $this->success([
...$user->toArray(),
'is_vip' => $isVip ? 1 : 0,
]);
}
public function isVip()
{
$all = request()->all();
$validator = Validator::make($all, [
'mobile' => 'required',
], [
'mobile.required' => '手机号必填',
]);
if ($validator->fails()) {
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
$isVip = VipCustomer::where('mobile', $all['mobile'])->where('status', 1)->exists();
return $this->success(['is_vip' => $isVip ? 1 : 0]);
3 years ago
}
3 years ago
/**
* @OA\Get(
* path="/api/mobile/user/my-visit",
* tags={"小程序-拜访列表"},
* summary="列表",
* description="",
* @OA\Parameter(name="type", in="query", @OA\Schema(type="string"), required=false, description="类型1访客2施工3车辆"),
* @OA\Parameter(name="page_size", in="query", @OA\Schema(type="string"), required=false, description="每页显示的条数"),
* @OA\Parameter(name="page", in="query", @OA\Schema(type="string"), required=false, description="页码"),
* @OA\Parameter(name="sort_name", in="query", @OA\Schema(type="string"), required=false, description="排序字段名字"),
* @OA\Parameter(name="sort_type", in="query", @OA\Schema(type="string"), required=false, description="排序类型"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function myVisit()
{
$all = request()->all();
3 years ago
$list = Visit::with('accompany.department','logs.admin', 'logs.user', 'visitTime', 'admin', 'visitArea', 'acceptAdmin.department', 'acceptAdminSignFile', 'acceptGoodsAdmin.department')->where('user_id', $this->getUserId())
3 years ago
->where(function ($query) use ($all) {
if (isset($all['type'])) {
$query->where('type', $all['type']);
}
})->orderBy($all['sort_name'] ?? 'id', $all['sort_type'] ?? 'desc')
->paginate($all['page_size'] ?? 20);
return $this->success($list);
}
/**
* @OA\Get(
* path="/api/mobile/user/my-visit-detail",
* tags={"小程序-拜访详情"},
* summary="列表",
* description="",
* @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=false, description="id"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function myVisitDetail()
{
$all = \request()->all();
$messages = [
'id.required' => 'Id必填',
];
$validator = Validator::make($all, [
'id' => 'required'
], $messages);
if ($validator->fails()) {
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
3 years ago
$detail = Visit::with('accompany.department','logs.admin', 'logs.user', 'visitTime', 'admin', 'visitArea', 'acceptAdmin.department', 'acceptAdminSignFile', 'acceptGoodsAdmin.department')->find($all['id']);
3 years ago
return $this->success($detail);
}
3 years ago
/**
* @OA\Get(
* path="/api/mobile/user/config",
* tags={"小程序-用户管理"},
* summary="配置信息",
* description="",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function config()
{
$list = Config::get();
return $this->success($list);
}
3 years ago
}