MemberInstantMessage.php 13 KB

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