diff --git a/app/api/controller/UserAddressController.php b/app/api/controller/UserAddressController.php new file mode 100644 index 0000000..da92704 --- /dev/null +++ b/app/api/controller/UserAddressController.php @@ -0,0 +1,157 @@ +user_id; + $result = UserAddressLogic::infoUserAddress($user_id); + $this->_success('获取成功', $result); + } + + + + //获取一条地址详情 + public function detail() + { + $get = $this->request->get(); + $result = $this->validate($get, 'app\api\validate\UserAddress.one'); + if ($result === true) { + $user_id = $this->user_id; + $result = UserAddressLogic::getOneAddress($user_id, $get); + if ($result) { + $this->_success('获取成功', $result); + } + $result = '获取失败'; + } + $this->_error($result); + } + + + //获取默认地址 + public function getDefault() + { + $user_id = $this->user_id; + $result = UserAddressLogic::getDefaultAddress($user_id); + if ($result) { + $this->_success('获取成功', $result); + } + $this->_error('获取失败'); + } + + + //设置默认地址 + public function setDefault() + { + $post = $this->request->post(); + $result = $this->validate($post, 'app\api\validate\UserAddress.set'); + if ($result === true) { + $user_id = $this->user_id; + $result = UserAddressLogic::setDefaultAddress($user_id, $post); + if ($result) { + $this->_success('设置成功', $result); + } + $result = '设置失败'; + } + $this->_error($result); + } + + + + //添加收货地址 + public function add() + { + $post = $this->request->post(); + $result = $this->validate($post, 'app\api\validate\UserAddress.add'); + if ($result === true) { + $user_id = $this->user_id; + $result = UserAddressLogic::addUserAddress($user_id, $post); + if ($result) { + $this->_success('添加成功', $result); + } + $result = '添加失败'; + } + $this->_error($result); + } + + + //更新收货地址 + public function update() + { + $post = $this->request->post(); + $result = $this->validate($post, 'app\api\validate\UserAddress.edit'); + if ($result === true) { + $user_id = $this->user_id; + $result = UserAddressLogic::editUserAddress($user_id, $post); + if ($result) { + $this->_success('修改成功', $result); + } + $result = '修改失败'; + } + $this->_error($result); + } + + + //删除收货地址 + public function del() + { + $post = $this->request->post(); + $result = $this->validate($post, 'app\api\validate\UserAddress.del'); + if ($result === true) { + $user_id = $this->user_id; + $result = UserAddressLogic::delUserAddress($user_id, $post); + if ($result) { + $this->_success('删除成功', $result); + } + $result = '删除失败'; + } + $this->_error($result); + } + + + //将省市区名称转换成省市区id + public function handleRegion() + { + $post = $this->request->post(); + $result = $this->validate($post, 'app\api\validate\UserAddress.handleRegion'); + if ($result === true) { + $province = $this->request->post('province'); + $city = $this->request->post('city'); + $district = $this->request->post('district'); + $res = UserAddressLogic::handleRegion($province, $city, $district); + if ($res) { + $this->_success('获取成功', $res); + } + $result = '获取失败'; + } + $this->_error($result); + } + +} \ No newline at end of file diff --git a/app/api/logic/UserAddressLogic.php b/app/api/logic/UserAddressLogic.php new file mode 100644 index 0000000..95bd27c --- /dev/null +++ b/app/api/logic/UserAddressLogic.php @@ -0,0 +1,340 @@ +where(['user_id'=>$user_id,'del'=>0]) + ->field('id,contact,telephone,province_id,city_id,district_id,address,is_default') + ->select(); + foreach ($info as &$item) { + $item['province'] = AreaServer::getAddress($item['province_id']); + $item['city'] = AreaServer::getAddress($item['city_id']); + $item['district'] = AreaServer::getAddress($item['district_id']); + } + return $info; + } + + /** + * 获取一条地址信息 + * @param $user_id + * @param $get + * @return array|\PDOStatement|string|\think\Model|null + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public static function getOneAddress($user_id,$get) { + $info = Db::name('user_address') + ->where(['id'=>$get['id'],'user_id'=>$user_id,'del'=>0]) + ->field('id,contact,telephone,province_id,city_id,district_id,address,is_default') + ->find(); + + $info['province'] = AreaServer::getAddress($info['province_id']); + $info['city'] = AreaServer::getAddress($info['city_id']); + $info['district'] = AreaServer::getAddress($info['district_id']); + return $info; + } + + /** + * 获取默认地址 + * @param $user_id + * @return array|\PDOStatement|string|\think\Model|null + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public static function getDefaultAddress($user_id){ + $info = Db::name('user_address') + ->where(['is_default'=>1,'user_id'=>$user_id,'del'=>0]) + ->field('id,contact,telephone,province_id,city_id,district_id,address,is_default') + ->find(); + + if (!$info){ + return []; + } + + $info['province'] = AreaServer::getAddress($info['province_id']); + $info['city'] = AreaServer::getAddress($info['city_id']); + $info['district'] = AreaServer::getAddress($info['district_id']); + + return $info; + } + + /** + * 设置默认地址 + * @param $user_id + * @param $post + * @return int|string + */ + public static function setDefaultAddress($user_id,$post){ + + try { + Db::startTrans(); + + Db::name('user_address') + ->where(['del'=> 0 ,'user_id'=>$user_id]) + ->update(['is_default'=>0]); + + $result = Db::name('user_address') + ->where(['id'=>$post['id'],'del'=> 0 ,'user_id'=>$user_id]) + ->update(['is_default'=>1]); + + Db::commit(); + } catch (Exception $e) { + Db::rollback(); + return false; + } + + return $result; + } + + /** + * 添加收货地址 + * @param $user_id + * @param $post + * @return int|string + */ + public static function addUserAddress($user_id,$post) { + try { + Db::startTrans(); + + if (isset($post['is_default']) && $post['is_default'] == 1){ + Db::name('user_address') + ->where(['del'=> 0 ,'user_id'=>$user_id]) + ->update(['is_default'=>0]); + }else{ + $is_first = Db::name('user_address') + ->where(['del'=> 0 ,'user_id'=>$user_id]) + ->find(); + if (empty($is_first)){ + $post['is_default'] = 1; + } + } + + $data = [ + 'user_id' => $user_id, + 'contact' => $post['contact'], + 'telephone' => $post['telephone'], + 'province_id' => $post['province_id'], + 'city_id' => $post['city_id'], + 'district_id' => $post['district_id'], + 'address' => $post['address'], + 'is_default' => $post['is_default'] ?? 0, + 'create_time' => time() + ]; + + $result = Db::name('user_address')->insert($data); + + Db::commit(); + } catch (Exception $e) { + Db::rollback(); + return $e->getMessage(); + } + + return $result; + } + + /** + * 编辑用户地址 + * @param $user_id + * @param $post + * @return int|string + * @throws \think\Exception + * @throws \think\exception\PDOException + */ + public static function editUserAddress($user_id,$post) { + + try { + Db::startTrans(); + + if ($post['is_default'] == 1){ + Db::name('user_address') + ->where(['del'=> 0 ,'user_id'=>$user_id]) + ->update(['is_default'=>0]); + } + + $data = [ + 'contact' => $post['contact'], + 'telephone' => $post['telephone'], + 'province_id' => $post['province_id'], + 'city_id' => $post['city_id'], + 'district_id' => $post['district_id'], + 'address' => $post['address'], + 'is_default' => $post['is_default'] ?? 0, + 'update_time' => time() + ]; + + $result = Db::name('user_address') + ->where(['id'=>$post['id'],'del'=> 0 ,'user_id'=>$user_id]) + ->update($data); + + Db::commit(); + } catch (Exception $e) { + Db::rollback(); + return false; + } + + return $result; + } + + /** + * 删除用户地址 + * @param $user_id + * @param $post + * @return int|string + * @throws \think\Exception + * @throws \think\exception\PDOException + */ + public static function delUserAddress($user_id,$post) { + + $data = [ + 'del' => 1, + 'update_time' => time() + ]; + + return Db::name('user_address') + ->where(['id'=>$post['id'],'del'=>0,'user_id'=>$user_id]) + ->update($data); + } + + + /** + * 获取省市区id + * @param $province + * @param $city + * @param $district + * @return array + */ + public static function handleRegion($province,$city,$district){ + if (!$province || !$city || !$district){ + return []; + } + $result = []; + $result['province'] = self::handleRegionField($province,1); + if (!$result['province']){ + return []; + } + $result['city'] = self::handleRegionField($city,2); + $result['district'] = self::handleRegionField($district,3); + + return $result; + } + + /** + * 获取对应省,市,区的id + * @param $keyword + * @param int $level + * @return mixed|string + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public static function handleRegionField($keyword, $level = 1){ + $data = ''; + $list = Db::name('dev_region')->where('level',$level)->select(); + foreach ($list as $k => $v){ + if ($keyword == $v['name']){ + $data = $v['id']; + } + } + if (empty($data)) { + foreach ($list as $k => $v) { + if (strpos($v['name'], $keyword) !== false) { + $data = $v['id']; + } + } + if (empty($data)) { + foreach ($list as $v) { + if (strpos($keyword, $v['name']) !== false) { + $data = $v['id']; + } + } + } + } + return $data; + } + + + /** + * User: 意象信息科技 mjf + * Desc: 获取用户指定id的地址 + * @param $address + * @param $user_id + * @return array|\PDOStatement|string|\think\Model|null + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public static function getUserAddressById($address, $user_id) + { + $info = Db::name('user_address') + ->where(['id' => $address, 'user_id' => $user_id, 'del' => 0]) + ->field('id,contact,telephone,province_id,city_id,district_id,address,is_default') + ->find(); + + if (!$info) { + return []; + } + + $info['province'] = AreaServer::getAddress($info['province_id']); + $info['city'] = AreaServer::getAddress($info['city_id']); + $info['district'] = AreaServer::getAddress($info['district_id']); + + return $info; + } + + + //获取订单用户地址 + + /** + * User: 意象信息科技 mjf + * Desc: 获取下单时用户地址 + * @param $data + * @param $user_id + * @return array|\PDOStatement|string|\think\Model|null + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public static function getOrderUserAddress($data, $user_id) + { + if (isset($data['address_id']) && $data['address_id'] != 0){ + return self::getUserAddressById($data['address_id'], $user_id); + } + return self::getDefaultAddress($user_id); + } +} \ No newline at end of file diff --git a/app/common/service/AreaServer.php b/app/common/service/AreaServer.php new file mode 100644 index 0000000..c124bee --- /dev/null +++ b/app/common/service/AreaServer.php @@ -0,0 +1,64 @@ +set(3600); + if (!is_array($val)) { + return isset($area_id_name[$val]) ? $area_id_name[$val] : ''; + } + $long_address = ''; + foreach ($val as $id) { + $long_address .= isset($area_id_name[$id]) ? $area_id_name[$id] : ''; + } + return $long_address . $address; + } + + /** + * 通过id获取地址经纬度上 + * @param $id + * @return array|\PDOStatement|string|\think\Model|null + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public static function getDb09LngAndLat($id) + { + return Db::name('dev_region') + ->where('id', '=', $id) + ->field(['db09_lng', 'db09_lat']) + ->find(); + } +} \ No newline at end of file diff --git a/app/common/service/cache/CacheBase.php b/app/common/service/cache/CacheBase.php new file mode 100644 index 0000000..fbddb95 --- /dev/null +++ b/app/common/service/cache/CacheBase.php @@ -0,0 +1,125 @@ +tag = $this->setTag(); + $this->name = $this->tag . '_' . $key; + $this->extend = $extend; + } + + + public abstract function setTag(); + + /** + * 子类实现查出数据 + * @return mixed + */ + public abstract function setData(); + + /** + * 创建并获取数据 + * @param null $expire + * @return mixed + */ + public function set($expire = null) + { + $data = self::cacheGet($this->name); + if ($data !== false) { + return $data; + } + $data = $this->setData(); + self::cacheSet($this->name, $data, $expire); + return $data; + } + + /** + * User: 意象信息科技 lr + * Desc: 获取数据 + * @return mixed + */ + public function get() + { + return self::cacheGet($this->name); + } + + + /** + * 判断数据是否为空 + * @return bool + */ + public function isEmpty() + { + $data = self::cacheGet($this->name); + if ($data !== false) { + return false; + } + return true; + } + + /** + * 删除数据 + * @return bool + */ + public function del() + { + return self::cacheRm($this->name); + } + + public function delAll() + { + return Cache::clear($this->tag); + } + + + /** + * 刷新并获取 + * @return mixed + */ + public function refresh() + { + $this->del(); + return $this->set(); + } + + protected function cacheSet($name, $value, $expire = null) + { + Cache::tag($this->tag)->set($name, $value, $expire); + } + + protected function cacheRm($name) + { + return Cache::rm($name); + } + + protected function cacheGet($name, $default = false) + { + return Cache::get($name, $default); + } +} \ No newline at end of file diff --git a/app/common/service/cache/RegionCache.php b/app/common/service/cache/RegionCache.php new file mode 100644 index 0000000..3503b30 --- /dev/null +++ b/app/common/service/cache/RegionCache.php @@ -0,0 +1,43 @@ +column('name', 'id'); + } +} \ No newline at end of file