| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- namespace App\Http\Controllers\Api;
- use App\Helpers\Jwt;
- use App\Models\UserModel;
- use App\Services\RedisService;
- use App\Services\SnapshotService;
- use App\Services\WechatService;
- use Illuminate\Support\Facades\Session;
- /**
- * 授权控制器基类
- * @author wesmiler
- * @since 2020/11/10
- * Class AuthController
- * @package App\Http\Controllers
- */
- class AuthController extends BaseController
- {
- /**
- * 构造函数
- * @author wesmiler
- * @since 2020/11/11
- * AuthController constructor.
- */
- public function __construct()
- {
- parent::__construct();
- }
- public function index(){
- $code = request()->get('code');
- if(empty($code)){
- return message('code参数错误',false);
- }
- // 授权
- $this->userInfo = WechatService::auth();
- $openid = isset($this->userInfo['openid'])? $this->userInfo['openid'] : '';
- $status = isset($this->userInfo['status'])? $this->userInfo['status'] : '';
- $userId = isset($this->userInfo['id'])? $this->userInfo['id'] : 0;
- if(empty($this->userInfo) || empty($openid) || $userId<=0){
- return message('用户授权失败',false);
- }
- if($status == 3){
- return message('用户已被拉入黑名单',false);
- }
- if($status != 1){
- return message('用户账户已被冻结不可操作',false);
- }
- // 获取授权TOKEN
- $jwt = new Jwt('jwt_wx');
- $token = $jwt->getToken($userId, 3);
- // 结果返回
- $result = [
- 'access_token' => $token,
- 'info'=> $this->userInfo,
- ];
- // 用户信息
- RedisService::set("auths:request:$userId", request()->all(), 600);
- RedisService::set("auths:info:{$userId}", $this->userInfo, 4*24*3600);
- return message('获取授权成功', true, $result);
- }
- /**
- * 获取授权跳转地址
- * @return array
- */
- public function authUrl(){
- $url = request()->get('url');
- return message('获取授权成功', true, WechatService::makeRedirectUrl($url));
- }
- /**
- * 微信授权配置入口
- */
- public function check(){
- return WechatService::valid();
- }
- }
|