Files
zyt/server/app/adminapi/controller/auth/AdminController.php
T
2026-05-11 17:49:38 +08:00

214 lines
6.8 KiB
PHP
Executable File

<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台(PHP版)
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用,可去除界面版权logo
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
// | github下载:https://github.com/likeshop-github/likeadmin
// | 访问官网:https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\adminapi\controller\auth;
use app\adminapi\controller\BaseAdminController;
use app\adminapi\lists\auth\AdminLists;
use app\adminapi\validate\auth\AdminValidate;
use app\adminapi\logic\auth\AdminLogic;
use app\adminapi\logic\LoginLogic;
use app\adminapi\validate\auth\editSelfValidate;
use app\common\cache\AdminTokenCache;
use app\common\model\auth\Admin;
/**
* 管理员控制器
* Class AdminController
* @package app\adminapi\controller\auth
*/
class AdminController extends BaseAdminController
{
/**
* @notes 查看管理员列表
* @return \think\response\Json
* @author 段誉
* @date 2021/12/29 9:55
*/
public function lists()
{
return $this->dataLists(new AdminLists());
}
/**
* @notes 添加管理员
* @return \think\response\Json
* @author 段誉
* @date 2021/12/29 10:21
*/
public function add()
{
$params = (new AdminValidate())->post()->goCheck('add');
$result = AdminLogic::add($params);
if (true === $result) {
return $this->success('操作成功', [], 1, 1);
}
return $this->fail(AdminLogic::getError());
}
/**
* @notes 编辑管理员
* @return \think\response\Json
* @author 段誉
* @date 2021/12/29 11:03
*/
public function edit()
{
$params = (new AdminValidate())->post()->goCheck('edit');
$result = AdminLogic::edit($params);
if (true === $result) {
return $this->success('操作成功', [], 1, 1);
}
return $this->fail(AdminLogic::getError());
}
/**
* @notes 删除管理员
* @return \think\response\Json
* @author 段誉
* @date 2021/12/29 11:03
*/
public function delete()
{
$params = (new AdminValidate())->post()->goCheck('delete');
$result = AdminLogic::delete($params);
if (true === $result) {
return $this->success('操作成功', [], 1, 1);
}
return $this->fail(AdminLogic::getError());
}
/**
* @notes 查看管理员详情
* @return \think\response\Json
* @author 段誉
* @date 2021/12/29 11:07
*/
public function detail()
{
$params = (new AdminValidate())->goCheck('detail');
$result = AdminLogic::detail($params);
return $this->data($result);
}
/**
* @notes 获取当前管理员信息
* @return \think\response\Json
* @author 段誉
* @date 2021/12/31 10:53
*/
public function mySelf()
{
$result = AdminLogic::detail(['id' => $this->adminId], 'auth');
return $this->data($result);
}
/**
* @notes 编辑超级管理员信息
* @return \think\response\Json
* @author 段誉
* @date 2022/4/8 17:54
*/
public function editSelf()
{
$params = (new editSelfValidate())->post()->goCheck('', ['admin_id' => $this->adminId]);
$result = AdminLogic::editSelf($params);
return $this->success('操作成功', [], 1, 1);
}
/**
* @notes 企业微信扫码绑定(用 code 换取 userid 并保存到当前管理员)
*/
public function bindWorkWechat()
{
$code = $this->request->post('code', '');
if (empty($code)) {
return $this->fail('缺少授权code');
}
$corpId = env('work_wechat.corp_id', '');
$secret = env('work_wechat.secret', '');
if (empty($corpId) || empty($secret)) {
return $this->fail('企业微信未配置');
}
$accessToken = LoginLogic::getWorkWechatAccessTokenStatic($corpId, $secret);
if (!$accessToken) {
return $this->fail('获取企业微信凭证失败');
}
$url = "https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?access_token={$accessToken}&code={$code}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = curl_exec($ch);
curl_close($ch);
$response = json_decode($result, true);
if (!$response || ($response['errcode'] ?? -1) != 0) {
return $this->fail('企业微信授权失败: ' . ($response['errmsg'] ?? '未知错误'));
}
$wxUserId = LoginLogic::workWechatUserIdFromAuthResponse($response);
if ($wxUserId === '') {
$hasOpenId = trim((string) ($response['openid'] ?? $response['OpenId'] ?? '')) !== '';
return $this->fail(
$hasOpenId
? '当前扫码账号非企业通讯录成员(或尚未同步到通讯录),无法绑定。请使用已在企业微信通讯录中的成员扫码,或联系管理员将你加入企业后再试'
: '未获取到企业微信成员 userid。请确认 .env 中 work_wechat.secret 为「该自建应用」的 Secret(与 agent_id 对应),且绑定页完整 URL 已加入应用可信域名'
);
}
// 检查是否已被其他管理员绑定
$exists = Admin::where('work_wechat_userid', $wxUserId)
->where('id', '<>', $this->adminId)
->find();
if ($exists) {
return $this->fail('该企业微信账号已被其他管理员绑定');
}
$affected = Admin::where('id', $this->adminId)->update(['work_wechat_userid' => $wxUserId]);
if ($affected === 0) {
return $this->fail('保存绑定失败,请确认账号有效后重试');
}
$token = $this->request->header('token');
if ($token) {
(new AdminTokenCache())->deleteAdminInfo($token);
}
return $this->success('绑定成功', ['work_wechat_userid' => $wxUserId]);
}
/**
* @notes 解绑企业微信
*/
public function unbindWorkWechat()
{
Admin::where('id', $this->adminId)->update(['work_wechat_userid' => '']);
return $this->success('解绑成功');
}
}