Initial commit
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\common\controller\Api;
|
||||
use app\common\model\User;
|
||||
use app\common\service\UserService;
|
||||
use think\exception\HttpResponseException;
|
||||
use think\Validate;
|
||||
use EasyWeChat\Factory;
|
||||
|
||||
/**
|
||||
* 登录接口
|
||||
*/
|
||||
class Auth extends Api
|
||||
{
|
||||
protected $noNeedLogin = ['wxLogin'];
|
||||
protected $noNeedRight = ['*'];
|
||||
|
||||
/**
|
||||
* 手机号登录
|
||||
*
|
||||
* @param string $mobile 手机号
|
||||
* @param string $code 验证码
|
||||
*/
|
||||
public function mobileLogin()
|
||||
{
|
||||
$mobile = $this->request->post('mobile');
|
||||
$code = $this->request->post('code');
|
||||
|
||||
$validate = new Validate([
|
||||
'mobile' => 'require|regex:/^1[3-9]\d{9}$/',
|
||||
'code' => 'require|number|length:6'
|
||||
]);
|
||||
|
||||
if (!$validate->check(['mobile' => $mobile, 'code' => $code])) {
|
||||
$this->error($validate->getError());
|
||||
}
|
||||
|
||||
// 验证码校验逻辑 - 此处需要和您的验证码发送系统对接
|
||||
if (!validate_sms_code($mobile, $code)) {
|
||||
$this->error('验证码错误');
|
||||
}
|
||||
|
||||
$user = User::where('mobile', $mobile)->find();
|
||||
if (!$user) {
|
||||
// 新用户自动注册
|
||||
$user = new User;
|
||||
$user->mobile = $mobile;
|
||||
$user->nickname = substr($mobile, 0, 3) . '****' . substr($mobile, -4);
|
||||
$user->save();
|
||||
}
|
||||
|
||||
$this->auth->direct($user->id);
|
||||
$this->success('登录成功', ['userinfo' => $user, 'token' => $this->auth->getToken()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信小程序登录
|
||||
*/
|
||||
public function wxLogin(): void
|
||||
{
|
||||
try {
|
||||
$code = $this->request->post('code');
|
||||
if (!$code) {
|
||||
$this->error('参数错误');
|
||||
}
|
||||
$user = UserService::wxLogin($code);
|
||||
$this->auth->direct($user->id);
|
||||
$userInfo = $this->auth->getUserinfo();
|
||||
$this->success('登录成功', ['userinfo' => $this->auth->getUserinfo(), 'token' => $userInfo['token']]);
|
||||
} catch (\Exception $e) {
|
||||
if ($e instanceof HttpResponseException) {
|
||||
throw $e;
|
||||
}
|
||||
$this->error('微信登录异常:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信小程序更新用户信息
|
||||
*/
|
||||
public function updateUserInfo()
|
||||
{
|
||||
try {
|
||||
$userInfo = $this->request->post();
|
||||
if (!$this->auth->isLogin()) {
|
||||
$this->error('请先登录');
|
||||
}
|
||||
|
||||
$user = $this->auth->getUser();
|
||||
$updateData = array_filter([
|
||||
'nickname' => $userInfo['nickname'] ?? '',
|
||||
'avatar' => $userInfo['avatar'] ?? '',
|
||||
'gender' => $userInfo['gender'] ?? '',
|
||||
'mobile' => $userInfo['mobile'] ?? '',
|
||||
]);
|
||||
|
||||
if ($updateData && $user->save($updateData)) {
|
||||
$this->success('更新成功');
|
||||
} else {
|
||||
$this->error('更新失败');
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
if ($e instanceof HttpResponseException) {
|
||||
throw $e;
|
||||
}
|
||||
$this->error('微信登录异常:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user