예제 #1
0
 public function actionUpdate($id)
 {
     $params = $this->getParams();
     $accountId = $this->getAccountId();
     $id = new \MongoId($id);
     if (empty($params['badge'])) {
         throw new InvalidParameterException(['number' => Yii::t('common', 'required_filed')]);
     }
     $helpDesk = HelpDesk::getByBadge($params['badge'], $accountId);
     if (!empty($helpDesk)) {
         throw new InvalidParameterException(['number' => Yii::t('helpDesk', 'badge_has_used')]);
     } else {
         HelpDesk::updateAll(['badge' => $params['badge']], ['_id' => $id]);
         $helpDesk = HelpDesk::findOne(['_id' => $id]);
         return $helpDesk;
     }
 }
예제 #2
0
 public function getUser()
 {
     $userId = $this->getUserId();
     return HelpDesk::findOne(['_id' => $userId]);
 }
예제 #3
0
 /**
  * Get the latest documents of the pending clients.
  * The clients should ping the server in the threshold or sourced from WeChat.
  * @param Integer $count the count of the elements to be dequeued
  * @return array the client.
  */
 public static function deQueue($deskId, $count = 1)
 {
     $helpdesk = HelpDesk::findOne($deskId);
     $condition = ['$or' => [['lastPingTime' => ['$gt' => TimeUtil::msTime(time() - self::PING_THRESHOLD)]], ['source' => ['$in' => [ChatConversation::TYPE_WECHAT, ChatConversation::TYPE_WEIBO, ChatConversation::TYPE_ALIPAY]]]], 'accountId' => $helpdesk->accountId];
     if (!empty($helpdesk->tags)) {
         $condition['tags'] = ['$in' => $helpdesk->tags];
         $pendingClients = self::find()->where($condition)->orderBy(['requestTime' => SORT_ASC])->limit($count)->all();
         if (count($pendingClients) < $count) {
             $condition['tags'] = ['$nin' => $helpdesk->tags];
             $excludeTagClients = self::find()->where($condition)->orderBy(['requestTime' => SORT_ASC])->limit($count - count($pendingClients))->all();
             $pendingClients = array_merge_recursive($pendingClients, $excludeTagClients);
         }
     } else {
         $pendingClients = self::find()->where($condition)->orderBy(['requestTime' => SORT_ASC])->limit($count)->all();
     }
     $clients = [];
     foreach ($pendingClients as $pendingClient) {
         $client = ['nick' => $pendingClient->nick, 'avatar' => $pendingClient->avatar, 'openId' => $pendingClient->openId, 'source' => $pendingClient->source, 'sourceChannel' => $pendingClient->sourceChannel];
         if ($pendingClient->accountInfo) {
             $client['accountInfo'] = $pendingClient->accountInfo;
         }
         $clients[] = $client;
         $pendingClient->delete();
     }
     return $clients;
 }
예제 #4
0
 /**
  * Update help desk password
  *
  * <b>Request Type</b>: PUT<br/><br/>
  * <b>Request Endpoint</b>:http://{server-domain}/chat/help-desk<br/><br/>
  * <b>Content-type</b>: application/json<br/><br/>
  * <b>Summary</b>: This api is used for help desk to update password.
  * <br/><br/>
  *
  * <b>Request Params</b>:<br/>
  *     id: int, the user id, required<br/>
  *     currentPwd: string, the user currentPwd, required<br/>
  *     newPwd: string, the user newPwd, required<br/>
  *     newPwdC: string, the user newPwdC, required<br/>
  *     <br/><br/>
  *
  * <b>Response Params:</b><br/>
  *     ack: integer, mark the update result, 0 means update successfully, 1 means update fail<br/>
  *     data: array, json array to describe the user updated<br/>
  *     <br/><br/>
  *
  * <b>Request Example:</b><br/>
  * <pre>
  * {
  *     "id" : "547eaf82e9c2fb52478b4567,
  *     "currentPwd" : "6c302344ab2117ee4ce52b7d8952c689",
  *     "newPwd" : "6c302344ab2117ee4ce52b7d8952c689",
  *     "newPwdC" : "6c302344ab2117ee4ce52b7d8952c689"
  * }
  * </pre>
  * <br/><br/>
  *
  * <b>Response Example</b>:<br/>
  * <pre>
  * {
  *    "result" : "success"
  * }
  * </pre>
  */
 public function actionUpdatepassword()
 {
     $params = $this->getParams();
     if (empty($params['id']) || empty($params['currentPwd']) || empty($params['newPwd']) || empty($params['newPwdC'])) {
         throw new BadRequestHttpException("Parameters missing");
     }
     // validate if the userid is correct
     $user = HelpDesk::findOne(['_id' => new \MongoId($params['id'])]);
     if (empty($user)) {
         throw new BadRequestHttpException("Incorrect userid");
     }
     // validate if the current password is correct
     if (!$user->validatePassword($params['currentPwd'])) {
         throw new InvalidParameterException(['old-password' => Yii::t('common', 'common_user_currentpwd_error')]);
     }
     // check if the two passwords match
     if ($params['newPwd'] !== $params['newPwdC']) {
         throw new InvalidParameterException(['new-password' => Yii::t('common', 'common_user_currentpwd_error')]);
     }
     // check the new password is same as the current password
     if ($params['currentPwd'] == $params['newPwd']) {
         throw new InvalidParameterException(['new-password' => Yii::t('chat', 'password_error')]);
     }
     // update the user information
     $user->password = HelpDesk::encryptPassword($params['newPwd'], $user->salt);
     if (!$user->save()) {
         throw new ServerErrorHttpException("Save help desk failed!");
     }
     return ['result' => 'success'];
 }
예제 #5
0
 /**
  * Activate a new user
  *
  * <b>Request Type</b>: POST<br/><br/>
  * <b>Request Endpoint</b>:http://{server-domain}/site/update-info<br/><br/>
  * <b>Content-type</b>: application/json<br/><br/>
  * <b>Summary</b>: This api is used for a user to activate account
  * <br/><br/>
  *
  * <b>Request Params</b>:<br/>
  *     name: string, the user name, required<br/>
  *     password: string, the user password, required<br/>
  *     id: string, the user id, required<br/>
  *     avatar: string, the user avatar, required<br/>
  *     code: string, the user validation code, required<br/>
  *     <br/><br/>
  *
  * <b>Response Params:</b><br/>
  *     ack: integer, mark the create result, 0 means create successfully, 1 means create fail<br/>
  *     data: array, json array to describe user id<br/>
  *     <br/><br/>
  *
  * <b>Request Example:</b><br/>
  * <pre>
  * {
  *     "name" : "sarazhang",
  *     "password" : "45345345gdfgdf",
  *     "id" : "643hfjht567",
  *     "avatar" : "http://www.baidu.com/1.jpg",
  *     "code" : "543gfdg45745sd",
  *
  * }
  * </pre>
  * <br/><br/>
  *
  * <b>Response Example</b>:<br/>
  * <pre>
  * {
  *    'ack' : 1,
  *    'data': {"id": "5345gdfg45745"}
  * }
  * </pre>
  */
 public function actionUpdateInfo()
 {
     $data = $this->getParams();
     if (empty($data['password']) || empty($data['name']) || empty($data['id']) || $data['password'] === md5('')) {
         throw new BadRequestHttpException(Yii::t('common', 'parameters_missing'));
     }
     $code = empty($data['code']) ? '' : $data['code'];
     $type = empty($data['type']) ? '' : $data['type'];
     $result = Validation::validateCode($code, false);
     if ($result == Validation::LINK_INVALID) {
         throw new GoneHttpException(Yii::t('common', 'link_invalid'));
     } else {
         if ($result == Validation::LINK_EXPIRED) {
             throw new GoneHttpException(Yii::t('common', 'link_invalid'));
         }
     }
     $salt = StringUtil::rndString(6);
     $password = User::encryptPassword($data['password'], $salt);
     $name = $data['name'];
     $avatar = $data['avatar'];
     $id = $data['id'];
     if (!empty($type) && $type == self::ACCOUNT_INVITATION) {
         $user = User::findOne(['_id' => $id]);
         $accountId = $user->accountId;
         if (empty(User::getByName($accountId, $name))) {
             $user->isActivated = User::ACTIVATED;
             $user->salt = $salt;
             $user->language = Yii::$app->language;
             $user->password = $password;
             $user->name = $name;
             $user->avatar = $avatar;
             $flag = $user->save();
         } else {
             throw new InvalidParameterException(['name' => Yii::t('common', 'name_exist')]);
         }
     } else {
         if (!empty($type) && $type == self::HELPDESK_INVITATION) {
             $helpDesk = HelpDesk::findOne(['_id' => $id]);
             $accountId = $helpDesk->accountId;
             if (empty(HelpDesk::getByName($accountId, $name))) {
                 $helpDesk->isActivated = User::ACTIVATED;
                 $helpDesk->language = Yii::$app->language;
                 $helpDesk->salt = $salt;
                 $helpDesk->password = $password;
                 $helpDesk->name = $name;
                 $helpDesk->avatar = $avatar;
                 $flag = $helpDesk->save();
             } else {
                 throw new InvalidParameterException(['name' => Yii::t('common', 'name_exist')]);
             }
         }
     }
     if ($flag) {
         Validation::deleteAll(['code' => $code]);
         return ['id' => $id, 'type' => $type];
     }
     throw new ServerErrorHttpException('activate fail');
 }
예제 #6
0
 /**
  * Initialize the conversation between helpdesk and client under an account.
  * Update redis cache and store the chat conversations in mongodb
  * @param  array $conversations the conversations stored in redis cache
  * @param  array $client the client information, containing openId, nick, avatar
  * @param  string $deskId the helpdesk UUID
  * @param  MongoId $accountId the account UUID
  * @return array response for pushing clientJoined state and connect event for wechat
  */
 public static function createConnection($client, $deskId, $accountId)
 {
     //update the conversation information in cache
     $conversations = Yii::$app->cache->get('conversations' . $accountId);
     $conversations[$deskId][] = $client['openId'];
     Yii::$app->cache->set('conversations' . $accountId, $conversations);
     //get the helpDesk information
     $deskMongoId = new \MongoId($deskId);
     $helpDesk = HelpDesk::findOne(['_id' => $deskMongoId]);
     //create chatConversation record in db
     $chatConversation = ChatConversation::saveRecord($helpDesk, $client, ChatConversation::STATUS_OPEN, $accountId);
     //get the history statistics
     $lastChatTime = ChatConversation::getLastChatTime($deskMongoId, $client['openId'], $accountId);
     $previousChatTime = ChatConversation::getLastChatTime($deskMongoId, $client['openId'], $accountId, 1);
     $chatTimes = ChatConversation::getChatTimes($client['openId'], $accountId);
     //trigger the client joined event
     $desk = $chatConversation->desk;
     $desk['id'] = $deskId;
     $channelName = ChatConversation::getChannelName($deskId, $client['openId']);
     $data = ['conversationId' => (string) $chatConversation->_id, 'desk' => $desk, 'client' => $client, 'channel' => $channelName, 'lastChatTime' => $lastChatTime, 'previousChatTime' => $previousChatTime, 'chatTimes' => $chatTimes, 'startTime' => TimeUtil::msTime()];
     Yii::$app->tuisongbao->triggerEvent(ChatConversation::EVENT_CLIENT_JOINED, $data, [ChatConversation::CHANNEL_GLOBAL . $accountId]);
     self::sendSystemReplyByType($client, $accountId, HelpDeskSetting::REPLY_SUCCESS);
     //push the client join event to helpdesk when helpdesk login in mobile
     $pushExtra = ['openId' => $client['openId'], 'conversationId' => (string) $chatConversation->_id, 'sentTime' => TimeUtil::msTime()];
     $pushMessage = ChatConversation::PUSH_MESSAGE_NEW_CLIENT;
     ChatConversation::pushMessage($desk['id'], ChatConversation::EVENT_CLIENT_JOINED, $pushExtra, $pushMessage);
     LogUtil::info(['event' => ChatConversation::EVENT_CLIENT_JOINED, 'desk' => $helpDesk->toArray(), 'client' => $client], 'helpdesk');
     return array_merge($data, ['status' => 'ok']);
 }