Auth.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace app\seller\controller;
  3. use app\common\controller\BaseController;
  4. use app\http\IResponse;
  5. use app\seller\service\JWTAuth as IAuth;
  6. use think\App;
  7. use think\facade\Cache;
  8. class Auth extends BaseController
  9. {
  10. protected $auth;
  11. /**
  12. * Auth constructor.
  13. * @param App $app
  14. * @param IAuth $auth
  15. */
  16. public function __construct(App $app, IAuth $auth)
  17. {
  18. parent::__construct($app);
  19. $this->auth = $auth->guard('user');
  20. }
  21. /**
  22. * 创建登
  23. *
  24. * @author 许祖兴 < zuxing.xu@lettered.cn>
  25. * @date 2020/7/10 10:55
  26. *
  27. */
  28. public function login()
  29. {
  30. $sid = md5($this->request->header('cookie') . strtotime(date('YmdHi')));
  31. // 查找
  32. $load = Cache::get($sid);
  33. // 解码
  34. $load = dejson($load);
  35. // 不存在或者过期
  36. if (!$load || $load['expired'] <= time()){
  37. // 数据
  38. $data = [
  39. 'client_id' => $sid,
  40. 'status' => 'wait',
  41. 'expired' => time() + 300
  42. ];
  43. // 创建登录记
  44. Cache::set($sid,enjson($data),259200);
  45. // 返回编码数据
  46. return IResponse::success($data);
  47. }
  48. // 登录成功,删除本次缓存
  49. if (isset($load['token'])){
  50. Cache::rm($sid);
  51. }
  52. return IResponse::success($load);
  53. }
  54. /**
  55. * 商家小程序传来登录
  56. *
  57. * @author 许祖兴 < zuxing.xu@lettered.cn>
  58. * @date 2020/6/16 11:57
  59. *
  60. * @return \think\response\Json
  61. * @throws \Lettered\Support\Exceptions\FailedException
  62. */
  63. public function auth()
  64. {
  65. $param = $this->request->param();
  66. // 内置验证
  67. $valid = $this->validate($param, [
  68. 'client_id|登录客户端ID' => 'require'
  69. ]);
  70. // 错误
  71. if (true !== $valid){
  72. return json([
  73. 'code' => -1,
  74. 'message' => $valid
  75. ]);
  76. }
  77. // 读取缓存
  78. $load = Cache::get($param['client_id']);
  79. if ($load){
  80. // 1. 验证商户状态
  81. $userId = $this->auth->user()['id'];
  82. $seller = model('common/Seller')->getBy(['user_id' => $userId]);
  83. if($seller['status'] == 1){
  84. // 解码
  85. $load = dejson($load);
  86. // 二维码是否过期
  87. if ($load['expired']>= time()){
  88. if ($load['status'] == 'wait'){
  89. // 2. 变更登录状态
  90. // 数据
  91. $data = [
  92. 'client_id' => $param['client_id'],
  93. 'token' => $this->request->header('Authorization'),
  94. 'status' => 'active',
  95. 'expired' => time() + (5 * 60 * 60)
  96. ];
  97. // 创建登录记
  98. Cache::set($param['client_id'], enjson($data),259200);
  99. return json([
  100. 'code' => 0,
  101. 'message' => "登录成功!"
  102. ]);
  103. }
  104. }else {
  105. return json([
  106. 'code' => -1,
  107. 'message' => "二维码过期,请重新获取!"
  108. ]);
  109. }
  110. }
  111. return json([
  112. 'code' => -1,
  113. 'message' => "商户状态异常,暂无法登录!"
  114. ]);
  115. }
  116. return json([
  117. 'code' => -1,
  118. 'message' => "二维码状态异常,请重新获取!"
  119. ]);
  120. }
  121. }