WechatController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. /**
  10. * 微信公众号
  11. * Class WechatController
  12. * @package app\api\controller\wechat
  13. */
  14. class WechatController
  15. {
  16. /**
  17. * 微信公众号服务接口
  18. *
  19. */
  20. public function serve()
  21. {
  22. ob_clean();
  23. return WechatService::serve();
  24. }
  25. /**
  26. * 公众号支付异步回调
  27. *
  28. */
  29. public function notify()
  30. {
  31. ob_clean();
  32. WechatService::handleNotify();
  33. }
  34. /**
  35. * @api {get} /wechat/config 公众号权限配置信息获取
  36. * @apiName GetWechatConfig
  37. * @apiGroup Public
  38. *
  39. */
  40. public function config(Request $request)
  41. {
  42. return app('json')->success(json_decode(WechatService::jsSdk($request->get('url')), true));
  43. }
  44. /**
  45. * @api {get} /wechat/auth 公众号授权登陆
  46. * @apiName GetWechatAuth
  47. * @apiGroup Public
  48. *
  49. */
  50. public function auth(Request $request)
  51. {
  52. $spreadId = intval($request->param('spread'));
  53. $login_type = $request->param('login_type', '');
  54. try {
  55. $wechatInfo = WechatService::oauthService()->user()->getOriginal();
  56. } catch (\Exception $e) {
  57. return app('json')->fail('授权失败', ['message' => $e->getMessage(), 'line' => $e->getLine()]);
  58. }
  59. if (!isset($wechatInfo['nickname'])) {
  60. try {
  61. $wechatInfo = WechatService::getUserInfo($wechatInfo['openid']);
  62. } catch (\Exception $e) {
  63. return app('json')->fail('获取信息失败', ['message' => $e->getMessage(), 'line' => $e->getLine()]);
  64. }
  65. if (!$wechatInfo['subscribe'] && !isset($wechatInfo['nickname']))
  66. exit(WechatService::oauthService()->scopes(['snsapi_userinfo'])
  67. ->redirect($this->request->url(true))->send());
  68. if (isset($wechatInfo['tagid_list']))
  69. $wechatInfo['tagid_list'] = implode(',', $wechatInfo['tagid_list']);
  70. } else {
  71. if (isset($wechatInfo['privilege'])) unset($wechatInfo['privilege']);
  72. if (!WechatUser::be(['openid' => $wechatInfo['openid']]))
  73. $wechatInfo['subscribe'] = 0;
  74. }
  75. $openid = $wechatInfo['openid'];
  76. event('WechatOauthAfter', [$openid, $wechatInfo, $spreadId, $login_type]);
  77. $user = User::where('uid', WechatUser::openidToUid($openid, 'openid'))->find();
  78. if (!$user)
  79. return app('json')->fail('获取用户信息失败');
  80. if ($user->login_type == 'h5' && ($h5UserInfo = User::where(['account' => $user->phone, 'phone' => $user->phone, 'user_type' => 'h5'])->find()))
  81. $token = UserToken::createToken($h5UserInfo, 'wechat');
  82. else
  83. $token = UserToken::createToken($user, 'wechat');
  84. // 设置推广关系
  85. User::setSpread(intval($spreadId), $user->uid);
  86. if ($token) {
  87. event('UserLogin', [$user, $token]);
  88. return app('json')->success('登录成功', ['token' => $token->token, 'expires_time' => $token->expires_time]);
  89. } else
  90. return app('json')->fail('登录失败');
  91. }
  92. /**
  93. * @api {get} /wechat/auth_app app 授权登陆
  94. * @apiName GetWechatAuthApp
  95. * @apiGroup Public
  96. *
  97. */
  98. public function auth_app(Request $request)
  99. {
  100. // 见文档 帐号.md
  101. }
  102. /**
  103. * @api {get} /wechat/follow 获取关注微信公众号海报
  104. * @apiName GetWechatFollow
  105. * @apiGroup Public
  106. *
  107. */
  108. public function follow()
  109. {
  110. $canvas = Canvas::instance();
  111. $path = 'uploads/follow/';
  112. $imageType = 'jpg';
  113. $name = 'follow';
  114. $siteUrl = sys_config('site_url');
  115. $imageUrl = $path . $name . '.' . $imageType;
  116. // if (file_exists($imageUrl)) {
  117. // return app('json')->success('ok', ['path' => $siteUrl . '/' . $imageUrl]);
  118. // }
  119. $canvas->setImageUrl('static/qrcode/follow.png')->setImageHeight(720)->setImageWidth(500)->pushImageValue();
  120. $wechatQrcode = sys_config('wechat_qrcode');
  121. if (($strlen = stripos($wechatQrcode, 'uploads')) !== false) {
  122. $wechatQrcode = substr($wechatQrcode, $strlen);
  123. }
  124. if (!$wechatQrcode)
  125. return app('json')->fail('请上传二维码');
  126. $canvas->setImageUrl($wechatQrcode)->setImageHeight(344)->setImageWidth(344)->setImageLeft(76)->setImageTop(76)->pushImageValue();
  127. $image = $canvas->setFileName($name)->setImageType($imageType)->setPath($path)->setBackgroundWidth(500)->setBackgroundHeight(720)->starDrawChart();
  128. return app('json')->success('ok', ['path' => $image ? $siteUrl . '/' . $image : '']);
  129. }
  130. }