MemberInstantMessage.php 15 KB

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