123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- namespace app\api\controller;
- use app\common\model\Address as AddressModel;
- use app\common\model\Area;
- use app\common\Enum\StatusEnum;
- /**
- * 地址
- */
- class Address extends Base
- {
- protected $noNeedLogin = [];
- //地址列表
- public function index()
- {
- $list = AddressModel::getAddressList($this->auth->id);
- $this->success('', $list);
- }
- //地址详情
- public function detail()
- {
- $id = $this->request->param('id');
- if (!$id) {
- $this->error('参数错误');
- }
- $row = AddressModel::where('user_id', $this->auth->id)->where('id', $id)->find();
- if (!$row) {
- $this->error('未找到记录');
- }
- $this->success('获取成功', $row);
- }
- /**
- * @ 默认地址
- */
- public function def_address()
- {
- $row = AddressModel::where('user_id', $this->auth->id)->where('is_default', 1)->find();
- if (!$row) {
- $this->error('未找到记录');
- }
- $this->success('获取成功', $row);
- }
- //添加(编辑)地址
- public function addedit()
- {
- $id = $this->request->post('id');
- $address = $this->request->post('address');
- $area_id = $this->request->post('area_id');
- $isdefault = $this->request->post('is_default');
- $mobile = $this->request->post('mobile');
- $receiver = $this->request->post('receiver');
-
- // 增加 验证器
- $validate = new \app\api\validate\Address();
- $data_to_validate = [
- 'receiver' => $receiver,
- 'mobile' => $mobile,
- 'address' => $address,
- 'area_id' => $area_id,
- 'is_default' => $isdefault,
- ];
-
- // 验证数据
- $scene = $id ? 'edit' : 'add';
- if (!$validate->scene($scene)->check($data_to_validate)) {
- $this->error($validate->getError());
- }
- $area = Area::field('a.id,a.zipcode,c.id as city_id,p.id as province_id')
- ->alias('a')
- ->join('shop_area c', 'a.pid=c.id')
- ->join('shop_area p', 'c.pid=p.id')
- ->where('a.id', $area_id)
- ->where('a.level', 3)
- ->find();
- if (!$area) {
- $this->error('未找到地区记录');
- }
- if (!$area['city_id'] || !$area['province_id']) {
- $this->error('地址错误');
- }
- $data = [
- 'address' => $address,
- 'is_default' => $isdefault,
- 'mobile' => $mobile,
- 'receiver' => $receiver,
- 'area_id' => $area_id,
- 'user_id' => $this->auth->id,
- 'city_id' => $area['city_id'],
- 'province_id' => $area['province_id'],
- 'zipcode' => $area['zipcode'],
- 'status' => StatusEnum::ENABLED
- ];
- if ($id) { //编辑
- $row = AddressModel::where('id', $id)
- ->where('user_id', $this->auth->id)
- ->find();
- if (!$row) {
- $this->error('未找到记录');
- }
- $row->save($data);
- } else { //添加
- (new AddressModel)->save($data);
- }
- $this->success('操作成功');
- }
- //删除地址
- public function del()
- {
- $id = $this->request->post('id');
- if (!$id) {
- $this->error('参数错误');
- }
- $row = AddressModel::where('user_id', $this->auth->id)->where('id', $id)->find();
- if (!$row) {
- $this->error('未找到记录');
- }
- $row->delete();
- $this->success('删除成功');
- }
- }
|