model = model('SubscribeDelayPush'); $this->view->assign("statusList", $this->model->getStatusList()); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法 * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 查看 */ public function index() { $adminConfig = model("AdminConfig")->where('admin_id', 'eq', $this->auth->id)->find(); $data = $this->model->where('status', 'normal')->where('admin_id', $this->auth->id)->select(); usort($data, function($v1, $v2){ if ($v1['type'] == '1') { $v1Delay = $v1['delay']; } else { $v1Delay = $v1['delay'] * 60; } if ($v2['type'] == '1') { $v2Delay = $v2['delay']; } else { $v2Delay = $v2['delay'] * 60; } if ($v1Delay > $v2Delay) { return 1; } }); $return = []; $miniOpen = MiniProgramService::instance()->miniWrite($this->auth->id,$this->group); foreach ($data as $index=>$item) { $tmp_data = json_decode($item['news_set'], true); $tmp_data['id'] = $item['id']; $tmp_data['content_type'] = $item['content_type']??1; $tmp_data['mini_open'] = $miniOpen; $tmp_data['text_content'] = $tmp_data['text_content'] ?? []; $tmp_data['is_sign'] = (int)($tmp_data['is_sign'] ?? 1); $tmp_data['sex'] = (int)($tmp_data['sex'] ?? 0); $tmp_data['mobile_system'] = $tmp_data['mobile_system']??0; $tmp_data['user_type'] = $tmp_data['user_type']??1; //小程序默认类型 $tmp_data['mini_type'] = (!isset($tmp_data['mini_type']) || $tmp_data['mini_type'] < 3) ? 3 : $tmp_data['mini_type']; $return[] = $tmp_data; } if ($this->request->isPost()) { return json([ 'data' => $return, 'switch' => $adminConfig['subscribe_delay'], ]); } $this->assignconfig('mini_open', $miniOpen); $this->assignconfig('data', $return); $this->assignconfig('switch', $adminConfig['subscribe_delay']); return $this->view->fetch(); } /** * 开关 */ public function operate() { $state = $this->request->param('state'); $subscribeDelay = '1'; if ($state == 'normal') { $subscribeDelay = '2'; } if (model("AdminConfig")->update(['subscribe_delay' => $subscribeDelay], ['admin_id' => $this->auth->id])) { //需要删除一下缓存 model("AdminConfig")->delAdminInfoAllCache($this->auth->id); $result = [ 'code' => '200', 'msg' => '操作成功', ]; } else { $result = [ 'code' => '202', 'msg' => '操作失败', ]; } //清除缓存 $this->model->delCache($this->auth->id); exit(json_encode($result, JSON_UNESCAPED_UNICODE)); } /** * 保存 */ public function add() { if ($this->request->isPost()) { $params = $this->request->post('data'); $params = json_decode($params,true); if ($params) { if ($this->dataLimit) { $params[$this->dataLimitField] = $this->auth->id; } try { $params['url_type'] = (int)($params['url_type'] ?? 1); $params['user_type'] = (int)($params['user_type'] ?? 1); $params['mobile_system'] = (int)($params['mobile_system'] ?? 0); $params['content_type'] = intval($params['content_type']??1);//1是图片 2是文本 $params['mini_type'] = intval($params['mini_type']??1);//小程序类型 $content = [ 'title' => $params['title'], 'image' => $params['image'], 'url_type' => $params['url_type'], 'description' => $params['description'], 'user_type' => $params['user_type'], 'mobile_system' => $params['mobile_system'], 'sex' =>$params['sex'], 'mini_type' =>$params['mini_type'] ?? 0, ]; if ($params['content_type'] == 2){ //文本类型 if (empty($params['text_content'])){ $this->error('文本消息不能为空'); } $content['description'] = SubscribeVipDelayService::instance()->getContent($params,$this->getCurrentAccountChannelId()); }else{ switch ($params['url_type']) { case '1': $content['url'] = $params['referral_url']??''; break; case '2'://活动链接 $content['url'] = $params['activity_url']??''; break; case '3'://继续阅读 $content['url'] = getCurrentDomain($this->getCurrentAccountChannelId(), '/index/book/chapter'); break; case '4'://自定义链接 $content['url'] = $params['self_url']; break; case '5'://自动签到 $content['url'] = "weixin://bizmsgmenu?msgmenucontent=" . Message::MESSAGE_KEYWORD . "&msgmenuid={{" . time() . "}}"; break; case '6'://小程序 $content['mini_content'] = MiniProgramService::instance()->getMiniContent($this->auth->id,$params); break; } } $content = json_encode($content, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE); $content = UrlService::instance()->replaceReferralHost($this->getCurrentAccountChannelId(), $content)->data; if (!empty($params['id'])) { $row = $this->model->get($params['id']); $id = $params['id']; } else { $where = [ 'admin_id' => $this->auth->id, 'type' => $params['unit'], 'delay' => $params['current_time'], ]; $row = $this->model->where($where)->find(); $id = $row['id']; } if ($row) { if (!$row) { $this->error('客服消息不存在'); } // $params['text_content'] = json_encode($params['text_content'], JSON_UNESCAPED_UNICODE); if (md5($content) != md5($row['content'])) { $data = [ 'type' => $params['unit'], 'delay' => $params['current_time'], 'content' => $content, 'content_type'=>$params['content_type'], 'news_set' => json_encode($params, JSON_UNESCAPED_UNICODE), 'status' => 'normal', 'updatetime' => time() ]; $result = $row->allowField(true)->save($data); if ($result !== false) { //需要清理缓存 $this->model->delCache($this->auth->id); $this->success('', '', ['id' => $id]); } else { $this->error($row->getError()); } } else { //无改动 $this->success('', '', ['id' => $id]); } } else { //新增 $data = [ 'admin_id' => $this->auth->id, 'type' => $params['unit'], 'delay' => $params['current_time'], 'content' => $content, 'content_type'=>$params['content_type'], 'news_set' => json_encode($params, JSON_UNESCAPED_UNICODE), 'status' => 'normal', 'updatetime' => time(), 'createtime' => time(), ]; $id = $this->model->allowField(true)->insertGetId($data); if ($id !== false) { //需要清理一下缓存 $this->model->delCache($this->auth->id); $this->success('', '', ['id' => $id]); } else { $this->error($this->model->getError()); } } } catch (\think\exception\PDOException $e) { $this->error($e->getMessage()); } } $this->error(__('Parameter %s can not be empty', '')); } } /** * 删除 */ public function delete($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->save(['status' => 'hidden', 'updatetime' => time()]); } //清理缓存 $this->model->delCache($this->auth->id); if ($count) { $this->success(); } else { $this->error(__('No rows were deleted')); } } $this->error(__('Parameter %s can not be empty', 'ids')); } public function getMiniPage() { $pageStr = config('site.mini_page'); if (!$pageStr){ return []; } $pageArr = explode(PHP_EOL,$pageStr); foreach ($pageArr as $k=>$v){ $temp = explode('@',$v); $miniPages[] = array( 'name'=>$temp[0], 'page'=>$temp[1] ); } return json($miniPages); } }