public function actionIndex() { $conversationId = $this->getQuery('conversationId'); if (empty($conversationId)) { throw new BadRequestHttpException("Missing conversationId"); } $query = ChatMessage::find(); if ($orderBy = $this->getQuery('orderBy', 'createdAt')) { if (StringUtil::isJson($orderBy)) { $orderBy = Json::decode($orderBy, true); foreach ($orderBy as $key => $value) { if ($value === 'asc' || $value === 'ASC') { $orderBy[$key] = SORT_ASC; } else { $orderBy[$key] = SORT_DESC; } } } else { $orderBy = [$orderBy => SORT_DESC]; } $query->orderBy($orderBy); } $query->where(['isDeleted' => false, 'conversationId' => new \MongoId($this->getQuery('conversationId'))]); return new ActiveDataProvider(['query' => $query]); }
public function actionIndex() { $accountId = $this->getAccountId(); $currentHelpdeskId = $this->getUserId(); $query = HelpDesk::find(); $clientOpenId = $this->getQuery('clientOpenId'); if ($orderBy = $this->getQuery('orderBy')) { if (StringUtil::isJson($orderBy)) { $orderBy = Json::decode($orderBy, true); foreach ($orderBy as $key => $value) { if ($value === 'asc' || $value === 'ASC') { $orderBy[$key] = SORT_ASC; } else { $orderBy[$key] = SORT_DESC; } } } else { $orderBy = [$orderBy => SORT_DESC]; } $query->orderBy($orderBy); } $allHelpdesks = $query->where(['accountId' => $accountId, 'isDeleted' => false, 'isActivated' => true, 'isEnabled' => true])->andWhere(['not in', '_id', [$currentHelpdeskId]])->orderBy(['clientCount' => SORT_ASC])->all(); $result = []; if ($allHelpdesks) { foreach ($allHelpdesks as $helpdesk) { array_push($result, $helpdesk->toArray()); } } $lastChatConversation = ChatConversation::getLastChatByClient($clientOpenId, $currentHelpdeskId, $accountId); if ($lastChatConversation) { $lastServeHelpdeskId = (string) $lastChatConversation->desk['id']; if (!empty($result)) { $allOnlineHelpdesks = []; foreach ($result as $index => $item) { if ($item['isOnline'] && $item['id'] === $lastServeHelpdeskId) { $item['isLastChat'] = true; array_unshift($allOnlineHelpdesks, $item); } else { if ($item['isOnline']) { array_push($allOnlineHelpdesks, $item); } } } $result = $allOnlineHelpdesks; } } return $result; }
/** * Request the service with basic auth * @param string $url requested url * @param string $method requested method * @param string $params requested parameters */ private function _requestService($url, $method, $params = NULL) { //format header and params for post and put if (in_array($method, [self::METHOD_POST, self::METHOD_PUT])) { $method = $method . 'Json'; $params = Json::encode($params); } $resultJson = Yii::$app->curl->{$method}($url, $params); $logUrl = strtoupper($method) . ' ' . $url; $logTarget = 'webhook'; LogUtil::info(['url' => $logUrl, 'response' => $resultJson, 'params' => $params], $logTarget); if (StringUtil::isJson($resultJson)) { return Json::decode($resultJson, true); } else { LogUtil::error(['url' => $logUrl, 'response' => $resultJson, 'params' => $params], $logTarget); } }
/** * Push open message by target * @param Array $taget * @param string $message * @throws ApiDataException */ public function pushMessage($taget, $badge, $extra, $message) { $url = $this->domain . '/v2/open/messages'; $content['ttl'] = \Yii::$app->params['PUSH_MESSAGE_MAX_SAVE_TIME']; $content['extra'] = $extra; if ($message !== null) { $content['alert'] = $message; $content['apns'] = ['badge' => $badge, 'sound' => 'default', 'content-available' => 1]; } else { $content['apns'] = ['content-available' => 1]; } $params = ['content' => $content, 'target' => ['tokens' => $taget], 'trigger' => ['now' => true]]; $resultJson = $this->requestService($url, self::METHOD_POST, $params, true); LogUtil::info(['url' => $url, 'params' => $params, 'result' => $resultJson], 'helpdesk'); if (StringUtil::isJson($resultJson)) { $result = Json::decode($resultJson, true); } else { throw new ApiDataException($url, $resultJson, $params, 'helpdesk'); } if (empty($result['nid'])) { throw new ApiDataException($url, $result, $params, 'helpdesk'); } }
/** * Prepares the data provider that should return the requested collection of the models. * @return ActiveDataProvider */ protected function prepareDataProvider() { if ($this->prepareDataProvider !== null) { return call_user_func($this->prepareDataProvider, $this); } /* @var $modelClass \yii\db\BaseActiveRecord */ $modelClass = $this->modelClass; $query = $modelClass::find(); $token = $this->getAccessToken(); $accountId = $this->getAccountId(); $whereCondition = ['accountId' => $accountId]; if (!new $modelClass() instanceof PlainModel) { $whereCondition['isDeleted'] = $modelClass::NOT_DELETED; } if ($orderBy = Yii::$app->request->get('orderBy', 'createdAt')) { if (StringUtil::isJson($orderBy)) { $orderBy = Json::decode($orderBy, true); foreach ($orderBy as $key => $value) { if ($value === 'asc' || $value === 'ASC') { $orderBy[$key] = SORT_ASC; } else { $orderBy[$key] = SORT_DESC; } } } else { $orderBy = [$orderBy => SORT_DESC]; } $query->orderBy($orderBy); } if ($where = Yii::$app->request->get('where')) { $keys = ['lt', 'lte', 'gt', 'gte', 'ne', 'in', 'nin', 'all']; $where = Json::decode($where, true); $newWhere = []; //set a default method to get whitch value in a array need to conver to mongoId $conver2MongoIds = []; if (method_exists($modelClass, 'conver2MongoId')) { $conver2MongoIds = $modelClass::conver2MongoId(); } foreach ($where as $index => $condition) { if ($index == '_id' || in_array($index, $conver2MongoIds)) { $condition = $this->ensureMongoId($condition); } if (is_array($condition)) { $newCondition = []; foreach ($condition as $key => $value) { if (in_array($key, $keys)) { $key = '$' . $key; } $newCondition[$key] = $value; } $condition = $newCondition; } $newWhere[$index] = $condition; } $whereCondition = array_merge($whereCondition, $newWhere); } if ($search = Yii::$app->request->get('search')) { $search = Json::decode($search, true); foreach ($search as $key => $value) { $value = trim($value); $value = StringUtil::regStrFormat($value); $where[$key] = new \MongoRegex("/{$value}/i"); } $whereCondition = array_merge($whereCondition, $where); } $query->where($whereCondition); $unlimited = Yii::$app->request->get('unlimited', false); if ($unlimited) { return ['items' => $query->all()]; } return new ActiveDataProvider(['query' => $query]); }
/** * Normalize formate orderBy * @param array $params * $params['orderBy'] can be specified in either a string (e.g. {"id":"asc"}, {"name":"desc"}) or an array * (e.g. `['id' => SORT_ASC, 'name' => SORT_DESC]`). * @return array * if $params['orderBy'] is empty, return ['createdAt' => SORT_DESC] */ public static function normalizeOrderBy($params) { if (!empty($params['orderBy'])) { $orderBy = $params['orderBy']; if (StringUtil::isJson($orderBy)) { $orderBy = Json::decode($orderBy, true); foreach ($orderBy as $key => $value) { if ($value === 'asc' || $value === 'ASC') { $orderBy[$key] = SORT_ASC; } else { $orderBy[$key] = SORT_DESC; } } } else { $orderBy = [$orderBy => SORT_DESC]; } } else { $orderBy = ['createdAt' => SORT_DESC]; } return $orderBy; }
/** * Query mass message statistics * * <b>Request Type</b>: GET<br/><br/> * <b>Request Endpoint</b>:http://{server-domain}/api/dashboard/mass-messages<br/><br/> * <b>Response Content-type</b>: application/json<br/><br/> * <b>Summary</b>: This api is used for querying mass message statistics * <br/><br/> * * <b>Request Params</b>:<br/> * channelId: string, id of the channel<br/> * startDate: string * endDate: string * title: string * orderby: String, {"sentDate":"DESC"}, sentDate, sentUser, intPageReadUser,oriPageReadUser,shareUser,addToFavUser * per-page: int, required<br/> * page: int, required<br/> * <br/><br/> * * <b>Response Params:</b><br/> * items: array, json array to queried channels detail information<br/> * _meta: array, the pagination information. * <br/><br/> * * <br/><br/> * * <b>Response Example</b>:<br/> * <pre> * { * "items": [ * { * "accountId": "548116bb978ca81ec68d593b", * "massSendId": "548116bb978ca81ec68d593b", * "sentDate": "2014-12-29", * "sentUser": 100, * "title": "12月27日 DiLi日报", * "refDate": 1419857357482, * "intPageReadUser": 23676, * "intPageReadCount": 25615, * "oriPageReadUser": 29, * "oriPageReadCount": 34, * "shareUser": 122, * "shareCount": 994, * "addToFavUser": 1, * "addToFavCount": 3, * "dailyStatistics": [ * { * "refDate": "2014-12-29", * "intPageReadUser": 23676, * "intPageReadCount": 25615, * "oriPageReadUser": 29, * "oriPageReadCount": 34, * "shareUser": 122, * "shareCount": 994, * "addToFavUser": 1, * "addToFavCount": 3 * }, * { * "refDate": "2014-12-29", * "intPageReadUser": 23676, * "intPageReadCount": 25615, * "oriPageReadUser": 29, * "oriPageReadCount": 34, * "shareUser": 122, * "shareCount": 994, * "addToFavUser": 1, * "addToFavCount": 3 * }, * { * "refDate": "2014-12-29", * "intPageReadUser": 23676, * "intPageReadCount": 25615, * "oriPageReadUser": 29, * "oriPageReadCount": 34, * "shareUser": 122, * "shareCount": 994, * "addToFavUser": 1, * "addToFavCount": 3 * }, * { * "refDate": "2014-12-29", * "intPageReadUser": 23676, * "intPageReadCount": 25615, * "oriPageReadUser": 29, * "oriPageReadCount": 34, * "shareUser": 122, * "shareCount": 994, * "addToFavUser": 1, * "addToFavCount": 3 * }, * { * "refDate": "2014-12-29", * "intPageReadUser": 23676, * "intPageReadCount": 25615, * "oriPageReadUser": 29, * "oriPageReadCount": 34, * "shareUser": 122, * "shareCount": 994, * "addToFavUser": 1, * "addToFavCount": 3 * }, * { * "refDate": "2014-12-29", * "intPageReadUser": 23676, * "intPageReadCount": 25615, * "oriPageReadUser": 29, * "oriPageReadCount": 34, * "shareUser": 122, * "shareCount": 994, * "addToFavUser": 1, * "addToFavCount": 3 * }, * { * "refDate": "2014-12-29", * "intPageReadUser": 23676, * "intPageReadCount": 25615, * "oriPageReadUser": 29, * "oriPageReadCount": 34, * "shareUser": 122, * "shareCount": 994, * "addToFavUser": 1, * "addToFavCount": 3 * } * ] * } * ], * "_meta": { * "totalCount": 1, * "pageCount": 1, * "currentPage": 1, * "perPage": 5 * } * } * </pre> */ public function actionIndex() { $query = $this->getQuery(); $accountId = $this->getQuery('channelId'); if (!$accountId) { throw new BadRequestHttpException('Missing channel id'); } if (isset($query['orderby'])) { $orderBy = $query['orderby']; unset($query['orderby']); if (StringUtil::isJson($orderBy)) { $orderBy = Json::decode($orderBy, true); foreach ($orderBy as $key => $value) { if ($value === 'asc') { $query['orderBy'] = $key; $query['ordering'] = 'ASC'; } else { $query['orderBy'] = $key; $query['ordering'] = 'DESC'; } } } else { $query['orderBy'] = $orderBy; $query['ordering'] = 'DESC'; } } $query['pageSize'] = isset($query['per-page']) ? $query['per-page'] : 5; $query['pageNum'] = isset($query['page']) ? $query['page'] : 1; unset($query['per-page'], $query['page'], $query['channelId']); $raw = Yii::$app->weConnect->searchMassArticlesStatistics($accountId, $query); if (array_key_exists('results', $raw)) { return ['items' => $raw['results'] ? $raw['results'] : [], '_meta' => ['totalCount' => $raw['totalAmount'], 'pageCount' => ceil($raw['totalAmount'] / $raw['pageSize']), 'currentPage' => $raw['pageNum'], 'perPage' => $raw['pageSize']]]; } else { throw new ServerErrorHttpException('Query mass message statistics fail'); } }
/** * Get a short url statistics * @param string $shortUrl a short url or key * @return array|null the short url statistics object * * * If use $shortUrl like the following: * * ~~~ * http://gourl.im/i/BhTR?from=20150101&to=20150115 * ~~~ * * or * * ~~~ * BhTR?from=20150101&to=20150115 * ~~~ * * The short url statistics object should be like the following: * * ~~~ * { * "Clicks": 2, * "Daily": [ * { * "Key": "BhTq", * "Day": "20150117", * "TotalClicks": 2, * "Hours": { * "1": 0, * "2": 0, * "3": 0, * "4": 0, * "5": 0, * "6": 0, * "7": 0, * "8": 0, * "9": 0, * "10": 0, * "11": 0, * "12": 0, * "13": 0, * "14": 0, * "15": 0, * "16": 2, * "17": 0, * "18": 0, * "19": 0, * "20": 0, * "21": 0, * "22": 0, * "23": 0, * "24": 0 * } * } * ], * "Key": "BhTq", * "Long": "http://staging.cp.augmarketing.cn/site/login", * "Short": "http://u.augmarketing.cn/BhTq" * } * ~~~ * * If use $shortUrl without form and to params like the following: * * ~~~ * http://gourl.im/i/BhTR * ~~~ * * or * * ~~~ * BhTR * ~~~ * * The short url statistics object should be like the following: * * ~~~ * { * "Clicks": 2, * "Key": "BhTq", * "Long": "http://staging.cp.augmarketing.cn/site/login", * "Short": "http://u.augmarketing.cn/BhTq" * } * ~~~ * */ public function statistics($shortUrl) { if (!$this->isUrl($shortUrl)) { $shortUrl = $this->shortenKey2Url($shortUrl); } $shortUrl = str_replace($this->shortUrlDomain, $this->statisticsUrl, $shortUrl); $statisticsStr = Yii::$app->curl->get($shortUrl); $statisticsObj = StringUtil::isJson($statisticsStr) ? Json::decode($statisticsStr, true) : null; return $statisticsObj; }
/** * filter json msg * @param $data,array */ private static function _filterJsonMsg($data) { $msg = ''; foreach (self::$filterStrings as $filterString) { if (preg_match($filterString, $data)) { self::$openLog = false; return ''; } } $data = Json::decode($data, true); if (isset($data['code'])) { //api json log if (empty($data['response'])) { //response is not json type $msg = self::_filterHtml($data['response']); } else { //operate by based on the code of response //check the response whether is json or array if (StringUtil::isJson($data['response'])) { $data['response'] = Json::decode($data['response'], true); } $msg = self::_getApiMsg($data['response']); } } else { //local json log or fronted $msg = 'info level'; if (!empty($data['msg'])) { $msg = $data['msg']; } else { if (!empty($data['message'])) { $msg = $data['message']; } else { if (!empty($data['response'])) { $response = $data['response']; $msg = empty($response['message']) ? $data['response'] : $response['message']; } } } } return $msg; }
private function _curl($method, $url, $logTarget, $params = [], $returnResultData = true, $exception = null) { if (empty($this->weconnectDomain)) { return []; } //format header and params for post and put if (in_array($method, [self::METHOD_POST, self::METHOD_PUT])) { $method = $method . 'Json'; $params = $this->_a2j($params); } $resultJson = Yii::$app->curl->{$method}($url, $params); $logUrl = strtoupper($method) . ' ' . $url; if (StringUtil::isJson($resultJson)) { $result = Json::decode($resultJson, true); } else { throw new ApiDataException($logUrl, $resultJson, $params, $logTarget); } LogUtil::info(['url' => $logUrl, 'response' => $resultJson, 'params' => $params], $logTarget); if ($result && isset($result['code']) && 412 == $result['code'] && !empty($exception)) { LogUtil::error(['url' => $logUrl, 'response' => $resultJson, 'params' => $params, 'code' => LogUtil::WECONNECT_SERVER_ERROR], $logTarget); throw $exception; } if ($result && isset($result['code']) && (200 == $result['code'] || 204 == $result['code'])) { if ($returnResultData && $method != self::METHOD_DELETE) { return empty($result['data']) ? [] : $result['data']; } else { return true; } } else { throw new ApiDataException($logUrl, $resultJson, $params, $logTarget); } }