作者 杨育虎

2

  1 +<?php
  2 +
  3 +namespace app\admin\controller;
  4 +
  5 +use app\common\controller\Backend;
  6 +
  7 +/**
  8 + * 管理员管理
  9 + *
  10 + * @icon fa fa-circle-o
  11 + */
  12 +class Admin extends Backend
  13 +{
  14 +
  15 + /**
  16 + * Admin模型对象
  17 + * @var \app\admin\model\Admin
  18 + */
  19 + protected $model = null;
  20 +
  21 + public function _initialize()
  22 + {
  23 + parent::_initialize();
  24 + $this->model = new \app\admin\model\Admin;
  25 +
  26 + }
  27 +
  28 + /**
  29 + * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  30 + * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  31 + * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  32 + */
  33 +
  34 +
  35 +}
  1 +<?php
  2 +
  3 +return [
  4 + 'Id' => 'ID',
  5 + 'Username' => '用户名',
  6 + 'Nickname' => '昵称',
  7 + 'Password' => '密码',
  8 + 'Salt' => '密码盐',
  9 + 'Avatar' => '头像',
  10 + 'Email' => '电子邮箱',
  11 + 'Loginfailure' => '失败次数',
  12 + 'Logintime' => '登录时间',
  13 + 'Loginip' => '登录IP',
  14 + 'Createtime' => '创建时间',
  15 + 'Updatetime' => '更新时间',
  16 + 'Token' => 'Session标识',
  17 + 'Status' => '状态'
  18 +];
@@ -3,32 +3,46 @@ @@ -3,32 +3,46 @@
3 namespace app\admin\model; 3 namespace app\admin\model;
4 4
5 use think\Model; 5 use think\Model;
6 -use think\Session; 6 +
7 7
8 class Admin extends Model 8 class Admin extends Model
9 { 9 {
10 10
11 - // 开启自动写入时间戳字段 11 +
  12 +
  13 +
  14 +
  15 + // 表名
  16 + protected $name = 'admin';
  17 +
  18 + // 自动写入时间戳字段
12 protected $autoWriteTimestamp = 'int'; 19 protected $autoWriteTimestamp = 'int';
  20 +
13 // 定义时间戳字段名 21 // 定义时间戳字段名
14 protected $createTime = 'createtime'; 22 protected $createTime = 'createtime';
15 protected $updateTime = 'updatetime'; 23 protected $updateTime = 'updatetime';
  24 + protected $deleteTime = false;
  25 +
  26 + // 追加属性
  27 + protected $append = [
  28 + 'logintime_text'
  29 + ];
  30 +
16 31
17 - /**  
18 - * 重置用户密码  
19 - * @author baiyouwen  
20 - */  
21 - public function resetPassword($uid, $NewPassword) 32 +
  33 +
  34 +
  35 +
  36 + public function getLogintimeTextAttr($value, $data)
22 { 37 {
23 - $passwd = $this->encryptPassword($NewPassword);  
24 - $ret = $this->where(['id' => $uid])->update(['password' => $passwd]);  
25 - return $ret; 38 + $value = $value ? $value : (isset($data['logintime']) ? $data['logintime'] : '');
  39 + return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
26 } 40 }
27 41
28 - // 密码加密  
29 - protected function encryptPassword($password, $salt = '', $encrypt = 'md5') 42 + protected function setLogintimeAttr($value)
30 { 43 {
31 - return $encrypt($password . $salt); 44 + return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
32 } 45 }
33 46
  47 +
34 } 48 }
@@ -6,50 +6,22 @@ use think\Validate; @@ -6,50 +6,22 @@ use think\Validate;
6 6
7 class Admin extends Validate 7 class Admin extends Validate
8 { 8 {
9 -  
10 /** 9 /**
11 * 验证规则 10 * 验证规则
12 */ 11 */
13 protected $rule = [ 12 protected $rule = [
14 - 'username' => 'require|regex:\w{3,12}|unique:admin',  
15 - 'nickname' => 'require',  
16 - 'password' => 'require|regex:\S{32}',  
17 - 'email' => 'require|email|unique:admin,email',  
18 ]; 13 ];
19 -  
20 /** 14 /**
21 * 提示消息 15 * 提示消息
22 */ 16 */
23 protected $message = [ 17 protected $message = [
24 ]; 18 ];
25 -  
26 - /**  
27 - * 字段描述  
28 - */  
29 - protected $field = [  
30 - ];  
31 -  
32 /** 19 /**
33 * 验证场景 20 * 验证场景
34 */ 21 */
35 protected $scene = [ 22 protected $scene = [
36 - 'add' => ['username', 'email', 'nickname', 'password'],  
37 - 'edit' => ['username', 'email', 'nickname', 'password'], 23 + 'add' => [],
  24 + 'edit' => [],
38 ]; 25 ];
39 -  
40 - public function __construct(array $rules = [], $message = [], $field = [])  
41 - {  
42 - $this->field = [  
43 - 'username' => __('Username'),  
44 - 'nickname' => __('Nickname'),  
45 - 'password' => __('Password'),  
46 - 'email' => __('Email'),  
47 - ];  
48 - $this->message = array_merge($this->message, [  
49 - 'username.regex' => __('Please input correct username'),  
50 - 'password.regex' => __('Please input correct password')  
51 - ]);  
52 - parent::__construct($rules, $message, $field);  
53 - }  
54 - 26 +
55 } 27 }
  1 +<form id="add-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
  2 +
  3 + <div class="form-group">
  4 + <label class="control-label col-xs-12 col-sm-2">{:__('Username')}:</label>
  5 + <div class="col-xs-12 col-sm-8">
  6 + <input id="c-username" data-rule="required" class="form-control" name="row[username]" type="text" value="">
  7 + </div>
  8 + </div>
  9 + <div class="form-group">
  10 + <label class="control-label col-xs-12 col-sm-2">{:__('Nickname')}:</label>
  11 + <div class="col-xs-12 col-sm-8">
  12 + <input id="c-nickname" data-rule="required" class="form-control" name="row[nickname]" type="text" value="">
  13 + </div>
  14 + </div>
  15 + <div class="form-group">
  16 + <label class="control-label col-xs-12 col-sm-2">{:__('Password')}:</label>
  17 + <div class="col-xs-12 col-sm-8">
  18 + <input id="c-password" data-rule="required" class="form-control" name="row[password]" type="text" value="">
  19 + </div>
  20 + </div>
  21 + <div class="form-group">
  22 + <label class="control-label col-xs-12 col-sm-2">{:__('Salt')}:</label>
  23 + <div class="col-xs-12 col-sm-8">
  24 + <input id="c-salt" data-rule="required" class="form-control" name="row[salt]" type="text" value="">
  25 + </div>
  26 + </div>
  27 + <div class="form-group">
  28 + <label class="control-label col-xs-12 col-sm-2">{:__('Avatar')}:</label>
  29 + <div class="col-xs-12 col-sm-8">
  30 + <div class="input-group">
  31 + <input id="c-avatar" data-rule="required" class="form-control" size="50" name="row[avatar]" type="text" value="">
  32 + <div class="input-group-addon no-border no-padding">
  33 + <span><button type="button" id="plupload-avatar" class="btn btn-danger plupload" data-input-id="c-avatar" data-mimetype="image/gif,image/jpeg,image/png,image/jpg,image/bmp" data-multiple="false" data-preview-id="p-avatar"><i class="fa fa-upload"></i> {:__('Upload')}</button></span>
  34 + <span><button type="button" id="fachoose-avatar" class="btn btn-primary fachoose" data-input-id="c-avatar" data-mimetype="image/*" data-multiple="false"><i class="fa fa-list"></i> {:__('Choose')}</button></span>
  35 + </div>
  36 + <span class="msg-box n-right" for="c-avatar"></span>
  37 + </div>
  38 + <ul class="row list-inline plupload-preview" id="p-avatar"></ul>
  39 + </div>
  40 + </div>
  41 + <div class="form-group">
  42 + <label class="control-label col-xs-12 col-sm-2">{:__('Email')}:</label>
  43 + <div class="col-xs-12 col-sm-8">
  44 + <input id="c-email" data-rule="required" class="form-control" name="row[email]" type="text" value="">
  45 + </div>
  46 + </div>
  47 + <div class="form-group">
  48 + <label class="control-label col-xs-12 col-sm-2">{:__('Loginfailure')}:</label>
  49 + <div class="col-xs-12 col-sm-8">
  50 + <input id="c-loginfailure" data-rule="required" class="form-control" name="row[loginfailure]" type="number" value="0">
  51 + </div>
  52 + </div>
  53 + <div class="form-group">
  54 + <label class="control-label col-xs-12 col-sm-2">{:__('Logintime')}:</label>
  55 + <div class="col-xs-12 col-sm-8">
  56 + <input id="c-logintime" class="form-control datetimepicker" data-date-format="YYYY-MM-DD HH:mm:ss" data-use-current="true" name="row[logintime]" type="text" value="{:date('Y-m-d H:i:s')}">
  57 + </div>
  58 + </div>
  59 + <div class="form-group">
  60 + <label class="control-label col-xs-12 col-sm-2">{:__('Loginip')}:</label>
  61 + <div class="col-xs-12 col-sm-8">
  62 + <input id="c-loginip" class="form-control" name="row[loginip]" type="text">
  63 + </div>
  64 + </div>
  65 + <div class="form-group">
  66 + <label class="control-label col-xs-12 col-sm-2">{:__('Token')}:</label>
  67 + <div class="col-xs-12 col-sm-8">
  68 + <input id="c-token" data-rule="required" class="form-control" name="row[token]" type="text" value="">
  69 + </div>
  70 + </div>
  71 + <div class="form-group">
  72 + <label class="control-label col-xs-12 col-sm-2">{:__('Status')}:</label>
  73 + <div class="col-xs-12 col-sm-8">
  74 + <input id="c-status" data-rule="required" class="form-control" name="row[status]" type="text" value="normal">
  75 + </div>
  76 + </div>
  77 + <div class="form-group layer-footer">
  78 + <label class="control-label col-xs-12 col-sm-2"></label>
  79 + <div class="col-xs-12 col-sm-8">
  80 + <button type="submit" class="btn btn-success btn-embossed disabled">{:__('OK')}</button>
  81 + <button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>
  82 + </div>
  83 + </div>
  84 +</form>
  1 +<form id="edit-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
  2 +
  3 + <div class="form-group">
  4 + <label class="control-label col-xs-12 col-sm-2">{:__('Username')}:</label>
  5 + <div class="col-xs-12 col-sm-8">
  6 + <input id="c-username" data-rule="required" class="form-control" name="row[username]" type="text" value="{$row.username|htmlentities}">
  7 + </div>
  8 + </div>
  9 + <div class="form-group">
  10 + <label class="control-label col-xs-12 col-sm-2">{:__('Nickname')}:</label>
  11 + <div class="col-xs-12 col-sm-8">
  12 + <input id="c-nickname" data-rule="required" class="form-control" name="row[nickname]" type="text" value="{$row.nickname|htmlentities}">
  13 + </div>
  14 + </div>
  15 + <div class="form-group">
  16 + <label class="control-label col-xs-12 col-sm-2">{:__('Password')}:</label>
  17 + <div class="col-xs-12 col-sm-8">
  18 + <input id="c-password" data-rule="required" class="form-control" name="row[password]" type="text" value="{$row.password|htmlentities}">
  19 + </div>
  20 + </div>
  21 + <div class="form-group">
  22 + <label class="control-label col-xs-12 col-sm-2">{:__('Salt')}:</label>
  23 + <div class="col-xs-12 col-sm-8">
  24 + <input id="c-salt" data-rule="required" class="form-control" name="row[salt]" type="text" value="{$row.salt|htmlentities}">
  25 + </div>
  26 + </div>
  27 + <div class="form-group">
  28 + <label class="control-label col-xs-12 col-sm-2">{:__('Avatar')}:</label>
  29 + <div class="col-xs-12 col-sm-8">
  30 + <div class="input-group">
  31 + <input id="c-avatar" data-rule="required" class="form-control" size="50" name="row[avatar]" type="text" value="{$row.avatar|htmlentities}">
  32 + <div class="input-group-addon no-border no-padding">
  33 + <span><button type="button" id="plupload-avatar" class="btn btn-danger plupload" data-input-id="c-avatar" data-mimetype="image/gif,image/jpeg,image/png,image/jpg,image/bmp" data-multiple="false" data-preview-id="p-avatar"><i class="fa fa-upload"></i> {:__('Upload')}</button></span>
  34 + <span><button type="button" id="fachoose-avatar" class="btn btn-primary fachoose" data-input-id="c-avatar" data-mimetype="image/*" data-multiple="false"><i class="fa fa-list"></i> {:__('Choose')}</button></span>
  35 + </div>
  36 + <span class="msg-box n-right" for="c-avatar"></span>
  37 + </div>
  38 + <ul class="row list-inline plupload-preview" id="p-avatar"></ul>
  39 + </div>
  40 + </div>
  41 + <div class="form-group">
  42 + <label class="control-label col-xs-12 col-sm-2">{:__('Email')}:</label>
  43 + <div class="col-xs-12 col-sm-8">
  44 + <input id="c-email" data-rule="required" class="form-control" name="row[email]" type="text" value="{$row.email|htmlentities}">
  45 + </div>
  46 + </div>
  47 + <div class="form-group">
  48 + <label class="control-label col-xs-12 col-sm-2">{:__('Loginfailure')}:</label>
  49 + <div class="col-xs-12 col-sm-8">
  50 + <input id="c-loginfailure" data-rule="required" class="form-control" name="row[loginfailure]" type="number" value="{$row.loginfailure|htmlentities}">
  51 + </div>
  52 + </div>
  53 + <div class="form-group">
  54 + <label class="control-label col-xs-12 col-sm-2">{:__('Logintime')}:</label>
  55 + <div class="col-xs-12 col-sm-8">
  56 + <input id="c-logintime" class="form-control datetimepicker" data-date-format="YYYY-MM-DD HH:mm:ss" data-use-current="true" name="row[logintime]" type="text" value="{:$row.logintime?datetime($row.logintime):''}">
  57 + </div>
  58 + </div>
  59 + <div class="form-group">
  60 + <label class="control-label col-xs-12 col-sm-2">{:__('Loginip')}:</label>
  61 + <div class="col-xs-12 col-sm-8">
  62 + <input id="c-loginip" class="form-control" name="row[loginip]" type="text" value="{$row.loginip|htmlentities}">
  63 + </div>
  64 + </div>
  65 + <div class="form-group">
  66 + <label class="control-label col-xs-12 col-sm-2">{:__('Token')}:</label>
  67 + <div class="col-xs-12 col-sm-8">
  68 + <input id="c-token" data-rule="required" class="form-control" name="row[token]" type="text" value="{$row.token|htmlentities}">
  69 + </div>
  70 + </div>
  71 + <div class="form-group">
  72 + <label class="control-label col-xs-12 col-sm-2">{:__('Status')}:</label>
  73 + <div class="col-xs-12 col-sm-8">
  74 + <input id="c-status" data-rule="required" class="form-control" name="row[status]" type="text" value="{$row.status|htmlentities}">
  75 + </div>
  76 + </div>
  77 + <div class="form-group layer-footer">
  78 + <label class="control-label col-xs-12 col-sm-2"></label>
  79 + <div class="col-xs-12 col-sm-8">
  80 + <button type="submit" class="btn btn-success btn-embossed disabled">{:__('OK')}</button>
  81 + <button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>
  82 + </div>
  83 + </div>
  84 +</form>
  1 +<div class="panel panel-default panel-intro">
  2 + {:build_heading()}
  3 +
  4 + <div class="panel-body">
  5 + <div id="myTabContent" class="tab-content">
  6 + <div class="tab-pane fade active in" id="one">
  7 + <div class="widget-body no-padding">
  8 + <div id="toolbar" class="toolbar">
  9 + <a href="javascript:;" class="btn btn-primary btn-refresh" title="{:__('Refresh')}" ><i class="fa fa-refresh"></i> </a>
  10 + <a href="javascript:;" class="btn btn-success btn-add {:$auth->check('admin/add')?'':'hide'}" title="{:__('Add')}" ><i class="fa fa-plus"></i> {:__('Add')}</a>
  11 + <a href="javascript:;" class="btn btn-success btn-edit btn-disabled disabled {:$auth->check('admin/edit')?'':'hide'}" title="{:__('Edit')}" ><i class="fa fa-pencil"></i> {:__('Edit')}</a>
  12 + <a href="javascript:;" class="btn btn-danger btn-del btn-disabled disabled {:$auth->check('admin/del')?'':'hide'}" title="{:__('Delete')}" ><i class="fa fa-trash"></i> {:__('Delete')}</a>
  13 + <a href="javascript:;" class="btn btn-danger btn-import {:$auth->check('admin/import')?'':'hide'}" title="{:__('Import')}" id="btn-import-file" data-url="ajax/upload" data-mimetype="csv,xls,xlsx" data-multiple="false"><i class="fa fa-upload"></i> {:__('Import')}</a>
  14 +
  15 + <div class="dropdown btn-group {:$auth->check('admin/multi')?'':'hide'}">
  16 + <a class="btn btn-primary btn-more dropdown-toggle btn-disabled disabled" data-toggle="dropdown"><i class="fa fa-cog"></i> {:__('More')}</a>
  17 + <ul class="dropdown-menu text-left" role="menu">
  18 + <li><a class="btn btn-link btn-multi btn-disabled disabled" href="javascript:;" data-params="status=normal"><i class="fa fa-eye"></i> {:__('Set to normal')}</a></li>
  19 + <li><a class="btn btn-link btn-multi btn-disabled disabled" href="javascript:;" data-params="status=hidden"><i class="fa fa-eye-slash"></i> {:__('Set to hidden')}</a></li>
  20 + </ul>
  21 + </div>
  22 +
  23 +
  24 + </div>
  25 + <table id="table" class="table table-striped table-bordered table-hover table-nowrap"
  26 + data-operate-edit="{:$auth->check('admin/edit')}"
  27 + data-operate-del="{:$auth->check('admin/del')}"
  28 + width="100%">
  29 + </table>
  30 + </div>
  31 + </div>
  32 +
  33 + </div>
  34 + </div>
  35 +</div>
  1 +define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
  2 +
  3 + var Controller = {
  4 + index: function () {
  5 + // 初始化表格参数配置
  6 + Table.api.init({
  7 + extend: {
  8 + index_url: 'admin/index' + location.search,
  9 + add_url: 'admin/add',
  10 + edit_url: 'admin/edit',
  11 + del_url: 'admin/del',
  12 + multi_url: 'admin/multi',
  13 + table: 'admin',
  14 + }
  15 + });
  16 +
  17 + var table = $("#table");
  18 +
  19 + // 初始化表格
  20 + table.bootstrapTable({
  21 + url: $.fn.bootstrapTable.defaults.extend.index_url,
  22 + pk: 'id',
  23 + sortName: 'id',
  24 + columns: [
  25 + [
  26 + {checkbox: true},
  27 + {field: 'id', title: __('Id')},
  28 + {field: 'username', title: __('Username')},
  29 + {field: 'nickname', title: __('Nickname')},
  30 + {field: 'password', title: __('Password')},
  31 + {field: 'salt', title: __('Salt')},
  32 + {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image},
  33 + {field: 'email', title: __('Email')},
  34 + {field: 'loginfailure', title: __('Loginfailure')},
  35 + {field: 'logintime', title: __('Logintime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
  36 + {field: 'loginip', title: __('Loginip')},
  37 + {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
  38 + {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
  39 + {field: 'token', title: __('Token')},
  40 + {field: 'status', title: __('Status'), formatter: Table.api.formatter.status},
  41 + {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
  42 + ]
  43 + ]
  44 + });
  45 +
  46 + // 为表格绑定事件
  47 + Table.api.bindevent(table);
  48 + },
  49 + add: function () {
  50 + Controller.api.bindevent();
  51 + },
  52 + edit: function () {
  53 + Controller.api.bindevent();
  54 + },
  55 + api: {
  56 + bindevent: function () {
  57 + Form.api.bindevent($("form[role=form]"));
  58 + }
  59 + }
  60 + };
  61 + return Controller;
  62 +});