model = model('ExportFans'); $this->view->assign('type', $this->request->param('type', 1)); $this->assignconfig('type', $this->request->param('type', 1)); $this->view->assign("statusList", $this->model->getStatusList()); $this->view->assign("payStatusList", $this->model->getPayStatusList()); } /** * 默认生成的控制器所继承的父类中有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(); $where_u = [ 'type' => $this->request->get('type', 0) ]; $total = $this->model ->where($where) ->where($where_u) ->order($sort, $order) ->count(); $list = $this->model ->where($where) ->where($where_u) ->order($sort, $order) ->limit($offset, $limit) ->select(); $result = array("total" => $total, "rows" => $list); return json($result); } return $this->view->fetch(); } public function upload($ids) { if ($this->request->isPost()) { $filePath = ROOT_PATH . DS . 'public' . DS . $this->request->param('file'); $list = file($filePath); $list = array_map(function ($v) { $v = trim($v); return $v; }, $list); $config = $this->model->field("limittime, type, starttime")->where('id', $ids)->find(); $limittime = $config['limittime']; $starttime = $config['starttime']; $type = $config['type']; $count = 0; if ($type == '1') { //二期设置用户写入 $expire_time = $limittime; foreach ($list as $id) { $key = CacheConstants::getLeadUser($id); $hashKey = 'id_'.$ids; $list = []; if ($data = Redis::instance()->hGetAll($key)) { //判断过期时间 foreach ($data as $k => $row) { $item = json_decode($row, true); if ($item['expire_time'] <= time()) { //删除 continue; } $expire_time = $expire_time < $item['expire_time'] ? $item['expire_time'] : $expire_time; $list[$k] = $row; } } //追加 $list[$hashKey] = json_encode(['id' => $ids, 'expire_time' => $limittime, 'start_time'=>$starttime]); Redis::instance()->hMSet($key, $list); Redis::instance()->expire($key, $expire_time - time()); $count++; } } else { foreach ($list as $id) { $key = CacheConstants::getExportFansUserCache($id); if ($exists = Redis::instance()->hGetAll($key)) { if ($exists['id'] > $ids) { continue; } } Redis::instance()->hMSet($key, ['id' => $ids, 'chapter' => '', 'expire' => $limittime]); Redis::instance()->expire($key, $limittime - time()); $count++; } } if ($count) { $this->success('上传成功用户数' . $count); } else { $this->error(); } } 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); } $result = $row->allowField(true)->save($params); if ($result !== false) { $cache = CacheConstants::getExportFansRow($ids); Redis::instance()->del($cache); $this->success(); } else { $this->error($row->getError()); } } catch (\think\exception\PDOException $e) { $this->error($e->getMessage()); } } $this->error(__('Parameter %s can not be empty', '')); } $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) { $cache = CacheConstants::getExportFansRow($ids); Redis::instance()->del($cache); $count += $v->delete(); } if ($count) { $this->success(); } else { $this->error(__('No rows were deleted')); } } $this->error(__('Parameter %s can not be empty', 'ids')); } }