MemberInstantMessage.php 15 KB

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