OutUserServices.php 8.6 KB

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