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 = $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('解绑成功'); } }