model = model('AdUserGroup'); $this->adUserGroupExtendModel = model('AdUserGroupExtend'); $this->view->assign('typeList', $this->model->getTypeList()); $this->view->assign('userCate', $this->model->getUserCate()); $this->view->assign('userSex', $this->model->getUserSex()); $this->view->assign('baseType', $this->model->getBaseType()); $this->view->assign('vipType', $this->model->getVipType()); $this->view->assign('tailNumType', $this->model->getTailNumType()); $this->view->assign('rechargeProperty', $this->model->getRechargeProperty()); $this->view->assign('rechargeType', $this->model->getRechargeType()); $this->view->assign('readProperty', $this->model->getReadProperty()); $this->view->assign('consumerProperty', $this->model->getConsumerProperty()); $this->view->assign('readType', $this->model->getReadType()); $this->view->assign('readRecordType', $this->model->getReadRecordType()); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法 * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 查看 */ public function index() { //设置过滤方法 $this->request->filter(['strip_tags']); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('pkey_name')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $map = ['is_del' => 0, 'create_by' => $this->auth->id]; $total = $this->model ->where($where) ->where($map) ->order($sort, $order) ->count(); $list = $this->model ->where($where) ->where($map) ->order($sort, $order) ->limit($offset, $limit) ->select(); $result = array("total" => $total, "rows" => $list); return json($result); } return $this->view->fetch(); } /** * 添加 */ public function add() { $selcted_categories = []; $this->assign('selcted_categories', $selcted_categories); $selcted_consumer_categories = []; $this->assign('selcted_consumer_categories', $selcted_consumer_categories); if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { /* * 已经弃用,如果为了兼容老版可取消注释 foreach ($params as $k => &$v) { $v = is_array($v) ? implode(',', $v) : $v; } */ if ($this->dataLimit) { $params[$this->dataLimitField] = $this->auth->id; } try { //是否采用模型验证 if ($this->modelValidate) { $name = basename(str_replace('\\', '/', get_class($this->model))); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate; $this->model->validate($validate); } if(empty($params['group_name'])){ $this->error('用户集合名词不能为空'); } $group_type = $params['group_type']; if($group_type == 0){ //全部用户 $data = []; $data['group_name'] = $params['group_name']; $data['group_type'] = $params['group_type']; $data['create_by'] = $this->auth->id; $data['edit_by'] = $this->auth->id; $result = $this->model->allowField(true)->save($data); }elseif ($group_type == 1){ //自定义 $data = []; $data['group_name'] = $params['group_name']; $data['group_type'] = $params['group_type']; $user_ids = $this->arr_uni_str($params['user_ids']); if (empty($user_ids)) { $this->error('用户ID不能为空'); } elseif (count(explode(',', $user_ids)) < 2) { $this->error('用户ID最少填写两个'); } $data['create_by'] = $this->auth->id; $data['edit_by'] = $this->auth->id; $rst = $this->model->allowField(true)->save($data); if ($rst) { $send_user_group_id = $this->model->id; $send_extend_data = [ 'user_group_id' => $send_user_group_id, 'user_ids' => $user_ids, 'create_by' => $this->auth->id, 'edit_by' => $this->auth->id ]; $result = $this->adUserGroupExtendModel->allowField(true)->save($send_extend_data); }else{ $result = false; } }else{ //条件筛选 $data = []; $data['group_name'] = $params['group_name']; $data['group_type'] = $params['group_type']; unset($params['group_name']); unset($params['group_type']); unset($params['user_ids']); if(!isset($params['vipType'])){ $params['vipType'] = ''; } if(!isset($params['tailNum'])){ $params['tailNum'] = ''; } if(!isset($params['rechargeType'])){ $params['rechargeType'] = ''; } if(!isset($params['readType'])){ $params['readType'] = ''; } //重构 $params $this->restruct_params($params); // $this->filterFromParams($params); // 校验表单 $this->validateForm($params); if($params['userSex'] == -1){ $params['userSex'] = ''; } $params['read_book_categories'] = implode(',', array_unique(array_filter(explode(',', $params['read_book_categories'])))); //$params['consumer_book_categories'] = implode(',', array_unique(array_filter(explode(',', $params['consumer_book_categories'])))); $user_json = json_encode($params); $data['user_json'] = $user_json; $data['create_by'] = $this->auth->id; $data['edit_by'] = $this->auth->id; $result = $this->model->allowField(true)->save($data); } if ($result !== false) { $this->success(); } else { $this->error($this->model->getError()); } } catch (\think\exception\PDOException $e) { $this->error($e->getMessage()); } } $this->error(__('Parameter %s can not be empty', '')); } return $this->view->fetch(); } /** * 编辑 */ public function edit($ids = NULL) { $row = $this->model->get($ids); if (!$row) $this->error(__('No Results were found')); $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { if (!in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__('You have no permission')); } } if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { /* * 已经弃用,如果为了兼容老版可取消注释 foreach ($params as $k => &$v) { $v = is_array($v) ? implode(',', $v) : $v; } */ try { //是否采用模型验证 if ($this->modelValidate) { $name = basename(str_replace('\\', '/', get_class($this->model))); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate; $row->validate($validate); } if(empty($params['group_name'])){ $this->error('用户集合名词不能为空'); } $group_type = $params['group_type']; if($group_type == 0){ //全部用户 $data = []; $data['group_name'] = $params['group_name']; $data['group_type'] = $params['group_type']; $data['edit_by'] = $this->auth->id; $result = $this->model->allowField(true)->update($data, ['id'=>$ids]); }elseif ($group_type == 1){ //自定义 $data = []; $data['group_name'] = $params['group_name']; $data['group_type'] = $params['group_type']; $user_ids = trim($this->arr_uni_str($params['user_ids'])); if (empty($user_ids)) { $this->error('用户ID不能为空'); } elseif (count(explode(',', $user_ids)) < 2) { $this->error('用户ID最少填写两个'); } $data['edit_by'] = $this->auth->id; $rst = $this->model->allowField(true)->update($data, ['id'=>$ids]); if ($rst) { $send_extend_data = [ 'user_ids' => $user_ids, 'edit_by' => $this->auth->id ]; $result = $this->adUserGroupExtendModel->where(['user_group_id' => $ids])->update($send_extend_data); }else{ $result = false; } }else{ //条件筛选 $data = []; $data['group_name'] = $params['group_name']; $data['group_type'] = $params['group_type']; unset($params['group_name']); unset($params['group_type']); unset($params['user_ids']); if(!isset($params['vipType'])){ $params['vipType'] = ''; } if(!isset($params['tailNum'])){ $params['tailNum'] = ''; } if(!isset($params['rechargeType'])){ $params['rechargeType'] = ''; } if(!isset($params['readType'])){ $params['readType'] = ''; } $params['read_book_categories'] = implode(',', array_unique(array_filter(explode(',', $params['read_book_categories'])))); // $this->filterFromParams($params); // 校验表单 $this->validateForm($params); if($params['userSex'] == -1){ $params['userSex'] = ''; } $user_json = json_encode($params); $data['user_json'] = $user_json; $data['edit_by'] = $this->auth->id; $result = $this->model->allowField(true)->update($data, ['id'=>$ids]); } if ($result !== false) { $this->success(); } else { $this->error($row->getError()); } } catch (\think\exception\PDOException $e) { $this->error($e->getMessage()); } } $this->error(__('Parameter %s can not be empty', '')); } if($row['group_type'] == 2){ $user_json = json_decode($row['user_json'], true); $this->restruct_params($user_json); }else{ $user_json = $this->initUserJson(); } if(!empty($user_json['read_book_categories'])){ $read_book_categories = model('BookCategory')->where('id', 'in', $user_json['read_book_categories'])->select(); }else{ $read_book_categories = []; } if($row['group_type'] === 1){ $extend_data = $this->adUserGroupExtendModel->where(['user_group_id' => $row['id']])->find(); $row['user_ids'] = $extend_data->user_ids ?? ''; } if(!isset($user_json['userSex']) || $user_json['userSex'] === ''){ $user_json['userSex'] = -1; } $this->view->assign('read_book_categories', $read_book_categories); $this->view->assign('user_json',$user_json); $this->view->assign("row", $row); return $this->view->fetch(); } /** * 删除 */ public function del($ids = "") { if ($ids) { $pk = $this->model->getPk(); $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { $count = $this->model->where($this->dataLimitField, 'in', $adminIds); } $list = $this->model->where($pk, 'in', $ids)->select(); $count = 0; foreach ($list as $k => $v) { $count += $v->where(['id'=>$v->id])->update(['is_del'=>1]); //$count += $v->delete(); } if ($count) { $this->success(); } else { $this->error(__('No rows were deleted')); } } $this->error(__('Parameter %s can not be empty', 'ids')); } /** * 详情页面 * @param $ids * @return string * @throws \think\Exception */ public function detail($ids) { $row = $this->model->get($ids); if ($row['group_type'] == 2) { $user_json = json_decode($row['user_json'], true); $this->restruct_params($user_json); } else { $user_json = $this->initUserJson(); } if(!empty($user_json['read_book_categories'])){ $read_book_categories = model('BookCategory')->where('id', 'in', $user_json['read_book_categories'])->select(); }else{ $read_book_categories = []; } if(!isset($user_json['userSex']) || $user_json['userSex'] === ''){ $user_json['userSex'] = -1; } if($row['group_type'] === 1){ $extend_data = $this->adUserGroupExtendModel->where(['user_group_id' => $row['id']])->find(); $row['user_ids'] = $extend_data->user_ids ?? ''; } $this->view->assign('read_book_categories', $read_book_categories); $this->view->assign('user_json',$user_json); $this->view->assign("row", $row); return $this->view->fetch(); } /** * 导入用户id */ public function idimport() { $file = $this->request->request('file'); if (!$file) { $this->error(__('Parameter %s can not be empty', 'file')); } $filePath = ROOT_PATH . DS . 'public' . DS . $file; if (!is_file($filePath)) { $this->error(__('No results were found')); } $PHPReader = new \PHPExcel_Reader_Excel2007(); if (!$PHPReader->canRead($filePath)) { $PHPReader = new \PHPExcel_Reader_Excel5(); if (!$PHPReader->canRead($filePath)) { $PHPReader = new \PHPExcel_Reader_CSV(); if (!$PHPReader->canRead($filePath)) { $this->error(__('Unknown data format')); } } } $fieldArr = ['id' =>'id']; $PHPExcel = $PHPReader->load($filePath); //加载文件 $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表 $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号 $allRow = $currentSheet->getHighestRow(); //取得一共有多少行 $maxColumnNumber = \PHPExcel_Cell::columnIndexFromString($allColumn); for ($currentRow = 1; $currentRow <= 1; $currentRow++) { for ($currentColumn = 0; $currentColumn < $maxColumnNumber; $currentColumn++) { $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue(); $fields[] = $val; } } $insert = []; for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) { $values = []; for ($currentColumn = 0; $currentColumn < $maxColumnNumber; $currentColumn++) { $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue(); $values[] = is_null($val) ? '' : $val; } $row = []; $temp = array_combine($fields, $values); foreach ($temp as $k => $v) { if (isset($fieldArr[$k]) && $k !== '') { $row[$fieldArr[$k]] = $v; } } if ($row) { $insert[] = $row; } } if (!$insert) { $this->error(__('上传文件有误,请核对数据')); } try { $str = implode(',', array_column($insert, 'id')); $this->success('Success', '', $str); } catch (\think\exception\PDOException $exception) { $this->error($exception->getMessage()); } } /** * 选择书籍分类 * @return string|\think\response\Json * @throws \think\Exception */ public function ajaxcategoryslect() { $this->model = model('BookCategory'); if ($this->request->isAjax()) { list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $total = $this->model ->where(['status' => 'normal']) ->where($where) ->count(); $list = $this->model ->where(['status' => 'normal']) ->where($where) ->limit($offset, $limit) ->select(); $result = array("total" => $total, "rows" => $list); return json($result); } return $this->view->fetch(); } /** * Ajax设置有效失效 */ public function ajaxValidate() { $id = $this->request->param("ids"); $data = $this->model->where('id', $id)->find(); $status = $data->status; $this->model->update(['status' => !$status], ['id' => $id]); $data = ['code' => 0, 'msg' => 'success']; echo json_encode($data); } /** * 批量生效 | 失效 */ public function validbatch() { $params = $this->request->param(); $ids = $params['ids']; $status = $params['status']; if (is_array($ids)) { foreach ($ids as $id) { $result = $this->model->update(['status' => $status], ['id' => $id]); } } else { $result = $this->model->update(['status' => $status], ['id' => $ids]); } if($result){ $this->success('设置成功'); }else{ $this->error('设置失败'); } } /** * 关联用户组 */ public function viewgroup() { //设置过滤方法 $this->request->filter(['strip_tags']); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('pkey_name')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $map = ['status' => 1, 'is_del' => 0, 'create_by' => $this->auth->id]; $total = $this->model ->where($where) ->where($map) ->order($sort, $order) ->count(); $list = $this->model ->where($where) ->where($map) ->order($sort, $order) ->limit($offset, $limit) ->select(); $result = array("total" => $total, "rows" => $list); return json($result); } return $this->view->fetch(); } // region 额外的方法 /** * 过滤字符串中重复的ID * @param $str 字符串 * @param string $delimiter 分隔符 */ private function arr_uni_str($str, $delimiter=',') { $str = str_replace(',', ',', $str); $arr = array_unique(explode($delimiter, $str)); // 过滤掉字符串 $arr = array_filter($arr, function ($v){ if(is_numeric($v)){ return true; }else{ return false; } }); return implode(",", $arr); } private function restruct_params(&$params) { if ($params) { foreach ($params as $key => $item) { if (is_array($params[$key])) { $params[$key] = $this->array_filter_group($item); } } } } private function array_filter_group(array $arr) { return array_filter($arr, function ($v) { if($v == ''){ return false; }else{ return true; } }); } public function validateForm(&$params) { // region 基础属性 if (in_array(0, $params['baseType']) && $params['baseType'][0] == '0') { if (empty($params['follow-etime'])) { $this->error('请完善关注时间'); }else{ if($params['follow-stime'] >= $params['follow-etime']){ $this->error('关注结束时间应大于关注开始时间'); } } } if (in_array(1, $params['baseType'])) { if (empty($params['interactive-etime'])) { $this->error('请完善互动时间'); } else { if ($params['interactive-stime'] >= $params['interactive-etime']) { $this->error('互动结束时间应大于互动开始时间'); } } } if (in_array(2, $params['baseType'])) { if ($params['vipType'] == '') { $this->error('请完善VIP用户'); } } if (in_array(3, $params['baseType'])) { if ($params['tailNum'] == '') { $this->error('请完善用户尾号'); } } // endregion 基础属性 // region 充值属性 if (in_array(0, $params['rechargeProperty']) && $params['rechargeProperty'][0] == '0') { if (empty($params['amount-e'])) { $this->error('请完善充值金额'); } else { if($params['amount-s'] >= $params['amount-e']){ $this->error('充值结束金额应大于充值开始金额'); } } } if (in_array(1, $params['rechargeProperty'])) { if (empty($params['recharge-times-e'])) { $this->error('请完善充值次数'); } else { if ($params['recharge-times-s'] >= $params['recharge-times-e']) { $this->error('充值结束次数应大于充值开始次数'); } } } if (in_array(2, $params['rechargeProperty'])) { if ($params['rechargeType'] == '') { $this->error('请完善充值用户'); } } if (in_array(3, $params['rechargeProperty'])) { if (empty($params['kandian-e'])) { $this->error('请完善账户余额'); } else { if ($params['kandian-s'] >= $params['kandian-e']) { $this->error('账户结束余额应大于账户开始余额'); } } } if (in_array(4, $params['rechargeProperty'])) { if (empty($params['giftkandian-e'])) { $this->error('请完善赠送书币金额'); } else { if ($params['giftkandian-s'] >= $params['giftkandian-e']) { $this->error('赠送书币结束金额应大于赠送书币开始金额'); } } } // endregion 充值属性 // region 阅读属性 if (in_array(0, $params['readProperty']) && $params['readProperty'][0] == '0') { if ($params['readType'] === '' || empty($params['readnum-e'])) { $this->error('请完善阅读章节数属性'); } else { if ($params['readnum-s'] >= $params['readnum-e']) { $this->error('阅读结束章节数应大于阅读开始章节数'); } } } if (in_array(1, $params['readProperty'])) { if ($params['book_id'] == '') { $this->error('请完善阅读记录属性'); } } if (in_array(2, $params['readProperty'])) { if (empty($params['read_book_categories'])) { $this->error('请完善阅读书籍分类'); } } // endregion 阅读属性 // region 消费属性 if (in_array(0, $params['consumerProperty']) && $params['consumerProperty'][0] == '0') { if (empty($params['spendkandian-e'])) { $this->error('请完善消费赠送书币数量'); } else { if ($params['spendkandian-s'] >= $params['spendkandian-e']) { $this->error('消费赠送书币结束数额应大于消费赠送书币开始数额'); } } } if (in_array(1, $params['consumerProperty'])) { if (empty($params['spendrecharge-e'])) { $this->error('请完善消费充值金额'); } else { if ($params['spendrecharge-s'] >= $params['spendrecharge-e']) { $this->error('消费充值结束金额应大于消费充值开始金额'); } } } // endregion 消费属性 } public function initUserJson(){ $user_json = [ 'baseType' => [], 'readRecordType' => [], 'read_book_categories' => '', 'vipType' => '', 'tailNum' => '', 'readType' => '', 'rechargeType' => '', 'follow-stime' => 0, 'follow-etime' => 0, 'interactive-stime' => 0, 'interactive-etime' => 0, 'amount-s' => 0, 'amount-e' => 0, 'recharge-times-s' => 0, 'recharge-times-e' => 0, 'giftkandian-s' => 0, 'giftkandian-e' => 0, 'kandian-s' => 0, 'kandian-e' => 0, 'readnum-s' => 0, 'readnum-e' => 0, 'book_id' => '', 'spendkandian-s' => 0, 'spendkandian-e' => 0, 'spendrecharge-s' => 0, 'spendrecharge-e' => 0, 'userCate' => 0, 'userSex' => '', 'consumerProperty' => [], 'rechargeProperty' =>[], 'readProperty' => [], ]; return $user_json; } public function filterFromParams(&$params) { if (!in_array(0, $params['baseType'])) { $params['follow-stime'] = ''; $params['follow-etime'] = ''; }else{ if(trim($params['follow-stime']) == '' && !empty($params['follow-etime'])){ $params['follow-stime'] = 0; } } if (!in_array(1, $params['baseType'])) { $params['interactive-stime'] = ''; $params['interactive-etime'] = ''; }else{ if(trim($params['interactive-stime']) == '' && !empty($params['interactive-etime'])){ $params['interactive-stime'] = 0; } } if (!in_array(2, $params['baseType'])) { $params['vipType'] = ''; } if (!in_array(3, $params['baseType'])) { $params['tailNum'] = ''; } // rechargeProperty if (!in_array(0, $params['rechargeProperty'])) { $params['amount-s'] = ''; $params['amount-e'] = ''; }else{ if(trim($params['amount-s']) == '' && !empty($params['amount-e'])){ $params['amount-s'] = 0; } } if (!in_array(1, $params['rechargeProperty'])) { $params['recharge-times-s'] = ''; $params['recharge-times-e'] = ''; }else{ if(trim($params['recharge-times-s']) == '' && !empty($params['recharge-times-e'])){ $params['recharge-times-s'] = 0; } } if (!in_array(2, $params['rechargeProperty'])) { $params['rechargeType'] = ''; } if (!in_array(3, $params['rechargeProperty'])) { $params['kandian-s'] = ''; $params['kandian-e'] = ''; }else{ if(trim($params['kandian-s']) == '' && !empty($params['kandian-e'])){ $params['kandian-s'] = 0; } } if (!in_array(4, $params['rechargeProperty'])) { $params['giftkandian-s'] = ''; $params['giftkandian-e'] = ''; }else{ if(trim($params['giftkandian-s']) == '' && !empty($params['giftkandian-e'])){ $params['giftkandian-s'] = 0; } } // readProperty if (!in_array(0, $params['readProperty'])) { $params['readType'] = ''; $params['readnum-s'] = ''; $params['readnum-e'] = ''; } if (!in_array(1, $params['readProperty'])) { $params['readRecordType'] = ''; $params['book_id'] = ''; } if (!in_array(2, $params['readProperty'])) { $params['read_book_categories'] = ''; } // consumerProperty if (!in_array(0, $params['consumerProperty'])) { $params['spendkandian-s'] = ''; $params['spendkandian-e'] = ''; }else{ if(trim($params['spendkandian-s']) == '' && !empty($params['spendkandian-e'])){ $params['spendkandian-s'] = 0; } } if (!in_array(1, $params['consumerProperty'])) { $params['spendrecharge-s'] = ''; $params['spendrecharge-e'] = ''; }else{ if(trim($params['spendrecharge-s']) == '' && !empty($params['spendrecharge-e'])){ $params['spendrecharge-s'] = 0; } } /*if (!in_array(2, $params['consumerProperty'])) { $params['consumer_book_categories'] = ''; }*/ } // endregion 额外的方法 }