<?php 
namespace app\common\model;
use think\Model;
class Menu extends Model
{
	/**
	 * 获取所有
	 */
	public static function getSelect($where=[])
	{
		$auth=self::where($where)->order('id asc')
		->withAttr('icon',function($v,$data){
			if ($v) {
				if ($data['icon_is_img']==2) {
					return config('web.url').$v;
				}else{
					return $v;
				}
			}else return $v;
		})
		->withAttr('title',function($v,$data){
			$config_text=substr_cut($v);
			if ($config_text) {
				return str_replace('{'.$config_text.'}',config($config_text),$v);
			}else{
				return $v;
			}
		})
		->withAttr('stitle',function($v,$data){
			$config_text=substr_cut($v);
			if ($config_text) {
				return str_replace('{'.$config_text.'}',config($config_text),$v);
			}else{
				return $v;
			}
		})
		->withAttr('url',function($v,$data){
			$config_text=substr_cut($v);
			if ($config_text) {
				return str_replace('{'.$config_text.'}',config($config_text),$v);
			}else{
				return $v;
			}
		})->select();
		foreach ($auth as $k => $v) {
			$auth[$k]['hasChildren']=self::where(['pid'=>$v['id']])->count();
		}
		return $auth;
	}
	public static function getCate()
	{
        $cate=self::where(['pid'=>0])->field('id,title')->order('id asc')->select();
        return $cate;
	}
	public static function getCache()
	{
        $cate=self::where(['pid'=>0,'status'=>1])->field('id,title,stitle,cla,url,icon,icon_is_img,alias,target')->order('sort asc,id asc')
			->withAttr('icon',function($v,$data){
				if ($v) {
					if ($data['icon_is_img']==2) {
						return config('web.url').$v;
					}else{
						return $v;
					}
				}
			})
			->withAttr('title',function($v,$data){
				$config_text=substr_cut($v);
				if ($config_text) {
					return str_replace('{'.$config_text.'}',config($config_text),$v);
				}else{
					return $v;
				}
			})
			->withAttr('stitle',function($v,$data){
				$config_text=substr_cut($v);
				if ($config_text) {
					return str_replace('{'.$config_text.'}',config($config_text),$v);
				}else{
					return $v;
				}
			})
			->withAttr('url',function($v,$data){
				$config_text=substr_cut($v);
				if ($config_text) {
					return str_replace('{'.$config_text.'}',config($config_text),$v);
				}else{
					return $v;
				}
			})->select();
        foreach ($cate as $k => $v) {
        	$cate[$k]['data']=self::where(['pid'=>$v['id'],'status'=>1])->field('id,title,stitle,cla,url,icon,icon_is_img,alias,target')->order('sort asc,id asc')
				->withAttr('icon',function($v,$data){
					if ($v) {
						if ($data['icon_is_img']==2) {
							return config('web.url').$v;
						}else{
							return $v;
						}
					}
				})
				->withAttr('title',function($v,$data){
					$config_text=substr_cut($v);
					if ($config_text) {
						return str_replace('{'.$config_text.'}',config($config_text),$v);
					}else{
						return $v;
					}
				})
				->withAttr('stitle',function($v,$data){
					$config_text=substr_cut($v);
					if ($config_text) {
						return str_replace('{'.$config_text.'}',config($config_text),$v);
					}else{
						return $v;
					}
				})
				->withAttr('url',function($v,$data){
					$config_text=substr_cut($v);
					if ($config_text) {
						return str_replace('{'.$config_text.'}',config($config_text),$v);
					}else{
						return $v;
					}
				})->select();
        }
        return $cate;
	}
}