文件
yjyf/server/application/api/controller/Auth.php
linda 376ce7b5c9 feat(api): 全链路后端 + 数据库设计 + E2E 测试(65 项断言全通过)
数据库:
- db/schema_phase1.sql  16 张业务表(类目/服务/地址/师傅/订单/流水/取证/报价/评价/券/钱包/提现/结算/消息)
- db/seed_phase1.sql    15 类目 + 32 服务项目 + 新人券(幂等)

后端 API (application/api/controller):
- Base/Auth/Home/Address/Order/Master/Review
- app/common/library/OrderStatus  11 状态机 + 流转矩阵
- application/api/common.php  PHP 8.5 兼容垫片(E_DEPRECATED 不再升级为异常)

测试:
- tests/e2e_flow.js  用户下单→支付→师傅抢单→履约→验收→结算→评价 全链路 65 项断言
- 含异常用例:越权/重复操作/非法状态流转/2h 未联系自动回流
2026-10-28 15:45:51 +08:00

103 行
3.3 KiB
PHP

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?php
namespace app\api\controller;
use fast\Random;
use think\Db;
use think\Validate;
/**
* 登录 / 角色 / 个人信息
*/
class Auth extends Base
{
protected $noNeedLogin = ['login'];
/**
* 手机号 + 验证码登录(验证码固定 123456,联调用;接入短信后走 Sms)
* @ApiMethod (POST)
*/
public function login()
{
$mobile = $this->request->post('mobile');
$code = $this->request->post('code');
if (!$mobile || !$code) {
$this->error('请填写手机号和验证码');
}
if (!Validate::regex($mobile, "^1\d{10}$")) {
$this->error('手机号格式不正确');
}
if ($code !== '123456') {
$this->error('验证码不正确');
}
$user = \app\common\model\User::getByMobile($mobile);
if ($user) {
if ($user['status'] !== 'normal') {
$this->error('账号已被禁用');
}
$this->auth->direct($user['id']);
} else {
$ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
if (!$ret) {
$this->error($this->auth->getError() ?: '注册失败');
}
}
$this->success('登录成功', [
'token' => $this->auth->getToken(),
'expires' => 2592000,
'userinfo' => $this->profileData(),
]);
}
/**
* 我的信息(含角色与师傅状态)
*/
public function profile()
{
$this->success('', $this->profileData());
}
/**
* 组装个人信息
*/
protected function profileData()
{
$uid = $this->uid();
$user = Db::name('user')->field('id,nickname,mobile,avatar,money,score,createtime')->where('id', $uid)->find();
$master = Db::name('master')->where('user_id', $uid)->find();
$couponCount = Db::name('user_coupon')->where('user_id', $uid)->where('status', 'unused')->count();
$unread = Db::name('message')->where('receive_type', 'user')->where('receive_id', $uid)->where('is_read', 0)->count();
$masterInfo = null;
if ($master) {
$masterInfo = [
'id' => (int)$master['id'],
'realname' => $master['realname'],
'verify_status' => $master['verify_status'],
'audit_msg' => $master['audit_msg'],
'work_status' => $master['work_status'],
'level' => (int)$master['level'],
'score' => (float)$master['score'],
'order_count' => (int)$master['order_count'],
'money' => (float)$master['money'],
'can_take' => $master['verify_status'] === 'pass',
];
}
return [
'id' => (int)$user['id'],
'nickname' => $user['nickname'] ?: ('用户' . substr((string)$user['mobile'], -4)),
'mobile' => $user['mobile'],
'avatar' => $user['avatar'] ?: '',
'balance' => (float)$user['money'],
'coupon_count' => (int)$couponCount,
'unread_count' => (int)$unread,
'is_master' => $master ? true : false,
'master' => $masterInfo,
];
}
}