UserSearchServices.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. declare (strict_types=1);
  12. namespace app\services\user;
  13. use app\services\BaseServices;
  14. use app\dao\user\UserSearchDao;
  15. use think\exception\ValidateException;
  16. /**
  17. *
  18. * Class UserLabelServices
  19. * @package app\services\user
  20. * * @method getColumn(array $where, string $field, string $key = '') 获取某个字段数组
  21. * * @method getKeywordResult(int $uid, string $keyword, int $preTime = 7200) 获取全局|用户某个关键词搜素结果
  22. */
  23. class UserSearchServices extends BaseServices
  24. {
  25. /**
  26. * UserSearchServices constructor.
  27. * @param UserSearchDao $dao
  28. */
  29. public function __construct(UserSearchDao $dao)
  30. {
  31. $this->dao = $dao;
  32. }
  33. /**
  34. * 获取用户搜索关键词列表
  35. * @param int $uid
  36. * @return array
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public function getUserList(int $uid)
  42. {
  43. if (!$uid) {
  44. return [];
  45. }
  46. [$page, $limit] = $this->getPageValue();
  47. return $this->dao->getList(['uid' => $uid, 'is_del' => 0], 'add_time desc,num desc', $page, $limit);
  48. }
  49. /**
  50. * 用户增加搜索记录
  51. * @param int $uid
  52. * @param string $key
  53. * @param array $result
  54. */
  55. public function saveUserSearch(int $uid, string $keyword, array $vicword, array $result)
  56. {
  57. $result = json_encode($result);
  58. $vicword = json_encode($vicword, JSON_UNESCAPED_UNICODE);
  59. $userkeyword = $this->dao->getKeywordResult($uid, $keyword, 0);
  60. $data = [];
  61. $data['result'] = $result;
  62. $data['vicword'] = $vicword;
  63. $data['add_time'] = time();
  64. if ($userkeyword) {
  65. $data['num'] = $userkeyword['num'] + 1;
  66. $this->dao->update(['id' => $userkeyword['id']], $data);
  67. } else {
  68. $data['uid'] = $uid;
  69. $data['keyword'] = $keyword;
  70. $this->dao->save($data);
  71. }
  72. return true;
  73. }
  74. }