更新
This commit is contained in:
@@ -18,7 +18,9 @@ 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\model\auth\Admin;
|
||||
|
||||
/**
|
||||
* 管理员控制器
|
||||
@@ -131,4 +133,74 @@ class AdminController extends BaseAdminController
|
||||
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 = $response['userid'] ?? '';
|
||||
if (empty($wxUserId)) {
|
||||
return $this->fail('未获取到企业微信用户身份');
|
||||
}
|
||||
|
||||
// 检查是否已被其他管理员绑定
|
||||
$exists = Admin::where('work_wechat_userid', $wxUserId)
|
||||
->where('id', '<>', $this->adminId)
|
||||
->find();
|
||||
if ($exists) {
|
||||
return $this->fail('该企业微信账号已被其他管理员绑定');
|
||||
}
|
||||
|
||||
Admin::update([
|
||||
'id' => $this->adminId,
|
||||
'work_wechat_userid' => $wxUserId,
|
||||
]);
|
||||
|
||||
return $this->success('绑定成功', ['work_wechat_userid' => $wxUserId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 解绑企业微信
|
||||
*/
|
||||
public function unbindWorkWechat()
|
||||
{
|
||||
Admin::update([
|
||||
'id' => $this->adminId,
|
||||
'work_wechat_userid' => '',
|
||||
]);
|
||||
|
||||
return $this->success('解绑成功');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user