UserAddress.php 9.1 KB
<?php

namespace app\api\controller;

use app\api\model\Area;
use app\api\model\AreaExtend;
use app\api\model\UserAddress as UserAddressModel;
use app\common\controller\Api;
use think\Config;
use think\Db;
use app\api\model\User;
use think\Validate;

/**
 * 用户地址接口
 */
class UserAddress extends Api
{
    protected $userModel;
    public function _initialize()
    {
        parent::_initialize();
        $this->userModel = new User();
    }

    /**
     * @ApiTitle    (添加用户收货地址)
     * @ApiSummary  (添加用户收货地址)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user_address/addUserAddress)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=name, type=string, required=true, description="收货人")
     * @ApiParams  (name=mobile, type=string, required=true, description="收货人手机号")
     * @ApiParams  (name=province_id, type=string, required=true, description="省")
     * @ApiParams  (name=city_id, type=string, required=true, description="市")
     * @ApiParams  (name=county_id, type=string, required=true, description="县")
     * @ApiParams  (name=address, type=string, required=true, description="详细地址")
     * @ApiParams  (name=is_default, type=string, required=true, description="默认:1=是,2=否")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587361014",
    "data":
    })
     */
    public function addUserAddress()
    {
        $model = new UserAddressModel();
        $userId = $this->getUserId();
        $data = $this->request->param();
        $data['user_id'] = $userId;
        $validate = new Validate([
            'name' => 'require',
            'mobile' => ['^1\d{10}$','require'],
            'province_id' => 'require',
            'city_id' => 'require',
            'county_id' => 'require',
            'address' => 'require',
            'is_default' => 'require',
        ]);

        $validate->message([
            'name' => '缺少参数 name!',
            'mobile.require' => '缺少参数 mobile!',
            'province_id' => '缺少参数 province_id!',
            'city_id' => '缺少参数 city_id!',
            'county_id' => '缺少参数 county_id!',
            'address' => '缺少参数 address!',
            'mobile' => '手机格式错误!',
            'is_default' => '缺少参数 is_default!',
        ]);

        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }

        if ($data['is_default'] == 1){
            $model->where(['user_id'=>$userId])->update(['is_default'=>2]);
        }

        $res = $model->save($data);
        if ($res) $this->success('SUCCESS');
        else $this->error('ERROR');
    }

    /**
     * @ApiTitle    (编辑用户收货地址)
     * @ApiSummary  (编辑用户收货地址)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user_address/editUserAddress)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=id, type=string, required=true, description="地址id")
     * @ApiParams  (name=name, type=string, required=true, description="收货人")
     * @ApiParams  (name=mobile, type=string, required=true, description="收货人手机号")
     * @ApiParams  (name=province_id, type=string, required=true, description="省")
     * @ApiParams  (name=city_id, type=string, required=true, description="市")
     * @ApiParams  (name=county_id, type=string, required=true, description="县")
     * @ApiParams  (name=address, type=string, required=true, description="详细地址")
     * @ApiParams  (name=is_default, type=string, required=true, description="默认:1=是,2=否")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587361014",
    "data":
    })
     */
    public function editUserAddress()
    {
        $model = new UserAddressModel();
        $userId = $this->getUserId();
        $data = $this->request->param();
        $data['user_id'] = $userId;
        $validate = new Validate([
            'id' => 'require',
            'name' => 'require',
            'mobile' => ['^1\d{10}$','require'],
            'province_id' => 'require',
            'city_id' => 'require',
            'county_id' => 'require',
            'address' => 'require',
            'is_default' => 'require',
        ]);

        $validate->message([
            'id' => '缺少参数 id!',
            'name' => '缺少参数 name!',
            'mobile.require' => '缺少参数 mobile!',
            'province_id' => '缺少参数 province_id!',
            'city_id' => '缺少参数 city_id!',
            'county_id' => '缺少参数 county_id!',
            'address' => '缺少参数 address!',
            'mobile' => '手机格式错误!',
            'is_default' => '缺少参数 is_default!',
        ]);

        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }

        if ($data['is_default'] == 1){
            $model->where(['user_id'=>$userId])->update(['is_default'=>2]);
        }


        $res = $model->update($data);
        if ($res) $this->success('SUCCESS');
        else $this->error('ERROR');
    }

    /**
     * @ApiTitle    (删除用户收货地址)
     * @ApiSummary  (删除用户收货地址)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user_address/delUserAddress)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=id, type=string, required=true, description="地址id")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587361014",
    "data":
    })
     */
    public function delUserAddress()
    {
        $model = new UserAddressModel();
        $userId = $this->getUserId();
        $id = $this->request->param('id');
        if (empty($id)) $this->error('缺少参数 id!');
        $where['id'] = $id;
        $res = $model->where($where)->delete();
        if ($res) $this->success('SUCCESS');
        else $this->error('ERROR');
    }

    /**
     * @ApiTitle    (用户收货地址详情)
     * @ApiSummary  (用户收货地址详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user_address/getUserAddressInfo)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=id, type=string, required=true, description="地址id")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587361014",
    "data":
    })
     */
    public function getUserAddressInfo()
    {
        $model = new UserAddressModel();
        $areaModel = new Area();
        $userId = $this->getUserId();
        $id = $this->request->param('id');
        if (empty($id)) $this->error('缺少参数 id!');
        $where['id'] = $id;
        $data = $model->where($where)->find();
        $data['province'] = $areaModel->where('id',$data['province_id'])->field('id,name')->find();
        $data['city'] = $areaModel->where('id',$data['city_id'])->field('id,name')->find();
        $data['county'] = $areaModel->where('id',$data['county_id'])->field('id,name')->find();
        $this->success('SUCCESS',$data);
    }

    /**
     * @ApiTitle    (用户收货地址列表)
     * @ApiSummary  (用户收货地址列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user_address/getUserAddressIList)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587361014",
    "data":
    })
     */
    public function getUserAddressIList()
    {
        $model = new UserAddressModel();
        $areaModel = new Area();
        $userId = $this->getUserId();

        $where['user_id'] = $userId;
        $data = $model->where($where)->field('id,province_id,city_id,county_id,address,name,mobile,is_default')->order('is_default')->select();
        foreach ($data as $k => $v){
            $ids = $data[$k]['province_id'].','.$data[$k]['city_id'].','.$data[$k]['county_id'];
            $area = $areaModel->where(['id'=>['in',$ids]])->column('name');
            $data[$k]['address'] = implode('',$area).$v['address'];
        }

        $this->success('SUCCESS',$data);
    }

    /**
     * @ApiTitle    (获取用户默认地址)
     * @ApiSummary  (获取用户默认地址)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user_address/getUserAddressDefault)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1589181766",
    "data": {
    "name": "name",    收货人姓名
    "mobile": "13752011792",   收货人电话
    "province_id": 1,   省id
    "city_id": 2,    市id
    "county_id": 3,     县id
    "address": "北京北京市东城区1",      详细地址
    "postage_type": "2"    配送类型
    }
    })
     */
    public function getUserAddressDefault()
    {
        $model = new UserAddressModel();
        $userId = $this->getUserId();
        $data = $model->getDefaultData(['user_id'=>$userId]);
        if (!$data) $this->error('该用户没有默认地址');
        $this->success('SUCCESS',$data);
    }
}