SellerInstantMessage.php 12 KB

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