InstantMessage.php 8.9 KB

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