UserWechatuserServices.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. declare (strict_types=1);
  12. namespace app\services\user;
  13. use app\model\user\User;
  14. use app\model\wechat\WechatUser;
  15. use qiniu\basic\BaseModel;
  16. use qiniu\basic\BaseServices;
  17. use think\db\exception\DbException;
  18. /**
  19. * Class UserWechatuserServices
  20. * @package app\services\user
  21. * @mixin User
  22. */
  23. class UserWechatuserServices extends BaseServices
  24. {
  25. /**
  26. * @var string
  27. */
  28. protected $alias = '';
  29. /**
  30. * @var string
  31. */
  32. protected $join_alis = '';
  33. /**
  34. * 精确搜索白名单
  35. * @var string[]
  36. */
  37. protected $withField = ['uid', 'nickname', 'user_type', 'phone'];
  38. protected $join_model;
  39. /**
  40. * UserWechatuserServices constructor.
  41. * @param User $model
  42. * @param WechatUser $join_model
  43. */
  44. public function __construct(User $model, WechatUser $join_model)
  45. {
  46. $this->model = $model;
  47. $this->join_model = $join_model;
  48. }
  49. /**
  50. * 关联模型
  51. * @param string $alias
  52. * @param string $join_alias
  53. * @param string $join
  54. * @return BaseModel
  55. */
  56. public function setModel(string $alias = 'u', string $join_alias = 'w', string $join = 'left')
  57. {
  58. $this->alias = $alias;
  59. $this->join_alis = $join_alias;
  60. $wechatUser = $this->join_model;
  61. $table = $wechatUser->getName();
  62. return $this->model->withTrashed()->alias($alias)->join($table . ' ' . $join_alias, $alias . '.uid = ' . $join_alias . '.uid', $join);
  63. }
  64. /**
  65. * 自定义简单查询总数
  66. * @param array $where
  67. * @return int
  68. * @throws DbException
  69. */
  70. public function getCount(array $where): int
  71. {
  72. return $this->setModel()->where($where)->count();
  73. }
  74. /**
  75. * 复杂条件搜索列表
  76. * @param array $where
  77. * @param string $field
  78. * @return array
  79. */
  80. public function getWhereUserList(array $where, string $field): array
  81. {
  82. [$page, $limit] = $this->getPageValue();
  83. $order_string = '';
  84. $order_arr = ['asc', 'desc'];
  85. if (isset($where['now_money']) && in_array($where['now_money'], $order_arr)) {
  86. $order_string = 'now_money ' . $where['now_money'];
  87. }
  88. $list = $this->searchWhere($where)->field($field)->when($page && $limit, function ($query) use ($page, $limit) {
  89. $query->page($page, $limit);
  90. })->group($this->alias . '.uid')->order(($order_string ? $order_string . ' ,' : '') . $this->alias . '.uid desc')->select()->toArray();
  91. $count = $this->searchWhere($where)->group($this->alias . '.uid')->count();
  92. return [$list, $count];
  93. }
  94. /**
  95. * @param $where
  96. * @param array|null $field
  97. * @return BaseModel
  98. */
  99. public function searchWhere($where, ?array $field = [])
  100. {
  101. $model = $this->setModel();
  102. $userAlias = $this->alias . '.';
  103. $wechatUserAlias = $this->join_alis . '.';
  104. if (isset($where['is_filter_del']) && $where['is_filter_del'] == 1) {
  105. $model = $model->where($userAlias . 'delete_time', null);
  106. }
  107. // 用户访问时间
  108. if (isset($where['user_time_type']) && isset($where['user_time'])) {
  109. //最后一次访问时间
  110. if ($where['user_time_type'] == 'visitno' && $where['user_time'] != '') {
  111. [$startTime, $endTime] = explode('-', $where['user_time']);
  112. if ($startTime && $endTime) {
  113. $endTime = strtotime($endTime) + 24 * 3600;
  114. $model = $model->where($userAlias . "last_time < " . strtotime($startTime) . " OR " . $userAlias . "last_time > " . $endTime);
  115. }
  116. }
  117. //访问时间
  118. if ($where['user_time_type'] == 'visit' && $where['user_time'] != '') {
  119. [$startTime, $endTime] = explode('-', $where['user_time']);
  120. if ($startTime && $endTime) {
  121. $model = $model->where($userAlias . 'last_time', '>', strtotime($startTime));
  122. $model = $model->where($userAlias . 'last_time', '<', strtotime($endTime) + 24 * 3600);
  123. }
  124. }
  125. //添加时间
  126. if ($where['user_time_type'] == 'add_time' && $where['user_time'] != '') {
  127. [$startTime, $endTime] = explode('-', $where['user_time']);
  128. if ($startTime && $endTime) {
  129. $model = $model->where($userAlias . 'add_time', '>', strtotime($startTime));
  130. $model = $model->where($userAlias . 'add_time', '<', strtotime($endTime) + 24 * 3600);
  131. }
  132. }
  133. }
  134. //用户等级
  135. if (isset($where['level']) && $where['level']) {
  136. $model = $model->where($userAlias . 'level', $where['level']);
  137. }
  138. //用户分组
  139. if (isset($where['group_id']) && $where['group_id']) {
  140. $model = $model->where($userAlias . 'group_id', $where['group_id']);
  141. }
  142. //用户状态
  143. if (isset($where['status']) && $where['status'] != '') {
  144. $model = $model->where($userAlias . 'status', $where['status']);
  145. }
  146. //用户是否为推广员
  147. if (isset($where['is_promoter']) && $where['is_promoter'] != '') {
  148. $model = $model->where($userAlias . 'is_promoter', $where['is_promoter']);
  149. }
  150. //用户昵称,uid,手机号搜索
  151. $fieldKey = $where['field_key'] ?? '';
  152. $nickname = $where['nickname'] ?? '';
  153. if ($fieldKey && $nickname && in_array($fieldKey, $this->withField)) {
  154. switch ($fieldKey) {
  155. case "nickname":
  156. case "phone":
  157. $model = $model->where($userAlias . trim($fieldKey), 'like', "%" . trim($nickname) . "%");
  158. break;
  159. case "uid":
  160. $model = $model->where($userAlias . trim($fieldKey), trim($nickname));
  161. break;
  162. }
  163. } else if ((!$fieldKey || $fieldKey == 'all') && $nickname) {
  164. $model = $model->where($userAlias . 'nickname|' . $userAlias . 'uid|' . $userAlias . 'phone', 'LIKE', "%$where[nickname]%");
  165. }
  166. //用户类型
  167. if (isset($where['user_type']) && $where['user_type']) {
  168. $model = $model->where($userAlias . 'user_type', $where['user_type']);
  169. }
  170. //用户性别
  171. if (isset($where['sex']) && $where['sex'] !== '' && in_array($where['sex'], [0, 1, 2])) {
  172. $model = $model->where(function ($query) use ($wechatUserAlias, $userAlias, $where) {
  173. $query->where($userAlias . 'sex', $where['sex']);
  174. });
  175. }
  176. //所在国家 所在省份 所在城市
  177. if ((isset($where['country']) && $where['country']) || (isset($where['province']) && $where['province']) || (isset($where['city']) && $where['city'])) {
  178. $model = $model->where(function ($query) use ($wechatUserAlias, $userAlias, $where) {
  179. $query->when(isset($where['country']) && $where['country'], function ($g) use ($wechatUserAlias, $userAlias, $where) {
  180. if ($where['country'] == 'domestic') {
  181. $g->where($wechatUserAlias . 'country', 'in', ['中国', 'China']);
  182. } else if ($where['country'] == 'abroad') {
  183. $g->whereOr($wechatUserAlias . 'country', 'not in', ['中国', 'China']);
  184. }
  185. })->when(isset($where['province']) && $where['province'], function ($q) use ($wechatUserAlias, $userAlias, $where) {
  186. $q->whereOr($wechatUserAlias . 'province', $where['province'])->whereOr($userAlias . 'provincials', 'Like', '%' . $where['province'] . '%');
  187. })->when(isset($where['city']) && $where['city'], function ($c) use ($wechatUserAlias, $userAlias, $where) {
  188. $c->whereOr($wechatUserAlias . 'city', $where['city'])->whereOr($userAlias . 'provincials', 'Like', '%' . $where['city'] . '%');
  189. });
  190. });
  191. }
  192. if (isset($where['time'])) {
  193. $model->withSearch(['time'], ['time' => $where['time'], 'timeKey' => 'u.add_time']);
  194. }
  195. return $field ? $model->field($field) : $model;
  196. }
  197. }