WechatController.php 755 B

12345678910111213141516171819202122232425
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\User;
  4. use App\Models\WechatInfo;
  5. class WechatController extends Controller
  6. {
  7. public function index()
  8. {
  9. $wechat_user = session('wechat.oauth_user.default');
  10. if($wechat_user && isset($wechat_user['original'])) {
  11. $official_open_id = $wechat_user['original']['openid'];
  12. $union_id = $wechat_user['original']['unionid'];
  13. WechatInfo::firstOrCreate([
  14. 'official_open_id' => $official_open_id,
  15. 'union_id' => $union_id
  16. ]);
  17. User::where('union_id', $union_id)->update(['official_open_id' => $official_open_id]);
  18. return '授权成功';
  19. }
  20. return '授权失败';
  21. }
  22. }