Mindex.php 17.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司,并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author:  hsj      
 * Date: 2015-09-09
 */
namespace app\mchat\controller; 
use think\AjaxPage;
use think\Controller;
use think\Url;
use think\Config;
use think\Page;
use think\Validate;
use think\Verify;
use think\Db;
use think\Cookie;
use qcloudcos\Myqcloudcos;
class Mindex extends Basechat
{
    public $admin_info=[];
    public $savePath="mchat/";
    public $savePath1="temp";
    public function index(){

        $this->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;$i<count($res);$i++)
        {
            $res[$i]['admin_id']=$this->admin_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)<count($uid)) {
             $result=array_diff($uid,$muuarr);
             foreach ($result as $k => $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);
    }

}