Wechat.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\UserRelation;
  4. use app\common\controller\Api;
  5. use app\common\library\Auth;
  6. use liuniu\WechatService;
  7. use app\common\model\User;
  8. use think\Exception;
  9. use think\Hook;
  10. use think\Request;
  11. class Wechat extends Api
  12. {
  13. protected $noNeedLogin = ['*'];
  14. /**
  15. * 微信公众号服务
  16. * @return \think\Response
  17. */
  18. public function serve()
  19. {
  20. ob_clean();
  21. return WechatService::serve();
  22. }
  23. /**
  24. * 支付异步回调
  25. */
  26. public function notify()
  27. {
  28. ob_clean();
  29. WechatService::handleNotify(input('cid',0));
  30. }
  31. /**
  32. * 公众号权限配置信息获取
  33. * @param Request $request
  34. * @return mixed
  35. */
  36. public function config(Request $request)
  37. {
  38. return app('json')->success(json_decode(WechatService::jsSdk($request->get('url')), true));
  39. }
  40. /**
  41. * 公众号授权登陆
  42. * @param Request $request
  43. * @return mixed
  44. * @throws \think\db\exception\DataNotFoundException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. * @throws \think\exception\DbException
  47. */
  48. public function auth(Request $request)
  49. {
  50. $spreadId = intval($request->param('spread'));
  51. $login_type = $request->param('login_type', 1);
  52. @file_put_contents("auth.txt",json_encode(input()));
  53. try {
  54. $wechatInfo = WechatService::oauthService($this->cid)->user()->getOriginal();
  55. } catch (\Exception $e) {
  56. $this->error(['message' => $e->getMessage(), 'line' => $e->getLine()]);
  57. }
  58. @file_put_contents("auth.txt","\r\n".json_encode($wechatInfo),8);
  59. try {
  60. if (!isset($wechatInfo['nickname'])) {
  61. try {
  62. $wechatInfo = WechatService::getUserInfo($this->cid, $wechatInfo['openid']);
  63. } catch (\Exception $e) {
  64. $this->error(['message' => $e->getMessage(), 'line' => $e->getLine()]);
  65. }
  66. if (!$wechatInfo['subscribe'] && !isset($wechatInfo['nickname']))
  67. exit(WechatService::oauthService($this->cid)->scopes(['snsapi_userinfo'])
  68. ->redirect($this->request->url(true))->send());
  69. if (isset($wechatInfo['tagid_list']))
  70. $wechatInfo['tagid_list'] = implode(',', $wechatInfo['tagid_list']);
  71. } else {
  72. if (isset($wechatInfo['privilege'])) unset($wechatInfo['privilege']);
  73. if (!UserRelation::where(['openid' => $wechatInfo['openid']])->find())
  74. $wechatInfo['subscribe'] = 0;
  75. }
  76. $openid = $wechatInfo['openid'];
  77. $wechatInfo['cid'] = $this->cid;
  78. $params = [$openid, $wechatInfo, $spreadId, $login_type];
  79. @file_put_contents("auth.txt", "\r\n" . json_encode($params), 8);
  80. Hook::exec("\\app\admin\\behavior\\User", "WechatOauth", $params);
  81. $user = User::where('id', UserRelation::openidToUid($openid, 'openid'))->find();
  82. if (!$user)
  83. $this->error('获取用户失败');
  84. $this->auth->direct($user['id']);
  85. // 设置推广关系
  86. User::setSpread(intval($spreadId), $user->id);
  87. return $this->success('登录成功', $this->auth->getUserinfo());
  88. }catch (Exception $e)
  89. {
  90. @file_put_contents("error.txt",$e->getFile().'-',$e->getLine(),'-'.$e->getMessage());
  91. }
  92. }
  93. }