// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\services\user; use app\model\user\UserAddress; use app\services\system\CityAreaServices; use app\validate\api\user\AddressValidate; use qiniu\basic\BaseServices; use qiniu\exceptions\AdminException; use think\db\exception\DataNotFoundException; use think\db\exception\DbException; use think\db\exception\ModelNotFoundException; use think\Exception; use think\exception\ValidateException; use think\Model; /** * * Class UserAddressServices * @package app\services\user * @mixin UserAddress */ class UserAddressServices extends BaseServices { /** * UserAddressServices constructor. * @param UserAddress $model */ public function __construct(UserAddress $model) { $this->model = $model; } /** * 获取单个地址 * @param $id * @param array $field * @return array * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function getAddress($id, $field = []) { return $this->get($id, $field); } /** * 获取所有地址 * @param array $where * @param string $field * @return array * @throws DbException */ public function getAddressList(array $where, string $field = '*'): array { [$page, $limit] = $this->getPageValue(); $list = $this->getList($where, $field, $page, $limit); $count = $this->getCount($where); return compact('list', 'count'); } /** * 获取某个用户的所有地址 * @param int $uid * @param string $field * @return array */ public function getUserAddressList(int $uid, string $field = '*'): array { [$page, $limit] = $this->getPageValue(); $where = ['uid' => $uid]; return $this->getList($where, $field, $page, $limit); } /** * @param int $uid * @param string $field * @return array|Model|null * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function getUserDefaultAddress(int $uid, string $field = '*') { return $this->getOne(['uid' => $uid, 'is_default' => 1], $field); } /** * 添加地址 * @param array $data * @return bool */ public function createAddress(array $data) { if (!$this->save($data)) throw new AdminException('写入失败'); return true; } /** * 修改地址 * @param int $id * @param array $data * @return bool */ public function updateAddress(int $id, array $data) { if (!$this->update($id, $data)) throw new AdminException('修改失败'); return true; } /** * 设置默认定制 * @param int $uid * @param int $id * @return bool * @throws Exception */ public function setDefault(int $uid, int $id) { if (!$this->be($id)) { throw new ValidateException('地址不存在'); } if (!$this->update($uid, ['is_default' => 0], 'uid')) throw new Exception('取消原来默认地址'); if (!$this->update($id, ['is_default' => 1])) throw new Exception('设置默认地址失败'); return true; } /** * 获取单个地址 * @param int $id * @return mixed * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function address(int $id) { $addressInfo = $this->getAddress($id); if (!$addressInfo) { throw new ValidateException('数据不存在'); } return $addressInfo; } /** * 添加|修改地址 * @param int $uid * @param array $addressInfo * @return array * @throws DataNotFoundException * @throws DbException * @throws Exception * @throws ModelNotFoundException */ public function editAddress(int $uid, array $addressInfo) { if ($addressInfo['type'] == 1 && !$addressInfo['id']) { $city = $addressInfo['address']['city']; /** @var CityAreaServices $systemCity */ $systemCity = app()->make(CityAreaServices::class); $cityInfo = $systemCity->getOne([['name', '=', $city], ['parent_id', '<>', 0]]); if ($cityInfo && $cityInfo['code']) { $addressInfo['address']['city_id'] = $cityInfo['code']; } else { $cityInfo = $systemCity->getOne([['name', 'like', "%$city%"], ['parent_id', '<>', 0]]); if (!$cityInfo) { throw new ValidateException('收货地址格式错误!修改后请重新导入!'); } $addressInfo['address']['city_id'] = $cityInfo['code']; } $addressInfo['province_id'] = $systemCity->getOne(['id' => $cityInfo['pid']])['code'] ?? ''; } if (!isset($addressInfo['address']['city_id']) || $addressInfo['address']['city_id'] == 0) throw new ValidateException('添加收货地址失败!'); $addressInfo['province'] = $addressInfo['address']['province'] ?? ''; $addressInfo['city'] = $addressInfo['address']['city'] ?? ''; $addressInfo['city_id'] = $addressInfo['address']['city_id'] ?? 0; $addressInfo['district'] = $addressInfo['address']['district'] ?? ''; $addressInfo['street'] = $addressInfo['address']['street'] ?? ''; $addressInfo['is_default'] = (int)$addressInfo['is_default'] == true ? 1 : 0; $addressInfo['uid'] = $uid; unset($addressInfo['address'], $addressInfo['type']); //数据验证 validate(AddressValidate::class)->check($addressInfo); $address_check = []; if ($addressInfo['id']) { $address_check = $this->getAddress((int)$addressInfo['id']); } if ($address_check && $address_check['is_del'] == 0 && $address_check['uid'] = $uid) { $id = (int)$addressInfo['id']; unset($addressInfo['id']); if (!$this->update($id, $addressInfo, 'id')) { throw new ValidateException('编辑收货地址失败'); } if ($addressInfo['is_default']) { $this->setDefault($uid, $id); } return ['type' => 'edit', 'msg' => '编辑地址成功', 'data' => []]; } else { $addressInfo['add_time'] = time(); if (!$address = $this->create($addressInfo)) { throw new ValidateException('添加收货地址失败'); } if ($addressInfo['is_default']) { $this->setDefault($uid, (int)$address->id); } return ['type' => 'add', 'msg' => '添加地址成功', 'data' => ['id' => $address->id]]; } } /** * 删除地址 * @param int $uid * @param int $id * @return bool * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function delAddress(int $uid, int $id) { $addressInfo = $this->getAddress($id); if (!$addressInfo || $addressInfo['uid'] != $uid) { throw new ValidateException('数据不存在'); } if ($this->delete($id)) { return true; } else throw new ValidateException('删除地址失败!'); } /** * 设置默认用户地址 * @param int $id * @param int $uid * @return bool */ public function setDefaultAddress(int $id, int $uid) { $res1 = $this->update($uid, ['is_default' => 0], 'uid'); $res2 = $this->update(['id' => $id, 'uid' => $uid], ['is_default' => 1]); return $res1 !== false && $res2 !== false; } }