SellerInstantMessage.php 12 KB

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