Address.php 6.5 KB
<?php

namespace app\api\controller\kuerp\order;

use app\common\controller\Api;
use app\api\library\kuerp\Auth;
use app\api\model\kuerp\UserAddress as Addresss;
use app\api\model\kuerp\Areas;
class Address extends Api
{
    protected $noNeedRight = '*';
    protected $noNeedLogin = '*';
    protected $auth_s;

    public function _initialize()
    {
        parent::_initialize();
        $this->auth_s = new Auth();
        $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('token')));
        $this->auth_s->init($token);
    }

    /**
     * 获取发货地址
     * @ApiMethod (POST)
     * @param int $page 页码
     * @param int $limit 每页条数
     * @param string $name 发货地址名称
     */
    public function getlist()
    {
        if ($this->request->isPost()) {
            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?config('app_limit'):$limit;

            $name = $this->request->post('name');
            $where = [];
            if(!empty($name)){
                $where['connect_name'] = ['like','%'.$name.'%'];
            }
            $phone = $this->request->post('phone');
            if(!empty($phone)){
                $where['connect_mobile'] = ['like','%'.$phone.'%'];
            }
            $user = $this->auth_s->getUserinfo();
            if(!isset($user['id'])){
                $this->error('请先登陆','',250);
            }
            $result = Addresss::getList($where,$page,$limit,$user['id']);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 获取发货地址详情
     * @ApiMethod (POST)
     * @param int $id 发货地址ID
     */
    public function getdetail()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id');
            if(empty($id)){
                $this->error('请输入查询参数','',500);
            }
            $user = $this->auth_s->getUserinfo();
            if(!isset($user['id'])){
                $this->error('请先登陆','',250);
            }
            $result = Addresss::getDetail($id,$user['id']);
            if($result['status']!=200){
                $this->error($result['msg'],'',$result['status']);
            }
            $this->success('查询成功',$result['msg'],200);
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 添加发货地址
     * @ApiMethod (POST)
     * @param array $detail 发货地址数据
     */
    public function adddetail()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            foreach ($data as $k=>$v){
                $data[$k] = is_array($v)?$v:html_entity_decode($v);
            }
            $user = $this->auth_s->getUserinfo();
            if(!isset($user['id'])){
                $this->error('请先登陆','',250);
            }
            $ret = Addresss::addDetail($data,$user['id']);
            if($ret['status']==200){
                $this->success('添加成功',$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 更新发货地址
     * @ApiMethod (POST)
     * @param array $detail 发货地址数据
     */
    public function editdetail()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            foreach ($data as $k=>$v){
                $data[$k] = is_array($v)?$v:html_entity_decode($v);
            }
            $user = $this->auth_s->getUserinfo();
            if(!isset($user['id'])){
                $this->error('请先登陆','',250);
            }
            $ret = Addresss::editDetail($data,$user['id']);
            if($ret['status']==200){
                $this->success('更新成功','',200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 删除发货地址
     * @ApiMethod (POST)
     * @param int $id 发货地址id
     */
    public function deldetail()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id');
            $user = $this->auth_s->getUserinfo();
            if(!isset($user['id'])){
                $this->error('请先登陆','',250);
            }
            $ret = Addresss::delDetail($id,$user['id']);
            if($ret['status']==200){
                $this->success('删除成功',$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 读取省市区数据,联动列表
     */
    public function area()
    {
        if ($this->request->isPost()) {
            $province = $this->request->post('province', '');
            $city = $this->request->post('city', '');
            $adcode = $this->request->post('adcode', '');
            if(!empty($adcode)){
                $area = (new Areas())->where(['adcode'=>$adcode])->find();
                if($area){
                    if($area['level']==1){
                        $province = $area['id'];
                    }elseif ($area['level']==2){
                        $city = $area['id'];
                    }
                }
            }

            $where = ['pid' => 0, 'level' => 1];
            $provincelist = null;
            if ($province !== '') {
                $where['pid'] = $province;
                $where['level'] = 2;
                if ($city !== '') {
                    $where['pid'] = $city;
                    $where['level'] = 3;
                }
            }
            $provincelist = (new Areas())->where($where)->field('id as value,name,adcode')->select();
            $this->success('查询成功', $provincelist, 200);
        }else{
            $this->error('请求方式错误','',250);
        }
    }

}