WechatController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\utils\Canvas;
  8. use crmeb\services\WechatService;
  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. return WechatService::serve();
  24. }
  25. /**
  26. * 支付异步回调
  27. */
  28. public function notify()
  29. {
  30. WechatService::handleNotify();
  31. }
  32. /**
  33. * 公众号权限配置信息获取
  34. * @param Request $request
  35. * @return mixed
  36. */
  37. public function config(Request $request)
  38. {
  39. return app('json')->success(json_decode(WechatService::jsSdk($request->get('url')), true));
  40. }
  41. /**
  42. * 公众号授权登陆
  43. * @param Request $request
  44. * @return mixed
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. * @throws \think\exception\DbException
  48. */
  49. public function auth(Request $request)
  50. {
  51. $spreadId = intval($request->param('spread'));
  52. $login_type = $request->param('login_type', '');
  53. try {
  54. $wechatInfo = WechatService::oauthService()->user()->getOriginal();
  55. } catch (\Exception $e) {
  56. return app('json')->fail('授权失败');
  57. }
  58. if (!isset($wechatInfo['nickname'])) {
  59. $wechatInfo = WechatService::getUserInfo($wechatInfo['openid']);
  60. if (!$wechatInfo['subscribe'] && !isset($wechatInfo['nickname']))
  61. exit(WechatService::oauthService()->scopes(['snsapi_userinfo'])
  62. ->redirect($this->request->url(true))->send());
  63. if (isset($wechatInfo['tagid_list']))
  64. $wechatInfo['tagid_list'] = implode(',', $wechatInfo['tagid_list']);
  65. } else {
  66. if (isset($wechatInfo['privilege'])) unset($wechatInfo['privilege']);
  67. if (!WechatUser::be(['openid' => $wechatInfo['openid']]))
  68. $wechatInfo['subscribe'] = 0;
  69. }
  70. $openid = $wechatInfo['openid'];
  71. event('WechatOauthAfter', [$openid, $wechatInfo, $spreadId, $login_type]);
  72. $user = User::where('uid', WechatUser::openidToUid($openid, 'openid'))->find();
  73. if (!$user)
  74. return app('json')->fail('获取用户信息失败');
  75. if ($user->login_type == 'h5' && ($h5UserInfo = User::where(['account' => $user->phone, 'phone' => $user->phone, 'user_type' => 'h5'])->find()))
  76. $token = UserToken::createToken($h5UserInfo, 'wechat');
  77. else
  78. $token = UserToken::createToken($user, 'wechat');
  79. // 设置推广关系
  80. User::setSpread(intval($spreadId), $user->uid);
  81. if ($token) {
  82. event('UserLogin', [$user, $token]);
  83. return app('json')->success('登录成功', ['token' => $token->token, 'expires_time' => $token->expires_time]);
  84. } else
  85. return app('json')->fail('登录失败');
  86. }
  87. public function follow()
  88. {
  89. $canvas = Canvas::instance();
  90. $path = 'uploads/follow/';
  91. $imageType = 'jpg';
  92. $name = 'follow';
  93. $siteUrl = sysConfig('site_url');
  94. if (file_exists($path . $name . '.' . $imageType)) {
  95. return app('json')->success('ok', ['path' => $siteUrl . '/' . $path . $name . '.' . $imageType]);
  96. }
  97. $canvas->setImageUrl('static/qrcode/follow.png')->setImageHeight(720)->setImageWidth(500)->pushImageValue();
  98. $wechatQrcode = sysConfig('wechat_qrcode');
  99. if (($strlen = stripos($wechatQrcode, 'uploads')) !== false) {
  100. $wechatQrcode = substr($wechatQrcode, $strlen);
  101. }
  102. if (!$wechatQrcode)
  103. return app('json')->fail('请上传二维码');
  104. $canvas->setImageUrl($wechatQrcode)->setImageHeight(344)->setImageWidth(344)->setImageLeft(76)->setImageTop(76)->pushImageValue();
  105. $image = $canvas->setFileName($name)->setImageType($imageType)->setPath($path)->setBackgroundWidth(500)->setBackgroundHeight(720)->starDrawChart();
  106. return app('json')->success('ok', ['path' => $image ? $siteUrl . '/' . $image : '']);
  107. }
  108. }