MemberInstantMessage.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Db;
  4. use think\facade\Lang;
  5. use GatewayClient\Gateway;
  6. /**
  7. * DSKMS多用户商城
  8. *
  9. * ----------------------------------------------------------------------------
  10. *
  11. * 用户消息控制器
  12. */
  13. class MemberInstantMessage extends MobileMember
  14. {
  15. public function initialize()
  16. {
  17. parent::initialize();
  18. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/live.lang.php');
  19. }
  20. public function add()
  21. {
  22. if (!config('ds_config.instant_message_register_url')) {
  23. ds_json_encode(10001, lang('instant_message_register_url_empty'));
  24. }
  25. // 设置GatewayWorker服务的Register服务ip和端口,请根据实际情况改成实际值(ip不能是0.0.0.0)
  26. Gateway::$registerAddress = config('ds_config.instant_message_register_url');
  27. $instant_message_model = model('instant_message');
  28. $to_id = input('param.to_id');
  29. $to_type = input('param.to_type', 0);
  30. $message = input('param.message');
  31. $message_type = input('param.message_type', 0);
  32. $res = word_filter($message);
  33. if (!$res['code']) {
  34. ds_json_encode(10001, $res['msg']);
  35. }
  36. $message = $res['data']['text'];
  37. $instant_message_data = array(
  38. 'instant_message_from_id' => $this->member_info['member_id'],
  39. 'instant_message_from_type' => 0,
  40. 'instant_message_from_name' => $this->member_info['member_name'],
  41. 'instant_message_from_ip' => request()->ip(),
  42. 'instant_message_to_id' => $to_id,
  43. 'instant_message_to_type' => $to_type,
  44. 'instant_message' => $message,
  45. 'instant_message_type' => $message_type,
  46. 'instant_message_add_time' => TIMESTAMP,
  47. );
  48. $instant_message_validate = ds_validate('instant_message');
  49. if (!$instant_message_validate->scene('instant_message_save')->check($instant_message_data)) {
  50. ds_json_encode(10001, $instant_message_validate->getError());
  51. }
  52. Db::startTrans();
  53. try {
  54. $instant_message_data = $instant_message_model->addInstantMessage($instant_message_data);
  55. $res = $instant_message_model->sendInstantMessage($instant_message_data, true);
  56. if (!$res['code']) {
  57. throw new \think\Exception($res['msg'], 10006);
  58. }
  59. } catch (\Exception $e) {
  60. Db::rollback();
  61. ds_json_encode(10001, $e->getMessage());
  62. }
  63. Db::commit();
  64. ds_json_encode(10000, lang('message_send_success'), array('instant_message_data' => $instant_message_data));
  65. }
  66. public function join()
  67. {
  68. $client_id = input('param.client_id');
  69. if (!$client_id) {
  70. ds_json_encode(10001, lang('param_error'));
  71. }
  72. if (!config('ds_config.instant_message_register_url')) {
  73. ds_json_encode(10001, lang('instant_message_register_url_empty'));
  74. }
  75. $instant_message_model = model('instant_message');
  76. // 设置GatewayWorker服务的Register服务ip和端口,请根据实际情况改成实际值(ip不能是0.0.0.0)
  77. try {
  78. Gateway::$registerAddress = config('ds_config.instant_message_register_url');
  79. // client_id与uid绑定
  80. Gateway::bindUid($client_id, '0:' . $this->member_info['member_id']);
  81. $online_item = array(
  82. 'instant_message_from_avatar' => get_member_avatar($this->member_info['member_avatar']),
  83. 'instant_message_from_id' => $this->member_info['member_id'],
  84. 'instant_message_from_type' => 0,
  85. 'instant_message_from_name' => $this->member_info['member_name']
  86. );
  87. Gateway::setSession($client_id, $online_item);
  88. $condition = array();
  89. $condition[] = array('instant_message_to_id', '=', $this->member_info['member_id']);
  90. $condition[] = array('instant_message_to_type', '=', 0);
  91. $condition[] = array('instant_message_from_type', '=', 0);
  92. $condition[] = array('instant_message_state', '=', 2);
  93. $msg_list = $instant_message_model->getInstantMessageList($condition, '', 'instant_message_id asc');
  94. foreach ($msg_list as $key => $val) {
  95. $msg_list[$key] = $instant_message_model->formatInstantMessage($val);
  96. }
  97. //发送未读消息
  98. Gateway::sendToClient($client_id, json_encode(array(
  99. 'type' => 'get_msg',
  100. 'msg_list' => $msg_list,
  101. )));
  102. } catch (\Exception $e) {
  103. ds_json_encode(10001, $e->getMessage());
  104. }
  105. ds_json_encode(10000, '');
  106. }
  107. public function set_message()
  108. {
  109. $max_id = intval(input('param.max_id'));
  110. $f_id = intval(input('param.f_id'));
  111. if (!$max_id || !$f_id) {
  112. ds_json_encode(10001, lang('param_error'));
  113. }
  114. $instant_message_model = model('instant_message');
  115. $condition = array();
  116. $condition[] = array('instant_message_to_id', '=', $this->member_info['member_id']);
  117. $condition[] = array('instant_message_to_type', '=', 0);
  118. $condition[] = array('instant_message_from_id', '=', $f_id);
  119. $condition[] = array('instant_message_from_type', '=', 0);
  120. $condition[] = array('instant_message_id', '<=', $max_id);
  121. $instant_message_model->editInstantMessage(array('instant_message_state' => 1), $condition);
  122. ds_json_encode(10000);
  123. }
  124. /**
  125. * @api {POST} api/MemberInstantMessage/get_chat_log 聊天记录查询
  126. * @apiVersion 1.0.0
  127. * @apiGroup MemberInstantMessage
  128. *
  129. * @apiHeader {String} X-DS-KEY 用户授权token
  130. *
  131. * @apiParam {Int} t_id 用户ID
  132. * @apiParam {Int} page 页码
  133. * @apiParam {Int} pagesize 每页显示数量
  134. *
  135. * @apiSuccess {String} code 返回码,10000为成功
  136. * @apiSuccess {String} message 返回消息
  137. * @apiSuccess {Object} result 返回数据
  138. * @apiSuccess {Object[]} result.list 消息
  139. * @apiSuccess {Int} result.page_total 总页数
  140. * @apiSuccess {Boolean} result.hasmore 是否有更多 true是false否
  141. */
  142. public function get_chat_log()
  143. {
  144. $instant_message_model = model('instant_message');
  145. $t_id = intval(input('param.t_id'));
  146. $condition1 = array();
  147. $condition1[] = array('instant_message_from_id', '=', $this->member_info['member_id']);
  148. $condition1[] = array('instant_message_from_type', '=', 0);
  149. $condition1[] = array('instant_message_to_type', '=', 0);
  150. $condition1[] = array('instant_message_to_id', '=', $t_id);
  151. $condition2 = array();
  152. $condition2[] = array('instant_message_to_id', '=', $this->member_info['member_id']);
  153. $condition2[] = array('instant_message_to_type', '=', 0);
  154. $condition2[] = array('instant_message_from_type', '=', 0);
  155. $condition2[] = array('instant_message_from_id', '=', $t_id);
  156. //最近联系人最多取100个
  157. $result = Db::name('instant_message')->where(function ($query) use ($condition1, $condition2) {
  158. $query->whereOr([$condition1, $condition2]);
  159. })->order('instant_message_add_time desc')->paginate(['list_rows' => 10, 'query' => request()->param()], false);
  160. $instant_message_list = $result->items();
  161. foreach ($instant_message_list as $key => $val) {
  162. $instant_message_list[$key] = $instant_message_model->formatInstantMessage($val);
  163. }
  164. $result = array_merge(array('list' => $instant_message_list), mobile_page($result));
  165. ds_json_encode(10000, '', $result);
  166. }
  167. /**
  168. * @api {POST} api/MemberInstantMessage/get_user_list 最近联系人
  169. * @apiVersion 1.0.0
  170. * @apiGroup MemberInstantMessage
  171. *
  172. * @apiHeader {String} X-DS-KEY 用户授权token
  173. *
  174. * @apiParam {Int} n 显示数量
  175. * @apiParam {Int} recent 只显示最近消息的用户 1是
  176. *
  177. * @apiSuccess {String} code 返回码,10000为成功
  178. * @apiSuccess {String} message 返回消息
  179. * @apiSuccess {Object} result 返回数据
  180. * @apiSuccess {Object} result.list 最近联系人列表,键为用户ID
  181. * @apiSuccess {String} result.list.avatar 用户头像
  182. * @apiSuccess {Int} result.list.r_state 是否已读 1为已读,2为未读
  183. * @apiSuccess {Int} result.list.recent 是否最近 0否1是
  184. * @apiSuccess {String} result.list.time 联系时间描述
  185. * @apiSuccess {Int} result.list.u_id 用户ID
  186. * @apiSuccess {String} result.list.u_name 用户名称
  187. */
  188. public function get_user_list()
  189. {
  190. $condition1 = array();
  191. $condition1[] = array('instant_message_from_id', '=', $this->member_info['member_id']);
  192. $condition1[] = array('instant_message_from_type', '=', 0);
  193. $condition1[] = array('instant_message_to_type', '=', 0);
  194. $condition2 = array();
  195. $condition2[] = array('instant_message_to_id', '=', $this->member_info['member_id']);
  196. $condition2[] = array('instant_message_to_type', '=', 0);
  197. $condition2[] = array('instant_message_from_type', '=', 0);
  198. //最近联系人最多取100个
  199. $instant_message_list = Db::name('instant_message')->whereOr([$condition1, $condition2])->distinct(true)->field('instant_message_to_id,instant_message_to_type,instant_message_from_id,instant_message_from_type')->order('instant_message_add_time desc')->limit(100)->select()->toArray();
  200. $user_list = array();
  201. $member_model = model('member');
  202. $instant_message_model = model('instant_message');
  203. foreach ($instant_message_list as $val) {
  204. $_condition1 = $condition1;
  205. $_condition2 = $condition2;
  206. if ($val['instant_message_from_id'] == $this->member_info['member_id'] && $val['instant_message_from_type'] == 0) {
  207. $type = $val['instant_message_to_type'];
  208. $id = $val['instant_message_to_id'];
  209. } else {
  210. $type = $val['instant_message_from_type'];
  211. $id = $val['instant_message_from_id'];
  212. }
  213. $_condition1[] = array('instant_message_to_id', '=', $id);
  214. $_condition2[] = array('instant_message_from_id', '=', $id);
  215. if (!isset($user_list[$type . '_' . $id])) {
  216. $temp = $member_model->getMemberInfo(array('member_id' => $id, 'member_state' => 1));
  217. if ($temp) {
  218. $user_info = array(
  219. 'u_id' => $id,
  220. 'u_type' => $type,
  221. 'u_name' => $temp['member_name'],
  222. 'avatar' => get_member_avatar($temp['member_avatar'])
  223. );
  224. }
  225. if (!empty($user_info)) {
  226. $instant_message_info = Db::name('instant_message')->whereOr([$_condition1, $_condition2])->order('instant_message_add_time desc')->find();
  227. if ($instant_message_info) {
  228. $user_info['recent'] = 1;
  229. $user_info['message_type'] = $instant_message_info['instant_message_type'];
  230. $message = $instant_message_info['instant_message'];
  231. if ($instant_message_info['instant_message_type'] == 1) {
  232. $message = '[商品]';
  233. }
  234. $user_info['r_state'] = $instant_message_info['instant_message_state'];
  235. $user_info['message'] = $message;
  236. $user_info['time'] = date("Y-m-d H:i:s", $instant_message_info['instant_message_add_time']);
  237. $user_list[$type . '_' . $id] = $user_info;
  238. }
  239. }
  240. }
  241. }
  242. $user_list = array_values($user_list);
  243. ds_json_encode(10000, '', array('list' => $user_list));
  244. }
  245. /**
  246. * 未读消息查询
  247. *
  248. */
  249. public function get_msg_count()
  250. {
  251. $instant_message_model = model('instant_message');
  252. $condition = array();
  253. $condition[] = array('instant_message_to_id', '=', $this->member_info['member_id']);
  254. $condition[] = array('instant_message_to_type', '=', 0);
  255. $condition[] = array('instant_message_from_type', '=', 0);
  256. $condition[] = array('instant_message_state', '=', 2);
  257. $n = $instant_message_model->getInstantMessageCount($condition);
  258. ds_json_encode(10000, '', $n);
  259. }
  260. }