WechatController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace app\api\controller\wechat;
  3. use app\models\user\User;
  4. use app\models\user\UserToken;
  5. use app\models\user\WechatUser;
  6. use app\Request;
  7. use crmeb\services\WechatService;
  8. use crmeb\utils\Canvas;
  9. use think\facade\Cookie;
  10. /**
  11. * 微信公众号
  12. * Class WechatController
  13. * @package app\api\controller\wechat
  14. */
  15. class WechatController
  16. {
  17. /**
  18. * 微信公众号服务
  19. * @return \think\Response
  20. */
  21. public function serve()
  22. {
  23. ob_clean();
  24. return WechatService::serve();
  25. }
  26. /**
  27. * 支付异步回调
  28. */
  29. public function notify()
  30. {
  31. ob_clean();
  32. WechatService::handleNotify();
  33. }
  34. /**
  35. * 公众号权限配置信息获取
  36. * @param Request $request
  37. * @return mixed
  38. */
  39. public function config(Request $request)
  40. {
  41. return app('json')->success(json_decode(WechatService::jsSdk($request->get('url')), true));
  42. }
  43. /**
  44. * 公众号授权登陆
  45. * @param Request $request
  46. * @return mixed
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. * @throws \think\exception\DbException
  50. */
  51. public function auth(Request $request)
  52. {
  53. $spreadId = intval($request->param('spread'));
  54. $login_type = $request->param('login_type', '');
  55. try {
  56. $wechatInfo = WechatService::oauthService()->user()->getOriginal();
  57. } catch (\Exception $e) {
  58. return app('json')->fail('授权失败', ['message' => $e->getMessage(), 'line' => $e->getLine()]);
  59. }
  60. if (!isset($wechatInfo['nickname'])) {
  61. try {
  62. $wechatInfo = WechatService::getUserInfo($wechatInfo['openid']);
  63. } catch (\Exception $e) {
  64. return app('json')->fail('获取信息失败', ['message' => $e->getMessage(), 'line' => $e->getLine()]);
  65. }
  66. if (!$wechatInfo['subscribe'] && !isset($wechatInfo['nickname']))
  67. exit(WechatService::oauthService()->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 (!WechatUser::be(['openid' => $wechatInfo['openid']]))
  74. $wechatInfo['subscribe'] = 0;
  75. }
  76. $openid = $wechatInfo['openid'];
  77. event('WechatOauthAfter', [$openid, $wechatInfo, $spreadId, $login_type]);
  78. $user = User::where('uid', WechatUser::openidToUid($openid, 'openid'))->find();
  79. if (!$user)
  80. return app('json')->fail('获取用户信息失败');
  81. if ($user->login_type == 'h5' && ($h5UserInfo = User::where(['account' => $user->phone, 'phone' => $user->phone, 'user_type' => 'h5'])->find()))
  82. $token = UserToken::createToken($h5UserInfo, 'wechat');
  83. else
  84. $token = UserToken::createToken($user, 'wechat');
  85. if ($user['status'] == 0) return app('json')->fail('用户已被冻结24小时,请联系管理员解除');
  86. // 设置推广关系
  87. User::setSpread(intval($spreadId), $user->uid);
  88. if ($token) {
  89. event('UserLogin', [$user, $token]);
  90. return app('json')->success('登录成功', ['token' => $token->token, 'expires_time' => $token->expires_time]);
  91. } else
  92. return app('json')->fail('登录失败');
  93. }
  94. public function follow()
  95. {
  96. $canvas = Canvas::instance();
  97. $path = 'uploads/follow/';
  98. $imageType = 'jpg';
  99. $name = 'follow';
  100. $siteUrl = sys_config('site_url');
  101. $imageUrl = $path . $name . '.' . $imageType;
  102. // if (file_exists($imageUrl)) {
  103. // return app('json')->success('ok', ['path' => $siteUrl . '/' . $imageUrl]);
  104. // }
  105. $canvas->setImageUrl('static/qrcode/follow.png')->setImageHeight(720)->setImageWidth(500)->pushImageValue();
  106. $wechatQrcode = sys_config('wechat_qrcode');
  107. if (($strlen = stripos($wechatQrcode, 'uploads')) !== false) {
  108. $wechatQrcode = substr($wechatQrcode, $strlen);
  109. }
  110. if (!$wechatQrcode)
  111. return app('json')->fail('请上传二维码');
  112. $canvas->setImageUrl($wechatQrcode)->setImageHeight(344)->setImageWidth(344)->setImageLeft(76)->setImageTop(76)->pushImageValue();
  113. $image = $canvas->setFileName($name)->setImageType($imageType)->setPath($path)->setBackgroundWidth(500)->setBackgroundHeight(720)->starDrawChart();
  114. return app('json')->success('ok', ['path' => $image ? $siteUrl . '/' . $image : '']);
  115. }
  116. }