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.

249 lines
9.4 KiB

<?php
namespace App\Http\Controllers\Mobile;
use App\Helpers\ResponseCode;
use App\Helpers\StarterResponseCode;
use App\Models\Config;
use App\Models\User;
use App\Models\VipCustomer;
use App\Models\Visit;
use EasyWeChat\Factory;
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');
$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_BUSINESS, 'code异常']);
}
$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()
{
$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]);
}
/**
* @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();
$list = Visit::with('accompany.department','logs.admin', 'logs.user', 'visitTime', 'admin', 'visitArea', 'acceptAdmin.department', 'acceptAdminSignFile', 'acceptGoodsAdmin.department')->where('user_id', $this->getUserId())
->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())]);
}
$detail = Visit::with('accompany.department','logs.admin', 'logs.user', 'visitTime', 'admin', 'visitArea', 'acceptAdmin.department', 'acceptAdminSignFile', 'acceptGoodsAdmin.department')->find($all['id']);
return $this->success($detail);
}
/**
* @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);
}
}