| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- namespace app\seller\controller;
- use app\common\controller\BaseController;
- use app\http\IResponse;
- use app\seller\service\JWTAuth as IAuth;
- use think\App;
- use think\facade\Cache;
- class Auth extends BaseController
- {
- protected $auth;
- /**
- * Auth constructor.
- * @param App $app
- * @param IAuth $auth
- */
- public function __construct(App $app, IAuth $auth)
- {
- parent::__construct($app);
- $this->auth = $auth->guard('user');
- }
- /**
- * 创建登
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/7/10 10:55
- *
- */
- public function login()
- {
- $sid = md5($this->request->header('cookie') . strtotime(date('YmdHi')));
- // 查找
- $load = Cache::get($sid);
- // 解码
- $load = dejson($load);
- // 不存在或者过期
- if (!$load || $load['expired'] <= time()){
- // 数据
- $data = [
- 'client_id' => $sid,
- 'status' => 'wait',
- 'expired' => time() + 300
- ];
- // 创建登录记
- Cache::set($sid,enjson($data),259200);
- // 返回编码数据
- return IResponse::success($data);
- }
- // 登录成功,删除本次缓存
- if (isset($load['token'])){
- Cache::rm($sid);
- }
- return IResponse::success($load);
- }
- /**
- * 商家小程序传来登录
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/6/16 11:57
- *
- * @return \think\response\Json
- * @throws \Lettered\Support\Exceptions\FailedException
- */
- public function auth()
- {
- $param = $this->request->param();
- // 内置验证
- $valid = $this->validate($param, [
- 'client_id|登录客户端ID' => 'require'
- ]);
- // 错误
- if (true !== $valid){
- return json([
- 'code' => -1,
- 'message' => $valid
- ]);
- }
- // 读取缓存
- $load = Cache::get($param['client_id']);
- if ($load){
- // 1. 验证商户状态
- $userId = $this->auth->user()['id'];
- $seller = model('common/Seller')->getBy(['user_id' => $userId]);
- if($seller['status'] == 1){
- // 解码
- $load = dejson($load);
- // 二维码是否过期
- if ($load['expired']>= time()){
- if ($load['status'] == 'wait'){
- // 2. 变更登录状态
- // 数据
- $data = [
- 'client_id' => $param['client_id'],
- 'token' => $this->request->header('Authorization'),
- 'status' => 'active',
- 'expired' => time() + (5 * 60 * 60)
- ];
- // 创建登录记
- Cache::set($param['client_id'], enjson($data),259200);
- return json([
- 'code' => 0,
- 'message' => "登录成功!"
- ]);
- }
- }else {
- return json([
- 'code' => -1,
- 'message' => "二维码过期,请重新获取!"
- ]);
- }
- }
- return json([
- 'code' => -1,
- 'message' => "商户状态异常,暂无法登录!"
- ]);
- }
- return json([
- 'code' => -1,
- 'message' => "二维码状态异常,请重新获取!"
- ]);
- }
- }
|