<?php
/**
 * Created by PhpStorm.
 * User: sicilon_IT
 * Date: 2020/1/22
 * Time: 22:16
 */
namespace app\admin\controller;

use app\admin\logic\webuserlogic;
use app\admin\logic\webuserloglogic;
use think\Controller;
use think\facade\Log;

class Weblogin extends AdminBase
{
    /**
     * 设置请求数据规则
     * 20220107
     * wj
     */
    protected function setrules()
    {
        $list = [
            'login' => [
                ['name' => 'userName', 'title' => '用户名', 'require' => true, 'type' => 'string'],
                ['name' => 'password', 'title' => '密码', 'require' => true, 'type' => 'string'],
            ],
            'loginout' => [
                ['name' => 'token', 'title' => 'token', 'require' => true, 'type' => 'string'],
            ],
            'queryinfobytoken' => [
                ['name' => 'token', 'title' => 'token', 'require' => true, 'type' => 'string'],
            ],
        ];
        return $list;
    }
    /**
     * 后台登录
     * 20211116
     * wj
     **/
    public function login()
    {
        $userinfo = request()->param();
        $wm = new webuserlogic();
        $l_wul = new webuserloglogic();
        $result = $wm->userlogin($userinfo['userName'], $userinfo['password']);
        $logData = ['type' => 1, 'tablename' => 'webuser'];
        if (1 == $result['status']) {
            $logData['wuid'] = $result['data']['id'];
            $logData['node'] = "登录成功";
            $result1 = $l_wul->addlog($logData);
            log::info($result1);
            return backjson2(200, '登录成功', $result['data']);
        } else {
            $logData['node'] = "登录失败：" . $result['msg'];
            $result1 = $l_wul->addlog($logData);
            log::info($result1);
            return backjson2(0, $result['msg']);
        }
    }

    /***
     * 后台退出
     * 20211116
     * wj
     */
    public function loginout()
    {
        $param = request()->param();
        $logData = [
            'type' => 1,
            'tablename' => 'webuser',
            'token' => $param['token'],
            'node' => '退出',
        ];
        $m_w = new webuserlogic();
        $l_wul = new webuserloglogic();
        $result = $l_wul->addlog($logData);
        if (1 === $result['status']) {
            $result = $m_w->updateToken();
            if (1 != $result['status']) {
                return backjson2(0, $result['msg']);
            }
            return backjson2(200, '操作成功');
        } else {
            return backjson2(0, '操作失败');
        }
    }

    /***
     * 根据token获取用户信息
     */
    public function queryinfobytoken()
    {
        $param = request()->param();
        $wm = new webuserlogic();
        $result = $wm->queryinfobytoken($param);
        if (1 == $result['status']) {
            return backjson2(200, '登录成功', $result['data']);
        } else {
            return backjson2(0, $result['msg']);
        }
    }
}
