quanwei
2026-01-17 e1e2fe5710a5b5cd9c19bd3aa99c998a1a613ca8
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
 
namespace app\api\model\user;
 
use app\common\model\settings\Region;
use app\common\model\user\UserAddress as UserAddressModel;
 
/**
 * 用户收货地址模型
 */
class UserAddress extends UserAddressModel
{
    /**
     * 隐藏字段
     */
    protected $hidden = [
        'app_id',
        'create_time',
        'update_time'
    ];
 
    /**
     * 获取列表
     */
    public function getList($user_id)
    {
        return $this->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 : '',
        ];
    }
 
}