where("user_id", $user['user_id'])->where("store_id", $sid)->find(); session('user', $user); //覆盖session 中的 user $this->user = $user; $this->user_id = $user['user_id']; $this->assign('user', $user); //存储用户信息 mlog('56565','Mbchat/'.getMobileStoId()); } else { mlog('78787878','Mbchat/'.getMobileStoId()); $usrid=Cookie::get('user_id'); if($usrid){ $user = M('users')->where("user_id", $usrid)->where("store_id", $sid)->find(); session('user', $user); //覆盖session 中的 user $this->user = $user; $this->user_id = $user['user_id']; $this->assign('user', $user); //存储用户信息 } } if (!$this->user_id ) { $this->redirect(U('mobile/User/login', array('stoid' => getMobileStoId()))); //$this->redirect(U('mobile/User/login', array('stoid' => getMobileStoId());); exit; } $this->chat_db=Db::connect('chat_db'); } private $sub_name = array('date', 'Y/m-d'); private $savePath = 'temp/'; private $savePath1 = ''; //保存聊天内容 public function save_msg() { $data=input('post.'); $data['admin_id']=$data['admin_id']; $data['store_id']=getMobileStoId(); $data['chat_type']=$data['chat_type']; $data['msg_type']=$data['msg_type']; $data['msg']=$data['msg']; $data['add_time']=time(); $res=$this->chat_db->name('chat_msg')->insert($data); $res_history=$this->chat_db->name('chat_msg_history')->insert($data); return $res; } public function save_image() { $file = request()->file('imgfile'); $result = $this->validate( ['file2' => $file], ['file2'=>'image','file2'=>'fileSize:10000000000'], ['file2.image' => '上传文件必须为图片','file2.fileSize' => '上传文件过大'] ); if(true !== $result){ return json($result); } else { // 移动到框架应用根目录/public/uploads/ 目录下 if (empty($this->savePath1)) $this->savePath = $this->savePath . date('Y') . '/' . date('m-d') . '/'; else $this->savePath = $this->savePath . $this->savePath1 . '/' . date('Y') . '/' . date('m-d') . '/'; $info = $file // ->rule(function ($file) { // $saveFileName = md5(mt_rand()); // return $saveFileName; // 使用自定义的文件保存规则 // }) ->move('public/upload/' . $this->savePath); if ($info) { $saveFileName = $this->savePath . $info->getSaveName(); $upfilepath = ROOT_PATH . "public/upload/" . $saveFileName; //保存到腾讯云 vendor ('qcloudcos.myqcloudcos'); $resfolder=Myqcloudcos::statFolder('wxd',UPLOAD_PATH. $this->savePath); if ($resfolder && $resfolder['code']!=0){//不存在创建 Myqcloudcos::createFolder('wxd',UPLOAD_PATH. $this->savePath); } $saveFileName=$this->savePath.$info->getSaveName(); m_cut_img(basename($saveFileName),ROOT_PATH.'/public/upload/'.dirname($saveFileName).'/'); //上传到腾讯云 $localpath=ROOT_PATH.'/'.UPLOAD_PATH.dirname($saveFileName).'/'.basename($saveFileName); $ypath='/'.UPLOAD_PATH.'mchat/'.dirname($saveFileName).'/'.basename($saveFileName); $res=Myqcloudcos::upload('wxd',$localpath,$ypath); if ($res && $res['code']==0)//成功 { // mdelFile($localpath); } return json(['code'=>1,'msg'=>'上传成功','localpath'=>$localpath,'ypath'=>$ypath]); } else { return json(['code'=>-1,'msg'=>$file->getError()]); } } } public function del_image(){ $url = I('url'); mdelFile($url); } private $ybpath='wxd'; public function save_img() { // //上传到腾讯云 // $localpath=ROOT_PATH.'/'.UPLOAD_PATH.dirname($saveFileName).'/'.basename($saveFileName); // $ypath='/'.UPLOAD_PATH.'mchat/'.dirname($saveFileName).'/'.basename($saveFileName); $contents = I('POST.contents'); $uploadpath='/'.UPLOAD_PATH.'/mchat/mbchat/'; $ypath='/'.UPLOAD_PATH.'/mchat/mbchat/'.date('Ym',time()).'/'; $img = base64_decode($contents); if (!file_exists(ROOT_PATH.$uploadpath)){ mkdir($uploadpath); } $simg=time(); $imgname=$uploadpath.$simg; $a = file_put_contents($imgname, $img);//返回的是字节数 //生成小图 m_cut_img($simg,ROOT_PATH .$uploadpath, 100, 1); //保存到存储云 vendor('qcloudcos.myqcloudcos'); $resfolder=Myqcloudcos::statFolder($this->ybpath,$ypath); if ($resfolder && $resfolder['code']!=0){ //不存在创建 Myqcloudcos::createFolder($this->ybpath,$ypath); } $res=Myqcloudcos::upload($this->ybpath,ROOT_PATH .$uploadpath.$simg,$ypath.$simg); if ($res && $res['code']==0){//成功 //删除本地 mdelFile(ROOT_PATH . $uploadpath . $simg); // return $simg; return json(['code'=>1,'msg'=>'上传成功','localpath'=>ROOT_PATH .$uploadpath.$simg,'ypath'=>$ypath.$simg]); } else { return json(['code'=>-1,'msg'=>'上传失败']); } } public function test(){ return $this->fetch(); } public function index(){ //这两个缓存都得删除 暂时应用于分配方法中 $user_id= $this->user_id; $user = $this->user; $stoid = I('stoid'); $goods_id = I('goods_id/d',0); $sto_log=tpCache('shop_info',$stoid); if ($goods_id) { $goods = M('goods')->where("goods_id", $goods_id)->where("store_id", $stoid)->find(); $this->assign('goods',$goods); $this->assign('goods_id', $goods_id); } //查询该会员是否在线聊天 $where['user_id']= $user_id; $is_online=$this->chat_db->name('chat_receive')->where($where)->find(); if(isset($is_online['state'])) { if($is_online['state']!=0) { $this->assign('is_online','1'); } }else { $this->assign('is_online','1'); } //商家在getinfo设置了定值记得修改 // $this->assign('admin_id',$this->admin_info['admin_id']); $user['part_time']=time(); $this->assign('user_id', $user_id); $this->assign('store_logo', $sto_log['store_logo']); $this->assign('user',json_encode($user)); $this->assign('head_pic',$user['head_pic']); $this->assign('nickname',$user['nickname']); $this->assign('stoid', $stoid); return $this->fetch(); } public function dis_online_man() { $data=input('post.'); $stoid=I('stoid'); $user_id=$data['user_id']; $get_ip=$data['user_ip']; //$where['user_id']=session('user_id') mlog('00'.$data['user_id'],'dis_online_man/'.$stoid); $where['user_id']=$user_id; $where['store_id']=$stoid; $res=$this->chat_db->name('chat_receive')->where($where)->find(); //是否存在于接待表 if(!empty($res)) { mlog('11'.$data['user_id'],'dis_online_man/'.$stoid); //判断admin_id是否为空 if(!empty($res['admin_id'])&&$res['state']==0) { return $res; //不需要分配 直接返回admin_id // $dis_admin_id['admin_id'] =$res['admin_id']; // $dis_admin_id['admin_ip'] =$res['admin_ip']; } if(empty($res['admin_id'])||$res['state']=1) { $help = new \app\mchat\controller\Getinfo(); $get_admin_id = $help -> user_to_admin($stoid); //分配在线客服 if(!empty($get_admin_id)) { //确保admin是同一个stoid $admin_f = M('admin') ->where("admin_id", $get_admin_id['admin_id']) ->where("store_id", $stoid) ->field('admin_id') ->find(); if($admin_f) { $dis_admin_id['admin_id'] = $get_admin_id['admin_id']; $dis_admin_id['admin_ip'] = $get_admin_id['admin_id'] . '111'; $dis_admin_id['state'] = 0; $dis_admin_id['num'] = $res['num'] + 1; $dis_admin_id['add_time'] = time(); $dis_admin_id['store_id'] = $stoid; //更新接待表和聊天记录表 $result = $this->chat_db->name('chat_receive')->where('user_id=' . $user_id . '') ->where('store_id', $stoid)->update($dis_admin_id); $result = $this->chat_db->name('chat_msg')->where('user_id=' . $user_id . '')->where('is_read=0') ->where('store_id', $stoid)->save(['admin_id' => $get_admin_id['admin_id']]); } }else{ // 没有客服登录 怎么办 } } }else { mlog('22'.$data['user_id'],'dis_online_man/'.$stoid); $help = new \app\mchat\controller\Getinfo(); $get_admin_id = $help -> user_to_admin($stoid); $dis_admin_id['state']=0; $dis_admin_id['user_id']=$user_id; $dis_admin_id['user_ip']=$get_ip; $dis_admin_id['add_time']=time(); $dis_admin_id['num']=1; $dis_admin_id['store_id']=$stoid; if(!empty($get_admin_id)) { $dis_admin_id['admin_id']=$get_admin_id['admin_id']; $dis_admin_id['admin_ip']=$get_admin_id['admin_id'].'111'; }else{ $dis_admin_id['admin_id']=''; $dis_admin_id['admin_ip']=''; } $result=$this->chat_db->name('chat_receive')->insert($dis_admin_id); } $return_data['admin_id']=$dis_admin_id['admin_id']; $return_data['admin_ip']=$dis_admin_id['admin_id'].'111'; return $return_data; } //分配 public function dis_admin_id($user_id='',$user_ip='',$type='',$num='') { $help = new \app\mchat\controller\Getinfo(); $res = $help -> user_to_admin(); if(isset($res['admin_id'])) { $dis_admin_id['admin_id']=$res['admin_id']; $dis_admin_id['admin_ip']=$res['admin_ip']; }else{ // $online_man=$this->chat_db->query('select * ,max(id) as id from wxd_chat_login_state where state=0 and login_timeout>UNIX_TIMESTAMP()'); $where['state']=0; $where['login_timeout']=['>',time()]; $where['admin_ip']=['!=','']; $online_man=$this->chat_db->name('chat_login_state')->where($where)->order('login_time desc')->find(); $dis_admin_id['admin_id']=$online_man['admin_id']; $dis_admin_id['admin_ip']=$online_man['admin_ip']; } $dis_admin_id['user_id']=$user_id; $dis_admin_id['user_ip']=$user_ip; $dis_admin_id['state']=0; $dis_admin_id['add_time']=time(); //$dis_admin_id['session_id']=uuid(); $dis_admin_id['msg_uc']=1; //历史有接待过的 if(!empty($type)) { $dis_admin_id['num']=$num+1; $result=$this->chat_db->name('chat_receive')->where('user_id='.$user_id.'')->update($dis_admin_id); }else{ $dis_admin_id['num']=1; $result=$this->chat_db->name('chat_receive')->insert($dis_admin_id); } $data['admin_ip']=$dis_admin_id['admin_ip']; $data['admin_id']=$dis_admin_id['admin_id']; return $data; } public function sel_history_list() { $data=input('post.'); // $where['admin_id']=$data['admin_id']; $where['user_id']=$data['user_id']; //$where['add_time']=$data['date']; //var_dump($were);die; $result=$this->chat_db->name('chat_msg_history')->where($where)->select(); return json_encode($result); } //判断是否在线 public function is_online(){ $where['admin_id']=I('tadmin_id'); $where['store_id']=I('stoid'); $where['state']=0; $rs=$this->chat_db->name('chat_login_state')->where($where)->find(); if($rs){ return json(['ison'=>1]); }else{ return json(['ison'=>0]); } } }