初始化项目
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 萤火科技 <admin@yiovo.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace app\admin\model\store;
|
||||
|
||||
use app\common\model\store\Api as ApiModel;
|
||||
|
||||
/**
|
||||
* 商家后台API权限模型
|
||||
* Class Api
|
||||
* @package app\admin\model\store
|
||||
*/
|
||||
class Api extends ApiModel
|
||||
{
|
||||
/**
|
||||
* 获取权限列表
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function getList()
|
||||
{
|
||||
$all = static::getAll();
|
||||
return $this->getTreeData($all);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增记录
|
||||
* @param $data
|
||||
* @return false|int
|
||||
*/
|
||||
public function add(array $data)
|
||||
{
|
||||
return $this->allowField(['name', 'parent_id', 'url', 'sort'])->save($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新记录
|
||||
* @param $data
|
||||
* @return bool
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function edit($data)
|
||||
{
|
||||
// 判断上级角色是否为当前子级
|
||||
if ($data['parent_id'] > 0) {
|
||||
// 获取所有上级id集
|
||||
$parentIds = $this->getTopApiIds($data['parent_id']);
|
||||
if (in_array($this['api_id'], $parentIds)) {
|
||||
$this->error = '上级权限不允许设置为当前子权限';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return $this->allowField(['name', 'parent_id', 'url', 'sort'])->save($data) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除权限
|
||||
* @return bool
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function remove()
|
||||
{
|
||||
// 判断是否存在下级权限
|
||||
if (self::detail(['parent_id' => $this['api_id']])) {
|
||||
$this->error = '当前权限下存在子权限,请先删除';
|
||||
return false;
|
||||
}
|
||||
return $this->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有上级id集
|
||||
* @param int $apiId
|
||||
* @param null $all
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
private function getTopApiIds(int $apiId, $all = null)
|
||||
{
|
||||
static $ids = [];
|
||||
is_null($all) && $all = $this->getAll();
|
||||
foreach ($all as $item) {
|
||||
if ($item['api_id'] == $apiId && $item['parent_id'] > 0) {
|
||||
$ids[] = $item['parent_id'];
|
||||
$this->getTopApiIds($item['parent_id'], $all);
|
||||
}
|
||||
}
|
||||
return $ids;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取树状列表
|
||||
* @param array $list
|
||||
* @param int $parentId
|
||||
* @return array
|
||||
*/
|
||||
private function getTreeData(array &$list, int $parentId = 0)
|
||||
{
|
||||
$data = [];
|
||||
foreach ($list as $key => $item) {
|
||||
if ($item['parent_id'] == $parentId) {
|
||||
$children = $this->getTreeData($list, (int)$item['api_id']);
|
||||
!empty($children) && $item['children'] = $children;
|
||||
$data[] = $item;
|
||||
unset($list[$key]);
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 萤火科技 <admin@yiovo.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace app\admin\model\store;
|
||||
|
||||
use app\common\model\store\Menu as MenuModel;
|
||||
use app\admin\model\store\MenuApi as MenuApiModel;
|
||||
|
||||
/**
|
||||
* 商家后台菜单模型
|
||||
* Class Menu
|
||||
* @package app\admin\model\store
|
||||
*/
|
||||
class Menu extends MenuModel
|
||||
{
|
||||
/**
|
||||
* 新增记录
|
||||
* @param $data
|
||||
* @return false|int
|
||||
*/
|
||||
public function add(array $data)
|
||||
{
|
||||
return $this->save($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新记录
|
||||
* @param array $data
|
||||
* @return bool
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function edit(array $data)
|
||||
{
|
||||
// 判断上级角色是否为当前子级
|
||||
if (isset($data['parent_id']) && $data['parent_id'] > 0) {
|
||||
// 获取所有上级id集
|
||||
$parentIds = $this->getTopMenuIds($data['parent_id']);
|
||||
if (in_array($this['menu_id'], $parentIds)) {
|
||||
$this->error = '上级菜单不允许设置为当前子菜单';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// 如果模块是操作, 不允许修改上级菜单id
|
||||
if ($this['module'] == 20 && isset($data['parent_id'])) {
|
||||
unset($data['parent_id']);
|
||||
}
|
||||
return $this->save($data) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置菜单的API权限
|
||||
* @param array $data
|
||||
* @return bool|int
|
||||
*/
|
||||
public function setApis(array $data)
|
||||
{
|
||||
if (!isset($data['apiIds']) || empty($data['apiIds'])) {
|
||||
$this->error = 'API权限不能为空';
|
||||
return false;
|
||||
}
|
||||
// 根据菜单id批量更新API关联记录
|
||||
return (new MenuApiModel)->updateByMenuId($this['menu_id'], $data['apiIds']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除菜单
|
||||
* @return bool
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function remove()
|
||||
{
|
||||
// 判断是否存在下级菜单
|
||||
if (self::detail(['parent_id' => $this['menu_id']])) {
|
||||
$this->error = '当前菜单下存在子菜单或操作,请先删除';
|
||||
return false;
|
||||
}
|
||||
return $this->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有上级id集
|
||||
* @param int $menuId
|
||||
* @param null $menuList
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
private function getTopMenuIds(int $menuId, $menuList = null)
|
||||
{
|
||||
static $ids = [];
|
||||
is_null($menuList) && $menuList = $this->getAll();
|
||||
foreach ($menuList as $item) {
|
||||
if ($item['menu_id'] == $menuId && $item['parent_id'] > 0) {
|
||||
$ids[] = $item['parent_id'];
|
||||
$this->getTopMenuIds($item['parent_id'], $menuList);
|
||||
}
|
||||
}
|
||||
return $ids;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 萤火科技 <admin@yiovo.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace app\admin\model\store;
|
||||
|
||||
use app\common\model\store\MenuApi as MenuApiModel;
|
||||
|
||||
/**
|
||||
* 商家后台用户角色与菜单权限关系表模型
|
||||
* Class MenuApi
|
||||
* @package app\admin\model\store
|
||||
*/
|
||||
class MenuApi extends MenuApiModel
|
||||
{
|
||||
/**
|
||||
* 根据菜单id批量更新记录
|
||||
* @param int $menuId
|
||||
* @param array $apiIds
|
||||
* @return int
|
||||
*/
|
||||
public function updateByMenuId(int $menuId, array $apiIds)
|
||||
{
|
||||
return $this->transaction(function () use ($menuId, $apiIds) {
|
||||
$this->removeByMenuId($menuId);
|
||||
return $this->insertByMenuId($menuId, $apiIds);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据菜单id批量删除记录
|
||||
* @param int $menuId
|
||||
* @return bool
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function removeByMenuId(int $menuId)
|
||||
{
|
||||
return $this->where('menu_id', '=', $menuId)->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据菜单id批量新增记录
|
||||
* @param int $menuId
|
||||
* @param array $apiIds
|
||||
* @return bool
|
||||
*/
|
||||
private function insertByMenuId(int $menuId, array $apiIds)
|
||||
{
|
||||
$data = [];
|
||||
foreach ($apiIds as $api) {
|
||||
$data[] = ['menu_id' => $menuId, 'api_id' => $api];
|
||||
}
|
||||
return (bool)$this->addAll($data);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 萤火科技 <admin@yiovo.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace app\admin\model\store;
|
||||
|
||||
use cores\exception\BaseException;
|
||||
use app\common\model\store\User as StoreUserModel;
|
||||
|
||||
/**
|
||||
* 商家用户模型
|
||||
* Class StoreUser
|
||||
* @package app\admin\model
|
||||
*/
|
||||
class User extends StoreUserModel
|
||||
{
|
||||
/**
|
||||
* 商家用户登录
|
||||
* @param int $storeId
|
||||
* @throws BaseException
|
||||
* @throws \think\Exception
|
||||
*/
|
||||
public function login(int $storeId)
|
||||
{
|
||||
// 获取该商户管理员用户信息
|
||||
$userInfo = $this->getSuperStoreUser($storeId);
|
||||
if (empty($userInfo)) {
|
||||
throwError('超级管理员用户信息不存在');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取该商户管理员用户信息
|
||||
* @param int $storeId
|
||||
* @return static|null
|
||||
*/
|
||||
private function getSuperStoreUser(int $storeId)
|
||||
{
|
||||
return static::detail(['store_id' => $storeId, 'is_super' => 1], ['wxapp']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除小程序下的商家用户
|
||||
* @param $storeId
|
||||
* @return false|int
|
||||
*/
|
||||
public static function setDelete(int $storeId)
|
||||
{
|
||||
static::update(['is_delete' => '1'], ['store_id' => $storeId]);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user