Login.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\kefu;
  12. use app\Request;
  13. use crmeb\basic\BaseController;
  14. use crmeb\services\CacheService;
  15. use app\services\kefu\LoginServices;
  16. use app\validate\kefu\LoginValidate;
  17. use think\facade\App;
  18. /**
  19. * Class Login
  20. * @package app\kefu\controller
  21. */
  22. class Login extends BaseController
  23. {
  24. /**
  25. * Login constructor.
  26. * @param LoginServices $services
  27. */
  28. public function __construct(App $app, LoginServices $services)
  29. {
  30. parent::__construct($app);
  31. $this->services = $services;
  32. }
  33. /**
  34. * 客服登录
  35. * @param Request $request
  36. * @return mixed
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public function login(Request $request)
  42. {
  43. [$account, $password] = $request->postMore([
  44. ['account', ''],
  45. ['password', ''],
  46. ], true);
  47. $this->validate(['account' => $account, 'password' => $password], LoginValidate::class);
  48. $token = $this->services->authLogin($account, $password);
  49. return $this->success('登录成功', $token);
  50. }
  51. /**
  52. * 开放平台扫码登录
  53. * @return mixed
  54. * @throws \think\db\exception\DataNotFoundException
  55. * @throws \think\db\exception\DbException
  56. * @throws \think\db\exception\ModelNotFoundException
  57. */
  58. public function wechatAuth()
  59. {
  60. return $this->success($this->services->wechatAuth());
  61. }
  62. /**
  63. * 获取公众平台id
  64. * @return mixed
  65. */
  66. public function getAppid()
  67. {
  68. return $this->success([
  69. 'appid' => sys_config('wechat_open_app_id', 'wxc736972a4ca1e2a1'),
  70. 'version' => get_crmeb_version(),
  71. 'site_name' => sys_config('site_name')
  72. ]);
  73. }
  74. /**
  75. * 获取登录唯一code
  76. * @return mixed
  77. */
  78. public function getLoginKey()
  79. {
  80. $key = md5(time() . uniqid());
  81. $time = time() + 600;
  82. CacheService::set($key, 1, 600);
  83. return $this->success(['key' => $key, 'time' => $time]);
  84. }
  85. /**
  86. * 验证登录
  87. * @param string $key
  88. * @return mixed
  89. * @throws \Psr\SimpleCache\InvalidArgumentException
  90. * @throws \think\db\exception\DataNotFoundException
  91. * @throws \think\db\exception\DbException
  92. * @throws \think\db\exception\ModelNotFoundException
  93. */
  94. public function scanLogin(string $key)
  95. {
  96. return $this->success($this->services->scanLogin($key));
  97. }
  98. }