2026-09-17 16:25:31 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\api\controller;
|
|
|
|
|
|
|
|
|
|
use app\common\controller\Api;
|
|
|
|
|
use app\common\model\User;
|
2026-10-14 14:44:01 +08:00
|
|
|
use app\common\service\PromotionService;
|
2026-09-17 16:25:31 +08:00
|
|
|
use app\common\service\UserService;
|
|
|
|
|
use think\exception\HttpResponseException;
|
|
|
|
|
use think\Validate;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 登录接口
|
|
|
|
|
*/
|
|
|
|
|
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);
|
2026-10-14 14:44:01 +08:00
|
|
|
//通过分享链接/小程序码进入时携带推广人ID,首次进入即绑定
|
|
|
|
|
PromotionService::bindPromoter(intval($user->id), intval($this->request->post('promoter_id', 0)));
|
2026-09-17 16:25:31 +08:00
|
|
|
$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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|