OutAccountServices.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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\services\out;
  12. use app\dao\out\OutAccountDao;
  13. use crmeb\basic\BaseAuth;
  14. use app\services\BaseServices;
  15. use crmeb\exceptions\AuthException;
  16. use crmeb\utils\ApiErrorCode;
  17. use think\exception\ValidateException;
  18. /**
  19. * 获取token
  20. * Class LoginServices
  21. * @package app\services\kefu
  22. * @mixin OutAccountDao
  23. */
  24. class OutAccountServices extends BaseServices
  25. {
  26. const FEPAORPL = 'OSeCVa';
  27. /**
  28. * LoginServices constructor.
  29. * @param OutAccountDao $dao
  30. */
  31. public function __construct(OutAccountDao $dao)
  32. {
  33. $this->dao = $dao;
  34. }
  35. /**
  36. * 账号密码登录
  37. * @param string $appid
  38. * @param string $appsecret
  39. * @return array
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\DbException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. */
  44. public function authLogin(string $appid, string $appsecret = null)
  45. {
  46. $autInfo = $this->dao->get(['appid' => $appid, 'is_del' => 0]);
  47. if (!$autInfo) {
  48. throw new ValidateException('没有此用户');
  49. }
  50. if ($appsecret && !password_verify($appsecret, $autInfo->appsecret)) {
  51. throw new ValidateException('appid或appsecret错误');
  52. }
  53. if ($autInfo->status == 2) {
  54. throw new ValidateException('您已被禁止登录');
  55. }
  56. $token = $this->createToken($autInfo->id, 'out', $autInfo->appsecret);
  57. $data['last_time'] = time();
  58. $data['ip'] = request()->ip();
  59. $this->update($autInfo['id'], $data);
  60. return [
  61. 'token' => $token['token'],
  62. 'exp_time' => $token['params']['exp'],
  63. 'autInfo' => $autInfo->hidden(['appsecret', 'ip', 'is_del', 'add_time', 'status', 'last_time'])->toArray()
  64. ];
  65. }
  66. /**
  67. * 解析token
  68. * @param string $token
  69. * @return array
  70. * @throws \Psr\SimpleCache\InvalidArgumentException
  71. * @throws \think\db\exception\DataNotFoundException
  72. * @throws \think\db\exception\DbException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. */
  75. public function parseToken(string $token)
  76. {
  77. /** @var BaseAuth $services */
  78. $services = app()->make(BaseAuth::class);
  79. $adminInfo = $services->parseToken($token, function ($id) {
  80. return $this->dao->get($id);
  81. });
  82. if (isset($adminInfo->auth) && $adminInfo->auth !== md5($adminInfo->appsecret)) {
  83. throw new AuthException(ApiErrorCode::ERR_LOGIN_INVALID);
  84. }
  85. return $adminInfo->hidden(['appsecret', 'ip', 'status']);
  86. }
  87. /**
  88. * 获取一条
  89. * @return array|\think\Model|null
  90. * @throws \think\db\exception\DataNotFoundException
  91. * @throws \think\db\exception\DbException
  92. * @throws \think\db\exception\ModelNotFoundException
  93. */
  94. public function getOne($where = [])
  95. {
  96. $info = $this->dao->getOne($where);
  97. return $info ? $info->toArray() : [];
  98. }
  99. /**
  100. * 获取列表
  101. * @param array $where
  102. * @return array
  103. */
  104. public function getList(array $where = [])
  105. {
  106. [$page, $limit] = $this->getPageValue();
  107. $where['is_del'] = 0;
  108. $list = $this->dao->getList((array)$where, (int)$page, (int)$limit);
  109. $count = $this->dao->count($where);
  110. if ($list) {
  111. foreach ($list as &$item) {
  112. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '暂无';
  113. $item['last_time'] = $item['last_time'] ? date('Y-m-d H:i:s', $item['last_time']) : '暂无';
  114. }
  115. }
  116. return compact('count', 'list');
  117. }
  118. }