MemberInstantMessage.php 13 KB

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