123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- /**
- * 私聊相关Api
- * @author benzhan
- */
- class PersonController extends BaseController {
- protected $ajaxLoginActions = [
- 'msg',
- 'sendMsg',
- 'repealMsg',
- ];
- public function __construct() {
- parent::__construct(true);
- }
- /**
- * 私聊 获取最新消息
- * @author benzhan
- */
- public function actionNewMsg($args) {
- $rules = [
- 'session_id' => ['string', 'desc' => '会话id'],
- 'client_hash' => ['string', 'nullable' => true, 'desc' => '客户端上的最新消息的hash'],
- ];
- Param::checkParam2($rules, $args);
- $load_type = 0;
- return $this->_msg($args['session_id'], $load_type, $args['client_hash']);
- }
- /**
- * 私聊 获取历史消息
- * @author benzhan
- */
- public function actionHistoryMsg($args) {
- $rules = [
- 'session_id' => ['string', 'desc' => '会话id'],
- 'client_hash' => ['string', 'desc' => '客户端上的最旧消息的hash'],
- ];
- Param::checkParam2($rules, $args);
- $load_type = -1;
- return $this->_msg($args['session_id'], $load_type, $args['client_hash']);
- }
- /**
- * 获取消息列表
- * @param $session_id string 会话id
- * @param $load_type int 加载方式:-1:历史消息, 0:最新消息
- * @param $client_hash string 客户端的hash
- *
- * @return array
- */
- private function _msg($session_id, $load_type, $client_hash) {
- $where = compact('session_id');
- $keyWord = [
- '_field' => 'is_group, read_hash, read_num',
- ];
- $objSession = new Session();
- $row = $objSession->objTable->getRow($where, $keyWord);
- if (!$row) {
- return [
- 'userMap' => null,
- 'list' => [],
- 'need_clear' => false,
- ];
- } else if ($row['is_group']) {
- Response::error(CODE_NO_PERMITION, 'session is not personal');
- }
- $data = $objSession->getMsgList($session_id, $client_hash, $load_type, 0);
- $userId = User::getUserId();
- if ($userId) {
- $c = count($data['list']);
- $lastNum = $data['list'][$c-1]['msg_num'];
- if ($lastNum > $row['read_num']) {
- $objSession->updateState($session_id, ['read_num' => $lastNum], ['user_id' => $userId]);
- }
- }
- $data['list'] = array_filter($data['list'], function ($v) {
- return $v['state'] == 1;
- });
- return $data;
- }
- /**
- * 发送私聊消息
- * @author solu
- * @param $args
- * @return array
- */
- public function actionSendMsg($args) {
- $rules = [
- 'session_id' => ['string', 'desc' => '会话id'],
- 'msg_type' => ['int', 'nullable' => true, 'default' => 0, 'desc' => '消息类型:0:文本,1:图片,2:视频'],
- 'msg' => ['string', 'desc' => '内容'],
- ];
- Param::checkParam2($rules, $args);
- $userId = User::getUserId();
- $objSession = new Session();
- $data = [];
- try {
- $data = $objSession->sendPersonMsg($userId, $args['session_id'], $args['msg_type'], $args['msg']);
- } catch (Exception $e) {
- Response::error($e->getCode(), $e->getMessage());
- }
- return $data;
- }
- /**
- * 撤销消息
- * @author solu
- * @param $args
- */
- public function actionRepealMsg($args) {
- $rules = [
- 'session_id' => ['string', 'desc' => '会话id'],
- 'hash' => ['string', 'desc' => '消息hash'],
- ];
- Param::checkParam2($rules, $args);
- $userId = User::getUserId();
- $objMsg = new PersonMsg();
- try {
- $objMsg->repeal($userId, $args['session_id'], $args['hash']);
- } catch (Exception $e) {
- Response::error($e->getCode(), $e->getMessage());
- }
- }
- }
|