UserSearchServices.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\user;
  13. use app\services\BaseServices;
  14. use app\dao\user\UserSearchDao;
  15. /**
  16. *
  17. * Class UserLabelServices
  18. * @package app\services\user
  19. * * @method getColumn(array $where, string $field, string $key = '') 获取某个字段数组
  20. * * @method getKeywordResult(int $uid, string $keyword, int $preTime = 7200) 获取全局|用户某个关键词搜素结果
  21. */
  22. class UserSearchServices extends BaseServices
  23. {
  24. /**
  25. * UserSearchServices constructor.
  26. * @param UserSearchDao $dao
  27. */
  28. public function __construct(UserSearchDao $dao)
  29. {
  30. $this->dao = $dao;
  31. }
  32. /**
  33. * 获取用户搜索关键词列表
  34. * @param int $uid
  35. * @return array
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function getUserList(int $uid)
  41. {
  42. if (!$uid) {
  43. return [];
  44. }
  45. [$page, $limit] = $this->getPageValue();
  46. return $this->dao->getList(['uid' => $uid, 'is_del' => 0], 'add_time desc,num desc', $page, $limit);
  47. }
  48. /**
  49. * 用户增加搜索记录
  50. * @param int $uid
  51. * @param string $key
  52. * @param array $result
  53. */
  54. public function saveUserSearch(int $uid, string $keyword, array $vicword, array $result)
  55. {
  56. $result = json_encode($result);
  57. $vicword = json_encode($vicword, JSON_UNESCAPED_UNICODE);
  58. $userkeyword = $this->dao->getKeywordResult($uid, $keyword, 0);
  59. $data = [];
  60. $data['result'] = $result;
  61. $data['vicword'] = $vicword;
  62. $data['add_time'] = time();
  63. if ($userkeyword) {
  64. $data['num'] = $userkeyword['num'] + 1;
  65. $this->dao->update(['id' => $userkeyword['id']], $data);
  66. } else {
  67. $data['uid'] = $uid;
  68. $data['keyword'] = $keyword;
  69. $this->dao->save($data);
  70. }
  71. return true;
  72. }
  73. }