UserServices.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\kefu;
  12. use app\dao\user\UserDao;
  13. use app\services\BaseServices;
  14. use crmeb\traits\ServicesTrait;
  15. use think\exception\ValidateException;
  16. use app\services\user\UserServices as BaseUserServices;
  17. use app\services\user\label\UserLabelServices;
  18. use app\services\user\level\SystemUserLevelServices;
  19. use app\services\user\label\UserLabelRelationServices;
  20. use app\services\message\service\StoreServiceRecordServices;
  21. /**
  22. * Class UserServices
  23. * @package app\services\kefu
  24. * @mixin UserDao
  25. */
  26. class UserServices extends BaseServices
  27. {
  28. use ServicesTrait;
  29. const KXUAMBKF = '$MAO8w';
  30. /**
  31. * UserServices constructor.
  32. * @param UserDao $dao
  33. */
  34. public function __construct(UserDao $dao)
  35. {
  36. $this->dao = $dao;
  37. }
  38. /**
  39. * 获取用户信息
  40. * @param int $uid
  41. * @param int $store_id
  42. * @return array
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\DbException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. */
  47. public function getUserInfo(int $uid, int $store_id = 0)
  48. {
  49. /** @var StoreServiceRecordServices $kefuService */
  50. $kefuService = app()->make(StoreServiceRecordServices::class);
  51. if (!$kefuService->count(['to_uid' => $uid])) {
  52. throw new ValidateException('不存在此用户');
  53. }
  54. $userInfo = $this->dao->get($uid, ['nickname', 'avatar', 'spread_uid', 'is_promoter', 'birthday', 'now_money', 'user_type', 'level', 'group_id', 'phone', 'is_money_level'], ['userGroup']);
  55. if (!$userInfo) {
  56. throw new ValidateException('用户不存在');
  57. }
  58. /** @var BaseUserServices $userServices */
  59. $userServices = app()->make(BaseUserServices::class);
  60. $userInfo['is_promoter'] = $userServices->checkUserPromoter($uid);
  61. /** @var UserLabelRelationServices $labalServices */
  62. $labalServices = app()->make(UserLabelRelationServices::class);
  63. if ($store_id) {
  64. $where = ['uid' => $uid, 'type' => 1, 'relation_id' => $store_id];
  65. } else {
  66. $where = ['uid' => $uid, 'type' => 0];
  67. }
  68. $labalId = $labalServices->getColumn($where, 'label_id', 'label_id');
  69. /** @var UserLabelServices $services */
  70. $services = app()->make(UserLabelServices::class);
  71. $labelNames = $services->getColumn([['id', 'in', $labalId]], 'label_name');
  72. $userInfo->labelNames = $labelNames;
  73. $userInfo->spread_name = $userInfo->level_name = '';
  74. if ($userInfo->spread_uid) {
  75. $userInfo->spread_name = $this->dao->value(['uid' => $userInfo->spread_uid], 'nickname');
  76. }
  77. if ($userInfo->level) {
  78. /** @var SystemUserLevelServices $levelService */
  79. $levelService = app()->make(SystemUserLevelServices::class);
  80. $userInfo->level_name = $levelService->value(['id' => $userInfo->level], 'name');
  81. }
  82. if ($userInfo->userGroup) {
  83. $userInfo->group_name = $userInfo->userGroup->group_name;
  84. unset($userInfo->userGroup);
  85. }
  86. return $userInfo->toArray();
  87. }
  88. }