如何開發陪玩系統原始碼的列表頁面,相關實現程式碼

雲豹科技程式設計師發表於2021-09-29
在陪玩系統原始碼開發過程中,需要實現的列表頁面還是非常多的,如何保證列表頁面的流暢、整潔很重要,接下來我們一起來看看具體的實現程式碼吧。
route.php
<?php
use think\Route;
Route::get('test', 'api/test/index');
Route::put('test/:id', 'api/test/update');
Route::delete('test/:id', 'api/test/delete');
Route::resource('test', 'api/test');
Route::get('api/:ver/cat', 'api/:ver.cat/read');
Route::get('api/:ver/index', 'api/:ver.index/index');
Route::resource('api/:ver/news', 'api/:ver.news');
News.php(Model)
<?php/**
 * Created by PhpStorm.
 * User: tong
 * Date: 2017/11/20
 * Time: 16:34
 */namespace app\common\model;class News extends Base{
    public function getNews($data = [])
    {
        $data['status'] = [
            'neq', config('code.status_delete'),
        ];
        $order = ['id' => 'desc'];
        $result = $this->where($data)
            ->order($order)
            ->paginate();
        return $result;
    }
    public function getNewsByCondition($condition = [], $from, $size = 5)
    {
        if (!isset($condition['status'])) {
            $condition['status'] = [
                'neq', config('code.status_delete')
            ];
        }
        $order = ['id' => 'desc'];
        $result = $this->where($condition)
            ->field($this->getListField())
            ->limit($from, $size)
            ->order($order)
            ->select();
        return $result;
    }
    public function getNewsByCountCondition($condition = [])
    {
        if (!isset($condition['status'])) {
            $condition['status'] = [
                'neq', config('code.status_delete')];
        }
        return $this->where($condition)
            ->count();
    }
    public function getIndexHadNormalNews($num = 4)
    {
        $data = [
            'status' => 1,
            'is_head_figure' => 1,
        ];
        $order = [
            'id' => 'desc',
        ];
        return $this->where($data)
            ->field($this->getListField())
            ->order($order)
            ->limit($num)
            ->select();
    }
    public function getPositionNormalNews($num = 20)
    {
        $data = [
            'status' => 1,
            'is_position' => 1,
        ];
        $order = [
            'id' => 'desc',
        ];
        return $this->where($data)
            ->field($this->getListField())
            ->order($order)
            ->limit($num)
            ->select();
    }
    private function getListField()
    {
        return [
            'id',
            'catid',
            'image',
            'title',
            'read_count',
            'status',
            'is_position',
            'update_time',
            'create_time',
        ];
    }}

->field($this->getListField())

private function getListField()
    {
        return [
            'id',
            'catid',
            'image',
            'title',
            'read_count',
            'status',
            'is_position',
            'update_time',
            'create_time',
        ];
    }
Common.php
<?php
/**
 * Created by PhpStorm.
 * User: tong
 * Date: 2017/11/23
 * Time: 11:30
 */
namespace app\api\controller;
use app\common\lib\Aes;
use app\common\lib\exception\ApiException;
use app\common\lib\IAuth;
use app\common\lib\Time;
use think\Cache;
use think\Controller;
class Common extends Controller
{
    public $headers = '';
    public $page = 1;
    public $size = 5;
    public $from = 0;
    protected function _initialize()
    {
        $this->checkRequestAuth();
//        $this->testAes();
    }
    public function checkRequestAuth()
    {
        $headers = request()->header();
        if (empty($headers['sign'])) {
            throw new ApiException('sign不存在', 400);
        }
        if (!in_array($headers['app_type'], config('app.apptypes'))) {
            throw new ApiException('app_type不合法', 400);
        }
        if (!IAuth::checkSignPass($headers)) {
            throw new ApiException('授權碼sign失敗', 401);
        }
        Cache::set($headers['sign'], config('app.app_sign_cache_time'));
        $headers = $this->headers;
    }
    public function testAes()
    {
        $data = [
            'did' => '12345dg',
            'version' => 1,
            'time' => Time::get13TimeStamp(),
        ];
//        $str = 'sRCvj52mZ8G+u2OdHYwmysvczmCw+RrAYWiEaXFI/5A=';
//        echo (new Aes())->decrypt($str);
//        exit;
        echo IAuth::setSign($data);
        exit;
    }
    public function getDealNews($news = [])
    {
        if (empty($news)) {
            return [];
        }
        $cats = config('cat.list');
        foreach ($news as $key => $new) {
            $news[$key]['catname'] = $cats[$new['catid']] ? $cats[$new['catid']] : '-';
        }
        return $news;
    }
    public function getPageAndSize($data)
    {
        $this->page = !empty($data['page']) ? $data['page'] : 1;
        $this->size = !empty($data['size']) ? $data['size'] : config('paginate.list_rows');
        $this->from = ($this->page - 1) * $this->size;
    }
}

 public $page = 1;
 public $size = 5;
 public $from = 0;

public function getPageAndSize($data){
     $this->page = !empty($data['page']) ? $data['page'] : 1;
     $this->size = !empty($data['size']) ? $data['size'] : config('paginate.list_rows');
     $this->from = ($this->page - 1) * $this->size;}
News.php(Controller)
<?php/**
 * Created by PhpStorm.
 * User: tong
 * Date: 2017/11/23
 * Time: 17:03
 */namespace app\api\controller\v1;use app\api\controller\Common;class News extends Common{
    public function index()
    {
        //仿照之前講解的validate驗證機制 做相關檢驗
        $data = input('get.');
        $whereData['status'] = config('code.status_normal');
        $whereData['catid'] = input('get.catid');
        $this->getPageAndSize($data);
        $total = model('News')->getNewsByCountCondition($whereData);
        $news = model('News')->getNewsByCondition($whereData, $this->from, $this->size);
        $result = [
            'total' => $total,
            'page_num' => ceil($total / $this->size),
            'list' => $this->getDealNews($news),
        ];
        return show(1, 'OK', $result, 200);
    }}
以上就是“陪玩系統原始碼開發,如何實現列表頁面的開發?”的全部內容,陪玩系統原始碼的開發不是一蹴而就的,在開發完成只有還需要不斷的進行優化和更新,希望本篇內容能為大家開發陪玩系統原始碼提供一些幫助。
本文轉載自網路,轉載僅為分享乾貨知識,如有侵權歡迎聯絡雲豹科技進行刪除處理


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69996194/viewspace-2794436/,如需轉載,請註明出處,否則將追究法律責任。

相關文章