admin_info=$this->sel_admin_info(); if($this->admin_info['is_custom_service']==0) { $this->redirect('/err_mobile.html?msg=该账号无客服权限'); } $this->assign('admin_id',$this->admin_info['admin_id']); $this->assign('host_url', curHostURL()); $this->assign('admin_name',$this->admin_info['ERPName']); $this->assign('store_id',$this->admin_info['store_id']); $this->assign('store_name',$this->admin_info['store_name']); $this->assign('store_logo',$this->admin_info['store_logo']); $this->save_admin_login(); $this->dis_f_man(); return $this->fetch(); } public function testindex(){ $this->admin_info=$this->sel_admin_info(); if($this->admin_info['is_custom_service']==0) { $this->redirect('/err.html?msg=该账号无客服权限'); } $this->assign('admin_id',$this->admin_info['admin_id']); $this->assign('host_url', curHostURL()); $this->assign('admin_name',$this->admin_info['ERPName']); $this->assign('store_id',$this->admin_info['store_id']); $this->assign('store_name',$this->admin_info['store_name']); $this->assign('store_logo',$this->admin_info['store_logo']); $this->save_admin_login(); $this->dis_f_man(); return $this->fetch(); } //聊天框 public function userchat() { $this->admin_info = $this->sel_admin_info(); $getuserid = I('userid'); $where['store_id'] = $this->admin_info['store_id']; $where['user_id'] = $getuserid; $res = M('users')->where($where)->find(); if ($res) { $res_pickup = M('pick_up')->where(array('store_id'=> $this->admin_info['store_id'],'pickup_id'=>$res['pickup_id']))->field('pickup_name')->find(); if ($res_pickup) { $res['pickup_name']=$res_pickup['pickup_name']; } $avg = M('order') -> field('FROM_UNIXTIME(MAX(add_time)) as add_time,SUM(total_amount) as sum,COUNT(1) as count,ROUND(SUM(total_amount)/COUNT(1),2) as avg') -> where('user_id='.$getuserid.' and pay_status=1') -> order('add_time desc') -> select(); $r = array( 'count' => $avg[0]['count'], //总单数 'sum' => $avg[0]['sum'] ? $avg[0]['sum'] : 0 , //总金额 'avg' => $avg[0]['avg'] ? $avg[0]['avg'] : 0 , //每单均价 'last_time' => $avg[0]['add_time'] ? $avg[0]['add_time'] : '无记录', //最后购买时间 ); } $ypath='/public/upload/mchat/temp/'.date('Y') . '/' . date('m-d').'/'; $this->assign("imgpath",$ypath); $this->assign("userinfo",$res); $this->assign("userinfo_count",$r); $this->assign('host_url', curHostURL()); $this->assign('admin_id',$this->admin_info['admin_id']); $this->assign('admin_name',$this->admin_info['ERPName']); $this->assign('store_id',$this->admin_info['store_id']); $this->assign('store_name',$this->admin_info['store_name']); $this->assign('store_logo',$this->admin_info['store_logo']); return $this->fetch(); } //分配未接入的用户 public function dis_f_man() { $where['admin_id']=''; $where['store_id']=$this->admin_info['store_id']; $res=$this->chat_db->name('chat_receive')->where($where)->select(); if(count($res)>0) { for($i=0;$iadmin_info['admin_id']; $res[$i]['admin_ip']=$this->admin_info['admin_id']."111"; $res[$i]['num']=$res[$i]['num']+1; $result=$this->chat_db->name('chat_receive')->update($res[$i]); } } } public function sel_admin_info() { //未继承Base,取不到session('admin_id'),故取cookie $admin_id = cookie("newshop_admin_id"); if (empty($admin_id)) { $this->redirect('/err_mobile.html?msg=未登陆无法获取'); } $r = M('admin') -> alias('a') -> join('store b','a.store_id=b.store_id','left') -> field('a.*,b.store_name,b.store_logo') -> where('a.admin_id = '.$admin_id) -> find(); return $r; } //更新快捷回复 public function save_reply(){ $input=input('post.'); $data['admin_id']=$input['admin_id']; $data['store_id']=$input['store_id']; $data['msg']=$input['msg']; $data['edit_time']=time(); $res=$this->chat_db->name('chat_quick_reply')->where('id',$input['id'])->Update($data); return json($res); } //删除快捷回复 public function del_reply() { $input=input('post.'); $res=$this->chat_db->name('chat_quick_reply')->where('id',$input['id'])->delete(); if($res) { return json(['code'=>1,'msg'=>'删除成功']); }else { return json(['code'=>-1,'msg'=>'删除失败']); } } //新增快捷回复 public function add_reply() { $data=input('post.'); $datas['admin_id']=$data['admin_id']; $datas['store_id']=$data['store_id']; $datas['edit_ip']=$data['admin_id']; $datas['edit_man']=$data['store_id']; $datas['msg']=$data['msg']; $datas['add_time']=time(); $res=$this->chat_db->name('chat_quick_reply')->insertGetId($datas); if($res) { return json(['code'=>1,'msg'=>'新增成功','id'=>$res]); }else { return json(['code'=>-1,'msg'=>'新增失败','']); } } public function out_line(){ $data=input('post.'); $where['admin_id']=$data['admin_id']; $where['store_id']=getAdmStoId(); mlog('s:'.$data['state'],'out_line/'.getAdmStoId()); $update['state']=$data['state']; $res=$this->chat_db->name('chat_login_state')->where($where)->save($update); return json_encode($res); } //保存聊天内容 public function save_msg() { $data=input('post.'); $datas['admin_id']=$data['admin_id']; $datas['store_id']=getAdmStoId();; $datas['chat_type']=$data['chat_type']; $datas['msg_type']=$data['msg_type']; $datas['is_read']=$data['is_read']; $datas['msg']=$data['msg']; $datas['add_time']=time(); if(isset($data['user_id'])) { $datas['user_id']=$data['user_id']; } $res=$this->chat_db->name('chat_msg')->insert($datas); $res_history=$this->chat_db->name('chat_msg_history')->insert($datas); if($res) { return json(['code'=>1,'msg'=>'新增成功','']); }else{ return json(['code'=>-1,'msg'=>'新增失败','']); } } //保存admin_ip public function save_admin_login() { $where['admin_id']=$this->admin_info['admin_id']; $where['store_id']=$this->admin_info['store_id']; //查看客服登录表是否有记录 $is_exit=$this->chat_db->name('chat_login_state')->where($where)->select(); $data['admin_id']=$this->admin_info['admin_id']; $data['store_id']=$this->admin_info['store_id']; $data['admin_ip']=$this->admin_info['admin_id'].'111'; $data['login_time']=time(); $data['state']=0; if($is_exit) { $res=$this->chat_db->name('chat_login_state')->where('id',$is_exit[0]['id'])->Update($data); }else { $res=$this->chat_db->name('chat_login_state')->insert($data); } } //查询快捷回复列表 public function reply_list() { $data = I('Post.'); $where['admin_id'] = $data['admin_id']; $res= $this->chat_db->name('chat_quick_reply')->where($where)->limit(0,6)->select(); return json($res); } //查询历史记录 public function sel_history_list() { $data=input('post.'); $where['user_id'] = $data['user_id']; $where['admin_id'] =$data['admin_id']; $where['msg'] = ['like','%'.$data['keyword'].'%']; if ($data['keyword']!=='%'){ //按关键值搜索时,忽略图片、链接 $where['msg_type'] = ['<>',2]; } if ($data['date']){ $where['DATE_FORMAT(FROM_UNIXTIME(add_time),"%Y-%m-%d")'] = $data['date']; } $count=$this->chat_db->name('chat_msg_history')-> where($where)->order('add_time asc')->count(); $pagecount = $data['pagecount'];//每页条数 $curpage = $data['curpage'];//当前页 $pages = ceil($count/$data['pagecount']);//总页数 $res=$this->chat_db->name('chat_msg_history') -> where($where)->order('add_time asc')->page($curpage,$pagecount)->select(); $r = [ 'data' => $res, 'count' => $count, 'pages' => $pages, ]; return json($r); } //查询最新聊天记录 public function sel_history() { /*--- $data=input('post.');//wxd_chat_msg_history $where['user_id']=$data['user_id']; if ($data['date']){ $where['DATE_FORMAT(FROM_UNIXTIME(add_time),"%Y-%m-%d")'] = $data['date']; } $res=$this->chat_db->name('chat_msg_history')->where($where)->select(); return json_encode($res);--*/ $data=input('post.');//wxd_chat_msg_history $where['user_id']=$data['user_id']; $time=strtotime(date("Y-m-d")); $resdata=$this->chat_db->name('chat_msg_history') ->where($where)->order('add_time desc') ->limit(30)->select(); if($resdata) { $num = count($resdata); if($num>1) { $num=$num-1; $time = $resdata[$num]['add_time']; }else{ return json_encode($resdata); } } //if ($data['date']){ // $where['DATE_FORMAT(FROM_UNIXTIME(add_time),"%Y-%m-%d")'] = $data['date']; //} $res=$this->chat_db->name('chat_msg_history') ->where($where) ->where("add_time>=".$time) ->select(); return json_encode($res); } //商品查询 public function sel_goods_list() { $data=input('post.'); $where['is_on_sale']=1; $where['prom_type']=0; $where['store_count']=['>',0]; $where['store_id']=$data['store_id']; $where1='on_time < now() and (down_time > now() or down_time = 0)'; if(!empty($data['keyword'])) { $kword = urldecode(urldecode(I('keyword/s'))); $where['goods_name']= ['like','%'.$kword.'%']; } $r = M('goods')->field('original_img,shop_price,goods_name,store_id,goods_id,goods_name')->where($where)->where($where1)->select(); return json_encode($r); } public function not_recive() { //$data=input('post.'); //$admin_id=$this->sel_admin_info()['admin_id']; $admin_id=getAdminId(); $where1="admin_id='' or admin_id=0"; $res=$this->chat_db->name('chat_receive')->field('id,user_id') ->where('store_id',getAdmStoId()) ->where($where1)->select(); $msg=$this->chat_db->query('select user_id from wxd_chat_msg where (admin_id=0 or admin_id in(select a.admin_id from wxd_chat_receive a LEFT JOIN wxd_chat_login_state b on a.admin_id=b.admin_id where a.state=1 and b.state>0 and a.store_id='.getAdmStoId().')) and is_read=0 GROUP BY user_id'); $history=$this->chat_db->name('chat_msg_history')->field('id,user_id') ->where('store_id',getAdmStoId()) ->where($where1)->select(); if($res) { $res0['admin_id']=$admin_id; $res0['admin_ip']=$admin_id."111"; $res0['state']=0; $result=$this->chat_db->name('chat_receive')->where($where1)->update($res0); } if($msg) { $msg0['admin_id']=$admin_id; $result=$this->chat_db->name('chat_msg') ->where('store_id',getAdmStoId()) ->where($where1)->update($msg0); //将相对应的客户接到此客户上 $uid=get_arr_column($msg,'user_id'); $res1['state']=0; $res1['admin_id']=$admin_id; $res1['admin_ip']=$admin_id."111"; $result=$this->chat_db->name('chat_receive') ->where('store_id',getAdmStoId()) ->where('user_id','in',$uid)->update($res1); /*--如果会员本身就不在接待表上--*/ $muu=$this->chat_db->name('chat_receive') ->where('store_id',getAdmStoId()) ->where($res1)->where('state',0)->field('user_id')->select(); $muuarr=get_arr_column($muu,'user_id'); if(count($muuarr) $v) { $dis_admin_id['state'] = 0; $dis_admin_id['user_id'] = $v; $dis_admin_id['user_ip'] = $v.'111'; $dis_admin_id['add_time'] = time(); $dis_admin_id['num'] = 1; $dis_admin_id['store_id'] = getAdmStoId(); $dis_admin_id['admin_id'] = $admin_id; $dis_admin_id['admin_ip'] = $admin_id . '111'; $result = $this->chat_db->name('chat_receive')->insert($dis_admin_id); } } } if($history) { $history0['admin_id']=$admin_id; $result=$this->chat_db->name('chat_msg_history') ->where('store_id',getAdmStoId()) ->where($where1)->update($history0); } return "1"; } public function check_state() { $data=input('post.'); $where['user_id']=$data['user_id']; $result=$this->chat_db->name('chat_receive')->field('state,admin_id')->where($where)->find(); if($result['state']==0) { return json(['code'=>1,'msg'=>'接待中','']); } if($result['state']==1) { return json(['code'=>-1,'msg'=>'未接待','']); } } public function update_login_state() { $data=input('post.'); $where['admin_id']=$data['admin_id']; $result['admin_id']=$data['admin_id']; $result['state']=1; $result['login_time']=1213; $result['store_id']=1213; $a=$this->chat_db->name('chat_login_state')->insert($result); $res=$this->chat_db->name('chat_login_state')->where($where)->update($result); return json_encode($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); } }