基于thinkphp5.1和easyWeChat实现第三方平台扫码授权
1 <?php 2 3 namespace app\wechat\controller; 4 5 use EasyWeChat\Factory; 6 use EasyWeChat\Kernel\Messages\News; 7 use EasyWeChat\Kernel\Messages\NewsItem; 8 use EasyWeChat\OpenPlatform\Server\Guard; 9 use EasyWeChat\OpenPlatform\Auth; 10 use think\Controller; 11 use think\Db; 12 use think\facade\Cookie; 13 use think\facade\Cache; 14 15 class Wxnotify extends Action 16 { 17 18 public function notify(){ 19 20 $config = $this->getOpenConfig(); 21 $openPlatform = Factory::openPlatform($config); 22 23 $server = $openPlatform->server; 24 25 return $server->serve(); 26 } 27 28 public function auth(){ 29 30 $config = $this->getOpenConfig(); 31 $openPlatform = Factory::openPlatform($config); 32 $response = $openPlatform->getPreAuthorizationUrl('http://t.lznews.cn/wxcallback'); 33 34 header("Location: $response"); 35 } 36 37 public function wxcallback(){ 38 $config = $this->getOpenConfig(); 39 $openPlatform = Factory::openPlatform($config); 40 $server = $openPlatform->server; 41 $server->push(function ($message) { 42 43 }, Guard::EVENT_AUTHORIZED); 44 45 $aa = request()->param(); 46 47 /*array(2) { 48 ["auth_code"] => string(102) "queryauthcode@@@wZUEjJoHPKu-tQhOIsMDA-soRJGxfIp6frb42Geeoqj4F7-qoNV1TgooEsA2GnkaA9kAXa3Ee8udYgsSop1rUQ" 49 ["expires_in"] => string(4) "3600" 50 }*/ 51 52 $code = $openPlatform->handleAuthorize($aa['auth_code']); 53 54 /*array(1) { 55 ["authorization_info"] => array(5) { 56 ["authorizer_appid"] => string(18) "wx99ed51ac55c8b9c1" 57 ["authorizer_access_token"] => string(157) "17_IoCi_gV1D_o3xaU1Ifsh8RR5DNKEUldVQoR975StqVHRs5pDpvIPYu8NNGwUjc9NezrQiATPiyMweLSo0OQg3wc6OQDYSdbVorUQGVk403sCNm-DrT5ZksWLw2J8exZI-1caib3gJguiED8YZPNeAMDMCA" 58 ["expires_in"] => int(7200) 59 ["authorizer_refresh_token"] => string(58) "refreshtoken@@@G-xqVs6CF_cPNX2z24m74GoCse9kCvzMJlUqd9Xrrn8" 60 ["func_info"] => array(21) { 61 [0] => array(1) { 62 ["funcscope_category"] => array(1) { 63 ["id"] => int(1) 64 } 65 } 66 [1] => array(1) { 67 ["funcscope_category"] => array(1) { 68 ["id"] => int(15) 69 } 70 } 71 [2] => array(1) { 72 ["funcscope_category"] => array(1) { 73 ["id"] => int(4) 74 } 75 } 76 [3] => array(1) { 77 ["funcscope_category"] => array(1) { 78 ["id"] => int(7) 79 } 80 } 81 [4] => array(1) { 82 ["funcscope_category"] => array(1) { 83 ["id"] => int(2) 84 } 85 } 86 [5] => array(1) { 87 ["funcscope_category"] => array(1) { 88 ["id"] => int(3) 89 } 90 } 91 [6] => array(1) { 92 ["funcscope_category"] => array(1) { 93 ["id"] => int(11) 94 } 95 } 96 [7] => array(1) { 97 ["funcscope_category"] => array(1) { 98 ["id"] => int(6) 99 } 100 } 101 [8] => array(1) { 102 ["funcscope_category"] => array(1) { 103 ["id"] => int(5) 104 } 105 } 106 [9] => array(1) { 107 ["funcscope_category"] => array(1) { 108 ["id"] => int(8) 109 } 110 } 111 [10] => array(1) { 112 ["funcscope_category"] => array(1) { 113 ["id"] => int(13) 114 } 115 } 116 [11] => array(1) { 117 ["funcscope_category"] => array(1) { 118 ["id"] => int(9) 119 } 120 } 121 [12] => array(1) { 122 ["funcscope_category"] => array(1) { 123 ["id"] => int(10) 124 } 125 } 126 [13] => array(1) { 127 ["funcscope_category"] => array(1) { 128 ["id"] => int(12) 129 } 130 } 131 [14] => array(1) { 132 ["funcscope_category"] => array(1) { 133 ["id"] => int(22) 134 } 135 } 136 [15] => array(1) { 137 ["funcscope_category"] => array(1) { 138 ["id"] => int(23) 139 } 140 } 141 [16] => array(1) { 142 ["funcscope_category"] => array(1) { 143 ["id"] => int(26) 144 } 145 } 146 [17] => array(2) { 147 ["funcscope_category"] => array(1) { 148 ["id"] => int(27) 149 } 150 ["confirm_info"] => array(3) { 151 ["need_confirm"] => int(0) 152 ["already_confirm"] => int(0) 153 ["can_confirm"] => int(0) 154 } 155 } 156 [18] => array(2) { 157 ["funcscope_category"] => array(1) { 158 ["id"] => int(33) 159 } 160 ["confirm_info"] => array(3) { 161 ["need_confirm"] => int(0) 162 ["already_confirm"] => int(0) 163 ["can_confirm"] => int(0) 164 } 165 } 166 [19] => array(1) { 167 ["funcscope_category"] => array(1) { 168 ["id"] => int(35) 169 } 170 } 171 [20] => array(2) { 172 ["funcscope_category"] => array(1) { 173 ["id"] => int(44) 174 } 175 ["confirm_info"] => array(3) { 176 ["need_confirm"] => int(0) 177 ["already_confirm"] => int(0) 178 ["can_confirm"] => int(0) 179 } 180 } 181 } 182 } 183 }*/ 184 185 $sq = Db::name('wechat_account')->where([['appid','=',$code['authorization_info']['authorizer_appid']], ['shouquan','=','1']])->find(); 186 187 if(!$sq) { 188 return '请先注册公众号或小程序信息,并勾选为扫码授权'; 189 } 190 191 $name = $openPlatform->getAuthorizer($code['authorization_info']['authorizer_appid']); 192 /*array(2) { 193 ["authorizer_info"] => array(11) { 194 ["nick_name"] => string(9) "小鲁哥" 195 ["head_img"] => string(128) "http://wx.qlogo.cn/mmopen/2h8qNVmIGxDBibx0LzLo7zbibVxvzwVWkVeNwWpLoiaw8HFtDf9TvhBkTkIfU6KJMJgibH7ulKI2MafvudBP4moEb98tz3N8n72O/0" 196 ["service_type_info"] => array(1) { 197 ["id"] => int(2) 198 } 199 ["verify_type_info"] => array(1) { 200 ["id"] => int(0) 201 } 202 ["user_name"] => string(15) "gh_f2ee5d98dbd4" 203 ["alias"] => string(9) "lznewsxlg" 204 ["qrcode_url"] => string(122) "http://mmbiz.qpic.cn/mmbiz_jpg/9qH8V6o0A0J2joL7hbYgGCt5m0icdMXTh52C2EBEBh3Hzuiah6E9eYqHgYGGBs1Z6M9aiayho0Zl8w7sP4MI6zFmA/0" 205 ["business_info"] => array(5) { 206 ["open_pay"] => int(1) 207 ["open_shake"] => int(0) 208 ["open_scan"] => int(0) 209 ["open_card"] => int(1) 210 ["open_store"] => int(1) 211 } 212 ["idc"] => int(1) 213 ["principal_name"] => string(30) "山东大鲁网络有限公司" 214 ["signature"] => string(108) "线索提供、闲聊扯皮、互动交流、同城活动。小鲁哥,一个逼格很高的公众平台。" 215 } 216 ["authorization_info"] => array(3) { 217 ["authorizer_appid"] => string(18) "wx99ed51ac55c8b9c1" 218 ["authorizer_refresh_token"] => string(58) "refreshtoken@@@G-xqVs6CF_cPNX2z24m74GoCse9kCvzMJlUqd9Xrrn8" 219 ["func_info"] => array(21) { 220 [0] => array(1) { 221 ["funcscope_category"] => array(1) { 222 ["id"] => int(1) 223 } 224 } 225 [1] => array(1) { 226 ["funcscope_category"] => array(1) { 227 ["id"] => int(15) 228 } 229 } 230 [2] => array(1) { 231 ["funcscope_category"] => array(1) { 232 ["id"] => int(4) 233 } 234 } 235 [3] => array(1) { 236 ["funcscope_category"] => array(1) { 237 ["id"] => int(7) 238 } 239 } 240 [4] => array(1) { 241 ["funcscope_category"] => array(1) { 242 ["id"] => int(2) 243 } 244 } 245 [5] => array(1) { 246 ["funcscope_category"] => array(1) { 247 ["id"] => int(3) 248 } 249 } 250 [6] => array(1) { 251 ["funcscope_category"] => array(1) { 252 ["id"] => int(11) 253 } 254 } 255 [7] => array(1) { 256 ["funcscope_category"] => array(1) { 257 ["id"] => int(6) 258 } 259 } 260 [8] => array(1) { 261 ["funcscope_category"] => array(1) { 262 ["id"] => int(5) 263 } 264 } 265 [9] => array(1) { 266 ["funcscope_category"] => array(1) { 267 ["id"] => int(8) 268 } 269 } 270 [10] => array(1) { 271 ["funcscope_category"] => array(1) { 272 ["id"] => int(13) 273 } 274 } 275 [11] => array(1) { 276 ["funcscope_category"] => array(1) { 277 ["id"] => int(9) 278 } 279 } 280 [12] => array(1) { 281 ["funcscope_category"] => array(1) { 282 ["id"] => int(10) 283 } 284 } 285 [13] => array(1) { 286 ["funcscope_category"] => array(1) { 287 ["id"] => int(12) 288 } 289 } 290 [14] => array(1) { 291 ["funcscope_category"] => array(1) { 292 ["id"] => int(22) 293 } 294 } 295 [15] => array(1) { 296 ["funcscope_category"] => array(1) { 297 ["id"] => int(23) 298 } 299 } 300 [16] => array(1) { 301 ["funcscope_category"] => array(1) { 302 ["id"] => int(26) 303 } 304 } 305 [17] => array(2) { 306 ["funcscope_category"] => array(1) { 307 ["id"] => int(27) 308 } 309 ["confirm_info"] => array(3) { 310 ["need_confirm"] => int(0) 311 ["already_confirm"] => int(0) 312 ["can_confirm"] => int(0) 313 } 314 } 315 [18] => array(2) { 316 ["funcscope_category"] => array(1) { 317 ["id"] => int(33) 318 } 319 ["confirm_info"] => array(3) { 320 ["need_confirm"] => int(0) 321 ["already_confirm"] => int(0) 322 ["can_confirm"] => int(0) 323 } 324 } 325 [19] => array(1) { 326 ["funcscope_category"] => array(1) { 327 ["id"] => int(35) 328 } 329 } 330 [20] => array(2) { 331 ["funcscope_category"] => array(1) { 332 ["id"] => int(44) 333 } 334 ["confirm_info"] => array(3) { 335 ["need_confirm"] => int(0) 336 ["already_confirm"] => int(0) 337 ["can_confirm"] => int(0) 338 } 339 } 340 } 341 } 342 }*/ 343 344 Db::name('wechat_account')->where('id', $sq['id'])->update(['token' => $code['authorization_info']['authorizer_refresh_token']]); 345 346 return $server->serve(); 347 } 348 }
notify接收微信返回第三方平台的信息 auth发起授权 wxcallback授权成功回调
1 <?php 2 3 namespace app\wechat\controller; 4 5 use EasyWeChat\Factory; 6 use EasyWeChat\Kernel\Messages\News; 7 use EasyWeChat\Kernel\Messages\NewsItem; 8 use EasyWeChat\OpenPlatform\Server\Guard; 9 use EasyWeChat\OpenPlatform\Auth; 10 use think\Controller; 11 use think\Db; 12 use think\facade\Cookie; 13 use think\facade\Cache; 14 15 class WechatIndex extends Action 16 { 17 public function index($appid){ 18 19 $this->sq = Db::name('wechat_account')->where([['appid','=',$appid], ['shouquan','=','1']])->find(); 20 21 if($this->sq) { 22 23 $config = $this->getOpenConfig(); 24 25 $openPlatform = Factory::openPlatform($config); 26 27 $officialAccount = $openPlatform->officialAccount($appid,$this->sq['token']); 28 29 $server = $officialAccount->server; 30 $server->push(function ($message) { 31 $id = $this->sq['id']; 32 // $message['ToUserName'] 接收方帐号(该公众号 ID) 33 // $message['FromUserName'] 发送方帐号(OpenID, 代表用户的唯一标识) 34 // $message['CreateTime'] 消息创建时间(时间戳) 35 // $message['MsgId'] 消息 ID(64位整型) 36 // $message['MsgType'] 消息类型 37 // $message['Event'] 事件类型 - 针对 event 事件消息 38 // $message['Content'] 消息内容 - 针对 text 文字消息 39 40 switch ($message['MsgType']) { 41 case 'event': //事件消息 42 switch ($message['Event']) { 43 case 'subscribe': //关注 44 Db::name('wechat_user')->insert([ 45 'wechat_account_id' => $id, 46 'user_id' => 0, 47 'nickname' => '', 48 'openid' => $message['FromUserName'], 49 'unionid' => '', 50 'avatarUrl' => '', 51 'sex' => '', 52 'create_time' => time(), 53 ]); 54 return '您好!欢迎您关注'; 55 break; 56 case 'unsubscribe': //取消关注 57 Db::name('wechat_user') 58 ->where('openid', $message['FromUserName']) 59 ->delete(); 60 break; 61 case 'CLICK': //文字消息 62 $replays = Db::name('wechat_reply') 63 ->field('id,type,message,title,picurl,url') 64 ->where('wechat_id', $id) 65 ->where('status', '>', 5) 66 ->where('keyword', '=', $message['EventKey']) 67 ->order('sort', 'desc') 68 ->find(); 69 if ($replays) { 70 if ($replays['type'] == 'news') { 71 $items = [ 72 new NewsItem([ 73 'title' => $replays['title'], 74 'description' => $replays['message'], 75 'url' => $replays['url'], 76 'image' => 'http://' . $_SERVER['HTTP_HOST'] . $replays['picurl'], 77 ]), 78 ]; 79 return new News($items); 80 } else { 81 return $replays['message']; 82 } 83 } 84 break; 85 } 86 break; 87 case 'text': //文字消息 88 89 if (is_numeric($message['Content'])) { 90 $vote = new \app\vote\controller\Index; 91 // msg_file($message['FromUserName']); 92 $data = $vote->wechatVoteSubmit($message['Content'], $id, $message['FromUserName']); 93 if ($data['code'] >= 1) { 94 return $data['msg']; 95 } 96 } 97 98 $replays = Db::name('wechat_reply') 99 ->field('id,type,message,title,picurl,url') 100 ->where('wechat_id', $id) 101 ->where('status', '>', 5) 102 ->where('keyword', '=', $message['Content']) 103 ->order('sort', 'desc') 104 ->find(); 105 106 if ($replays) { 107 if ($replays['type'] == 'news') { 108 $items = [ 109 new NewsItem([ 110 'title' => $replays['title'], 111 'description' => $replays['message'], 112 'url' => $replays['url'], 113 'image' => 'http://' . $_SERVER['HTTP_HOST'] . $replays['picurl'], 114 ]), 115 ]; 116 return new News($items); 117 } else { 118 return $replays['message']; 119 } 120 } else { 121 return ''; 122 } 123 break; 124 case 'image': 125 return '收到图片消息'; 126 break; 127 // case 'voice': 128 // return '收到语音消息'; 129 // break; 130 // case 'video': 131 // return '收到视频消息'; 132 // break; 133 // case 'location': 134 // return '收到坐标消息'; 135 // break; 136 // case 'link': 137 // return '收到链接消息'; 138 // break; 139 // case 'file': 140 // return '收到文件消息'; 141 default: 142 return ''; 143 break; 144 } 145 146 }); 147 $response = $server->serve(); 148 $response->send(); 149 } 150 } 151 }
用户发送信息接收与反馈