WechatController.php 5.0 KB

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