StoreUserServices.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\store;
  12. use app\dao\store\StoreUserDao;
  13. use app\services\BaseServices;
  14. use app\services\user\level\SystemUserLevelServices;
  15. use app\services\user\level\UserLevelServices;
  16. use app\services\user\UserServices;
  17. use think\exception\ValidateException;
  18. /**
  19. * 门店用户
  20. * Class StoreUser
  21. * @package app\services\store
  22. * @mixin StoreUserDao
  23. */
  24. class StoreUserServices extends BaseServices
  25. {
  26. /**
  27. * 构造方法
  28. * StoreUser constructor.
  29. * @param StoreUserDao $dao
  30. */
  31. public function __construct(StoreUserDao $dao)
  32. {
  33. $this->dao = $dao;
  34. }
  35. /**
  36. * @param array $where
  37. * @param int $store_id
  38. * @return array
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public function index(array $where, int $store_id)
  44. {
  45. $where['is_filter_del'] = 1;
  46. $where['store_id'] = $store_id;
  47. /** @var UserStoreUserServices $userStoreUserServices */
  48. $userStoreUserServices = app()->make(UserStoreUserServices::class);
  49. $fields = 'u.*';
  50. [$list, $count] = $userStoreUserServices->getWhereUserList($where, $fields);
  51. if ($list) {
  52. $uids = array_column($list, 'uid');
  53. /** @var UserServices $userServices */
  54. $userServices = app()->make(UserServices::class);
  55. $userlabel = $userServices->getUserLablel($uids, 1, $store_id);
  56. $levelName = app()->make(SystemUserLevelServices::class)->getUsersLevel(array_unique(array_column($list, 'level')));
  57. $userLevel = app()->make(UserLevelServices::class)->getUsersLevelInfo($uids);
  58. $spread_names = $userServices->getColumn([['uid', 'in', $uids]], 'nickname', 'uid');
  59. /** @var SystemStoreStaffServices $staffServices */
  60. $staffServices = app()->make(SystemStoreStaffServices::class);
  61. $staffNames = $staffServices->getColumn([['store_id', '=', $store_id], ['uid', 'in', array_column($list, 'uid')], ['is_del', '=', 0]], 'uid,staff_name', 'uid');
  62. foreach ($list as &$item) {
  63. $item['status'] = ($item['status'] == 1) ? '正常' : '禁止';
  64. $item['birthday'] = $item['birthday'] ? date('Y-m-d', (int)$item['birthday']) : '';
  65. $item['spread_uid_nickname'] = $item['spread_uid'] ? ($spread_names[$item['spread_uid']] ?? '') . '/' . $item['spread_uid'] : '无';
  66. $item['staff_name'] = $staffNames[$item['uid']]['staff_name'] ?? '';
  67. //用户类型
  68. if ($item['user_type'] == 'routine') {
  69. $item['user_type'] = '小程序';
  70. } else if ($item['user_type'] == 'wechat') {
  71. $item['user_type'] = '公众号';
  72. } else if ($item['user_type'] == 'h5') {
  73. $item['user_type'] = 'H5';
  74. } else if ($item['user_type'] == 'pc') {
  75. $item['user_type'] = 'PC';
  76. } else if ($item['user_type'] == 'app') {
  77. $item['user_type'] = 'APP';
  78. } else $item['user_type'] = '其他';
  79. //等级名称
  80. $item['level'] = $levelName[$item['level']] ?? '无';
  81. //用户等级
  82. $item['vip_name'] = false;
  83. $levelinfo = $userLevel[$item['uid']] ?? null;
  84. if ($levelinfo) {
  85. if ($levelinfo && ($levelinfo['is_forever'] || time() < $levelinfo['valid_time'])) {
  86. $item['vip_name'] = $item['level'] != '无' ? $item['level'] : false;
  87. }
  88. }
  89. $item['labels'] = $userlabel[$item['uid']] ?? '';
  90. $item['isMember'] = $item['is_money_level'] > 0 ? 1 : 0;
  91. }
  92. }
  93. return compact('count', 'list');
  94. }
  95. /**
  96. * 同步写入门店用户
  97. * @param int $uid
  98. * @param int $store_id
  99. * @return bool
  100. * @throws \think\db\exception\DataNotFoundException
  101. * @throws \think\db\exception\DbException
  102. * @throws \think\db\exception\ModelNotFoundException
  103. */
  104. public function setStoreUser(int $uid, int $store_id)
  105. {
  106. $storeUser = $this->dao->getOne(['uid' => $uid, 'store_id' => $store_id]);
  107. if (!$storeUser) {
  108. if (!$this->dao->save(['uid' => $uid, 'store_id' => $store_id, 'add_time' => time()])) {
  109. throw new ValidateException('新增门店用户失败');
  110. }
  111. }
  112. return true;
  113. }
  114. }