bztang-admin/plugins/article/src/admin/ManagerController.php

221 lines
7.9 KiB
PHP

<?php
namespace Yunshop\Article\admin;
use app\common\components\BaseController;
use app\common\exceptions\ShopException;
use app\common\helpers\PaginationHelper;
use app\common\helpers\Url;
use app\common\models\user\UserPermission;
use app\common\models\user\WeiQingUsers;
use Illuminate\Support\Facades\DB;
use Yunshop\Article\models\Manager;
use Yunshop\Article\models\UniAccountUser;
use Yunshop\Supplier\admin\models\Supplier;
class ManagerController extends BaseController
{
public function index()
{
$search = request()->search;
$pageSize = 20;
$list = Manager::getList($search)->paginate($pageSize);
$pager = PaginationHelper::show($list->total(), $list->currentPage(), $list->perPage());
$list = $list->toArray();
return view('Yunshop\Article::admin.manager.list', [
'list' => $list,
'pager' => $pager,
'search' => $search,
'total' => Manager::uniacid()->count()
])->render();
}
public function changeStatus()
{
$id = (int)request()->id;
$manager = Manager::find($id);
if ($manager->status == 1) {
$manager->status = 0;
if ($manager->save()) {
return $this->successJson('禁用成功');
} else {
return $this->errorJson('禁用失败');
}
} else {
$manager->status = 1;
if ($manager->save()) {
return $this->successJson('取消禁用');
} else {
return $this->errorJson('取消禁用失败');
}
}
}
public function add()
{
if (\Request::getMethod() == 'POST') {
$data = request()->data;
$result = WeiQingUsers::getUserByUserName($data['username'])->first();
if ($result) {
return $this->message('此用户为系统存在用户,无法添加', Url::absoluteWeb('plugin.supplier.admin.controllers.supplier.supplier-list', $data), 'error');
}
if (!$data['password']) {
throw new ShopException('请输入密码');
}
if ($data['password'] != $data['password_confirm']) {
throw new ShopException('两次输入密码不一样,请检查');
}
$model = new Manager();
$data['uniacid'] = \YunShop::app()->uniacid;
$data['status'] = 1;
unset($data['password_confirm']);
DB::beginTransaction();
try {
$model->fill($data);
$validator = $model->validator();
if ($validator->fails()) {
throw new ShopException($validator->messages()->first());
}
$verifyPassword = verifyPasswordStrength($data['password']);
if ($verifyPassword !== true) {
throw new ShopException($verifyPassword);
}
$uid = $this->addWeiqingTables($data['username'], $data['password']);
if (is_array($uid) && $uid['errno'] == -1) {
throw new ShopException($uid['message']);
} else {
if ($model->save()) {
$data['uid'] = $uid;
$model->uid = $data['uid'];
$model->save();
}
}
DB::commit();
return $this->message('添加成功', Url::absoluteWeb('plugin.article.admin.manager'));
} catch (\Exception $e) {
DB::rollBack();
throw new ShopException($e->getMessage());
}
}
return view('Yunshop\Article::admin.manager.add_manager', [
])->render();
}
public function edit()
{
$id = intval(\YunShop::request()->id);
$Model = Manager::uniacid()->where('id', $id)
->select(['id', 'member_id', 'nickname', 'username'])
->with('member')
->first();
if (!$Model) {
return $this->message('无此管理员或已被删除', '', 'error');
}
if (\Request::getMethod() == 'POST') {
$data = request()->data;
if ($Model->member_id != $data['member_id']) {
$finder = Manager::uniacid()->where('member_id', $data['member_id'])->first();
if ($finder) {
throw new ShopException('角色已经存在');
}
}
if ($Model->nickname != $data['nickname']) {
$find = Manager::uniacid()->where('nickname', $data['nickname'])->first();
if ($find) {
throw new ShopException('管理员名称已经存在');
}
}
$Model->member_id = $data['member_id'];
$Model->nickname = $data['nickname'];
if (!$Model->save()) {
return $this->message('MySql error, please try again', '', 'error');
} else {
return $this->message('修改管理员成功', Url::absoluteWeb('plugin.article.admin.manager'));
}
}
return view('Yunshop\Article::admin.manager.edit_info', [
'manager' => $Model->toArray()
])->render();
}
public function editPwd()
{
$id = intval(\YunShop::request()->id);
$Model = Manager::uniacid()->where('id', $id)
->select(['id', 'uid', 'member_id', 'username'])
->with('member')
->first();
if (!$Model) {
return $this->message('无此管理员或已被删除', '', 'error');
}
if (\Request::getMethod() == 'POST') {
$data = request()->data;
if (!$data['password']) {
throw new ShopException('请输入密码');
}
if ($data['password'] != $data['password_confirm']) {
throw new ShopException('两次输入密码不一样,请检查');
}
$user = WeiQingUsers::getUserByUid($Model->uid)->first();
if (!$user) {
throw new ShopException('操作员不存在');
}
$verifyPassword = verifyPasswordStrength($data['password']);
if ($verifyPassword !== true) {
return $this->message($verifyPassword);
}
DB::beginTransaction();
try {
$password = user_hash($data['password'], $user->salt);
$user->password = $password;
$user->save();
$Model->password = $data['password'];
$Model->save();
DB::commit();
return $this->message('修改密码成功', Url::absoluteWeb('plugin.article.admin.manager'));
} catch (\Exception $e) {
DB::rollBack();
throw new ShopException($e->getMessage());
}
}
return view('Yunshop\Article::admin.manager.edit_pwd', [
'manager' => $Model->toArray()
])->render();
}
private function addWeiqingTables($username, $password)
{
$uid = user_register(array('username' => $username, 'password' => $password), '');
if (is_array($uid) || $uid == 0) {
return $uid;
}
UniAccountUser::AddUniAccountUser($uid);
WeiQingUsers::updateType($uid);
$model = new UserPermission();
$permission = [
'uniacid' => \YunShop::app()->uniacid,
'uid' => $uid,
'type' => 'yun_shop',
'permission' => 'yun_shop_cover_shop|yun_shop_rule|yun_shop_menu_shop',
'url' => 'bztang.cdlfjy.com'
];
if ($model->hasColumn('modules')) {
$permission['modules'] = 'yunzhong';
}
if ($model->hasColumn('templates')) {
$permission['templates'] = 'yunzhong';
}
$model->fill($permission);
$model->save();
return $uid;
}
}