MemberInstantMessage.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. namespace app\home\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 MemberInstantMessage extends BaseMember
  14. {
  15. public function initialize()
  16. {
  17. parent::initialize();
  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. try {
  27. Gateway::$registerAddress = config('ds_config.instant_message_register_url');
  28. $instant_message_model = model('instant_message');
  29. } catch (\Exception $e) {
  30. ds_json_encode(10001, $e->getMessage());
  31. }
  32. $to_id = input('param.to_id');
  33. $to_type = input('param.to_type', 0);
  34. $message = input('param.message');
  35. $message_type = input('param.message_type', 0);
  36. $res = word_filter($message);
  37. if (!$res['code']) {
  38. ds_json_encode(10001, $res['msg']);
  39. }
  40. $message = $res['data']['text'];
  41. $instant_message_data = array(
  42. 'instant_message_from_id' => $this->member_info['member_id'],
  43. 'instant_message_from_type' => 0,
  44. 'instant_message_from_name' => $this->member_info['member_name'],
  45. 'instant_message_from_ip' => request()->ip(),
  46. 'instant_message_to_id' => $to_id,
  47. 'instant_message_to_type' => $to_type,
  48. 'instant_message' => $message,
  49. 'instant_message_type' => $message_type,
  50. 'instant_message_add_time' => TIMESTAMP,
  51. );
  52. $instant_message_validate = ds_validate('instant_message');
  53. if (!$instant_message_validate->scene('instant_message_save')->check($instant_message_data)) {
  54. ds_json_encode(10001, $instant_message_validate->getError());
  55. }
  56. Db::startTrans();
  57. try {
  58. $instant_message_data = $instant_message_model->addInstantMessage($instant_message_data);
  59. $res = $instant_message_model->sendInstantMessage($instant_message_data, true);
  60. if (!$res['code']) {
  61. throw new \think\Exception($res['msg'], 10006);
  62. }
  63. } catch (\Exception $e) {
  64. Db::rollback();
  65. ds_json_encode(10001, $e->getMessage());
  66. }
  67. Db::commit();
  68. ds_json_encode(10000, lang('message_send_success'), array('instant_message_data' => $instant_message_data));
  69. }
  70. public function set_message()
  71. {
  72. $max_id = intval(input('param.max_id'));
  73. $f_id = intval(input('param.f_id'));
  74. if (!$max_id || !$f_id) {
  75. ds_json_encode(10001, lang('param_error'));
  76. }
  77. $instant_message_model = model('instant_message');
  78. $condition = array();
  79. $condition[] = array('instant_message_to_id', '=', $this->member_info['member_id']);
  80. $condition[] = array('instant_message_to_type', '=', 0);
  81. $condition[] = array('instant_message_from_id', '=', $f_id);
  82. $condition[] = array('instant_message_from_type', '=', 0);
  83. $condition[] = array('instant_message_id', '<=', $max_id);
  84. $instant_message_model->editInstantMessage(array('instant_message_state' => 1), $condition);
  85. ds_json_encode(10000);
  86. }
  87. public function join()
  88. {
  89. $client_id = input('param.client_id');
  90. if (!$client_id) {
  91. ds_json_encode(10001, lang('param_error'));
  92. }
  93. if (!config('ds_config.instant_message_register_url')) {
  94. ds_json_encode(10001, lang('instant_message_register_url_empty'));
  95. }
  96. $instant_message_model = model('instant_message');
  97. // 设置GatewayWorker服务的Register服务ip和端口,请根据实际情况改成实际值(ip不能是0.0.0.0)
  98. try {
  99. Gateway::$registerAddress = config('ds_config.instant_message_register_url');
  100. // client_id与uid绑定
  101. Gateway::bindUid($client_id, '0:' . $this->member_info['member_id']);
  102. $online_item = array(
  103. 'instant_message_from_avatar' => get_member_avatar($this->member_info['member_avatar']),
  104. 'instant_message_from_id' => $this->member_info['member_id'],
  105. 'instant_message_from_type' => 0,
  106. 'instant_message_from_name' => $this->member_info['member_name']
  107. );
  108. Gateway::setSession($client_id, $online_item);
  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_type', '=', 0);
  113. $condition[] = array('instant_message_state', '=', 2);
  114. $msg_list = $instant_message_model->getInstantMessageList($condition, '', 'instant_message_id asc');
  115. foreach ($msg_list as $key => $val) {
  116. $msg_list[$key] = $instant_message_model->formatInstantMessage($val);
  117. }
  118. //发送未读消息
  119. Gateway::sendToClient($client_id, json_encode(array(
  120. 'type' => 'get_msg',
  121. 'msg_list' => $msg_list,
  122. )));
  123. } catch (\Exception $e) {
  124. ds_json_encode(10001, $e->getMessage());
  125. }
  126. ds_json_encode(10000, '');
  127. }
  128. public function get_user_list()
  129. {
  130. $condition1 = array();
  131. $condition1[] = array('instant_message_from_id', '=', $this->member_info['member_id']);
  132. $condition1[] = array('instant_message_from_type', '=', 0);
  133. $condition1[] = array('instant_message_to_type', '=', 0);
  134. $condition2 = array();
  135. $condition2[] = array('instant_message_to_id', '=', $this->member_info['member_id']);
  136. $condition2[] = array('instant_message_to_type', '=', 0);
  137. $condition2[] = array('instant_message_from_type', '=', 0);
  138. //最近联系人最多取100个
  139. $instant_message_list = Db::name('instant_message')->whereOr([$condition1, $condition2])->distinct(true)->field('instant_message_to_id,instant_message_to_type,instant_message_to_name,instant_message_from_id,instant_message_from_type,instant_message_from_name')->order('instant_message_add_time desc')->limit(100)->select()->toArray();
  140. $user_list = array();
  141. $member_model = model('member');
  142. $snsfriend_model = model('snsfriend');
  143. $instant_message_model = model('instant_message');
  144. $snsfriend_list = $snsfriend_model->getSnsfriendList(array('friend_frommid' => $this->member_info['member_id']), '*', 100, 'simple');
  145. foreach ($instant_message_list as $val) {
  146. $_condition1 = $condition1;
  147. $_condition2 = $condition2;
  148. if ($val['instant_message_from_id'] == $this->member_info['member_id'] && $val['instant_message_from_type'] == 0) {
  149. $type = $val['instant_message_to_type'];
  150. $id = $val['instant_message_to_id'];
  151. $name = $val['instant_message_to_name'];
  152. } else {
  153. $type = $val['instant_message_from_type'];
  154. $id = $val['instant_message_from_id'];
  155. $name = $val['instant_message_from_name'];
  156. }
  157. $_condition1[] = array('instant_message_to_id', '=', $id);
  158. $_condition2[] = array('instant_message_from_id', '=', $id);
  159. if (!isset($user_list[$type . '_' . $id])) {
  160. $user_info = array(
  161. 'u_id' => $id,
  162. 'u_type' => $type,
  163. 'u_name' => $name,
  164. 'avatar' => get_member_avatar_for_id($id)
  165. );
  166. if (!empty($user_info)) {
  167. $instant_message_info = Db::name('instant_message')->whereOr([$_condition1, $_condition2])->order('instant_message_add_time desc')->find();
  168. if ($instant_message_info) {
  169. if ($type == 0) {
  170. $snsfriend_info = $snsfriend_model->getOneSnsfriend(array('friend_frommid' => $this->member_info['member_id'], 'friend_tomid' => $id));
  171. } else {
  172. $snsfriend_info = false;
  173. }
  174. $user_info['recent'] = 1;
  175. $user_info['friend'] = ($snsfriend_info && $snsfriend_info['friend_followstate'] == 2) ? 1 : 0;
  176. $user_info['follow'] = ($snsfriend_info && $snsfriend_info['friend_followstate'] == 1) ? 1 : 0;
  177. $user_info['message_type'] = $instant_message_info['instant_message_type'];
  178. $message = $instant_message_info['instant_message'];
  179. if ($instant_message_info['instant_message_type'] == 1) {
  180. $message = '[商品]';
  181. }
  182. $user_info['r_state'] = $instant_message_info['instant_message_state'];
  183. $user_info['message'] = $message;
  184. $user_info['time'] = date("Y-m-d H:i:s", $instant_message_info['instant_message_add_time']);
  185. $user_list[$type . '_' . $id] = $user_info;
  186. }
  187. }
  188. }
  189. }
  190. foreach ($snsfriend_list as $val) {
  191. $id = $val['friend_tomid'];
  192. if (!isset($user_list['0_' . $id])) {
  193. $user_list['0_' . $id] = array(
  194. 'u_id' => $id,
  195. 'u_type' => 0,
  196. 'u_name' => $val['friend_tomname'],
  197. 'avatar' => get_member_avatar_for_id($id),
  198. 'recent' => 0,
  199. 'friend' => ($val['friend_followstate'] == 2) ? 1 : 0,
  200. 'follow' => ($val['friend_followstate'] == 1) ? 1 : 0,
  201. 'message_type' => 0,
  202. 'r_state' => 1,
  203. 'message' => '',
  204. 'time' => '',
  205. );
  206. }
  207. }
  208. $user_list = array_values($user_list);
  209. ds_json_encode(10000, '', array('user_list' => $user_list));
  210. }
  211. public function get_chat_log()
  212. {
  213. $instant_message_model = model('instant_message');
  214. $t_id = intval(input('param.t_id'));
  215. $key = input('param.t');
  216. $add_time_to = date("Y-m-d");
  217. $time_from = array();
  218. $time_from['7'] = strtotime($add_time_to) - 60 * 60 * 24 * 7;
  219. $time_from['15'] = strtotime($add_time_to) - 60 * 60 * 24 * 15;
  220. $time_from['30'] = strtotime($add_time_to) - 60 * 60 * 24 * 30;
  221. $condition1 = array();
  222. $condition1[] = array('instant_message_from_id', '=', $this->member_info['member_id']);
  223. $condition1[] = array('instant_message_from_type', '=', 0);
  224. $condition1[] = array('instant_message_to_type', '=', 0);
  225. $condition1[] = array('instant_message_to_id', '=', $t_id);
  226. $condition2 = array();
  227. $condition2[] = array('instant_message_to_id', '=', $this->member_info['member_id']);
  228. $condition2[] = array('instant_message_to_type', '=', 0);
  229. $condition2[] = array('instant_message_from_type', '=', 0);
  230. $condition2[] = array('instant_message_from_id', '=', $t_id);
  231. if (!isset($time_from[$key])) {
  232. $key = '7';
  233. }
  234. $condition = array();
  235. $condition[] = array('instant_message_add_time', '>=', $time_from[$key]);
  236. //最近联系人最多取100个
  237. $result = Db::name('instant_message')->where(function ($query) use ($condition1, $condition2) {
  238. $query->whereOr([$condition1, $condition2]);
  239. })->where($condition)->order('instant_message_add_time desc')->paginate(['list_rows' => 10, 'query' => request()->param()], false);
  240. $instant_message_list = $result->items();
  241. foreach ($instant_message_list as $key => $val) {
  242. $instant_message_list[$key] = $instant_message_model->formatInstantMessage($val);
  243. }
  244. ds_json_encode(10000, '', array('instant_message_list' => $instant_message_list, 'total_page' => $result->lastPage()));
  245. }
  246. /**
  247. * 店铺推荐商品图片和名称
  248. *
  249. */
  250. public function get_goods_list()
  251. {
  252. $s_id = intval(input('s_id'));
  253. if ($s_id > 0) {
  254. $goods_model = model('goods');
  255. $list = $goods_model->getGoodsCommendList($s_id, 4);
  256. if (!empty($list) && is_array($list)) {
  257. foreach ($list as $k => $v) {
  258. $v['goods_promotion_price'] = ds_price_format($v['goods_promotion_price']);
  259. $v['url'] = (string) url('Goods/index', array('goods_id' => $v['goods_id']));
  260. $v['pic'] = goods_thumb($v, 240);
  261. $list[$k] = $v;
  262. }
  263. }
  264. ds_json_encode(10000, '', array('goods_list' => $list));
  265. }
  266. }
  267. public function get_info()
  268. {
  269. $member_id = intval(input('param.u_id'));
  270. $member_model = model('member');
  271. $condition = array();
  272. $condition[] = array('member_id', '=', $member_id);
  273. $member = $member_model->getMemberInfo($condition, 'member_id,member_name,member_avatar');
  274. if (empty($member)) {
  275. ds_json_encode(10001, lang('user_not_exist'));
  276. }
  277. $member['store_id'] = '';
  278. $member['store_name'] = '';
  279. $member['store_avatar'] = '';
  280. $member['grade_id'] = '';
  281. $member['member_avatar'] = get_member_avatar($member['member_avatar']);
  282. $seller_model = model('seller');
  283. $seller = $seller_model->getSellerInfo(array('member_id' => $member['member_id']));
  284. if (!empty($seller) && $seller['store_id'] > 0) {
  285. $store_info = Db::name('store')->field('store_id,store_name,grade_id,store_avatar')->where(array('store_id' => $seller['store_id']))->find();
  286. if (is_array($store_info) && !empty($store_info)) {
  287. $member['store_id'] = $store_info['store_id'];
  288. $member['store_name'] = $store_info['store_name'];
  289. $member['seller_name'] = $seller['seller_name'];
  290. $member['grade_id'] = $store_info['grade_id'];
  291. $member['store_avatar'] = get_store_logo($store_info['store_avatar']);
  292. }
  293. }
  294. ds_json_encode(10000, '', array('user_info' => $member));
  295. }
  296. }