OutUserServices.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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\dao\user\UserDao;
  14. use app\services\activity\coupon\StoreCouponIssueServices;
  15. use app\services\BaseServices;
  16. use app\services\system\SystemUserLevelServices;
  17. use crmeb\exceptions\ApiException;
  18. /**
  19. *
  20. * Class OutUserServices
  21. * @package app\services\user
  22. */
  23. class OutUserServices extends BaseServices
  24. {
  25. /**
  26. * UserServices constructor.
  27. * @param UserDao $dao
  28. */
  29. public function __construct(UserDao $dao)
  30. {
  31. $this->dao = $dao;
  32. }
  33. /**
  34. * 用户列表
  35. * @param array $where
  36. * @return array
  37. */
  38. public function getUserList(array $where): array
  39. {
  40. /** @var UserWechatuserServices $userWechatUser */
  41. $userWechatUser = app()->make(UserWechatuserServices::class);
  42. $fields = 'u.uid, u.real_name, u.mark, u.nickname, u.avatar, u.phone, u.now_money, u.brokerage_price, u.integral, u.exp, u.sign_num, u.user_type,
  43. u.status, u.level, u.agent_level, u.spread_open, u.spread_uid, u.spread_time, u.user_type, u.is_promoter, u.pay_count, u.is_ever_level, u.is_money_level,
  44. u.overdue_time, u.add_time';
  45. [$list, $count] = $userWechatUser->getWhereUserList($where, $fields);
  46. if ($list) {
  47. $uids = array_column($list, 'uid');
  48. $levelName = app()->make(SystemUserLevelServices::class)->getUsersLevel(array_unique(array_column($list, 'level')));
  49. $userLevel = app()->make(UserLevelServices::class)->getUsersLevelInfo($uids);
  50. $spreadNames = $this->dao->getColumn([['uid', 'in', array_unique(array_column($list, 'spread_uid'))]], 'nickname', 'uid');
  51. foreach ($list as &$item) {
  52. $item['spread_uid_nickname'] = $item['spread_uid'] ? ($spreadNames[$item['spread_uid']] ?? '') . '/' . $item['spread_uid'] : '';
  53. //用户类型
  54. if ($item['user_type'] == 'routine') {
  55. $item['user_type'] = '小程序';
  56. } else if ($item['user_type'] == 'wechat') {
  57. $item['user_type'] = '公众号';
  58. } else if ($item['user_type'] == 'h5') {
  59. $item['user_type'] = 'H5';
  60. } else if ($item['user_type'] == 'pc') {
  61. $item['user_type'] = 'PC';
  62. } else if ($item['user_type'] == 'app' || $item['user_type'] == 'apple') {
  63. $item['user_type'] = 'APP';
  64. } else $item['user_type'] = '其他';
  65. //用户等级
  66. $item['level_name'] = "";
  67. $levelInfo = $userLevel[$item['uid']] ?? null;
  68. if ($levelInfo) {
  69. if ($levelInfo['is_forever'] || time() < $levelInfo['valid_time']) {
  70. $item['level_name'] = $levelName[$item['level']] ?? '';
  71. }
  72. }
  73. }
  74. }
  75. return compact('list', 'count');
  76. }
  77. /**
  78. * 添加/修改用户
  79. * @param int $uid
  80. * @param array $data
  81. * @return int
  82. */
  83. public function saveUser(int $uid, array $data): int
  84. {
  85. if (empty($data['real_name'])) {
  86. throw new ApiException(400760);
  87. }
  88. if (empty($data['phone'])) {
  89. throw new ApiException(400132);
  90. }
  91. if (!check_phone($data['phone'])) {
  92. throw new ApiException(400252);
  93. }
  94. if ($uid < 1 && $this->count(['phone' => $data['phone'], 'is_del' => 0])) {
  95. throw new ApiException(400314);
  96. }
  97. if ($data['pwd']) {
  98. $data['pwd'] = md5($data['pwd']);
  99. } else {
  100. if ($uid < 1) {
  101. $data['pwd'] = md5(123456);
  102. } else {
  103. unset($data['pwd']);
  104. }
  105. }
  106. return $this->transaction(function () use ($uid, $data) {
  107. if ($uid) {
  108. $userInfo = $this->dao->update($uid, $data);
  109. } else {
  110. if (trim($data['real_name']) != '') {
  111. $data['nickname'] = $data['real_name'];
  112. } else {
  113. $data['nickname'] = substr_replace($data['phone'], '****', 3, 4);
  114. }
  115. $data['avatar'] = sys_config('h5_avatar');
  116. $data['user_type'] = 'h5';
  117. $data['add_time'] = time();
  118. $userInfo = $this->dao->save($data);
  119. $uid = (int)$userInfo->uid;
  120. }
  121. if (!$userInfo) {
  122. throw new ApiException(100006);
  123. }
  124. /** @var UserServices $userServices */
  125. $userServices = app()->make(UserServices::class);
  126. $level = (int)$data['level'];
  127. if ($level) {
  128. if (!$userServices->saveGiveLevel($uid, (int)$data['level'])) {
  129. throw new ApiException(400219);
  130. }
  131. }
  132. return $uid;
  133. });
  134. }
  135. /**
  136. * 赠送(积分/余额/付费会员)
  137. * @param int $id
  138. * @param array $data
  139. * @return bool
  140. */
  141. public function otherGive(int $id, array $data): bool
  142. {
  143. return $this->transaction(function () use ($id, $data) {
  144. /** @var UserServices $userServices */
  145. $userServices = app()->make(UserServices::class);
  146. $days = (int)$data['days'];
  147. $coupon = (int)$data['coupon'];
  148. unset($data['days'], $data['coupon']);
  149. if ($days > 0) {
  150. $userServices->saveGiveLevelTime($id, $days);
  151. }
  152. if ($coupon) {
  153. /** @var StoreCouponIssueServices $issueService */
  154. $issueService = app()->make(StoreCouponIssueServices::class);
  155. $coupon = $issueService->get($data['id']);
  156. if (!$coupon) {
  157. throw new ApiException(100026);
  158. } else {
  159. $coupon = $coupon->toArray();
  160. }
  161. $issueService->setCoupon($coupon, [$id]);
  162. }
  163. $data['adminId'] = 0;
  164. $data['is_other'] = true;
  165. $data['money'] = (string)$data['money'];
  166. $data['integration'] = (string)$data['integration'];
  167. return $userServices->updateInfo($id, $data);
  168. });
  169. }
  170. }