CacheController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. namespace app\modules\admin\controllers;
  3. use app\common\controllers\BController;
  4. use app\common\components\CacheId;
  5. use app\common\components\FormElements;
  6. use app\modules\cms\models\Category;
  7. use app\modules\cms\models\CategoryTypeRel;
  8. use Yii;
  9. class CacheController extends BController
  10. {
  11. public $layout = 'main';
  12. public $syscacheList;//系统缓存
  13. public function init()
  14. {
  15. parent::init();
  16. $this->syscacheList = array(
  17. array('name'=>'模型','function'=>'_contentModel'),//内容模型
  18. array('name' =>'栏目', 'function' => '_category'),//栏目
  19. array('name' =>'导航', 'function' => '_nav'),//导航
  20. array('name' =>'推荐位', 'function' => '_position'),//推荐位
  21. array('name' =>'联动菜单', 'function' => '_linkmenu'),//联动菜单
  22. array('name' =>'类别', 'function' => '_type'),//内容类别
  23. array('name' =>'用户组', 'function' => '_usergroup'),//用户组
  24. array('name' =>'来源', 'function' => '_copyfrom'),//来源
  25. array('name' =>'站点配置', 'function' => '_siteconfig'),//站点
  26. );
  27. }
  28. //更新系统缓存
  29. public function actionSyscache()
  30. {
  31. if (isset($_POST['docache']) || isset($_GET['docache'])) {
  32. $page = isset($_GET['page']) ? intval($_GET['page']) : 0;
  33. $part = $this->syscacheList[$page];
  34. if($part)
  35. {
  36. $function = $part['function'];
  37. $this->$function();
  38. $result = array('error'=>0,'msg'=>'更新'.$part['name'].'缓存成功..........','data'=>['num'=>($page+1)*12.5]);
  39. }
  40. else
  41. {
  42. $result = array('error'=>0,'msg'=>'更新'.$part['name'].'更新全站缓存成功','data'=>['num'=>100]);
  43. }
  44. echo_json($result);
  45. }
  46. return $this->render('syscache');
  47. }
  48. private function _contentModel()
  49. {
  50. define('FIELDS_PATH', BASE_PATH.'common'.DIRECTORY_SEPARATOR.'widget'.DIRECTORY_SEPARATOR.'fields'.DIRECTORY_SEPARATOR);
  51. $fields = FormElements::getFieldsInfo('types');
  52. //更新内容模型类:表单生成、入库、更新、输出
  53. $class_types = array('form','input','update','output');
  54. foreach($class_types as $class_type) {
  55. $cache_data = file_get_contents(FIELDS_PATH.'content_'.$class_type.'.class.php');
  56. $cache_data = str_replace('}?>','',$cache_data);
  57. foreach($fields as $field=>$field_value) {
  58. if(file_exists(FIELDS_PATH.$field.DIRECTORY_SEPARATOR.$class_type.'.inc.php')) {
  59. $cache_data .= file_get_contents(FIELDS_PATH.$field.DIRECTORY_SEPARATOR.$class_type.'.inc.php');
  60. }
  61. }
  62. $cache_data .= "\r\n } \r\n?>";
  63. file_put_contents(CACHE_PATH.'content_'.$class_type.'.class.php',$cache_data);
  64. }
  65. //更新模型数据缓存
  66. $array = array();
  67. $sql = "SELECT * FROM {{%content_model}} where disabled=0 order by list_order asc";
  68. $datas= Yii::$app->db->createCommand($sql)->queryAll();
  69. foreach ($datas as $r) {
  70. $array[$r['model_id']] = $r;
  71. $this->_contentmodel_field($r['model_id']);
  72. }
  73. Yii::$app->cache->set(CacheId::modelListCacheId(), json_encode($array),0);
  74. return true;
  75. }
  76. private function _category()
  77. {
  78. $categorys = array();
  79. $models = json_decode(Yii::$app->cache->get(CacheId::modelListCacheId()),true);
  80. if(is_array($models))
  81. {
  82. foreach ($models as $model_id=>$model)
  83. {
  84. $sql = "SELECT * FROM {{%category}} where content_model_id=".$model_id." and disabled=0 order by list_order asc";
  85. $datas= Yii::$app->db->createCommand($sql)->queryAll();
  86. $array = array();
  87. foreach ($datas as $r)
  88. {
  89. $parentModel = Category::findOne($r['parent_id']);
  90. if($parentModel)Category::updateParents($r['cat_id']);
  91. Category::updateChilds($r['cat_id']);
  92. Category::updateChilds($r['parent_id']);
  93. Category::updateTypes($r['cat_id']);
  94. if($r['cat_type']==1) $array[$r['cat_id']] = $r;
  95. }
  96. Yii::$app->cache->set(CacheId::categoryItemsCacheId($model_id), json_encode($array),0);
  97. }
  98. }
  99. $sql = "SELECT * FROM {{%category}} where disabled=0 order by list_order ASC";
  100. $categoryResult = Yii::$app->db->createCommand($sql)->queryAll();
  101. if(is_array($categoryResult))
  102. {
  103. foreach($categoryResult as $r) {
  104. $parentModel = Category::findOne($r['parent_id']);
  105. if($parentModel)Category::updateParents($r['cat_id']);
  106. Category::updateChilds($r['cat_id']);
  107. Category::updateChilds($r['parent_id']);
  108. Category::updateTypes($r['cat_id']);
  109. $settings = string2array($r['settings']);
  110. if(is_array($settings))foreach($settings as $kk=>$vv)
  111. {
  112. $r[$kk] = $vv;
  113. }
  114. $r['is_domain'] = '0';
  115. $categorys[$r['cat_id']] = $r;
  116. }
  117. }
  118. Yii::$app->cache->set(CacheId::categoryCacheId(), json_encode($categorys),0);
  119. return true;
  120. }
  121. private function _position()
  122. {
  123. $array = array();
  124. $sql = "SELECT * FROM {{%position}} order by list_order asc";
  125. $lists= Yii::$app->db->createCommand($sql)->queryAll();
  126. if($lists)foreach($lists as $_value) {
  127. $array[$_value['position_id']] = $_value;
  128. }
  129. Yii::$app->cache->set(CacheId::positionCacheId(), json_encode($array),0);
  130. return true;
  131. }
  132. private function _nav()
  133. {
  134. $array = array();
  135. $sql = "SELECT * FROM {{%nav}} where disabled=0 order by list_order asc";
  136. $lists= Yii::$app->db->createCommand($sql)->queryAll();
  137. if($lists)foreach($lists as $_value) {
  138. $array[$_value['type']][] = $_value;
  139. }
  140. Yii::$app->cache->set(CacheId::navCacheId(), json_encode($array),0);
  141. return true;
  142. }
  143. private function _linkmenu()
  144. {
  145. return true;
  146. }
  147. private function _type()
  148. {
  149. $array = array();
  150. $sql = "SELECT * FROM {{%category_type}} where disabled=0 order by list_order asc";
  151. $lists= Yii::$app->db->createCommand($sql)->queryAll();
  152. if(is_array($lists))foreach($lists as $_value) {
  153. $array[$_value['type_id']] = $_value;
  154. }
  155. Yii::$app->cache->set(CacheId::typeCacheId(), json_encode($array),0);
  156. return true;
  157. }
  158. private function _usergroup()
  159. {
  160. //更新模型数据缓存
  161. $array = array();
  162. $sql = "SELECT * FROM {{%user_group}} where disabled=0 order by list_order asc";
  163. $datas= Yii::$app->db->createCommand($sql)->queryAll();
  164. foreach ($datas as $r) {
  165. $array[$r['group_id']] = $r;
  166. }
  167. Yii::$app->cache->set(CacheId::groupCacheId(), json_encode($array),0);
  168. return true;
  169. }
  170. private function _copyfrom()
  171. {
  172. $array = array();
  173. $sql = "SELECT * FROM {{%copy_from}} order by list_order asc";
  174. $datas= Yii::$app->db->createCommand($sql)->queryAll();
  175. if(is_array($datas))foreach ($datas as $r) {
  176. $array[$r['id']] = $r;
  177. }
  178. Yii::$app->cache->set(CacheId::copyFromCacheId(), json_encode($array),0);
  179. return true;
  180. }
  181. private function _siteconfig()
  182. {
  183. return true;
  184. }
  185. //模型字段缓存
  186. private function _contentmodel_field($model_id)
  187. {
  188. $array = array();
  189. $sql = "SELECT * FROM {{%content_model_field}} where disabled=0 and model_id=".$model_id." order by list_order asc";
  190. $fields= Yii::$app->db->createCommand($sql)->queryAll();
  191. foreach($fields as $_value) {
  192. $setting = string2array($_value['setting']);
  193. $_value = array_merge($_value,$setting);
  194. $array[$_value['field']] = $_value;
  195. }
  196. Yii::$app->cache->set(CacheId::modelFieldCacheId($model_id), json_encode($array),0);
  197. return true;
  198. }
  199. //更新栏目子级信息
  200. private function _updateChilds($id)
  201. {
  202. if($id==0)return;
  203. $parentModel = Category::findOne($id);
  204. if($parentModel)
  205. {
  206. $childs = array();
  207. $childs[]=$id;
  208. $categoryList = Category::find()->where('parent_id='.$id)->all();
  209. if(is_array($categoryList))
  210. {
  211. foreach($categoryList as $category)
  212. {
  213. $childs[] = $category['cat_id'];
  214. }
  215. }
  216. if(!empty($childs)&&count($childs)>1)
  217. {
  218. //asort($childs);
  219. $parentModel->arr_child_ids =join(',',$childs);
  220. $parentModel->has_child = 1;
  221. $parentModel->save();
  222. }
  223. else
  224. {
  225. $parentModel->arr_child_ids ='';
  226. $parentModel->has_child = 0;
  227. $parentModel->save();
  228. }
  229. }
  230. }
  231. //更新栏目父级信息
  232. private function _updateParents($id,$parents = array())
  233. {
  234. $model = Category::findOne($id);
  235. if($model)
  236. {
  237. if(empty($parents)){
  238. $parents[] = $id;
  239. if($model->parent_id>0)$parents[]=$model->parent_id;
  240. }
  241. if(count($parents)==1)
  242. {
  243. $model->arr_parent_ids = '';
  244. $model->save();
  245. return;
  246. }
  247. else
  248. {
  249. $parentModel = Category::findOne($parents[count($parents)-1]);
  250. if($parentModel->parent_id==0)
  251. {
  252. $arr_parent_id =','.join(',',$parents).',';
  253. $model->arr_parent_ids = trim($arr_parent_id,',');
  254. $model->save();
  255. return;
  256. }
  257. else
  258. {
  259. $parents[] = $parentModel->parent_id;
  260. return $this->_updateParents($id,$parents);
  261. }
  262. }
  263. }
  264. }
  265. }