InstantMessage.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace app\common\model;
  3. use GatewayClient\Gateway;
  4. use think\facade\Db;
  5. /**
  6. * ============================================================================
  7. * DSKMS多用户商城
  8. * ============================================================================
  9. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  10. * 网站地址: https://www.valimart.net/
  11. * ----------------------------------------------------------------------------
  12. *
  13. * ============================================================================
  14. * 数据层模型
  15. */
  16. class InstantMessage extends BaseModel {
  17. public $page_info;
  18. /**
  19. * 获取服务机构通知列表
  20. * @access public
  21. * @author csdeshang
  22. * @param type $condition
  23. * @param type $pagesize
  24. * @param type $order
  25. * @return type
  26. */
  27. public function getInstantMessageList($condition,$pagesize='',$order='instant_message_id desc'){
  28. if ($pagesize) {
  29. $result = Db::name('InstantMessage')->where($condition)->order($order)->paginate(['list_rows'=>$pagesize,'query' => request()->param()],false);
  30. $this->page_info = $result;
  31. return $result->items();
  32. } else {
  33. return Db::name('InstantMessage')->where($condition)->order($order)->select()->toArray();
  34. }
  35. }
  36. /**
  37. * 取得服务机构通知信息
  38. * @access public
  39. * @author csdeshang
  40. * @param array $condition 检索条件
  41. * @param string $fields 字段
  42. * @param string $order 排序
  43. * @return array
  44. */
  45. public function getInstantMessageInfo($condition = array(), $fields = '*') {
  46. return Db::name('InstantMessage')->where($condition)->field($fields)->order('instant_message_id desc')->find();
  47. }
  48. /**
  49. * 添加服务机构通知信息
  50. * @access public
  51. * @author csdeshang
  52. * @param array $data 参数数据
  53. * @return type
  54. */
  55. public function addInstantMessage($data) {
  56. switch ($data['instant_message_to_type']) {
  57. case 0:
  58. $member_model = model('member');
  59. $member = $member_model->getMemberInfo(array('member_id' => $data['instant_message_to_id'], 'member_state' => 1));
  60. if (!$member) {
  61. throw new \think\Exception(lang('user_not_exist'), 10006);
  62. }
  63. if($data['instant_message_from_type']==0 && $member['member_id']==$data['instant_message_from_id']){
  64. throw new \think\Exception(lang('chat_self_error'), 10006);
  65. }
  66. $to_name = $member['member_name'];
  67. break;
  68. case 1:
  69. $store_model = model('store');
  70. $store = $store_model->getStoreOnlineInfoByID($data['instant_message_to_id']);
  71. if (!$store) {
  72. throw new \think\Exception(lang('store_not_exist'), 10006);
  73. }
  74. if($data['instant_message_from_type']==0 && $store['member_id']==$data['instant_message_from_id']){
  75. throw new \think\Exception(lang('chat_self_error'), 10006);
  76. }
  77. if($data['instant_message_from_type']==1 && $store['store_id']==$data['instant_message_from_id']){
  78. throw new \think\Exception(lang('chat_self_error'), 10006);
  79. }
  80. $to_name = $store['store_name'];
  81. break;
  82. case 2:
  83. $live_apply_model = model('live_apply');
  84. $live_apply = $live_apply_model->getLiveApplyInfo(array(array('live_apply_id' ,'=', $data['instant_message_to_id']), array('live_apply_state' ,'=', 1), array('live_apply_end_time','>', TIMESTAMP)));
  85. if (!$live_apply) {
  86. throw new \think\Exception(lang('live_not_exit'), 10006);
  87. }
  88. $to_name = $live_apply['live_apply_id'] . lang('live_room');
  89. break;
  90. default:
  91. throw new \think\Exception(lang('param_error'), 10006);
  92. }
  93. $data['instant_message_to_name']=$to_name;
  94. switch($data['instant_message_type']){
  95. case 1:
  96. $goods_id = $data['instant_message'];
  97. if(!$goods_id){
  98. throw new \think\Exception(lang('param_error'), 10006);
  99. }
  100. $goods_model=model('goods');
  101. $goods = $goods_model->getGoodsInfoByID($goods_id);
  102. if (is_array($goods) && !empty($goods)) {
  103. $data['instant_message']= json_encode(array(
  104. 'goods_id'=>$goods['goods_id'],
  105. 'goods_name'=>$goods['goods_name'],
  106. 'goods_price'=>$goods['goods_price'],
  107. 'goods_image'=>$goods['goods_image'],
  108. ));
  109. }else{
  110. throw new \think\Exception(lang('goods_not_exit'), 10006);
  111. }
  112. break;
  113. }
  114. $instant_message_id=Db::name('InstantMessage')->insertGetId($data);
  115. if (!$instant_message_id) {
  116. throw new \think\Exception(lang('send_fail'), 10006);
  117. }
  118. $data['instant_message_id']=$instant_message_id;
  119. $data=$this->formatInstantMessage($data);
  120. return $data;
  121. }
  122. public function formatInstantMessage($data){
  123. if($data['instant_message_type']==1){
  124. $data['instant_message']= json_decode($data['instant_message'],true);
  125. $data['instant_message']['goods_image_url']= goods_cthumb($data['instant_message']['goods_image']);
  126. }
  127. $member_model = model('member');
  128. $store_model = model('store');
  129. if($data['instant_message_to_type']==0){
  130. $member = $member_model->getMemberInfo(array('member_id' => $data['instant_message_to_id'], 'member_state' => 1));
  131. $data['instant_message_to_avatar']=get_member_avatar($member?$member['member_avatar']:'');
  132. $data['instant_message_to_info']=$member;
  133. }elseif($data['instant_message_to_type']==1){
  134. $store = $store_model->getStoreOnlineInfoByID($data['instant_message_to_id']);
  135. $data['instant_message_to_avatar']=get_store_logo($store?$store['store_avatar']:'');
  136. $data['instant_message_to_info']=$store;
  137. }
  138. if($data['instant_message_from_type']==0){
  139. $member = $member_model->getMemberInfo(array('member_id' => $data['instant_message_from_id'], 'member_state' => 1));
  140. $data['instant_message_from_avatar']=get_member_avatar($member?$member['member_avatar']:'');
  141. $data['instant_message_from_info']=$member;
  142. }elseif($data['instant_message_from_type']==1){
  143. $store = $store_model->getStoreOnlineInfoByID($data['instant_message_from_id']);
  144. $data['instant_message_from_avatar']=get_store_logo($store?$store['store_avatar']:'');
  145. $data['instant_message_from_info']=$store;
  146. }
  147. return $data;
  148. }
  149. /**
  150. * 编辑服务机构通知信息
  151. * @access public
  152. * @author csdeshang
  153. * @param array $data 更新数据
  154. * @param array $condition 条件
  155. * @return bool
  156. */
  157. public function editInstantMessage($data, $condition = array()) {
  158. return Db::name('InstantMessage')->where($condition)->update($data);
  159. }
  160. /**
  161. * 获取服务机构通知数量
  162. * @access public
  163. * @author csdeshang
  164. * @param array $condition 条件
  165. * @return bool
  166. */
  167. public function getInstantMessageCount($condition = array()) {
  168. return Db::name('InstantMessage')->where($condition)->count();
  169. }
  170. public function sendInstantMessage($instant_message,$auto=false){
  171. if(!config('ds_config.instant_message_register_url')){
  172. return ds_callback(false,'未设置直播聊天gateway地址');
  173. }
  174. // 设置GatewayWorker服务的Register服务ip和端口,请根据实际情况改成实际值(ip不能是0.0.0.0)
  175. try{
  176. Gateway::$registerAddress = config('ds_config.instant_message_register_url');
  177. if($instant_message['instant_message_to_type']==2){
  178. Gateway::sendToGroup('live_apply_'.$instant_message['instant_message_to_id'], json_encode($instant_message));
  179. }elseif($instant_message['instant_message_to_type']==0){
  180. Gateway::sendToUid('0:'.$instant_message['instant_message_to_id'], json_encode($instant_message));
  181. }
  182. }catch(\Exception $e){
  183. return ds_callback(false,$e->getMessage());
  184. }
  185. return ds_callback(true);
  186. }
  187. }
  188. ?>