12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <?php
- /**
- * Created by PhpStorm.
- * User: xiaofeng
- * Date: 2018/10/7
- * Time: 5:55 PM
- */
- namespace common\api;
- use common\models\Characteristic;
- use Yii;
- class Label{
- //特色主题
- public function Characteristic($id,$num = 3)
- {
- $cache = Yii::$app->cache;
- // $cache->delete(Yii::$app->params['cacheKey']['characteristic']);
- $cacheData = $cache->get(Yii::$app->params['cacheKey']['characteristic']);
- if(!$cacheData)
- {
- $char = \common\models\Characteristic::find();
- $char->andWhere(['del'=>1]);
- $char->andWhere(['is_show'=>1]);
- $data = $char->asArray()->all();
- if(!empty($data))
- {
- $cacheData = array_column($data,'name','id');
- $cache->set(Yii::$app->params['cacheKey']['characteristic'],$cacheData);
- }
- }
- $resultData = [];
- if(is_array($id))
- {
- $charData = [];
- foreach ($cacheData as $key=>$val)
- {
- if(in_array($key,$id))
- {
- $charData[$key] = $val;
- }
- }
- if(!empty($charData) && count($charData)>$num)
- {
- $rand = array_rand($charData,3);
- $resultData[] = $cacheData[$rand[0]];
- $resultData[] = $cacheData[$rand[1]];
- $resultData[] = $cacheData[$rand[2]];
- }
- else
- {
- $resultData = $charData;
- }
- }
- return $resultData;
- }
- }
|