model = model('CustomMediaPush'); $this->adminConfigModel = model('AdminConfig'); $this->subscriptionModel = model('Subscription'); } public function index() { //设置过滤方法 $this->request->filter(['strip_tags']); $status = $this->request->param('status') ?? 0; if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('pkey_name')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $filter = $this->request->get("filter", ''); $filter = json_decode($filter, true); $where = []; $subId = $adminId = false; if ($filter) { foreach ($filter as $k => $v) { switch ($k) { case 'sendtime': $v = str_replace(' - ', ',', $v); $arr = array_slice(explode(',', $v), 0, 2); if (stripos($v, ',') === false || !array_filter($arr)) { continue; } $arr[0] = strtotime($arr[0]); $arr[1] = strtotime($arr[1]); $where[$k] = ['BETWEEN', "{$arr[0]},{$arr[1]}"]; break; case 'official_account_type': $where[$k] = $v; break; case 'wechat_name': if (isset($filter['official_account_type'])) { if ($filter['official_account_type'] == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SERVICE) { $adminId = $this->adminConfigModel->getAdminIdByOfficialName($v); } elseif ($filter['official_account_type'] == OfficialAccount::OFFICIAL_ACCOUNT_TYPE_SUBSCRIPTION) { $subId = $this->subscriptionModel->getIdByOfficialName($v); } } else { $adminId = $this->adminConfigModel->getAdminIdByOfficialName($v); $subId = $this->subscriptionModel->getIdByOfficialName($v); } break; case 'id': $where[$k] = $v; break; case 'message_text$[*].book_name': $where[$k] = ['like', "%$v%"]; break; default: break; } } } $officialConditions = []; if ($adminId) { $officialConditions[] = "find_in_set($adminId,official_account_service_ids)"; } if ($subId) { $officialConditions[] = "find_in_set($subId,official_account_subscribe_ids)"; } $officialCondition = implode(' OR ', $officialConditions); $countFetchObj = $this->_buildPushObj($where, $officialCondition); $total = $countFetchObj ->count(); $listFetchObj = $this->_buildPushObj($where, $officialCondition); $list = $listFetchObj ->order($sort, $order) ->limit($offset, $limit) ->select(); $result = array("total" => $total, "rows" => $list); return json($result); } $this->assign('status', $status); $this->assignconfig('status', $status); return $this->view->fetch(); } /** * @param string $ids custom_media_push ID * @param string $idx 数组下标 */ public function getMediaPushMessage($ids = '', $idx = '') { $media = $this->model->get(['id' => $ids]); $message_json_arr = json_decode($media->message_text); $data = json_encode($message_json_arr[$idx]); if ($media->message_text) { return json(['code'=> 0, 'msg'=>'success', 'data'=> $data]); } else { return json(['code'=> 1, 'msg'=>'fail', 'data'=> '']); } } /** * 待发送文字客服消息,删除单条 * @param string $ids custom_media_push ID * @param string $idx 数组下标 */ public function delSinglePushPiece($ids = '', $idx = 0) { if ($ids) { $media = $this->model->get(['id' => $ids]); $media_arr = json_decode($media->message_text, true); unset($media_arr[$idx]); $media_arr = array_values($media_arr); # 替换值 $rst = $this->model->save(['message_text' => json_encode($media_arr)], ['id' => $ids]); if ($rst) { return json(['code' => 0, 'msg' => '删除成功']); } else { return json(['code' => 1, 'msg' => '删除失敗']); } } $this->error(__('Parameter %s can not be empty', 'ids')); } /** * 删除 客服消息素材推送记录 * @param string $ids * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function del($ids = "") { if ($ids) { $customService = new CustomService(); $result = $customService->delCustomMediaPush($ids); if ($result) { $this->success(); }else { $this->error(__('No rows were deleted')); } } $this->error(__('Parameter %s can not be empty', 'ids')); } /** * 查看详情 * @param string $ids * @return \think\response\Json * @throws \think\Exception * @throws \think\exception\DbException */ public function viewDetail($ids = '') { if ($ids) { $obj_arr = $this->model->get(['id' => $ids]) ->toArray(); if($this->request->isAjax()) { $json_data = CustomService::instance()->getCustomMediaPushDetail($ids, Message::MESSAGE_TYPE_LINK); return json($json_data); } $obj_arr['message_text'] = json_decode($obj_arr['message_text'], true); $obj_arr['user_json'] = json_decode($obj_arr['user_json'], true); $this->assign('bookcategorylist', BookCategory::getBookCategoryList()); $this->assign('obj', $obj_arr); $this->assign('officialSuccess', CustomService::instance()->getOfficialPushStatusList($obj_arr['official_account_type'], $ids, Custom::CUSTOM_STATUE_HIDDEN)); $this->assign('officialFail', CustomService::instance()->getOfficialPushStatusList($obj_arr['official_account_type'], $ids, Custom::CUSTOM_STATUE_FAIL)); return $this->fetch(); } $this->error(__('Parameter %s can not be empty', 'ids')); } private function _buildPushObj($where, $officialCondition) { $status = $this->request->param('status') ?? Custom::CUSTOM_MEDIA_PUSH_STATUS_PENDING; $channelId = $this->auth->id; $condition = [ 'created_admin_id' => $channelId, 'message_type' => Message::MESSAGE_TYPE_LINK, 'created_from' => Custom::CUSTOM_CREATED_FROM_GROUP_SEND, 'status' => $status, ]; $obj = $this->model ->where($where) ->where($condition); if (!empty($officialCondition)) { $obj->where($officialCondition); } return $obj; } }