auth->id; if (!$userId) { $this->error('请先登录'); } $list = Db::name('user_address') ->where('user_id', $userId) ->where('uniacid', UNIACID) ->order('is_default', 'desc') ->order('id', 'desc') ->select(); $this->success('获取成功', $list); } /** * 获取默认地址 * @return void */ public function default() { $userId = $this->auth->id; if (!$userId) { $this->error('请先登录'); } $address = Db::name('user_address') ->where('user_id', $userId) ->where('uniacid', UNIACID) ->where('is_default', 1) ->find(); if (!$address) { $this->error('暂无默认地址'); } $this->success('获取成功', $address); } /** * 获取地址详情 * @return void */ public function detail() { $userId = $this->auth->id; if (!$userId) { $this->error('请先登录'); } $id = $this->request->param('id'); if (empty($id)) { $this->error('参数错误'); } $address = Db::name('user_address') ->where('id', $id) ->where('user_id', $userId) ->where('uniacid', UNIACID) ->find(); if (!$address) { $this->error('地址不存在'); } $this->success('获取成功', $address); } /** * 添加/编辑地址 * @return void */ public function save() { $params = $this->request->param(); // 验证规则 $rule = [ 'name' => 'require|max:50', 'mobile' => 'require|regex:/^1[3-9]\d{9}$/', 'province' => 'require|max:50', 'city' => 'require|max:50', 'district' => 'require|max:50', 'address' => 'require|max:255', ]; $msg = [ 'name.require' => '收货人姓名不能为空', 'name.max' => '收货人姓名不能超过50个字符', 'mobile.require' => '手机号码不能为空', 'mobile.regex' => '手机号码格式不正确', 'province.require' => '省份不能为空', 'city.require' => '城市不能为空', 'district.require' => '区县不能为空', 'address.require' => '详细地址不能为空', 'address.max' => '详细地址不能超过255个字符', ]; $validate = new Validate($rule, $msg); if (!$validate->check($params)) { $this->error($validate->getError()); } $id = $params['id'] ?? 0; $isDefault = isset($params['is_default']) ? intval($params['is_default']) : 0; $data = [ 'user_id' => $this->auth->id, 'uniacid' => UNIACID, 'name' => $params['name'], 'mobile' => $params['mobile'], 'province' => $params['province'], 'city' => $params['city'], 'district' => $params['district'], 'address' => $params['address'], 'is_default' => $isDefault, 'updatetime' => time(), ]; Db::startTrans(); try { if ($id) { // 编辑 $exists = Db::name('user_address') ->where('id', $id) ->where('user_id', $this->auth->id) ->where('uniacid', UNIACID) ->find(); if (!$exists) { throw new \Exception('地址不存在'); } // 如果设置为默认地址,取消其他默认地址 if ($isDefault == 1) { Db::name('user_address') ->where('user_id', $this->auth->id) ->where('uniacid', UNIACID) ->update(['is_default' => 0]); } Db::name('user_address') ->where('id', $id) ->update($data); } else { // 新增 - 检查地址数量限制 $addressCount = Db::name('user_address') ->where('user_id', $this->auth->id) ->where('uniacid', UNIACID) ->count(); if ($addressCount >= 10) { throw new \Exception('最多只能添加10个收货地址'); } // 如果设置为默认地址,取消其他默认地址 if ($isDefault == 1) { Db::name('user_address') ->where('user_id', $this->auth->id) ->where('uniacid', UNIACID) ->update(['is_default' => 0]); } // 新增 $data['createtime'] = time(); $id = Db::name('user_address')->insertGetId($data); } Db::commit(); } catch (\Exception $e) { Db::rollback(); $this->error($e->getMessage()); } $this->success('保存成功', ['id' => $id]); } /** * 删除地址 * @return void */ public function delete() { $userId = $this->auth->id; if (!$userId) { $this->error('请先登录'); } $id = $this->request->param('id'); if (empty($id)) { $this->error('参数错误'); } $address = Db::name('user_address') ->where('id', $id) ->where('user_id', $userId) ->where('uniacid', UNIACID) ->find(); if (!$address) { $this->error('地址不存在'); } $result = Db::name('user_address') ->where('id', $id) ->where('user_id', $userId) ->where('uniacid', UNIACID) ->delete(); if ($result) { $this->success('删除成功'); } else { $this->error('删除失败'); } } /** * 设置默认地址 * @return void */ public function setDefault() { $userId = $this->auth->id; if (!$userId) { $this->error('请先登录'); } $id = $this->request->param('id'); if (empty($id)) { $this->error('参数错误'); } $address = Db::name('user_address') ->where('id', $id) ->where('user_id', $userId) ->where('uniacid', UNIACID) ->find(); if (!$address) { $this->error('地址不存在'); } Db::startTrans(); try { // 取消其他默认地址 Db::name('user_address') ->where('user_id', $userId) ->where('uniacid', UNIACID) ->update(['is_default' => 0]); // 设置当前为默认 Db::name('user_address') ->where('id', $id) ->update([ 'is_default' => 1, 'updatetime' => time() ]); Db::commit(); } catch (\Exception $e) { Db::rollback(); $this->error($e->getMessage()); } $this->success('设置成功'); } /** * 智能识别地址 * @return void */ public function parse() { $userId = $this->auth->id; if (!$userId) { $this->error('请先登录'); } $text = $this->request->param('text'); if (empty($text)) { $this->error('请输入地址文本'); } // 简单的地址识别逻辑 $result = $this->parseAddress($text); $this->success('识别成功', $result); } /** * 解析地址文本 * @param string $text * @return array */ private function parseAddress($text) { $result = [ 'name' => '', 'mobile' => '', 'province' => '', 'city' => '', 'district' => '', 'address' => '' ]; // 提取手机号 if (preg_match('/1[3-9]\d{9}/', $text, $matches)) { $result['mobile'] = $matches[0]; $text = str_replace($matches[0], '', $text); } // 提取省市区 $provincePattern = '/(北京|天津|上海|重庆|河北|山西|辽宁|吉林|黑龙江|江苏|浙江|安徽|福建|江西|山东|河南|湖北|湖南|广东|海南|四川|贵州|云南|陕西|甘肃|青海|台湾|内蒙古|广西|西藏|宁夏|新疆|香港|澳门)/'; if (preg_match($provincePattern, $text, $matches)) { $result['province'] = $matches[0]; $text = str_replace($matches[0], '', $text); } // 提取市 $cityPattern = '/(市|地区|自治州|盟)/'; if (preg_match('/([^省]+' . $cityPattern . ')/u', $text, $matches)) { $result['city'] = str_replace(['市', '地区', '自治州', '盟'], '', $matches[0]); $text = str_replace($matches[0], '', $text); } // 提取区县 $districtPattern = '/(区|县|市|旗|镇)/'; if (preg_match('/([^市区县]+' . $districtPattern . ')/u', $text, $matches)) { $result['district'] = str_replace(['区', '县', '旗', '镇'], '', $matches[0]); $text = str_replace($matches[0], '', $text); } // 剩余部分作为详细地址 $result['address'] = trim($text); // 尝试提取姓名(通常是开头或结尾的2-4个汉字) $textWithoutMobile = str_replace($result['mobile'], '', $this->request->param('text')); if (preg_match('/^([\x{4e00}-\x{9fa5}]{2,4})/u', $textWithoutMobile, $matches)) { $result['name'] = $matches[1]; } return $result; } }