quanwei
18 hours ago c441dea81bd86bdfb12dff35821fed51f4cc91c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
 
namespace app\common\model\user;
 
use app\common\model\BaseModel;
use app\common\model\settings\Region;
 
class UserAddress extends BaseModel
{
    protected $name = 'user_address';
    protected $pk = 'address_id';
    /**
     * 追加字段
     * @var array
     */
    protected $append = ['region'];
 
    /**
     * 地区名称
     */
    public function getRegionAttr($value, $data)
    {
        return [
            'province' => Region::getNameById($data['province_id']),
            'city' => Region::getNameById($data['city_id']),
            'region' => $data['region_id'] == 0 ? $data['district']
                : Region::getNameById($data['region_id']),
        ];
    }
 
    /**
     * 整理地图选择的地址 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 : '',
        ];
    }
}