Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

OutAccountServices.php 3.5KB

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