Address.php 9.2 KB
<?php
/**
 * Created by PhpStorm.
 * User: 86132
 * Date: 2020/7/14
 * Time: 8:59
 */

namespace app\api\controller;

use think\Db;
use app\common\controller\Api;

/**
 * 地址接口
 */
class Address extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }


    /**
     * @ApiTitle    (地址接口-我的地址页面渲染)
     * @ApiSummary  (我的地址页面渲染)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/address/MyAddressPageRendering)
     * @ApiParams   (name="token", type="string", required=true, description="token")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": [
    {
    "name": "收件人",
    "address": "所在地址",
    "address_con": "详细地址",
    "mobile": "联系手机",
    "id":"id",
    "status":"是否默认地址:1=是,0=否"
    }
    ]
    })
     */
    public function MyAddressPageRendering()
    {
        $user_id = $this->is_token($this->request->header());
        $address_arr = Db::name('address')->where(['user_id' => $user_id])->select();
        if (empty($address_arr)) {
            $return = [];
            $this->success('成功', $return);
        }
        foreach ($address_arr as $k => $v) {
            $return[$k]['name'] = $v['name'];
            $return[$k]['address'] = $v['address'];
            $return[$k]['address_con'] = $v['address_con'];
            $return[$k]['mobile'] = $v['mobile'];
            $return[$k]['id'] = $v['id'];
            $return[$k]['status'] = $v['status'];
        }
        $this->success('成功', $return);
    }


    /**
     * @ApiTitle    (地址接口-根据地址ID渲染地址)
     * @ApiSummary  (根据地址ID渲染地址)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/address/RenderAddressBasedOnAddressID)
     * @ApiParams   (name="token", type="string", required=true, description="token")
     * @ApiParams   (name="id", type="int", required=true, description="地址ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": {
    "name": "王二蛋",
    "address": "天津市",
    "address_con": "天津市西青区",
    "mobile": "13223332122",
    "id": 1,
    "status":"是否默认地址:1=是,0=否"
    }
    })
     */
    public function RenderAddressBasedOnAddressID()
    {
        $user_id = $this->is_token($this->request->header());
        $id = input('id');
        $address_arr = Db::name('address')->where(['user_id' => $user_id])->where(['id' => $id])->find();
        $return = [
            'name' => $address_arr['name'],
            'address' => $address_arr['address'],
            'address_con' => $address_arr['address_con'],
            'mobile' => $address_arr['mobile'],
            'id' => $address_arr['id'],
            'status' => $address_arr['status'],
        ];
        $this->success('成功', $return);
    }


    /**
     * @ApiTitle    (地址接口-新增地址)
     * @ApiSummary  (新增地址)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/address/NewAddress)
     * @ApiParams   (name="token", type="string", required=true, description="token")
     * @ApiParams   (name="address", type="string", required=true, description="地址信息")
     * @ApiParams   (name="address_con", type="int", required=true, description="详细地址")
     * @ApiParams   (name="name", type="int", required=true, description="收货人姓名")
     * @ApiParams   (name="mobile", type="int", required=true, description="手机号")
     * @ApiParams   (name="type", type="int", required=true, description="是否设置为默认[1=是,0=否]")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": {
    }
    })
     */
    public function NewAddress()
    {
        $user_id = $this->is_token($this->request->header());
        $param = $this->request->param();
        //判断是否有默认地址
        $count = Db::name('address')->where(['user_id' => $user_id])->find();
        if (count($count) > 5) {
            $this->error('最多只能设置五个地址!', 0);
        }
        if ($param['type'] == 1) {
            $is_TypeAdress = Db::name('address')->where(['user_id' => $user_id])->where(['status' => 1])->find();
            if ($is_TypeAdress) {
                Db::name('address')->where(['id' => $is_TypeAdress['id']])->update(['status' => 0]);
            }
        }
        $data = [
            'user_id' => $user_id,
            'address' => $param['address'],
            'address_con' => $param['address_con'],
            'name' => $param['name'],
            'mobile' => $param['mobile'],
            'status' => $param['type'],
            'createtime' => time(),
            'updatetime' => time(),
        ];
        $res = Db::name('address')->insert($data);
        if ($res) {
            $this->success('成功', 1);
        } else {
            $this->error('失败', 0);
        }
    }


    /**
     * @ApiTitle    (地址接口-修改地址)
     * @ApiSummary  (修改地址)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/address/ChangeAddress)
     * @ApiParams   (name="token", type="string", required=true, description="token")
     * @ApiParams   (name="address", type="string", required=true, description="地址信息")
     * @ApiParams   (name="address_con", type="int", required=true, description="详细地址")
     * @ApiParams   (name="name", type="int", required=true, description="收货人姓名")
     * @ApiParams   (name="mobile", type="int", required=true, description="手机号")
     * @ApiParams   (name="id", type="int", required=true, description="地址ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": {
    }
    })
     */
    public function ChangeAddress()
    {
        $user_id = $this->is_token($this->request->header());
        $param = $this->request->param();
        $res = Db::name('address')->where(['user_id' => $user_id])->where(['id' => $param['id']])
            ->update([
                'address' => $param['address'],
                'address_con' => $param['address_con'],
                'name' => $param['name'],
                'mobile' => $param['mobile'],
                'updatetime' => time()
            ]);
        if ($res) {
            $this->success('修改成功', 1);
        } else {
            $this->error('修改失败', 0);
        }
    }


    /**
     * @ApiTitle    (地址接口-设为默认地址)
     * @ApiSummary  (设为默认地址)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/address/SetAsDefaultAddress)
     * @ApiParams   (name="token", type="string", required=true, description="token")
     * @ApiParams   (name="id", type="int", required=true, description="地址ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": {
    }
    })
     */
    public function SetAsDefaultAddress()
    {
        $user_id = $this->is_token($this->request->header());
        $id = input('id');
        //检查是否已有默认地址
        $is_TypeAdress = Db::name('address')->where(['user_id' => $user_id])->where(['status' => 1])->find();
        if ($is_TypeAdress) {
            Db::name('address')->where(['id' => $is_TypeAdress['id']])->update(['status' => 0]);
        }
        $res = Db::name('address')->where(['id' => $id])->where(['user_id' => $user_id])->update(['status' => 1]);
        if ($res) {
            $this->success('成功', 1);
        } else {
            $this->error('失败', 0);
        }
    }


    /**
     * @ApiTitle    (地址接口-删除地址)
     * @ApiSummary  (删除地址)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/address/DeleteAddress)
     * @ApiParams   (name="token", type="string", required=true, description="token")
     * @ApiParams   (name="id", type="int", required=true, description="地址ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": {
    }
    })
     */
    public function DeleteAddress()
    {
        $user_id = $this->is_token($this->request->header());
        $id = input('id');
        $res = Db::name('address')->where(['user_id' => $user_id])->where(['id' => $id])->delete();
        if ($res) {
            $this->success('成功', 1);
        } else {
            $this->error('失败', 0);
        }
    }
}