huangsijun
2025-11-06 372494883079be03b921f6686691271355bfdd14
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
<?php
 
namespace app\api\controller\user;
 
use app\api\model\user\UserAddress;
use app\api\controller\Controller;
use app\common\model\settings\Region as RegionModel;
use UConverter;
 
/**
 * 收货地址控制器
 */
class Address extends Controller
{
    /**
     * 收货地址列表
     */
    public function lists()
    {
        $user = $this->getUser();
        $model = new UserAddress;
        $list = $model->getList($user['user_id']);
        return $this->renderSuccess('', [
            'list' => $list,
            'default_id' => $user['address_id'],
        ]);
    }
 
    /**
     * 添加收货地址
     */
    public function add()
    {
        $data = $this->request->post();
        if ($data['phone'] == '') {
            return $this->renderError('手机号不正确');
        }
        if ($data['name'] == '') {
            return $this->renderError('收货人不能为空');
        }
        if ($data['detail'] == '') {
            return $this->renderError('收货地址不能为空');
        }
        $model = new UserAddress;
        if ($model->add($this->getUser(), $data)) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError('添加失败');
    }
 
    /**
     * 收货地址详情
     */
    public function detail($address_id)
    {
        $user = $this->getUser();
        $detail = UserAddress::detail($user['user_id'], $address_id);
        $region = array_values($detail['region']);
        $regionData = RegionModel::getRegionForApi();
        return $this->renderSuccess('', compact('detail', 'region', 'regionData'));
    }
 
    /**
     * 编辑收货地址
     */
    public function edit($address_id)
    {
        $user = $this->getUser();
        $model = UserAddress::detail($user['user_id'], $address_id);
        if ($model->edit($this->request->post())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError('更新失败');
    }
 
    /**
     * 设为默认地址
     */
    public function setDefault($address_id)
    {
        $user = $this->getUser();
        $model = UserAddress::detail($user['user_id'], $address_id);
        if ($model->setDefault($user)) {
            return $this->renderSuccess('设置成功');
        }
        return $this->renderError('设置失败');
    }
 
    /**
     * 删除收货地址
     */
    public function delete($address_id)
    {
        $user = $this->getUser();
        $model = UserAddress::detail($user['user_id'], $address_id);
        if ($model->remove($user)) {
            return $this->renderSuccess('删除成功');
        }
        return $this->renderError('删除失败');
    }
 
    /**
     * 整理地图选择的地址
     */
    public function setLocationAddress($address)
    {
        $res = (new UserAddress)->setLocationAddress($address);
        $cityCode = $res['cityCode'];
        $region = $res['region'];
        $short_address = $res['short_address'];
        return $this->renderSuccess('', compact('cityCode', 'short_address', 'region'));
    }
    
}