1. Go to this page and download the library: Download zoujingli/thinkadmin library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
zoujingli / thinkadmin example snippets
namespace app\plugin\example;
use think\admin\Plugin;
class ExamplePlugin extends Plugin
{
public function install()
{
// 安装插件时的操作
$this->createTables();
$this->createMenus();
}
public function uninstall()
{
// 卸载插件时的操作
$this->dropTables();
$this->removeMenus();
}
}
namespace app\admin\controller;
use think\admin\Controller;
class UserController extends Controller
{
/**
* 用户列表
* @auth true
* @menu true
*/
public function index()
{
$this->title = '用户管理';
$this->_query('SystemUser')
->like('username,nickname,phone')
->equal('status')
->dateBetween('create_at')
->order('id desc')
->page();
}
}
namespace app\admin\service;
use think\admin\Storage;
class FileService
{
public function upload($file, $type = 'local')
{
$storage = Storage::instance($type);
// 文件去重检查
$hash = md5_file($file->getPathname());
$exists = $this->checkFileExists($hash);
if ($exists) {
return $exists['url'];
}
// 上传文件
$result = $storage->upload($file);
return $result['url'];
}
}
namespace app\admin\service;
use think\admin\service\QueueService;
class TaskService
{
// 注册邮件发送任务
public function sendEmail($to, $subject, $content)
{
$name = "发送邮件到 {$to}";
$command = "xadmin:service email {$to} {$subject}";
return QueueService::register($name, $command, 0);
}
}
// 邮件队列处理类
class EmailQueue extends QueueService
{
public function execute(array $data = [])
{
$this->setQueueProgress('开始发送邮件...', 10.00);
try {
// 发送邮件逻辑
$result = $this->sendMail($data['to'], $data['subject'], $data['content']);
if ($result) {
$this->setQueueProgress('邮件发送成功', 100.00);
$this->setQueueSuccess('邮件发送成功');
} else {
$this->setQueueError('邮件发送失败');
}
} catch (\Exception $e) {
$this->setQueueError('邮件发送异常: ' . $e->getMessage());
}
}
}
namespace app\admin\controller;
use think\admin\Controller;
class ProductController extends Controller
{
protected $dbQuery = 'Product';
public function index()
{
$this->title = '商品管理';
$this->_query($this->dbQuery)
->like('name,description')
->equal('category_id,status')
->dateBetween('create_at')
->order('id desc')
->page();
}
public function add()
{
$this->title = '添加商品';
$this->_form($this->dbQuery, 'form');
}
public function save()
{
$this->_save($this->dbQuery, $this->_vali([
'status.
namespace app\plugin\example;
use think\admin\Plugin;
class ExamplePlugin extends Plugin
{
public function install()
{
// 安装插件时的操作
$this->createTables();
$this->createMenus();
}
public function uninstall()
{
// 卸载插件时的操作
$this->dropTables();
$this->removeMenus();
}
}