php后台设计开发思路,基于(tp5.1、tp6.0)后台开发框架(设计思路参考laravel-admin)

本文档详细介绍了如何使用ThinkPHP框架实现会员管理功能,包括数据模型定义、搜索过滤、表格构建、表单编辑与保存。通过实例展示了如何进行条件查询、数据展示和状态管理,适合开发者理解和实践后台管理系统的实现。
摘要由CSDN通过智能技术生成

namespace app\admin\controller;

use app\common\logic\MemberLogic;

use app\common\model;

use think\Controller;

use tpext\builder\traits\HasBuilder;

/**

* Undocumented class

* @title 会员管理

*/

class Member extends Controller

{

use HasBuilder;

/**

* Undocumented variable

*

* @var model\Member

*/

protected $dataModel;

protected function initialize()

{

$this->dataModel = new model\Member;

$this->pageTitle = '会员管理';

$this->enableField = 'status';

$this->pagesize = 8;

/* 作为下拉选择数据源 相关设置 */

//显示

$this->selectTextField = '{id}#{nickname}({mobile})';

//like查询字段,$this->dataModel->where('username|nickname|mobile', 'like', $kwd);

$this->selectSearch = 'username|nickname|mobile';

}

protected function filterWhere()

{

$searchData = request()->post();

$where = [];

if (!empty($searchData['id'])) {

$where[] = ['id', 'eq', $searchData['id']];

}

if (!empty($searchData['username'])) {

$where[] = ['username', 'like', '%' . $searchData['username'] . '%'];

}

if (!empty($searchData['nickname'])) {

$where[] = ['nickname', 'like', '%' . $searchData['nickname'] . '%'];

}

if (!empty($searchData['mobile'])) {

$where[] = ['mobile', 'like', '%' . $searchData['mobile'] . '%'];

}

if (isset($searchData['status']) && $searchData['status'] != '') {

$where[] = ['status', 'eq', $searchData['status']];

}

if (isset($searchData['level']) && $searchData['level'] != '') {

$where[] = ['level', 'eq', $searchData['level']];

}

if (!empty($searchData['province'])) {

$where[] = ['province', 'eq', $searchData['province']];

if (!empty($searchData['city'])) {

$where[] = ['city', 'eq', $searchData['city']];

if (!empty($searchData['area'])) {

$where[] = ['area', 'eq', $searchData['area']];

}

}

}

return $where;

}

/**

* 构建搜索

*

* @return void

*/

protected function builSearch()

{

$search = $this->search;

$search->text('id', '会员id')->maxlength(20);

$search->text('username', '账号')->maxlength(20);

$search->text('nickname', '昵称')->maxlength(20);

$search->text('mobile', '手机号')->maxlength(20);

$search->select('level', '等级')->optionsData(model\MemberLevel::order('level')->select(), 'name', 'level')->afterOptions([0 => '普通会员']);

$search->select('status', '状态')->options([0 => '禁用', 1 => '正常']);

$search->select('province', '省份')->dataUrl(url('api/areacity/province'), 'ext_name')->withNext(

$search->select('city', '城市')->dataUrl(url('api/areacity/city'), 'ext_name')->withNext(

$search->select('area', '地区')->dataUrl(url('api/areacity/area'), 'ext_name')

)

);

}

/**

* 构建表格

*

* @return void

*/

protected function buildTable(&$data = [])

{

$table = $this->table;

$table->show('id', 'ID');

$table->image('avatar', '头像')->thumbSize(50, 50)->default('/static/images/touxiang.png');

$table->show('username', '账号');

$table->text('nickname', '昵称')->autoPost()->getWrapper()->addStyle('width:130px');

$table->show('mobile', '手机号')->getWrapper()->addStyle('width:100px');

$table->match('gender', '性别')->options([1 => '男', 2 => '女', 0 => '未知'])->getWrapper()->addStyle('width:50px');

$table->show('age', '性别');

$table->show('level_name', '等级');

$table->show('money', model\MemberAccount::$types['money']);

$table->show('points', model\MemberAccount::$types['points']);

$table->show('pca', '省市区');

$table->switchBtn('status', '状态')->default(1)->autoPost()->getWrapper()->addStyle('width:60px');

$table->show('last_login_time', '最近登录')->getWrapper()->addStyle('width:150px');

$table->show('create_time', '注册时间')->getWrapper()->addStyle('width:150px');

$table->sortable('id,sort,money,points,commission,re_comm,shares,last_login_time');

$table->getToolbar()

->btnAdd()

->btnEnableAndDisable('启用', '禁用')

->btnRefresh();

$table->getActionbar()

->btnEdit()

->btnView()

->btnLink('account', url('/admin/memberaccount/add', ['member_id' => '__data.pk__']), '', 'btn-success', 'mdi-square-inc-cash');

}

/**

* 构建表单

*

* @param boolean $isEdit

* @param array $data

*/

protected function builForm($isEdit, &$data = [])

{

$form = $this->form;

$form->tab('基本信息');

$form->image('avatar', '头像')->thumbSize(50, 50);

$form->text('username', '账号')->required()->maxlength(20);

$form->text('nickname', '昵称')->required()->maxlength(20);

$form->text('mobile', '手机号')->maxlength(11);

$form->text('email', '邮件')->maxlength(60);

$form->number('age', '年龄')->max(100)->min(1)->default(18);

$form->radio('gender', '性别')->options([0 => '未知', 1 => '男', 2 => '女'])->default(0);

$form->tab('其他信息');

$form->textarea('remark', '备注')->maxlength(255);

$form->switchBtn('status', '状态')->default(1);

if ($isEdit) {

$form->show('last_login_time', '最近登录时间');

$form->show('create_time', '注册时间');

$form->show('update_time', '修改时间');

}

}

/**

* 保存数据

*

* @param integer $id

* @return void

*/

private function save($id = 0)

{

$data = request()->only([

'avatar',

'username',

'nickname',

'mobile',

'email',

'gender',

'age',

'status',

'remark',

], 'post');

$result = $this->validate($data, [

'username|账号' => 'require',

'nickname|昵称' => 'require',

'mobile|手机号' => 'mobile',

'age|年龄' => 'number',

]);

if (true !== $result) {

$this->error($result);

}

return $this->doSave($data, $id);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值