MemberInstantMessage.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. * 网站地址: http://www.csdeshang.com
  12. * ----------------------------------------------------------------------------
  13. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  14. * 不允许对程序代码以任何形式任何目的的再发布。
  15. * ============================================================================
  16. * 用户消息控制器
  17. */
  18. class MemberInstantMessage extends BaseMember {
  19. public function initialize() {
  20. parent::initialize();
  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. try {
  29. Gateway::$registerAddress = config('ds_config.instant_message_register_url');
  30. $instant_message_model = model('instant_message');
  31. } catch (\Exception $e) {
  32. ds_json_encode(10001, $e->getMessage());
  33. }
  34. $to_id = input('param.to_id');
  35. $to_type = input('param.to_type', 0);
  36. $message = input('param.message');
  37. $message_type = input('param.message_type', 0);
  38. $res=word_filter($message);
  39. if(!$res['code']){
  40. ds_json_encode(10001,$res['msg']);
  41. }
  42. $message=$res['data']['text'];
  43. $instant_message_data = array(
  44. 'instant_message_from_id' => $this->member_info['member_id'],
  45. 'instant_message_from_type' => 0,
  46. 'instant_message_from_name' => $this->member_info['member_name'],
  47. 'instant_message_from_ip' => request()->ip(),
  48. 'instant_message_to_id' => $to_id,
  49. 'instant_message_to_type' => $to_type,
  50. 'instant_message' => $message,
  51. 'instant_message_type' => $message_type,
  52. 'instant_message_add_time' => TIMESTAMP,
  53. );
  54. $instant_message_validate = ds_validate('instant_message');
  55. if (!$instant_message_validate->scene('instant_message_save')->check($instant_message_data)) {
  56. ds_json_encode(10001, $instant_message_validate->getError());
  57. }
  58. Db::startTrans();
  59. try {
  60. $instant_message_data = $instant_message_model->addInstantMessage($instant_message_data);
  61. $res = $instant_message_model->sendInstantMessage($instant_message_data, true);
  62. if (!$res['code']) {
  63. throw new \think\Exception($res['msg'], 10006);
  64. }
  65. } catch (\Exception $e) {
  66. Db::rollback();
  67. ds_json_encode(10001, $e->getMessage());
  68. }
  69. Db::commit();
  70. ds_json_encode(10000, lang('message_send_success'), array('instant_message_data' => $instant_message_data));
  71. }
  72. public function set_message() {
  73. $max_id = intval(input('param.max_id'));
  74. $f_id = intval(input('param.f_id'));
  75. if (!$max_id || !$f_id) {
  76. ds_json_encode(10001, lang('param_error'));
  77. }
  78. $instant_message_model = model('instant_message');
  79. $condition = array();
  80. $condition[] = array('instant_message_to_id', '=', $this->member_info['member_id']);
  81. $condition[] = array('instant_message_to_type', '=', 0);
  82. $condition[] = array('instant_message_from_id', '=', $f_id);
  83. $condition[] = array('instant_message_from_type', '=', 0);
  84. $condition[] = array('instant_message_id', '<=', $max_id);
  85. $instant_message_model->editInstantMessage(array('instant_message_state' => 1), $condition);
  86. ds_json_encode(10000);
  87. }
  88. public function join() {
  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. $condition1 = array();
  130. $condition1[] = array('instant_message_from_id', '=', $this->member_info['member_id']);
  131. $condition1[] = array('instant_message_from_type', '=', 0);
  132. $condition1[] = array('instant_message_to_type', '=', 0);
  133. $condition2 = array();
  134. $condition2[] = array('instant_message_to_id', '=', $this->member_info['member_id']);
  135. $condition2[] = array('instant_message_to_type', '=', 0);
  136. $condition2[] = array('instant_message_from_type', '=', 0);
  137. //最近联系人最多取100个
  138. $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();
  139. $user_list = array();
  140. $member_model = model('member');
  141. $snsfriend_model = model('snsfriend');
  142. $instant_message_model = model('instant_message');
  143. $snsfriend_list=$snsfriend_model->getSnsfriendList(array('friend_frommid' => $this->member_info['member_id']), '*', 100, 'simple');
  144. foreach ($instant_message_list as $val) {
  145. $_condition1 = $condition1;
  146. $_condition2 = $condition2;
  147. if ($val['instant_message_from_id'] == $this->member_info['member_id'] && $val['instant_message_from_type'] == 0) {
  148. $type = $val['instant_message_to_type'];
  149. $id = $val['instant_message_to_id'];
  150. $name = $val['instant_message_to_name'];
  151. } else {
  152. $type = $val['instant_message_from_type'];
  153. $id = $val['instant_message_from_id'];
  154. $name = $val['instant_message_from_name'];
  155. }
  156. $_condition1[] = array('instant_message_to_id', '=', $id);
  157. $_condition2[] = array('instant_message_from_id', '=', $id);
  158. if (!isset($user_list[$type . '_' . $id])) {
  159. $user_info = array(
  160. 'u_id' => $id,
  161. 'u_type' => $type,
  162. 'u_name' => $name,
  163. 'avatar' => get_member_avatar_for_id($id)
  164. );
  165. if (!empty($user_info)) {
  166. $instant_message_info = Db::name('instant_message')->whereOr([$_condition1, $_condition2])->order('instant_message_add_time desc')->find();
  167. if ($instant_message_info) {
  168. if ($type == 0) {
  169. $snsfriend_info = $snsfriend_model->getOneSnsfriend(array('friend_frommid' => $this->member_info['member_id'], 'friend_tomid' => $id));
  170. } else {
  171. $snsfriend_info = false;
  172. }
  173. $user_info['recent'] = 1;
  174. $user_info['friend'] = ($snsfriend_info && $snsfriend_info['friend_followstate']==2) ? 1 : 0;
  175. $user_info['follow'] = ($snsfriend_info && $snsfriend_info['friend_followstate']==1) ? 1 : 0;
  176. $user_info['message_type'] = $instant_message_info['instant_message_type'];
  177. $message = $instant_message_info['instant_message'];
  178. if ($instant_message_info['instant_message_type'] == 1) {
  179. $message = '[商品]';
  180. }
  181. $user_info['r_state'] = $instant_message_info['instant_message_state'];
  182. $user_info['message'] = $message;
  183. $user_info['time'] = date("Y-m-d H:i:s", $instant_message_info['instant_message_add_time']);
  184. $user_list[$type . '_' . $id] = $user_info;
  185. }
  186. }
  187. }
  188. }
  189. foreach($snsfriend_list as $val){
  190. $id=$val['friend_tomid'];
  191. if(!isset($user_list['0_' . $id])){
  192. $user_list['0_' . $id] = array(
  193. 'u_id' => $id,
  194. 'u_type' => 0,
  195. 'u_name' => $val['friend_tomname'],
  196. 'avatar' => get_member_avatar_for_id($id),
  197. 'recent' => 0,
  198. 'friend' => ($val['friend_followstate']==2) ? 1 : 0,
  199. 'follow' => ($val['friend_followstate']==1) ? 1 : 0,
  200. 'message_type' => 0,
  201. 'r_state' => 1,
  202. 'message' => '',
  203. 'time' => '',
  204. );
  205. }
  206. }
  207. $user_list = array_values($user_list);
  208. ds_json_encode(10000, '', array('user_list' => $user_list));
  209. }
  210. public function get_chat_log() {
  211. $instant_message_model = model('instant_message');
  212. $t_id = intval(input('param.t_id'));
  213. $key = input('param.t');
  214. $add_time_to = date("Y-m-d");
  215. $time_from = array();
  216. $time_from['7'] = strtotime($add_time_to) - 60 * 60 * 24 * 7;
  217. $time_from['15'] = strtotime($add_time_to) - 60 * 60 * 24 * 15;
  218. $time_from['30'] = strtotime($add_time_to) - 60 * 60 * 24 * 30;
  219. $condition1 = array();
  220. $condition1[] = array('instant_message_from_id', '=', $this->member_info['member_id']);
  221. $condition1[] = array('instant_message_from_type', '=', 0);
  222. $condition1[] = array('instant_message_to_type', '=', 0);
  223. $condition1[] = array('instant_message_to_id', '=', $t_id);
  224. $condition2 = array();
  225. $condition2[] = array('instant_message_to_id', '=', $this->member_info['member_id']);
  226. $condition2[] = array('instant_message_to_type', '=', 0);
  227. $condition2[] = array('instant_message_from_type', '=', 0);
  228. $condition2[] = array('instant_message_from_id', '=', $t_id);
  229. if (!isset($time_from[$key])) {
  230. $key = '7';
  231. }
  232. $condition = array();
  233. $condition[] = array('instant_message_add_time', '>=', $time_from[$key]);
  234. //最近联系人最多取100个
  235. $result = Db::name('instant_message')->where(function ($query) use($condition1, $condition2) {
  236. $query->whereOr([$condition1, $condition2]);
  237. })->where($condition)->order('instant_message_add_time desc')->paginate(['list_rows' => 10, 'query' => request()->param()], false);
  238. $instant_message_list = $result->items();
  239. foreach ($instant_message_list as $key => $val) {
  240. $instant_message_list[$key] = $instant_message_model->formatInstantMessage($val);
  241. }
  242. ds_json_encode(10000, '', array('instant_message_list' => $instant_message_list, 'total_page' => $result->lastPage()));
  243. }
  244. /**
  245. * 店铺推荐商品图片和名称
  246. *
  247. */
  248. public function get_goods_list() {
  249. $s_id = intval(input('s_id'));
  250. if ($s_id > 0) {
  251. $goods_model = model('goods');
  252. $list = $goods_model->getGoodsCommendList($s_id, 4);
  253. if (!empty($list) && is_array($list)) {
  254. foreach ($list as $k => $v) {
  255. $v['goods_promotion_price'] = ds_price_format($v['goods_promotion_price']);
  256. $v['url'] = (string) url('Goods/index', array('goods_id' => $v['goods_id']));
  257. $v['pic'] = goods_thumb($v, 240);
  258. $list[$k] = $v;
  259. }
  260. }
  261. ds_json_encode(10000, '', array('goods_list' => $list));
  262. }
  263. }
  264. public function get_info() {
  265. $member_id = intval(input('param.u_id'));
  266. $member_model = model('member');
  267. $condition = array();
  268. $condition[] = array('member_id', '=', $member_id);
  269. $member = $member_model->getMemberInfo($condition, 'member_id,member_name,member_avatar');
  270. if (empty($member)) {
  271. ds_json_encode(10001, lang('user_not_exist'));
  272. }
  273. $member['store_id'] = '';
  274. $member['store_name'] = '';
  275. $member['store_avatar'] = '';
  276. $member['grade_id'] = '';
  277. $member['member_avatar'] = get_member_avatar($member['member_avatar']);
  278. $seller_model = model('seller');
  279. $seller = $seller_model->getSellerInfo(array('member_id' => $member['member_id']));
  280. if (!empty($seller) && $seller['store_id'] > 0) {
  281. $store_info = Db::name('store')->field('store_id,store_name,grade_id,store_avatar')->where(array('store_id' => $seller['store_id']))->find();
  282. if (is_array($store_info) && !empty($store_info)) {
  283. $member['store_id'] = $store_info['store_id'];
  284. $member['store_name'] = $store_info['store_name'];
  285. $member['seller_name'] = $seller['seller_name'];
  286. $member['grade_id'] = $store_info['grade_id'];
  287. $member['store_avatar'] = get_store_logo($store_info['store_avatar']);
  288. }
  289. }
  290. ds_json_encode(10000, '', array('user_info' => $member));
  291. }
  292. }
  293. ?>