文件
yjyf/server/application/api/controller/Address.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

123 行
3.9 KiB
PHP

<?php
namespace app\api\controller;
use think\Db;
/**
* 服务地址
*/
class Address extends Base
{
/**
* 地址列表(默认地址置顶)
*/
public function index()
{
$list = Db::name('address')
->where('user_id', $this->uid())
->order('is_default desc,id desc')
->select();
$this->success('', ['list' => $list]);
}
/**
* 地址详情
*/
public function detail()
{
$id = (int)$this->request->param('id');
$row = Db::name('address')->where('id', $id)->where('user_id', $this->uid())->find();
if (!$row) {
$this->error('地址不存在');
}
$this->success('', ['detail' => $row]);
}
/**
* 新增 / 编辑地址
* @ApiMethod (POST)
*/
public function save()
{
$id = (int)$this->request->post('id', 0);
$data = [
'contact' => trim((string)$this->request->post('contact')),
'mobile' => trim((string)$this->request->post('mobile')),
'province' => trim((string)$this->request->post('province')),
'city' => trim((string)$this->request->post('city')),
'district' => trim((string)$this->request->post('district')),
'address' => trim((string)$this->request->post('address')),
'lng' => trim((string)$this->request->post('lng')),
'lat' => trim((string)$this->request->post('lat')),
'floor' => max(1, (int)$this->request->post('floor', 1)),
'has_elevator' => (int)$this->request->post('has_elevator', 1),
'is_default' => (int)$this->request->post('is_default', 0),
'updatetime' => time(),
];
if ($data['contact'] === '' || $data['mobile'] === '' || $data['address'] === '') {
$this->error('请填写联系人、手机号和详细地址');
}
if (!preg_match('/^1\d{10}$/', $data['mobile'])) {
$this->error('手机号格式不正确');
}
if ($id > 0) {
$exist = Db::name('address')->where('id', $id)->where('user_id', $this->uid())->find();
if (!$exist) {
$this->error('地址不存在');
}
Db::name('address')->where('id', $id)->update($data);
} else {
$count = Db::name('address')->where('user_id', $this->uid())->count();
if ($count >= 20) {
$this->error('最多保存 20 个地址');
}
$data['user_id'] = $this->uid();
$data['createtime'] = time();
if ($count === 0) {
$data['is_default'] = 1;
}
$id = Db::name('address')->insertGetId($data);
}
if ($data['is_default'] === 1) {
Db::name('address')->where('user_id', $this->uid())->where('id', '<>', $id)->update(['is_default' => 0]);
}
$this->success('保存成功', ['id' => (int)$id]);
}
/**
* 设为默认
* @ApiMethod (POST)
*/
public function setDefault()
{
$id = (int)$this->request->post('id');
$exist = Db::name('address')->where('id', $id)->where('user_id', $this->uid())->find();
if (!$exist) {
$this->error('地址不存在');
}
Db::name('address')->where('user_id', $this->uid())->update(['is_default' => 0]);
Db::name('address')->where('id', $id)->update(['is_default' => 1]);
$this->success('已设为默认');
}
/**
* 删除地址
* @ApiMethod (POST)
*/
public function delete()
{
$id = (int)$this->request->post('id');
$exist = Db::name('address')->where('id', $id)->where('user_id', $this->uid())->find();
if (!$exist) {
$this->error('地址不存在');
}
Db::name('address')->where('id', $id)->delete();
$this->success('已删除');
}
}