OutUserServices.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 $uid
  80. * @return mixed
  81. * @author 吴汐
  82. * @email 442384644@qq.com
  83. * @date 2023/06/20
  84. */
  85. public function userInfo($uid)
  86. {
  87. $fields = ['uid', 'real_name', 'mark', 'nickname', 'avatar', 'phone', 'now_money', 'brokerage_price', 'integral', 'exp', 'sign_num', 'user_type', 'status', 'level',
  88. 'agent_level', 'spread_open', 'spread_uid', 'spread_time', 'user_type', 'is_promoter', 'pay_count', 'is_ever_level', 'is_money_level', 'overdue_time', 'add_time'];
  89. return app()->make(UserServices::class)->get($uid, $fields);
  90. }
  91. /**
  92. * 添加/修改用户
  93. * @param int $uid
  94. * @param array $data
  95. * @return int
  96. * @throws \think\db\exception\DataNotFoundException
  97. * @throws \think\db\exception\DbException
  98. * @throws \think\db\exception\ModelNotFoundException
  99. */
  100. public function saveUser(int $uid, array $data): int
  101. {
  102. if (empty($data['real_name'])) {
  103. throw new ApiException(400760);
  104. }
  105. if (empty($data['phone'])) {
  106. throw new ApiException(400132);
  107. }
  108. if (!check_phone($data['phone'])) {
  109. throw new ApiException(400252);
  110. }
  111. if ($uid < 1 && $this->count(['phone' => $data['phone'], 'is_del' => 0])) {
  112. throw new ApiException(400314);
  113. }
  114. if ($data['pwd']) {
  115. $data['pwd'] = md5($data['pwd']);
  116. } else {
  117. if ($uid < 1) {
  118. $data['pwd'] = md5(123456);
  119. } else {
  120. unset($data['pwd']);
  121. }
  122. }
  123. return $this->transaction(function () use ($uid, $data) {
  124. if ($uid) {
  125. $userInfo = $this->dao->update($uid, $data);
  126. } else {
  127. if (trim($data['real_name']) != '') {
  128. $data['nickname'] = $data['real_name'];
  129. } else {
  130. $data['nickname'] = substr_replace($data['phone'], '****', 3, 4);
  131. }
  132. $data['avatar'] = sys_config('h5_avatar');
  133. $data['user_type'] = 'h5';
  134. $data['add_time'] = time();
  135. $userInfo = $this->dao->save($data);
  136. $uid = (int)$userInfo->uid;
  137. }
  138. if (!$userInfo) {
  139. throw new ApiException(100006);
  140. }
  141. /** @var UserServices $userServices */
  142. $userServices = app()->make(UserServices::class);
  143. $level = (int)$data['level'];
  144. if ($level) {
  145. if (!$userServices->saveGiveLevel($uid, (int)$data['level'])) {
  146. throw new ApiException(400219);
  147. }
  148. }
  149. return $uid;
  150. });
  151. }
  152. /**
  153. * 赠送(积分/余额/付费会员)
  154. * @param int $id
  155. * @param array $data
  156. * @return bool
  157. * @throws \think\Exception
  158. * @throws \think\db\exception\DataNotFoundException
  159. * @throws \think\db\exception\DbException
  160. * @throws \think\db\exception\ModelNotFoundException
  161. */
  162. public function otherGive(int $id, array $data): bool
  163. {
  164. return $this->transaction(function () use ($id, $data) {
  165. /** @var UserServices $userServices */
  166. $userServices = app()->make(UserServices::class);
  167. $days = (int)$data['days'];
  168. $coupon = (int)$data['coupon'];
  169. unset($data['days'], $data['coupon']);
  170. if ($days > 0) {
  171. $userServices->saveGiveLevelTime($id, $days);
  172. }
  173. if ($coupon) {
  174. /** @var StoreCouponIssueServices $issueService */
  175. $issueService = app()->make(StoreCouponIssueServices::class);
  176. $coupon = $issueService->get($data['id']);
  177. if (!$coupon) {
  178. throw new ApiException(100026);
  179. } else {
  180. $coupon = $coupon->toArray();
  181. }
  182. $issueService->setCoupon($coupon, [$id]);
  183. }
  184. $data['adminId'] = 0;
  185. $data['is_other'] = true;
  186. $data['money'] = (string)$data['money'];
  187. $data['integration'] = (string)$data['integration'];
  188. return $userServices->updateInfo($id, $data);
  189. });
  190. }
  191. }