<?php
/**
 * @Statement: [ An Ordinary Person ]
 * @Author: 闻子 < QQ：270988107 >
 * @Copyright: 闻子 2016-2019 All rights reserved.
 *
 *    .--,       .--,
 *   ( (  \.---./  ) )
 *    '.__/o   o\__.'
 *       {=  ^  =}
 *        >  -  <
 *       /       \
 *      //       \\
 *     //|   .   |\\
 *     "'\       /'"_.-~^`'-.
 *        \  _  /--'         `
 *      ___)( )(___
 *     (((__) (__)))    高山仰止,景行行止.虽不能至,心向往之.
 *
 */

namespace app\api\controller;

use think\Db;

use app\common\logic\User as LogicUser;

use app\common\controller\Base as BaseController;

class User extends BaseController
{
	/**
	 * 定义逻辑
	 * @Author: 闻子 < QQ：270988107 >
	 */
	private static $UserLogic = null;

    /**
     * 实例化逻辑
     * @Author: 闻子 < QQ：270988107 >
     */
    public function __construct()
    {
        parent::__construct();
        self::$UserLogic = get_sington_object('LogicUser', LogicUser::class);
    }

    /**
     * session检测
     * @Author: 闻子 < QQ：270988107 >
     */
    public function getSession()
    {
        $AdminExistence = session('admin.id');
        if (!$AdminExistence) {
            return $this->error('登录超时，请重新登录','index/member/login');
        }
    }

    /**
     * 管理员检测
     * @Author: 闻子 < QQ：270988107 >
     */
    public function getManager()
    {
        $userinfo = Db::name('user')->where('id',session('admin.id'))->find();
        if ($userinfo['manager'] != 1) {
            $this->redirect('/member/index');
        } elseif (!$userinfo) {
            return $this->error('登录超时，请重新登录','index/member/login');
        }
    }

    /**
     * 会员接口
     * @Author: 闻子 < QQ：270988107 >
     */
    public function getUser()
    {
        $this->getManager();
        $count = self::$UserLogic->getUserCount();
        $page = $this->request->param('page',1,'intval');
        $limit = $this->request->param('limit',10,'intval');
        $all = Db::name('user')->limit(($page-1)*$limit,$limit)->order('id desc')->select();
        $data['code'] = 0;
        $data['msg'] = '';
        $data['count'] = $count;
        $data['data'] = $all;
        return json($data);
    }

    /**
     * 会员编辑
     * @Author: 闻子 < QQ：270988107 >
     */
    public function getedit()
    {
        $this->getManager();
        $data = self::$UserLogic->getedit(input('post.'));
        return $data;
    }

    /**
     * 会员删除
     * @Author: 闻子 < QQ：270988107 >
     */
    public function del($id = 0)
    {
        $this->getManager();
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '请求参数异常', 'data' => '']);
        }
        $data = self::$UserLogic->getUserDel($id);
        return json($data);
    }

    /**
     * 会员批量删除
     * @Author: 闻子 < QQ：270988107 >
     */
    public function alldel()
    {
        $this->getManager();
        $data = self::$UserLogic->getUseralldel(input('ids'));
        return $data;
    }
}