企微短剧业务系统

AuthController.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Log;
  5. use App\Models\System\Users;
  6. use App\Service\MsgService;
  7. use App\Support\qyApi\QyCommon;
  8. use Illuminate\Http\Request;
  9. include '../app/Libs/qyWeChatSDK/WXBizMsgCrypt.php';
  10. class AuthController extends Controller
  11. {
  12. private $aesKey;
  13. private $authToken;
  14. /*
  15. * 初始化
  16. * */
  17. public function __construct()
  18. {
  19. parent::__construct();
  20. $component = config('qyWechat.suite_info');
  21. if(empty($component)) {
  22. return self::returnValue([], 1001);
  23. }
  24. $this->aesKey = $component['aes_key'];
  25. $this->authToken = $component['auth_token'];
  26. }
  27. /*
  28. * 获取验证票据 ComponentVerifyTicket
  29. * */
  30. public function getVerifyTicket(Request $request)
  31. {
  32. $sVerifyMsgSig = $request->input('msg_signature');
  33. $sVerifyTimeStamp = $request->input('timestamp');
  34. $sVerifyNonce = $request->input('nonce');
  35. if($request->isMethod('get')){ // 初步校验回调链接合法性
  36. $sVerifyEchoStr = $request->input('echostr');
  37. $sEchoStr = ""; # 需要返回的明文
  38. $crypt = new \WXBizMsgCrypt($this->authToken, $this->aesKey, $this->corpid);
  39. $errCode = $crypt->VerifyURL($sVerifyMsgSig, $sVerifyTimeStamp, $sVerifyNonce, $sVerifyEchoStr, $sEchoStr);
  40. if ($errCode == 0) {
  41. echo $sEchoStr;
  42. } else {
  43. Log::logInfo('Errcode:'.$errCode, [], 'verifyTicketGetFail');
  44. }
  45. } elseif ($request->isMethod('post')){
  46. $requestMsg = file_get_contents('php://input');
  47. $sMsg = ""; # 需要返回的明文
  48. $crypt = new \WXBizMsgCrypt($this->authToken, $this->aesKey, $this->suiteId);
  49. $errCode = $crypt->DecryptMsg($sVerifyMsgSig, $sVerifyTimeStamp, $sVerifyNonce, $requestMsg, $sMsg);
  50. // Log::logInfo('解密后内容'. $sMsg, [], '0316');
  51. echo 'success';
  52. // if($errCode == 0) {
  53. $result = MsgService::responseCommand($sMsg);
  54. // if($result) {
  55. // echo 'success';
  56. // }
  57. // exit;
  58. // } else {
  59. // Log::logError('verifyTicket获取失败', ['errCode' => $errCode], 'verifyTicketPostFail');
  60. // }
  61. }
  62. }
  63. /**
  64. * 获取微信广告的授权二维码
  65. * @param Request $request
  66. */
  67. public function getWxadAuthCode(Request $request) {
  68. $url = 'https://developers.e.qq.com/oauth/authorize?';
  69. $wxadConfig = config('wxad');
  70. $sysGroupId = $request->input('sys_group_id');
  71. $userId = $request->input('admin_id');
  72. // 用户
  73. $user = Users::query()->find($userId);
  74. $sysGroupId = Users::query()
  75. ->where('enable', 1)
  76. ->where(function ($query) use($user, $userId, $sysGroupId) {
  77. if($user->is_system_admin) {
  78. $query->where('group_admin_id', $sysGroupId);
  79. } else {
  80. $query->where('id', $userId);
  81. }
  82. })
  83. ->value('group_admin_id');
  84. $query = http_build_query([
  85. 'client_id' => $wxadConfig['client_id'],
  86. 'state' => $sysGroupId,
  87. // 'state' => 'novel',
  88. 'scope' => '',
  89. 'redirect_uri' => $wxadConfig['redirect_uri'],
  90. 'account_type' => "ACCOUNT_TYPE_WECHAT",
  91. 'account_display_number' => 2
  92. ]);
  93. Log::logInfo('getWxadAuthCode', [
  94. 'params' => $request->all(),
  95. 'url' => $url.$query
  96. ], 'interface');
  97. echo $url.$query;
  98. }
  99. }