feat(平台后台): 添加会员app管理

master
yin5th 2023-09-27 15:46:05 +08:00
parent da41edc110
commit 701ce5fd9a
8 changed files with 591 additions and 1 deletions

View File

@ -0,0 +1,104 @@
<?php
namespace app\admin\controller\user;
use app\admin\logic\user\AppLogic;
use app\admin\validate\user\AppValidate;
use app\common\basics\AdminBase;
use app\common\server\JsonServer;
use think\exception\ValidateException;
class App extends AdminBase
{
public function lists()
{
if($this->request->isAjax()){
$get = $this->request->get();
$lists = AppLogic::lists($get);
return JsonServer::success('', $lists);
}
return view();
}
public function add()
{
if($this->request->isAjax()) {
try{
$post = $this->request->post();
validate(AppValidate::class)->scene('add')->check($post);
}catch(ValidateException $e) {
return JsonServer::error($e->getError());
}
$result = AppLogic::add($post);
if($result === true) {
return JsonServer::success('添加成功');
}
return JsonServer::error(AppLogic::getError());
}
return view();
}
public function getUserList()
{
$keyword = input('keyword/s');
$page = input('page/d', 1);
$size = 10;
$q = \app\common\model\user\User::where('nickname|mobile', 'like', '%'.$keyword.'%');
$total = $q->count();
$userList = $q->page($page, $size)
->field('id,nickname,mobile')
->select()
->toArray();
foreach ($userList as &$item) {
$item['name'] = $item['nickname'].'-'.$item['mobile'];
$item['value'] = $item['id'];
}
$res = [
'code' => 0,
'data' => [
'total' => $total,
'totalPage' => ceil($total/$size),
'list' => $userList,
'page' => $page,
'size' => $size,
],
'msg' => 'success'
];
return json($res);
}
public function edit(){
if($this->request->isAjax()){
try{
$post = $this->request->post();
validate(AppValidate::class)->scene('edit')->check($post);
}catch(ValidateException $e) {
return JsonServer::error($e->getError());
}
$result = AppLogic::edit($post);
if($result === true) {
return JsonServer::success('编辑成功');
}
return JsonServer::error(AppLogic::getError());
}
$id = $this->request->get('id', '', 'intval');
$detail = AppLogic::getUserApi($id);
return view('', [
'detail' => $detail
]);
}
public function del()
{
$id = $this->request->post('id', '', 'intval');
$result = AppLogic::del($id);
if($result === true) {
return JsonServer::success('删除成功');
}
return JsonServer::error(AppLogic::getError());
}
}

View File

@ -0,0 +1,105 @@
<?php
namespace app\admin\logic\user;
use app\common\basics\Logic;
use app\common\model\user\User;
use app\common\model\user\UserApi;
use think\facade\Db;
class AppLogic extends Logic
{
public static function lists($get)
{
$count = UserApi::count();
$lists = UserApi::alias('ua')->leftJoin('user u', 'u.id = ua.user_id')
->order('ua.id', 'desc')
->page($get['page'], $get['limit'])
->field('ua.*,u.nickname')
->select()
->toArray();
return ['count' => $count, 'lists' => $lists];
}
public static function add($post)
{
try{
if (empty($post['user_id']) || empty($post['app_id']) || empty($post['app_secret'])) {
throw new \Exception('参数错误');
}
$userLevel = UserApi::where(['app_id'=>trim($post['app_id'])])->findOrEmpty();
if(!$userLevel->isEmpty()) {
throw new \Exception('APP_ID已被使用请更换后重试');
}
$data = [
'user_id' => trim($post['user_id']),
'app_id' => trim($post['app_id']),
'app_secret' => trim($post['app_secret']),
'remarks' => trim($post['remarks']),
'status' => 1,
];
UserApi::create($data);
User::where('id', $post['user_id'])->save([
'is_api' => 1
]);
return true;
}catch(\Exception $e) {
self::$error = $e->getMessage();
return false;
}
}
public static function edit($post)
{
try{
$userLevel = UserApi::where([
['app_id', '=', trim($post['app_id'])],
['id', '<>', $post['id']]
])->findOrEmpty();
if(!$userLevel->isEmpty()) {
throw new \think\Exception('名称已被使用,请更换后重试');
}
$data = [
'id' => $post['id'],
'user_id' => trim($post['user_id']),
'app_id' => trim($post['app_id']),
'app_secret' => trim($post['app_secret']),
'remarks' => trim($post['remarks']),
];
UserApi::update($data);
return true;
}catch(\Exception $e) {
self::$error = $e->getMessage();
return false;
}
}
public static function del($id)
{
try{
UserApi::where('id', $id)->delete();
return true;
}catch(\Exception $e) {
self::$error = $e->getMessage();
return false;
}
}
public static function getUserApiList()
{
$levelArr = UserApi::field('id,name,phone')
->order('id desc')
->select()
->toArray();
return $levelArr;
}
public static function getUserApi($id){
$detail = UserApi::alias('ua')->leftJoin('user u', 'u.id=ua.user_id')->field('ua.*,u.nickname,u.mobile')->where(['ua.id'=>$id])->findOrEmpty();
if($detail->isEmpty()) {
return [];
}
$detail = $detail->toArray();
return $detail;
}
}

View File

@ -36,6 +36,11 @@ class UserLogic extends Logic
$where[] = ['team_id','=',$get['team_id']];
}
//是否API授权用户
if(isset($get['is_api']) && $get['is_api'] !== ''){
$where[] = ['is_api','=',$get['is_api']];
}
//等级查询
if(isset($get['level']) && $get['level'] !== ''){
$where[] = ['level','=',$get['level']];
@ -70,7 +75,7 @@ class UserLogic extends Logic
$user_count = User::where($where)->count();
$user_list = User::where($where)
->field('id,sn,nickname,avatar,level,total_order_amount,tag_ids,client,login_time,create_time,user_growth,user_money,earnings,first_leader,disable,team_id')
->field('id,sn,nickname,avatar,level,total_order_amount,tag_ids,client,login_time,create_time,user_growth,user_money,earnings,first_leader,disable,team_id,is_api')
->page($get['page'],$get['limit'])
->order('id desc')
->select()

View File

@ -0,0 +1,22 @@
<?php
namespace app\admin\validate\user;
use think\Validate;
class AppValidate extends Validate
{
protected $rule = [
'id|ID' => 'require',
'user_id|用户' => 'require',
'app_id|APP_ID' => 'require',
'app_secret|APP_SECRET' => 'require',
];
public function sceneAdd() {
return $this->only(['user_id', 'app_id', 'app_secret']);
}
public function sceneEdit() {
return $this->only(['user_id', 'app_id', 'app_secret']);
}
}

View File

@ -0,0 +1,97 @@
{layout name="layout2" /}
<style>
.layui-form-label {
color: #6a6f6c;
width: 100px;
}
.layui-input-block {
margin-left: 130px;
}
.tips{
color: red;
margin-right: 5px;
}
</style>
<div class="layui-form" lay-filter="layuiadmin-form-user_level" id="layuiadmin-form-user_level" style="padding: 20px 30px 0 0;">
<div class="layui-form-item">
<label class="layui-form-label"><span class="tips">*</span>选择用户:</label>
<div class="layui-input-block" style="width: 420px">
<div id="userList"></div>
</div>
</div>
<div class="layui-form-item" style="padding-bottom: 20px;">
<label class="layui-form-label"><span class="tips">*</span>APP_ID</label>
<div class="layui-input-block">
<input type="text" name="app_id" value="{:date('YmdHis')}{:uniqid()}" lay-verify="required" lay-verType="tips" autocomplete="off" class="layui-input">
<p class="word-aux">APP_ID和APP_SECRET为自动生产如不需要则自定义输入需要保证APP_ID全局唯一</p>
</div>
</div>
<div class="layui-form-item" style="padding-bottom: 20px;">
<label class="layui-form-label"><span class="tips">*</span>APP_SECRET</label>
<div class="layui-input-block">
<input type="text" name="app_secret" value="{:uniqid()}{:uniqid()}" lay-verify="required" lay-verType="tips" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">备注:</label>
<div class="layui-input-block">
<textarea name="remarks" class="layui-textarea"></textarea>
</div>
</div>
<div class="layui-form-item layui-hide">
<input type="button" lay-submit lay-filter="add-user_level-submit" id="add-user_level-submit" value="确认">
</div>
</div>
<script>
layui.config({
version:"{$front_version}",
base: '/static/lib/' //静态资源所在路径
}).extend({
xmSelect: 'xmSelect/xm-select'
}).use(['xmSelect', 'form'], function(){
var $ = layui.$,form = layui.form ;
var xmSelect = layui.xmSelect;
var xmIns = xmSelect.render({
el: '#userList',
radio: true,
name: "user_id",
autoRow: true,
pageSize: 3,
filterable: true,
pageEmptyShow: false,
paging: true,
//远程分页
pageRemote: true,
language: 'zn',
model: {
icon: 'hidden',
},
clickClose: true,
prop: {
name: 'name',
value: 'id',
},
remoteSearch: true,
remoteMethod: function(val, cb, show, pageIndex){
//val: 搜索框的内容, 不开启搜索默认为空, cb: 回调函数, show: 当前下拉框是否展开, pageIndex: 当前第几页
//这里如果val为空, 则不触发搜索
if(!val){
return cb([], 0);
}
$.get('/admin/user.app/getUserList', {keyword: val, page: pageIndex}, function (res) {
// var res = response.data;
// console.log(res, 'res')
//回调需要两个参数, 第一个: 数据数组, 第二个: 总页码
// console.log(res.data, 'data')
if (res.code === 0) {
cb(res.data.list, res.data.totalPage)
} else {
cb([], 0)
}
})
}
})
})
</script>

View File

@ -0,0 +1,49 @@
{layout name="layout2" /}
<style>
.layui-form-label {
color: #6a6f6c;
width: 100px;
}
.layui-input-block {
margin-left: 130px;
}
.tips{
color: red;
}
</style>
<div class="layui-form" lay-filter="layuiadmin-form-user_level" id="layuiadmin-form-user_level" style="padding: 20px 30px 0 0;">
<input type="hidden" name="id" value="{$detail.id}">
<div class="layui-form-item">
<label class="layui-form-label"><span class="tips">*</span>用户:</label>
<div class="layui-input-block">
<input type="text" readonly value="{$detail.nickname}-{$detail.mobile}" lay-verify="required" lay-verType="tips" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item" style="padding-bottom: 20px;">
<label class="layui-form-label"><span class="tips">*</span>APP_ID</label>
<div class="layui-input-block">
<input type="text" name="app_id" readonly value="{$detail.app_id}" lay-verify="required" lay-verType="tips" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item" style="padding-bottom: 20px;">
<label class="layui-form-label"><span class="tips">*</span>APP_SECRET</label>
<div class="layui-input-block">
<input type="text" name="app_secret" readonly value="{$detail.app_secret}" lay-verify="required" lay-verType="tips" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">备注:</label>
<div class="layui-input-block">
<textarea name="remarks" class="layui-textarea">{$detail.remarks}</textarea>
</div>
</div>
<div class="layui-form-item layui-hide">
<input type="hidden" name="user_id" value="{$detail.user_id}">
<input type="button" lay-submit lay-filter="edit-user_level-submit" id="edit-user_level-submit" value="确认">
</div>
</div>
<style>
.layui-form-label {
width: 100px;
}
</style>

View File

@ -0,0 +1,193 @@
{layout name="layout1" /}
<div class="wrapper">
<div class="layui-card">
<div class="layui-tab layui-tab-card" lay-filter="tab-all">
<div class="layui-card">
<div class="layui-card-body">
<div style="padding-bottom: 10px;" class="add">
<button class="layui-btn layui-btn-sm layuiadmin-btn-user_level {$view_theme_color}" data-type="add">新增</button>
</div>
<table id="user_level-lists" lay-filter="user_level-lists"></table>
<script type="text/html" id="user_level-operation">
<a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="edit">编辑</a>
<a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
</script>
</div>
</div>
</div>
</div>
</div>
<style>
.layui-table-cell {
height: auto;
}
</style>
<script>
layui.config({
version:"{$front_version}",
base: '/static/lib/' //静态资源所在路径
}).use(['table','form'], function(){
var $ = layui.$
,form = layui.form
,table = layui.table
,element = layui.element;
$('.layui-btn.layuiadmin-btn-user_level').on('click', function(){
var type = $(this).data('type');
active[type] ? active[type].call(this) : '';
});
layui.define(['table', 'form'], function(exports){
var $ = layui.$
,table = layui.table
,form = layui.form;
table.render({
id:'user_level-lists'
,elem: '#user_level-lists'
,url: '{:url("user.app/lists")}' //模拟接口
,cols: [[
{field: 'id', title: 'ID'},
{field: 'user_id', title: '用户ID'},
{field: 'nickname', title: '用户昵称'}
,{field: 'app_id',title:'APP_ID'}
,{field: 'app_secret',title:'APP_SECRET'}
,{fixed: 'right', title: '操作',align: 'center', toolbar: '#user_level-operation'}
]]
,page:true
,text: {none: '暂无数据!'}
,parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
return {
"code":res.code,
"msg":res.msg,
"count": res.data.count, //解析数据长度
"data": res.data.lists, //解析数据列表
};
}
,response: {
statusCode: 1
}
,done: function(res, curr, count){
// 解决操作栏因为内容过多换行问题
$(".layui-table-main tr").each(function (index, val) {
$($(".layui-table-fixed-l .layui-table-body tbody tr")[index]).height($(val).height());
$($(".layui-table-fixed-r .layui-table-body tbody tr")[index]).height($(val).height());
});
}
});
});
//事件
var active = {
add: function(){
var index = layer.open({
type: 2
,title: '新增'
,content: '{:url("user.app/add")}'
,area: ['90%', '90%']
,btn: ['保存', '取消']
,maxmin: true
,yes: function(index, layero){
var iframeWindow = window['layui-layer-iframe'+ index]
,submitID = 'add-user_level-submit'
,submit = layero.find('iframe').contents().find('#'+ submitID);
//监听提交
iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
var field = data.field;
like.ajax({
url:'{:url("user.app/add")}',
data:field,
type:"post",
success:function(res)
{
if(res.code == 1)
{
layui.layer.msg(res.msg, {
offset: '15px'
, icon: 1
, time: 1000
});
layer.close(index); //关闭弹层
table.reload('user_level-lists'); //数据刷新
}
}
});
});
submit.trigger('click');
}
});
},
}
table.on('tool(user_level-lists)', function(obj) {
var id = obj.data.id;
if (obj.event === 'edit') {
var index = layer.open({
type: 2
, title: '编辑'
, content: '{:url("user.app/edit")}?id=' + id
, area: ['90%', '90%']
, btn: ['保存', '取消']
, maxmin: true
, yes: function (index, layero) {
var iframeWindow = window['layui-layer-iframe' + index]
, submitID = 'edit-user_level-submit'
, submit = layero.find('iframe').contents().find('#' + submitID);
//监听提交
iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
var field = data.field;
like.ajax({
url: '{:url("user.app/edit")}',
data: field,
type: "post",
success: function (res) {
if (res.code == 1) {
layui.layer.msg(res.msg, {
offset: '15px'
, icon: 1
, time: 1000
});
layer.close(index); //关闭弹层
table.reload('user_level-lists'); //数据刷新
}
}
});
});
submit.trigger('click');
}
});
}
if (obj.event === 'del') {
layer.confirm('确定删除?如有正在使用该信息的应用将失效!', function (index) {
like.ajax({
url: '{:url("user.app/del")}',
data: {id: id},
type: "post",
success: function (res) {
if (res.code == 1) {
layui.layer.msg(res.msg, {
offset: '15px'
, icon: 1
, time: 1000
});
layer.close(index); //关闭弹层
table.reload('user_level-lists'); //数据刷新
}
}
});
layer.close(index);
})
}
})
});
</script>

View File

@ -79,6 +79,16 @@
</select>
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label">API授权</label>
<div class="layui-input-inline">
<select id="is_api" name="is_api" style="height:80px;width: 80px" >
<option value="">全部</option>
<option value="1"></option>
<option value="0"></option>
</select>
</div>
</div>
</div>
<div class="layui-form-item">
<div class="layui-inline">
@ -135,6 +145,10 @@
{{# } }}
</p>
<p>业务团队:{{d.team_name}}</p>
{{# if(d.is_api == 1){ }}
<p style="color: red">API授权用户</p>
{{# } }}
</div>
</script>
@ -233,6 +247,7 @@
$('#client').val(''); //清空输入框
$('#disable').val(''); //清空禁用状态
$('#team_id').val(''); //业务团队
$('#is_api').val(''); //业务团队
$('#total_amount_start').val(''); //清空输入框
$('#total_amount_end').val(''); //清空输入框
$('#start_time').val(''); //清空输入框