1. 程式人生 > >懶人載入,點選載入更多

懶人載入,點選載入更多

//js  
 //點選載入更多
    var page = 1;
    $(".load_more").on('click',function (){
        var thisID = $('.layui-this').attr("lay-id");
        setTimeout(function () {
                page++;
                $.ajax({
                    url: "/user/server/client_uid",
                    data: { client_uid: thisID, page: page,type:1},
                    success: function (data) {
                        var data = JSON.parse(data);
                        layui.each(data, function(index, item){
                            if(item.type==2){
                                $(".c_boom").prepend('<div class="c_dialogue">' +
                                '<img style="border-radius:50%" width="50px" height="50px" src="' + item.img + '" class="c_sendimg" alt="">' +
                                ' <span class="c_dialogueName">' + item.speaker + '</span>' +
                                '<span class="c_dialogueTime fr">' + item.chatdate + '</span>' +
                                '<div class="c_dialogueText"><img width="200px" height="auto" src="' + item.imgPath + '"></div>' +
                                '</div>');
                            }else{
                                $(".c_boom").prepend('<div class="c_dialogue">' +
                                '<img style="border-radius:50%" width="50px" height="50px" src="' + item.img + '" class="c_sendimg" alt="">' +
                                ' <span class="c_dialogueName">' + item.speaker + '</span>' +
                                '<span class="c_dialogueTime fr">' + item.chatdate + '</span>' +
                                '<div class="c_dialogueText"> ' + item.speech + '</div>' +
                                '</div>');
                            }
                            
                        });
                    }           
                })
        }, 300);
    })

//後臺接收

 public function client_uidOp() { 
        
          $type = input('type');
          $limit = input('limit',20);
          $status = input('status'); 
          $client_uid = input('client_uid'); 
          if(!$client_uid){
              return false; 
          }
          //將遊客uid存入session 
          session('s_to_c_uid',$client_uid); 
          $server_uid = session('server_uid');
          $date = strtotime("-1 day"); 
          //客服對遊客說的話 
          $data = model('chat')->whereOr(['listener'=>$client_uid , 'speaker'=>$client_uid])->where(['chatdate'=>['>',$date]])->order('chatdate desc')->paginate($limit, false, [
              'query' => request()->param(),
          ])->toArray();
          $stoc=$data['data'];
          if($type != 1){
              array_multisort(array_column($stoc,'chatdate'),SORT_ASC,$stoc);
          }
          //客服的 username 
          $servername = model('server')->where(['uid'=>$server_uid])->value('username'); 
          //客服頭像資訊 
          $img = $this->imgBase64(USER_HEAD_PATH.$this->img_path($server_uid)); 
          if($status == ''){
               //遊客頭像
              $data = model('client')->where(['uid'=>$client_uid])->column('username,img'); 
              $imgs= '';
              foreach($data as $k => $v) {
                  $imgs  =  $v;
                  $clientname = $k ;
              }
              if ($imgs) {
                  $imgs = $this->imgBase64(USER_HEAD_PATH.$imgs); 
              } else {
                  $imgs = model('conf')->where(['sysmark'=>'tourists_head'])->value('image'); 
              }
          }else {
              $clientname = model('server')->where(['uid'=>$client_uid])->value('username'); 
              //客服頭像資訊 
              $imgs = $this->imgBase64(USER_HEAD_PATH.$this->img_path($client_uid));  
          }
         
          //speaker 從 uid 改為 usernamer 
          foreach($stoc as $key => $row) { 
              if($stoc[$key]['speaker'] == $client_uid) {
                  $stoc[$key]['speaker'] = $clientname; 
                  $stoc[$key]['img'] = $imgs; 
              }else{
                  $stoc[$key]['speaker'] = $servername; 
                  $stoc[$key]['img'] = $img; 
              }
              $stoc[$key]['type'] = $row['type']; 
              if ($row['type'] == 2) { 
                  $stoc[$key]['speech'] = $this->imgBase64(UPLOADS_PATH.$row['speech']); 
              } 
              $stoc[$key]['chatdate'] = date('Y-m-d H:i:s', $row['chatdate']); 
            
          } 
          return json_encode($stoc); 
      }