UserInvoice.php 7.0 KB
<?php

namespace app\api\controller;

use app\api\model\Area;
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 UserInvoice extends Api
{
    protected $userModel;
    public function _initialize()
    {
        parent::_initialize();
        $this->userModel = new User();
    }

    /**
     * @ApiTitle    (添加用户发票)
     * @ApiSummary  (添加用户发票)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user_invoice/addUserInvoice)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=type, type=string, required=true, description="类型:1=个人或事业单位,2=企业")
     * @ApiParams  (name=taitou, type=string, required=true, description="发票抬头")
     * @ApiParams  (name=duty_num, type=string, required=false, description="税号")
     * @ApiParams  (name=is_default, type=string, required=true, description="默认:1=是,2=否")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587361014",
    "data":
    })
     */
    public function addUserInvoice()
    {
        $model = new \app\api\model\UserInvoice();
        $userId = $this->getUserId();
        $data = $this->request->param();
        $data['user_id'] = $userId;
        $validate = new Validate([
            'type' => 'require',
            'taitou' => 'require',
            'is_default' => 'require',
        ]);

        $validate->message([
            'type' => '缺少参数 type!',
            'taitou' => '缺少参数 taitou!',
            'is_default' => '缺少参数 is_default!',
        ]);

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

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

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

    /**
     * @ApiTitle    (编辑用户发票)
     * @ApiSummary  (编辑用户发票)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user_invoice/editUserInvoice)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=id, type=string, required=true, description="发票id")
     * @ApiParams  (name=type, type=string, required=true, description="类型:1=个人或事业单位,2=企业")
     * @ApiParams  (name=taitou, type=string, required=true, description="发票抬头")
     * @ApiParams  (name=duty_num, type=string, required=false, description="税号")
     * @ApiParams  (name=is_default, type=string, required=true, description="默认:1=是,2=否")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587361014",
    "data":
    })
     */
    public function editUserInvoice()
    {
        $model = new \app\api\model\UserInvoice();
        $userId = $this->getUserId();
        $data = $this->request->param();
        $data['user_id'] = $userId;
        $validate = new Validate([
            'id' => 'require',
            'type' => 'require',
            'taitou' => 'require',
            'is_default' => 'require',
        ]);

        $validate->message([
            'id' => '缺少参数 id!',
            'type' => '缺少参数 type!',
            'taitou' => '缺少参数 taitou!',
            'is_default' => '缺少参数 is_default!',
        ]);

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

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

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

    /**
     * @ApiTitle    (删除用户发票)
     * @ApiSummary  (删除用户发票)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user_invoice/delUserInvoice)
     * @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 delUserInvoice()
    {
        $model = new \app\api\model\UserInvoice();
        $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_invoice/getUserInvoiceInfo)
     * @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 getUserInvoiceInfo()
    {
        $model = new \app\api\model\UserInvoice();
        $userId = $this->getUserId();
        $id = $this->request->param('id');
        if (empty($id)) $this->error('缺少参数 id!');
        $where['id'] = $id;
        $data = $model->where($where)->find();

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

    /**
     * @ApiTitle    (用户发票列表)
     * @ApiSummary  (用户发票列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user_invoice/getUserInvoiceIList)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587361014",
    "data":
    })
     */
    public function getUserInvoiceIList()
    {
        $model = new \app\api\model\UserInvoice();
        $userId = $this->getUserId();

        $where['user_id'] = $userId;
        $data = $model->where($where)->order('is_default')->select();
        $this->success('SUCCESS',$data);
    }

    /**
     * @ApiTitle    (获取用户默认发票)
     * @ApiSummary  (获取用户默认发票)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/user_invoice/getUserInvoiceDefault)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=type, type=string, required=true, description="类型:1=个人或事业单位,2=企业")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1587361014",
    "data":
    })
     */
    public function getUserInvoiceDefault()
    {
        $model = new \app\api\model\UserInvoice();
        $userId = $this->getUserId();
        $type = $this->request->param('type');
        if (empty($type)) $this->error('缺少参数 type!');

        $data = $model->where(['user_id'=>$userId,'type'=>$type,'is_default'=>1])->find();

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