123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?php
- namespace app\api\controller;
- use app\admin\model\WechatPlan;
- use app\common\model\UserRelation;
- use app\common\controller\Api;
- use app\common\library\Auth;
- use liuniu\WechatService;
- use app\common\model\User;
- use Overtrue\Socialite\AuthorizeFailedException;
- use think\Exception;
- use think\Hook;
- use think\Request;
- class Wechat extends Api
- {
- protected $noNeedLogin = ['*'];
-
- public function serve()
- {
- ob_clean();
- return WechatService::serve();
- }
-
- public function notify()
- {
- ob_clean();
- WechatService::handleNotify(input('cid', 0));
- }
-
- public function config(Request $request)
- {
- return app('json')->success(json_decode(WechatService::jsSdk($request->get('url')), true));
- }
-
- public function auth(Request $request)
- {
- $spreadId = intval($request->param('spread'));
- $login_type = $request->param('login_type', 1);
- @file_put_contents("auth.txt", json_encode(input()));
- try {
- $wechatInfo = WechatService::oauthService($this->cid)->user()->getOriginal();
- } catch (\Exception $e) {
- $this->error(['message' => $e->getMessage(), 'line' => $e->getLine()]);
- }
- @file_put_contents("auth.txt", "\r\n" . json_encode($wechatInfo), 8);
- try {
- if (!isset($wechatInfo['nickname'])) {
- try {
- $wechatInfo = WechatService::getUserInfo($this->cid, $wechatInfo['openid']);
- } catch (\Exception $e) {
- $this->error(['message' => $e->getMessage(), 'line' => $e->getLine()]);
- }
- if (!$wechatInfo['subscribe'] && !isset($wechatInfo['nickname']))
- exit(WechatService::oauthService($this->cid)->scopes(['snsapi_userinfo'])
- ->redirect($this->request->url(true))->send());
- if (isset($wechatInfo['tagid_list']))
- $wechatInfo['tagid_list'] = implode(',', $wechatInfo['tagid_list']);
- } else {
- if (isset($wechatInfo['privilege'])) unset($wechatInfo['privilege']);
- if (!UserRelation::where(['openid' => $wechatInfo['openid']])->find())
- $wechatInfo['subscribe'] = 0;
- }
- $openid = $wechatInfo['openid'];
- $wechatInfo['cid'] = $this->cid;
- $params = [$openid, $wechatInfo, $spreadId, $login_type];
- @file_put_contents("auth.txt", "\r\n" . json_encode($params), 8);
- Hook::exec("\\app\admin\\behavior\\User", "WechatOauth", $params);
- $user = User::where('id', UserRelation::openidToUid($openid, 'openid'))->find();
- if (!$user)
- $this->error('获取用户失败');
- $this->auth->direct($user['id']);
-
- User::setSpread(intval($spreadId), $user->id);
- return $this->success('登录成功', $this->auth->getUserinfo());
- } catch (Exception $e) {
- @file_put_contents("error.txt", $e->getFile() . '-', $e->getLine(), '-' . $e->getMessage());
- }
- }
- public function test() {
- return '123456';
- }
- }
|