123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- namespace app\api\controller;
- use app\common\model\UserRelation;
- use app\common\controller\Api;
- use app\common\library\Auth;
- use liuniu\WechatService;
- use app\common\model\User;
- use think\Exception;
- use think\Hook;
- use think\Request;
- class Wechat extends Api
- {
- protected $noNeedLogin = ['*'];
- /**
- * 微信公众号服务
- * @return \think\Response
- */
- public function serve()
- {
- ob_clean();
- return WechatService::serve();
- }
- /**
- * 支付异步回调
- */
- public function notify()
- {
- ob_clean();
- WechatService::handleNotify(input('cid',0));
- }
- /**
- * 公众号权限配置信息获取
- * @param Request $request
- * @return mixed
- */
- public function config(Request $request)
- {
- return app('json')->success(json_decode(WechatService::jsSdk($request->get('url')), true));
- }
- /**
- * 公众号授权登陆
- * @param Request $request
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- 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());
- }
- }
- }
|