WechatController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. $wechatInfo = WechatService::getUserInfo($wechatInfo['openid']);
  62. if (!$wechatInfo['subscribe'] && !isset($wechatInfo['nickname']))
  63. exit(WechatService::oauthService()->scopes(['snsapi_userinfo'])
  64. ->redirect($this->request->url(true))->send());
  65. if (isset($wechatInfo['tagid_list']))
  66. $wechatInfo['tagid_list'] = implode(',', $wechatInfo['tagid_list']);
  67. } else {
  68. if (isset($wechatInfo['privilege'])) unset($wechatInfo['privilege']);
  69. if (!WechatUser::be(['openid' => $wechatInfo['openid']]))
  70. $wechatInfo['subscribe'] = 0;
  71. }
  72. $openid = $wechatInfo['openid'];
  73. event('WechatOauthAfter', [$openid, $wechatInfo, $spreadId, $login_type]);
  74. $user = User::where('uid', WechatUser::openidToUid($openid, 'openid'))->find();
  75. if (!$user)
  76. return app('json')->fail('获取用户信息失败');
  77. if ($user->login_type == 'h5' && ($h5UserInfo = User::where(['account' => $user->phone, 'phone' => $user->phone, 'user_type' => 'h5'])->find()))
  78. $token = UserToken::createToken($h5UserInfo, 'wechat');
  79. else
  80. $token = UserToken::createToken($user, 'wechat');
  81. // 设置推广关系
  82. User::setSpread(intval($spreadId), $user->uid);
  83. if ($token) {
  84. event('UserLogin', [$user, $token]);
  85. return app('json')->success('登录成功', ['token' => $token->token, 'expires_time' => $token->expires_time]);
  86. } else
  87. return app('json')->fail('登录失败');
  88. }
  89. public function follow()
  90. {
  91. $canvas = Canvas::instance();
  92. $path = 'uploads/follow/';
  93. $imageType = 'jpg';
  94. $name = 'follow';
  95. $siteUrl = sys_config('site_url');
  96. $imageUrl = $path . $name . '.' . $imageType;
  97. // if (file_exists($imageUrl)) {
  98. // return app('json')->success('ok', ['path' => $siteUrl . '/' . $imageUrl]);
  99. // }
  100. $canvas->setImageUrl('static/qrcode/follow.png')->setImageHeight(720)->setImageWidth(500)->pushImageValue();
  101. $wechatQrcode = sys_config('wechat_qrcode');
  102. if (($strlen = stripos($wechatQrcode, 'uploads')) !== false) {
  103. $wechatQrcode = substr($wechatQrcode, $strlen);
  104. }
  105. if (!$wechatQrcode)
  106. return app('json')->fail('请上传二维码');
  107. $canvas->setImageUrl($wechatQrcode)->setImageHeight(344)->setImageWidth(344)->setImageLeft(76)->setImageTop(76)->pushImageValue();
  108. $image = $canvas->setFileName($name)->setImageType($imageType)->setPath($path)->setBackgroundWidth(500)->setBackgroundHeight(720)->starDrawChart();
  109. return app('json')->success('ok', ['path' => $image ? $siteUrl . '/' . $image : '']);
  110. }
  111. }