AbWeChatAuthorization.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace app\admin\library;
  3. use app\common\library\Rabbitmq;
  4. use app\common\library\Redis;
  5. use app\common\library\WeChatObject;
  6. use app\main\service\WeChatAdService;
  7. use GuzzleHttp\Client;
  8. use Symfony\Component\Cache\Simple\RedisCache;
  9. use EasyWeChat\Factory;
  10. use think\Config;
  11. use think\Log;
  12. class AbWeChatAuthorization{
  13. protected $platform;
  14. protected $adminConfig;
  15. protected $weChat;
  16. public function __construct($platform_id,$channel_id){
  17. if(!$platform_id || !$channel_id){
  18. throw new \Exception('Platform_id OR Channel_id 为空');
  19. }
  20. //获取平台信息
  21. if(!$this->platform = model('Platform')->where(['id'=>$platform_id])->find()){
  22. throw new \Exception('获取平台信息失败');
  23. }
  24. Log::info(sprintf('微信授权: platform_id:%d channel_id:%d 取得平台信息',$platform_id,$channel_id));
  25. //获取渠道信息
  26. if(!$this->adminConfig = model('AdminConfig')->where(['admin_id'=>$channel_id])->find()){
  27. throw new \Exception('获取用户信息失败');
  28. }
  29. Log::info(sprintf('微信授权: platform_id:%d channel_id:%d 取得渠道信息',$platform_id,$channel_id));
  30. $wechatObj = new WeChatObject($this->adminConfig);
  31. $this->weChat = $wechatObj->getPlatform($platform_id);
  32. }
  33. /**
  34. * 授权处理
  35. * @param bool $isUpdate 是否是更新权限
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. * @throws \think\exception\DbException
  39. */
  40. public function authorization($isUpdate = false){
  41. // 使用授权码换取接口调用凭据和授权信息
  42. $auth = $this->weChat->handleAuthorize();
  43. // 检查当前授权的公众号与历史授权公众号是否一致
  44. $existsAppID = model('WechatAb')->where('appid',$auth['authorization_info']['authorizer_appid'])->where('admin_id','neq',$this->adminConfig->admin_id)->find();
  45. if($existsAppID){
  46. //更新平台用户refreshToken信息
  47. $this->updateUserRefrshToken($this->platform->id,$auth['authorization_info']['authorizer_refresh_token'],$existsAppID['admin_id'], $auth['authorization_info']['authorizer_appid']);
  48. throw new \Exception('一个服务号只能绑定一个账号!');
  49. }
  50. // 获取授权方的帐号基本信息
  51. $authInfo = $this->weChat->getAuthorizer($auth['authorization_info']['authorizer_appid']);
  52. Log::info(sprintf('Ab微信授权: platform_id:%d channel_id:%d 微信授权检测通过',$this->platform['id'],$this->adminConfig['admin_id']));
  53. //检测权限
  54. $this->checkWeChatAuth($authInfo);
  55. //添加微信AD数据源
  56. $data['appid'] = $auth['authorization_info']['authorizer_appid'];
  57. $wx_ad_source_id = WeChatAdService::instance()->createWxAdSourceId($data['appid'], $this->platform->id, $auth['authorization_info']['authorizer_refresh_token']);
  58. Log::info("数据AD:". $wx_ad_source_id);
  59. if (!$wx_ad_source_id) {
  60. throw new \Exception('Ab微信授权失败');
  61. }
  62. $data['wx_ad_source_id'] = $wx_ad_source_id;
  63. $data['admin_id'] = $this->adminConfig->admin_id;
  64. $data['platform_id'] = $this->platform->id;
  65. $data['refresh_token'] = $auth['authorization_info']['authorizer_refresh_token'];
  66. $data['json'] = json_encode($authInfo, 256);
  67. $data['createtime'] = time();
  68. $data['updatetime'] = time();
  69. Log::info(sprintf('Ab微信授权: platform_id:%d channel_id:%d 微信基本信息更新完成',$this->platform['id'],$this->adminConfig['admin_id']));
  70. if (!model('WechatAb')->allowField(true)->save($data)) {
  71. throw new \Exception('Ab更新授权信息失败');
  72. }
  73. //清除缓存
  74. $redis = Redis::instance();
  75. $redis->del('AA:' . $auth['authorization_info']['authorizer_appid']);
  76. }
  77. /**
  78. * 更新用户RefrshToken
  79. * @param $platform_id
  80. * @param $refresh_token
  81. * @param $admin_id
  82. * @throws \think\db\exception\DataNotFoundException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. * @throws \think\exception\DbException
  85. * @throws \Exception
  86. */
  87. private function updateUserRefrshToken($platform_id,$refresh_token,$admin_id = null, $appid){
  88. //更新ptoken信息
  89. $ptoken_map = ['admin_id'=>$this->adminConfig->admin_id,'platform_id'=>$platform_id, 'appid' => $appid];
  90. if($admin_id){
  91. $ptoken_map['admin_id'] = $admin_id;
  92. }
  93. if($ptoken = model('WechatAb')->where($ptoken_map)->find()){
  94. if(false === model('WechatAb')->where($ptoken_map)->update(['refresh_token'=>$refresh_token,'updatetime'=>time()])){
  95. throw new \Exception('Ab更新RefrshToken失败');
  96. }
  97. }
  98. Log::info(sprintf('Ab微信授权: platform_id:%d channel_id:%d 微信PTOKEN更新完成',$this->platform['id'],$this->adminConfig['admin_id']));
  99. }
  100. /**
  101. * 检测权限
  102. * @param $authUser
  103. * @throws \Exception
  104. */
  105. private function checkWeChatAuth($authUser){
  106. if (!isset($authUser['authorizer_info']['service_type_info']['id'])) {
  107. throw new \Exception('Ab获取授权信息失败,请重新扫码授权');
  108. }
  109. if ($authUser['authorizer_info']['service_type_info']['id'] != 2) {
  110. throw new \Exception('Ab当前授权公众号类型非服务号,请使用服务号进行扫码');
  111. }
  112. if ($authUser['authorizer_info']['verify_type_info']['id'] == -1) {
  113. throw new \Exception('Ab当前授权服务号未认证,请使用认证服务号进行扫码');
  114. }
  115. $authList = array_column(array_column($authUser['authorization_info']['func_info'], 'funcscope_category'), 'id');
  116. if(empty($authList)){
  117. throw new \Exception('Ab获取授权信息失败');
  118. }
  119. if(!in_array(1,$authList)){
  120. throw new \Exception('Ab必须授予-消息管理权限');
  121. }
  122. if(!in_array(15,$authList)){
  123. throw new \Exception('Ab必须授予-自定义菜单权限');
  124. }
  125. if(!in_array(4,$authList)){
  126. throw new \Exception('Ab必须授予-网页服务权限');
  127. }
  128. if(!in_array(2,$authList)){
  129. throw new \Exception('Ab必须授予-用户管理权限');
  130. }
  131. if(!in_array(3,$authList)){
  132. throw new \Exception('Ab必须授予-帐号服务权限');
  133. }
  134. if(!in_array(11,$authList)){
  135. throw new \Exception('Ab必须授予-素材管理权限');
  136. }
  137. if(!in_array(23,$authList)){
  138. throw new \Exception('Ab必须授予-广告管理权限');
  139. }
  140. }
  141. }