php代码优化---本人的例子

直接上货:

1:数据统计 店铺数量、提现金额、收益金额、用户数量

旧:

// //店铺
        // $storey = db( 'store' )->whereTime( 'addtime', 'yesterday' )->count();
        //昨天
        // $stored = db( 'store' )->whereTime( 'addtime', 'd' )->count();
        //今天
        // $storew = db( 'store' )->whereTime( 'addtime', 'w' )->count();
        //本周
        // $store = db( 'store' )->whereTime( 'addtime', 'm' )->count();
        //本月
        // //收益
        // $ordery = db( 'order' )->whereTime( 'addtime', 'yesterday' )->sum( 'money' );
        //昨天
        // $orderd = db( 'order' )->whereTime( 'addtime', 'd' )->sum( 'money' );
        //今天
        // $orderw = db( 'order' )->whereTime( 'addtime', 'w' )->sum( 'money' );
        //本周
        // $order = db( 'order' )->whereTime( 'addtime', 'm' )->sum( 'money' );
        //本月
        // //提现
        // $deposity = db( 'deposit' )->whereTime( 'addtime', 'yesterday' )->sum( 'money' );
        //昨天
        // $depositd = db( 'deposit' )->whereTime( 'addtime', 'd' )->sum( 'money' );
        //今天
        // $depositw = db( 'deposit' )->whereTime( 'addtime', 'w' )->sum( 'money' );
        //本周
        // $deposit = db( 'deposit' )->whereTime( 'addtime', 'm' )->sum( 'money' );
        //本月
        // //用户
        // $usery = db( 'userinfo' )->whereTime( 'addtime', 'yesterday' )->count();
        //昨天
        // $userd = db( 'userinfo' )->whereTime( 'addtime', 'd' )->count();
        //今天
        // $userw = db( 'userinfo' )->whereTime( 'addtime', 'w' )->count();
        //本周
        // $user = db( 'userinfo' )->whereTime( 'addtime', 'm' )->count();
        //本月

新:

$db = null;
        $data = [];
        for ( $i = 0; $i < 4; $i++ ) {
            if ( $i<3 ) {
                if ( $i == 0 ) {
                    $db = 'store';
                } else {
                    $db = 'userinfo';
                }
                $storey = db( $db )->whereTime( 'addtime', 'yesterday' )->count();
                //昨天
                $stored = db( $db )->whereTime( 'addtime', 'd' )->count();
                //今天
                $storew = db( $db )->whereTime( 'addtime', 'w' )->count();
                //本周
                $store = db( $db )->whereTime( 'addtime', 'm' )->count();
                //本月
                $data[ $i ] = [
                    'storey'=>$storey,
                    'stored'=>$stored,
                    'storew'=>$storew,
                    'store'=>$store
                ];

            } else {
                if ( $i == 2 ) {
                    $db = 'order';
                } else {
                    $db = 'deposit';
                }
                $storey = db( $db )->whereTime( 'addtime', 'yesterday' )->sum( 'money' );
                //昨天
                $stored = db( $db )->whereTime( 'addtime', 'd' )->sum( 'money' );
                //今天
                $storew = db( $db )->whereTime( 'addtime', 'w' )->sum( 'money' );
                //本周
                $store = db( $db )->whereTime( 'addtime', 'm' )->sum( 'money' );
                //本月
                $data[ $i ] = [
                    'storey'=>$storey,
                    'stored'=>$stored,
                    'storew'=>$storew,
                    'store'=>$store
                ];
            }

        }

2:where语句 查询token及过期时间

旧:

$list = db('userinfo')
           ->where('token',$tokens)
           ->where('pastdue','>=',$datetime)
           ->count();

新:

$list = db('userinfo')->where("token = '$tokens' and pastdue > '$datetime'")->count();

3:token验证

旧:

 if (!$this->auth->match($this->noNeedLogin)) {
            //初始化
            $this->auth->init($token);
           //检测是否登录
           if (!$this->auth->isLogin()) {
               $this->error(__('Please login first'), null, 401);
          }
          // 判断是否需要验证权限
          if (!$this->auth->match($this->noNeedRight)) {
             // 判断控制器和方法判断是否有对应权限
            if (!$this->auth->check($path)) {
               $this->error(__('You have no permission'), null, 403);
                 }
           }
        } else {
           // 如果有传递token才验证是否登录状态
            if ($token) {
                $this->auth->init($token);
           }
         }

新:

$date = ['imgfile/files','imgfile/list','sekhlp/index','userinfo/index','screen/index','storetype/index','store/index','common/upload'];
        $type = 1;
        foreach ($date as $k){
            if($k==$path){
                $type = 1;
                break;
            }else{
                $type = 0;
            }
        }
        if($type!==1){}

你可能感兴趣的:(php,php,开发语言)