// +---------------------------------------------------------------------- namespace App\Services\Common; use App\Models\ActionLogModel; use App\Models\MemberModel; use App\Services\BaseService; use App\Services\RedisService; /** * 会员管理-服务类 * @author laravel开发员 * @since 2020/11/11 * Class MemberService * @package App\Services\Common */ class MemberService extends BaseService { /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * MemberService constructor. */ public function __construct() { $this->model = new MemberModel(); } /** * 获取列表 * @param $params 参数 * @param int $pageSize 分页大小:默认 15 * @return array */ public function getDataList($params, $pageSize = 10, $field=[]) { $where = ['a.mark' => 1]; $status = isset($params['status'])? $params['status'] : 0; if($status>0){ $where['a.status'] = $status; } $list = $this->model->with(['level','parent','point']) ->from('member as a') ->leftJoin('member as b','b.id','a.parent_id') ->where($where) ->where(function($query) use($params){ $trcUrl = isset($params['trc_url'])? trim($params['trc_url']) : ''; if($trcUrl){ $query->where('a.trc_url','like',"%{$trcUrl}%"); } $parentUrl = isset($params['parent_trc_url'])? trim($params['parent_trc_url']) : ''; if($parentUrl){ $query->where('b.trc_url','like',"%{$parentUrl}%"); } $parentId = isset($params['parent_id'])? intval($params['parent_id']) : 0; if($parentId){ $query->where('a.parent_id',$parentId); } $pointId = isset($params['point_id'])? intval($params['point_id']) : 0; if($pointId){ $query->where('a.point_id',$pointId); } }) ->where(function($query) use($params){ $account = isset($params['account'])? trim($params['account']) : ''; if($account){ $query->where('a.username','like',"%{$account}%")->orWhere('a.nickname','like',"%{$account}%"); } }) ->select($field? $field : ['a.*','b.trc_url as parent_trc_url']) ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list? $list->toArray() :[]; if($list){ foreach($list['data'] as &$item){ $item['create_time_text'] = $item['create_time']? datetime($item['create_time']):''; $item['avatar'] = $item['avatar']? get_image_url($item['avatar']): get_image_url('/images/member/logo.png'); $item['invite_num'] = $this->model->where(['parent_id'=> $item['id'],'mark'=>1])->count('id'); $item['team_num'] = $this->model->where(['mark'=>1])->whereRaw('FIND_IN_SET(?,parents)', $item['id'])->count('id'); $item['point_num'] = $this->model->where(['point_id'=>$item['id'],'mark'=>1])->count('id'); } } return [ 'pageSize'=> $pageSize, 'total'=>isset($list['total'])? $list['total'] : 0, 'list'=> isset($list['data'])? $list['data'] : [] ]; } /** * 添加会编辑会员 * @return array * @since 2020/11/11 * @author laravel开发员 */ public function edit() { // 请求参数 $data = request()->all(); // 头像处理 $avatar = trim($data['avatar']); if (strpos($avatar, "temp")) { $data['avatar'] = save_image($avatar, 'member'); } else { $data['avatar'] = str_replace(IMG_URL, "", $data['avatar']); } // 出生日期 if ($data['birthday']) { $data['birthday'] = strtotime($data['birthday']); } // 城市处理 $city = isset($data['city']) ? $data['city'] : [3]; if (!empty($data['city'])) { // 省份 $data['province_id'] = $city[0]; // 城市 $data['city_id'] = $city[1]; // 县区 $data['district_id'] = $city[2]; } if(isset($data['password']) && $data['password']){ $data['password'] = get_password(trim($data['password'])); } unset($data['city']); ActionLogModel::setTitle("修改会员信息"); ActionLogModel::record(); return parent::edit($data); // TODO: Change the autogenerated stub } /** * 上级用户列表 * @return array */ public function parents() { // 获取参数 $param = request()->all(); // 用户ID $keyword = getter($param, "keyword"); $parentId = getter($param, "parent_id"); $userId = getter($param, "user_id"); $datas = $this->model->where(function($query) use($parentId){ if($parentId){ $query->where(['id'=> $parentId,'mark'=>1]); }else{ $query->where(['status'=> 1,'mark'=>1]); } }) ->where(function($query) use($userId){ if($userId){ $query->whereNotIn('id', [$userId]); } }) ->where(function($query) use($keyword){ if($keyword){ $query->where('username','like',"{$keyword}%")->orWhere('mobile','like',"{$keyword}%"); } }) ->select(['id','username','mobile','code','nickname','status']) ->get(); return $datas? $datas->toArray() : []; } /** * 用户选项 * @return array */ public function options() { // 获取参数 $param = request()->all(); // 用户ID $keyword = getter($param, "keyword"); $parentId = getter($param, "parent_id"); $userId = getter($param, "user_id"); $datas = $this->model->where(function($query) use($parentId){ if($parentId){ $query->where(['id'=> $parentId,'mark'=>1]); }else{ $query->where(['status'=> 1,'mark'=>1]); } }) ->where(function($query) use($userId){ if($userId){ $query->whereNotIn('id', [$userId]); } }) ->where(function($query) use($keyword){ if($keyword){ $query->where('nickname','like',"%{$keyword}%")->orWhere('username','like',"%{$keyword}%"); } }) ->select(['id','username','mobile','code','nickname','status']) ->get(); return $datas? $datas->toArray() : []; } /** * 推荐树 * @return array|false|mixed */ public function getTree() { // 请求参数 $keyword = request()->post('keyword',''); $cacheKey = "caches:member:trees:".md5('t'.$keyword); $datas = RedisService::get($cacheKey); if($datas){ return $datas; } $datas = $this->model->where(['status'=>1,'mark'=>1]) ->select(['id','username','nickname','mobile','parent_id','trc_url','point_id','status']) ->get() ->keyBy('id'); $datas = $datas? $datas->toArray() : []; $pid = 0; if($keyword){ $data = $this->model->where(function($query) use($keyword){ $query->where('id','=', $keyword) ->orWhere('nickname','like',"%{$keyword}%") ->orWhere('username','like',"%{$keyword}%") ->orWhere('trc_url','=',trim($keyword)); }) ->where(['status'=>1,'mark'=>1]) ->orderBy('parent_id','asc') ->select(['id','parent_id','nickname','trc_url','username']) ->first(); $id = isset($data['id'])? $data['id'] : 0; $nickname = isset($data['nickname'])? $data['nickname'] : ''; $username = isset($data['username'])? $data['username'] : ''; $trcUrl = isset($data['trc_url']) && $data['trc_url']? format_mobile($data['trc_url']) : ''; if($data){ $pid = isset($data['id'])? $data['id'] : 0; $data['label'] = $nickname."(ID:{$id} {$trcUrl})"; unset($data['nickname']); unset($data['username']); } } $datas = get_tree($datas, $pid); if($datas){ if($pid){ $data['children'] = $datas; $newDatas[0] = $data; $datas = $newDatas; } RedisService::set($cacheKey, $datas, rand(3,5)); } return $datas; } }