SellerInstantMessage.php 12 KB

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