getChangedData(); if (isset($changed['is_default']) && $changed['is_default']) { $info = self::where('is_default', 1)->where('user_id', $row['user_id'])->find(); if ($info && (!isset($row['id']) || $info['id'] != $row['id'])) { $info->is_default = 0; $info->save(); } } //$row['address'] = $row->address_full; }); } public function getCityAttr($value, $data) { $areainfo = $this->getAreainfoAttr($value, $data); return $areainfo['city'] ? $areainfo['city']['name'] : ''; } public function getAreaAttr($value, $data) { $areainfo = $this->getAreainfoAttr($value, $data); return $areainfo['area'] ? $areainfo['area']['name'] : ''; } public function getProvinceAttr($value, $data) { $areainfo = $this->getAreainfoAttr($value, $data); return $areainfo['province'] ? $areainfo['province']['name'] : ''; } public function getAddressBaseAttr($value, $data) { $areainfo = $this->getAreainfoAttr($value, $data); $province = $areainfo['province'] ? $areainfo['province']['name'] : ''; $city = $areainfo['city'] ? $areainfo['city']['name'] : ''; $area = $areainfo['area'] ? $areainfo['area']['name'] : ''; $value = $data['address']; $value = preg_replace("/^{$province}{$city}{$area}/", "", $value); return $value; } public function getAddressFullAttr($value, $data) { $areainfo = $this->getAreainfoAttr($value, $data); $province = $areainfo['province'] ? $areainfo['province']['name'] : ''; $city = $areainfo['city'] ? $areainfo['city']['name'] : ''; $area = $areainfo['area'] ? $areainfo['area']['name'] : ''; $value = $data['address']; $value = preg_replace("/^{$province}{$city}{$area}/", "", $value); $value = $province . $city . $area . $value; return $value; } /** * 获取城市和地区信息 * @param $value * @param $data * @return array */ public function getAreainfoAttr($value, $data) { $result = [ 'province' => null, 'city' => null, 'area' => null, ]; $areaList = Area::where('id', 'in', [$data['province_id'], $data['city_id'], $data['area_id']])->select(); foreach ($areaList as $index => $item) { $levelName = ($item['level'] == 1 ? 'province' : ($item['level'] == 2 ? 'city' : 'area')); $result[$levelName] = $item; } return $result; } /** * 获取会员地址列表 * @param $user_id * @return false|\PDOStatement|string|\think\Collection */ public static function getAddressList($user_id) { $addressList = self::where('user_id', $user_id)->where('status', StatusEnum::ENABLED)->order('used_nums desc,id desc')->select(); return $addressList; } }