Thinkphp用戶模塊接口

用戶增刪改查功能接口


<?php
namespace app\admin\controller;

class User extends Error
{
    /**
     * @api {post} /admin/user/list  後臺用戶列表接口
     * 
     * @param int       page      null
     * @param string    username  null
     * 
     * @return array
     */
    public function listApi()
    {
        $param = input('post.');
        $page = $param['page'] ?? 1;

        $where['is_delete'] = 0;
        $where['admin'] = 0;

        if (!empty($param['username'])) {
            $where['username'] = $param['username'];
        }

        $total = $this->db('yzm_user')->where($where)->count();
        if ($total == 0) {
            return [200,['total'=>0, 'data' => []]];
        }

        $data = $this->db('yzm_user')->where($where)->field('id,username,head_url,admin,status,time')->page($page,10)->order('id desc')->select();
        foreach ($data as &$v) {
            $v['time'] = date('Y-m-d H:i:s',$v['time']);
            $v['admin_text'] = $v['admin'] == 1? '管理員':'用戶';
        }

        return [200, ['total'=>$total ,'data' => $data]];
    }
    /**
     * @api {post} /admin/user/insert  後臺用戶新增接口
     * 
     * @param string    username require(必填)
     * @param string    head_url require(必填)
     * @param int    admin require(必填)
     * @param int    status require(必填)
     * 
     * @return array
     */
    public function insertApi()
    {
        $param = input('post.');

        $error = $this->validate($param,'User.insert');
        if (true !== $error) {
            return [501, $error];
        }

        $user = $this->db('yzm_user')->where('username',$param['username'])->where('is_delete',0)->find();
        if ($user) {
            return [501 ,'該用戶已存在,再想一下'];
        }

        $row = $this->db('yzm_user')->insert([
            'username' => $param['username'],
            'password' => md5(123456),
            'head_url' => $param['head_url'],
            'admin' => $param['admin'],
            'status' => $param['status'],
            'time' => time(),
            'is_delete' => 0,
        ]);

        if ($row < 1) {
            return [501 ,'操作失敗'];
        }

        return [200, true];

    }
    /**
     * @api {post} /admin/user/update  後臺用戶更新接口
     * 
     * @param string    username require(必填)
     * @param string    head_url require(必填)
     * @param int    admin require(必填)
     * @param int    status require(必填)
     * 
     * @return array
     */
    public function updateApi()
    {
        $param = input('post.');

        $error = $this->validate($param,'User.update');
        if (true !== $error) {
            return [501, $error];
        }

        $user = $this->db('yzm_user')->where('username',$param['username'])->where('id','<>',$param['id'])->where('is_delete',0)->find();
        if ($user) {
            return [501 ,'該用戶已存在,再想一下'];
        }

        $row = $this->db('yzm_user')->where('id',$param['id'])->update([
            'username' => $param['username'],
            'head_url' => $param['head_url'],
            'admin' => $param['admin'],
            'status' => $param['status'],
            'is_delete' => 0,
        ]);

        if ($row < 0) {
            return [501 ,'操作失敗'];
        }

        return [200, true];

    }
    /**
     * @api {post} /admin/user/info  後臺用戶信息接口
     *
     * @param int    id require(必填)
     * 
     * @return array
     */
    public function infoApi()
    {
        $id = input('post.id');

        $info = $this->db('yzm_user')->where('id',$id)->where('is_delete',0)->field('id,username,head_url,status,admin,time')->find();
        if (!$info) {
            return [501 ,'數據錯誤'];
        }

        $info['time'] = date('Y-m-d H:i:s',$info['time']);

        return [200, $info];
    }
    /**
     * @api {post} /admin/user/delete  後臺用戶僞刪除接口
     *
     * @param int    id require(必填)
     * 
     * @return array
     */
    public function deleteApi()
    {
        $id = input('post.id');

        $info = $this->db('yzm_user')->where('id',$id)->where('is_delete',0)->find();
        if (!$info) {
            return [501 ,'數據錯誤'];
        }

        $row = $this->db('yzm_user')->where('id',$id)->update(['is_delete'=> 1]);

        if ($row < 1) {
            return [501 , '刪除失敗'];
        }

        return [200, true];
    }
    /**
     * @api {post} /admin/user/changeStatus  後臺用戶狀態接口
     *
     * @param int    id require(必填)
     * 
     * @return array
     */
    public function changeStatusApi()
    {
        $id = input('post.id');

        $info = $this->db('yzm_user')->where('id',$id)->where('is_delete',0)->find();
        if (!$info) {
            return [501 ,'數據錯誤'];
        }

        $status = $info['status'] == 1 ? 2 : 1;

        $row = $this->db('yzm_user')->where('id',$id)->update(['status'=>$status]);

        if ($row < 1) {
            return [501 ,'操作失敗'];
        }

        return [200, true];

    }
}

顯示結果頁面
在這裏插入圖片描述

用戶模塊驗證器

<?php
namespace app\admin\validate;

use think\Validate;

class User extends Validate
{
    protected $rule = [
        'id'=>'require|number',
        'username'=>'require|chsDash',
        'oldpassword'=>'require',
        'password'=>'require|alphaDash|confirm:repassword',
        'repassword'=>'require|alphaDash',
        'head_url'=>'require|checkImg',
        'admin'=>'require|number|in:0,1',
        'status'=>'require|number|in:1,2',
    ];

    protected $scene = [
        'insert'=>['username','head_url','admin','status'],
        'update'=>['id','username','head_url','admin','status'],
        'changePass'=>['id','oldpassword','password','repassword'],
    ];
    
    protected function checkImg($img)
    {
        if(!in_array(pathinfo($img,PATHINFO_EXTENSION),['jpeg','jpg','png'])){
             return '請上傳jpeg、jpg、png格式的圖片';   
        }
        return true;
    }
}

上傳文件接口

<?php
namespace app\admin\controller;

class System extends Error
{
    /**
     * @api {post} /admin/system/upload  上傳接口
     * 
     * @param file    file require(必填)
     * 
     * @return array
     */
    public function uploadApi()
    {
        $file = request()->file('file');
        
        if (!$file) {
            return [501 ,'文件不存在,上傳失敗'];
        }

        $info = $file->move('./uploads');
        if (!$info){
            return [501, $file->getError()];
        }

        $name = $info->getSaveName();

        return [200 , 'http://uploads.myvue.com/'.$name];
    }
}

顯示結果頁面
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章