where('user_id', '=', $user_id)->select(); } /** * 新增收货地址 */ public function add($user, $data) { // 添加收货地址 $this->startTrans(); try { $address_id = $this->insertGetId([ 'name' => $data['name'], 'phone' => $data['phone'], 'province_id' => $data['province_id'], 'city_id' => $data['city_id'], 'region_id' => $data['region_id'], 'detail' => $data['detail'], 'district' => ($data['region_id'] === 0 && !empty($region[2])) ? $region[2] : '', 'user_id' => $user['user_id'], 'app_id' => self::$app_id, // 新增 by lyzflash 'longitude' => isset($data['longitude']) ? $data['longitude'] : '', 'latitude' => isset($data['latitude']) ? $data['latitude'] : '', 'location_address' => isset($data['location_address']) ? $data['location_address'] : '', 'house_number' => isset($data['house_number']) ? $data['house_number'] : '', ]); // 设为默认收货地址 !$user['address_id'] && $user->save(['address_id' => $address_id]); $this->commit(); return true; } catch (\Exception $e) { $this->error = $e->getMessage(); $this->rollback(); return false; } } /** * 编辑收货地址 */ public function edit($data) { // 添加收货地址 $region = explode(',', $data['region']); $province_id = Region::getIdByName($region[0], 1); $city_id = Region::getIdByName($region[1], 2, $province_id); $region_id = Region::getIdByName($region[2], 3, $city_id); return $this->save([ 'name' => $data['name'], 'phone' => $data['phone'], 'province_id' => $province_id, 'city_id' => $city_id, 'region_id' => $region_id, 'detail' => $data['detail'], 'district' => ($region_id === 0 && !empty($region[2])) ? $region[2] : '', // 新增 by lyzflash 'longitude' => isset($data['longitude']) ? $data['longitude'] : '', 'latitude' => isset($data['latitude']) ? $data['latitude'] : '', 'location_address' => isset($data['location_address']) ? $data['location_address'] : '', 'house_number' => isset($data['house_number']) ? $data['house_number'] : '', ]) !== false; } /** * 设为默认收货地址 */ public function setDefault($user) { // 设为默认地址 return $user->save(['address_id' => $this['address_id']]); } /** * 删除收货地址 * @return bool * @throws \Exception */ public function remove($user) { // 查询当前是否为默认地址 $user['address_id'] == $this['address_id'] && $user->save(['address_id' => 0]); return $this->delete(); } /** * 收货地址详情 */ public static function detail($user_id, $address_id) { $where = ['user_id' => $user_id, 'address_id' => $address_id]; return (new static())->where($where)->find(); } /** * 整理地图选择的地址 by lyzflash */ public function setLocationAddress($address) { $region = $this->checkPCA($address); $short_address = str_replace($region['province'] . $region['city'] . $region['area'], '', $address); if (in_array($region['province'], array('北京市', '天津市', '天津市', '天津市'))) { $region['area'] = $region['city']; $region['city'] = $region['province']; } $province_id = Region::getIdByName($region['province'], 1); $city_id = Region::getIdByName($region['city'], 2, $province_id); $region_id = Region::getIdByName($region['area'], 3, $city_id); return ['cityCode'=> [$province_id, $city_id, $region_id], 'region' => [$region['province'], $region['city'], $region['area']], 'short_address' => $short_address]; } /** * 截取省市区 by lyzflash */ private function checkPCA($address) { preg_match('/(.*?(省|自治区|北京市|天津市|上海市|重庆市))/', $address, $matches); if (count($matches) > 1) { $province = $matches[count($matches) - 2]; $address = str_replace($province, '', $address); } preg_match('/(.*?(市|自治州|地区|区划|县))/', $address, $matches); if (count($matches) > 1) { $city = $matches[count($matches) - 2]; $address = str_replace($city, '', $address); } preg_match('/(.*?(市|区|县))/', $address, $matches); if (count($matches) > 1) { $area = $matches[count($matches) - 2]; $address = str_replace($area, '', $address); } return [ 'province' => isset($province) ? $province : '', 'city' => isset($city) ? $city : '', 'area' => isset($area) ? $area : '', ]; } }