MemberService.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\ActionLogModel;
  13. use App\Models\MemberModel;
  14. use App\Services\BaseService;
  15. use App\Services\RedisService;
  16. /**
  17. * 会员管理-服务类
  18. * @author laravel开发员
  19. * @since 2020/11/11
  20. * Class MemberService
  21. * @package App\Services\Common
  22. */
  23. class MemberService extends BaseService
  24. {
  25. /**
  26. * 构造函数
  27. * @author laravel开发员
  28. * @since 2020/11/11
  29. * MemberService constructor.
  30. */
  31. public function __construct()
  32. {
  33. $this->model = new MemberModel();
  34. }
  35. /**
  36. * 获取列表
  37. * @param $params 参数
  38. * @param int $pageSize 分页大小:默认 15
  39. * @return array
  40. */
  41. public function getDataList($params, $pageSize = 10, $field=[])
  42. {
  43. $where = ['a.mark' => 1];
  44. $status = isset($params['status'])? $params['status'] : 0;
  45. if($status>0){
  46. $where['a.status'] = $status;
  47. }
  48. $list = $this->model->with(['level','parent','point'])
  49. ->from('member as a')
  50. ->leftJoin('member as b','b.id','a.parent_id')
  51. ->where($where)
  52. ->where(function($query) use($params){
  53. $trcUrl = isset($params['trc_url'])? trim($params['trc_url']) : '';
  54. if($trcUrl){
  55. $query->where('a.trc_url','like',"%{$trcUrl}%");
  56. }
  57. $parentUrl = isset($params['parent_trc_url'])? trim($params['parent_trc_url']) : '';
  58. if($parentUrl){
  59. $query->where('b.trc_url','like',"%{$parentUrl}%");
  60. }
  61. $parentId = isset($params['parent_id'])? intval($params['parent_id']) : 0;
  62. if($parentId){
  63. $query->where('a.parent_id',$parentId);
  64. }
  65. $pointId = isset($params['point_id'])? intval($params['point_id']) : 0;
  66. if($pointId){
  67. $query->where('a.point_id',$pointId);
  68. }
  69. })
  70. ->where(function($query) use($params){
  71. $account = isset($params['account'])? trim($params['account']) : '';
  72. if($account){
  73. $query->where('a.username','like',"%{$account}%")->orWhere('a.nickname','like',"%{$account}%");
  74. }
  75. })
  76. ->select($field? $field : ['a.*','b.trc_url as parent_trc_url'])
  77. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  78. $list = $list? $list->toArray() :[];
  79. if($list){
  80. foreach($list['data'] as &$item){
  81. $item['create_time_text'] = $item['create_time']? datetime($item['create_time']):'';
  82. $item['avatar'] = $item['avatar']? get_image_url($item['avatar']): get_image_url('/images/member/logo.png');
  83. $item['invite_num'] = $this->model->where(['parent_id'=> $item['id'],'mark'=>1])->count('id');
  84. $item['team_num'] = $this->model->where(['mark'=>1])->whereRaw('FIND_IN_SET(?,parents)', $item['id'])->count('id');
  85. $item['point_num'] = $this->model->where(['point_id'=>$item['id'],'mark'=>1])->count('id');
  86. }
  87. }
  88. return [
  89. 'pageSize'=> $pageSize,
  90. 'total'=>isset($list['total'])? $list['total'] : 0,
  91. 'list'=> isset($list['data'])? $list['data'] : []
  92. ];
  93. }
  94. /**
  95. * 添加会编辑会员
  96. * @return array
  97. * @since 2020/11/11
  98. * @author laravel开发员
  99. */
  100. public function edit()
  101. {
  102. // 请求参数
  103. $data = request()->all();
  104. // 头像处理
  105. $avatar = trim($data['avatar']);
  106. if (strpos($avatar, "temp")) {
  107. $data['avatar'] = save_image($avatar, 'member');
  108. } else {
  109. $data['avatar'] = str_replace(IMG_URL, "", $data['avatar']);
  110. }
  111. // 出生日期
  112. if ($data['birthday']) {
  113. $data['birthday'] = strtotime($data['birthday']);
  114. }
  115. // 城市处理
  116. $city = isset($data['city']) ? $data['city'] : [3];
  117. if (!empty($data['city'])) {
  118. // 省份
  119. $data['province_id'] = $city[0];
  120. // 城市
  121. $data['city_id'] = $city[1];
  122. // 县区
  123. $data['district_id'] = $city[2];
  124. }
  125. if(isset($data['password']) && $data['password']){
  126. $data['password'] = get_password(trim($data['password']));
  127. }
  128. unset($data['city']);
  129. ActionLogModel::setTitle("修改会员信息");
  130. ActionLogModel::record();
  131. return parent::edit($data); // TODO: Change the autogenerated stub
  132. }
  133. /**
  134. * 上级用户列表
  135. * @return array
  136. */
  137. public function parents()
  138. {
  139. // 获取参数
  140. $param = request()->all();
  141. // 用户ID
  142. $keyword = getter($param, "keyword");
  143. $parentId = getter($param, "parent_id");
  144. $userId = getter($param, "user_id");
  145. $datas = $this->model->where(function($query) use($parentId){
  146. if($parentId){
  147. $query->where(['id'=> $parentId,'mark'=>1]);
  148. }else{
  149. $query->where(['status'=> 1,'mark'=>1]);
  150. }
  151. })
  152. ->where(function($query) use($userId){
  153. if($userId){
  154. $query->whereNotIn('id', [$userId]);
  155. }
  156. })
  157. ->where(function($query) use($keyword){
  158. if($keyword){
  159. $query->where('username','like',"{$keyword}%")->orWhere('mobile','like',"{$keyword}%");
  160. }
  161. })
  162. ->select(['id','username','mobile','code','nickname','status'])
  163. ->get();
  164. return $datas? $datas->toArray() : [];
  165. }
  166. /**
  167. * 用户选项
  168. * @return array
  169. */
  170. public function options()
  171. {
  172. // 获取参数
  173. $param = request()->all();
  174. // 用户ID
  175. $keyword = getter($param, "keyword");
  176. $parentId = getter($param, "parent_id");
  177. $userId = getter($param, "user_id");
  178. $datas = $this->model->where(function($query) use($parentId){
  179. if($parentId){
  180. $query->where(['id'=> $parentId,'mark'=>1]);
  181. }else{
  182. $query->where(['status'=> 1,'mark'=>1]);
  183. }
  184. })
  185. ->where(function($query) use($userId){
  186. if($userId){
  187. $query->whereNotIn('id', [$userId]);
  188. }
  189. })
  190. ->where(function($query) use($keyword){
  191. if($keyword){
  192. $query->where('nickname','like',"%{$keyword}%")->orWhere('username','like',"%{$keyword}%");
  193. }
  194. })
  195. ->select(['id','username','mobile','code','nickname','status'])
  196. ->get();
  197. return $datas? $datas->toArray() : [];
  198. }
  199. /**
  200. * 推荐树
  201. * @return array|false|mixed
  202. */
  203. public function getTree()
  204. {
  205. // 请求参数
  206. $keyword = request()->post('keyword','');
  207. $cacheKey = "caches:member:trees:".md5('t'.$keyword);
  208. $datas = RedisService::get($cacheKey);
  209. if($datas){
  210. return $datas;
  211. }
  212. $datas = $this->model->where(['status'=>1,'mark'=>1])
  213. ->select(['id','username','nickname','mobile','parent_id','trc_url','point_id','status'])
  214. ->get()
  215. ->keyBy('id');
  216. $datas = $datas? $datas->toArray() : [];
  217. $pid = 0;
  218. if($keyword){
  219. $data = $this->model->where(function($query) use($keyword){
  220. $query->where('id','=', $keyword)
  221. ->orWhere('nickname','like',"%{$keyword}%")
  222. ->orWhere('username','like',"%{$keyword}%")
  223. ->orWhere('trc_url','=',trim($keyword));
  224. })
  225. ->where(['status'=>1,'mark'=>1])
  226. ->orderBy('parent_id','asc')
  227. ->select(['id','parent_id','nickname','trc_url','username'])
  228. ->first();
  229. $id = isset($data['id'])? $data['id'] : 0;
  230. $nickname = isset($data['nickname'])? $data['nickname'] : '';
  231. $username = isset($data['username'])? $data['username'] : '';
  232. $trcUrl = isset($data['trc_url']) && $data['trc_url']? format_mobile($data['trc_url']) : '';
  233. if($data){
  234. $pid = isset($data['id'])? $data['id'] : 0;
  235. $data['label'] = $nickname."(ID:{$id} {$trcUrl})";
  236. unset($data['nickname']);
  237. unset($data['username']);
  238. }
  239. }
  240. $datas = get_tree($datas, $pid);
  241. if($datas){
  242. if($pid){
  243. $data['children'] = $datas;
  244. $newDatas[0] = $data;
  245. $datas = $newDatas;
  246. }
  247. RedisService::set($cacheKey, $datas, rand(3,5));
  248. }
  249. return $datas;
  250. }
  251. }