areacache.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. final class areacache {
  3. public static function getCache($type, $param = "") {
  4. $type = strtoupper($type[0]) . strtolower(substr($type, 1));
  5. $function = "get" . $type . "Cache";
  6. try {
  7. do {
  8. if (method_exists(areacache, $function)) {
  9. break;
  10. } else {
  11. $error = lang('please_check_your_cache_type');
  12. throw new Exception($error);
  13. }
  14. } while (0);
  15. } catch (Exception $e) {
  16. Exception($e->getMessage(), "", "exception");
  17. }
  18. $result = self::$function($param);
  19. return $result;
  20. }
  21. private static function getAreaCache($param) {
  22. $deep = $param['deep'];
  23. $cache_file = ROOT_PATH . "extend" . DIRECTORY_SEPARATOR . "area" . DIRECTORY_SEPARATOR . "area_" . $deep . ".php";
  24. if (file_exists($cache_file) && empty($param['new'])) {
  25. require ($cache_file);
  26. return $data;
  27. }
  28. $where = array('area_deep' => $deep);
  29. $order = "area_sort asc";
  30. $area_mod = model('area');
  31. $result = $area_mod->getAreaList($where, '*', '', 0, $order);
  32. $tmp = "<?php \r\n";
  33. $tmp.= "\$data = array(\r\n";
  34. if (is_array($result)) {
  35. foreach ($result as $k => $v) {
  36. $tmp.= "\tarray(\r\n";
  37. $tmp.= "\t\t'area_id'=>'" . $v['area_id'] . "',\r\n";
  38. $tmp.= "\t\t'area_name'=>'" . htmlspecialchars($v['area_name']) . "',\r\n";
  39. $tmp.= "\t\t'area_region'=>'" . htmlspecialchars($v['area_region']) . "',\r\n";
  40. $tmp.= "\t\t'area_parent_id'=>'" . $v['area_parent_id'] . "',\r\n";
  41. $tmp.= "\t\t'area_sort'=>'" . $v['area_sort'] . "',\r\n";
  42. $tmp.= "\t\t'area_deep'=>'" . $v['area_deep'] . "',\r\n";
  43. $tmp.= "\t),\r\n";
  44. }
  45. }
  46. $tmp.= ");";
  47. try {
  48. $fp = @fopen($cache_file, "wb+");
  49. if (fwrite($fp, $tmp) === FALSE) {
  50. $error = lang('please_check_your_system_chmod_area');
  51. throw new Exception();
  52. }
  53. @fclose($fp);
  54. require ($cache_file);
  55. return $data;
  56. } catch (Exception $e) {
  57. exception($e->getMessage(), "", "exception");
  58. }
  59. }
  60. public static function makeallcache($type) {
  61. switch ($type) {
  62. case "area":
  63. $file_list = read_file_list(ROOT_PATH . "extend" . DIRECTORY_SEPARATOR . "area");
  64. if (is_array($file_list)) {
  65. foreach ($file_list as $v) {
  66. @unlink(ROOT_PATH . "extend" . DIRECTORY_SEPARATOR . "area" . DIRECTORY_SEPARATOR . $v);
  67. }
  68. }
  69. $maxdeep = 1;
  70. default:
  71. $where = array('area_deep' => $maxdeep);
  72. $order = "area_sort asc";
  73. $area_mod = model('area');
  74. $result = $area_mod->getAreaList($where, '*', '', 0, $order);
  75. if (!empty($result)) {
  76. $cache_file_area = ROOT_PATH . "extend" . DIRECTORY_SEPARATOR . "area" . DIRECTORY_SEPARATOR . "area_" . $maxdeep . ".php";
  77. $tmp = "<?php \r\n";
  78. $tmp.= "\$data = array(\r\n";
  79. if (is_array($result)) {
  80. foreach ($result as $k => $v) {
  81. $tmp.= "\tarray(\r\n";
  82. $tmp.= "\t\t'area_id'=>'" . $v['area_id'] . "',\r\n";
  83. $tmp.= "\t\t'area_name'=>'" . htmlspecialchars($v['area_name']) . "',\r\n";
  84. $tmp.= "\t\t'area_region'=>'" . htmlspecialchars($v['area_region']) . "',\r\n";
  85. $tmp.= "\t\t'area_parent_id'=>'" . $v['area_parent_id'] . "',\r\n";
  86. $tmp.= "\t\t'area_sort'=>'" . $v['area_sort'] . "',\r\n";
  87. $tmp.= "\t\t'area_deep'=>'" . $v['area_deep'] . "',\r\n";
  88. $tmp.= "\t),\r\n";
  89. }
  90. }
  91. $tmp.= ");";
  92. try {
  93. $fp = @fopen($cache_file_area, "wb+");
  94. if (fwrite($fp, $tmp) === FALSE) {
  95. $error = lang('please_check_your_system_chmod_area');
  96. throw new Exception();
  97. }
  98. unset($tmp);
  99. @fclose($fp);
  100. } catch (Exception $e) {
  101. exception($e->getMessage(), "", "exception");
  102. }
  103. }
  104. ++$maxdeep;
  105. }
  106. }
  107. function del_DirAndFile($dirName) {
  108. if (is_dir($dirName)) {
  109. if ($handle = opendir("$dirName")) {
  110. while (false !== ( $item = readdir($handle) )) {
  111. if ($item != "." && $item != "..") {
  112. if (is_dir("$dirName/$item")) {
  113. del_DirAndFile("$dirName/$item");
  114. } else {
  115. unlink("$dirName/$item");
  116. }
  117. }
  118. }
  119. closedir($handle);
  120. rmdir($dirName);
  121. }
  122. }
  123. }
  124. public static function deleteCacheFile() {
  125. $dirName = ROOT_PATH . "extend" . DIRECTORY_SEPARATOR . "area";
  126. if (is_dir($dirName)) {
  127. if ($handle = opendir("$dirName")) {
  128. while (false !== ( $item = readdir($handle) )) {
  129. if ($item != "." && $item != "..") {
  130. if (is_dir("$dirName/$item")) {
  131. del_DirAndFile("$dirName/$item");
  132. } else {
  133. unlink("$dirName/$item");
  134. }
  135. }
  136. }
  137. closedir($handle);
  138. }
  139. }
  140. }
  141. //自动更新“area_datas.js”
  142. public static function updateAreaArrayJs() {
  143. $cache_file = PUBLIC_PATH . DIRECTORY_SEPARATOR . "static" . DIRECTORY_SEPARATOR . "plugins" . DIRECTORY_SEPARATOR . "area_datas.js";
  144. $field = "area_parent_id";
  145. $order = "area_parent_id ASC";
  146. $group = "area_parent_id";
  147. $area_mod = model('area');
  148. $result = $area_mod->getAreaList(array(), $field, $group, 0, $order);
  149. $tmp = "ds_a = new Array();\n";
  150. if (is_array($result)) {
  151. foreach ($result as $k => $v) {
  152. $tmp.="ds_a[" . $v['area_parent_id'] . "]=[";
  153. $tmp_sub = "";
  154. //子地区
  155. $where = "area_parent_id = '" . $v['area_parent_id'] . "'";
  156. $order = "area_sort ASC,area_id ASC";
  157. $result_sub = $area_mod->getAreaList($where, '*', '', 0, $order);
  158. if (is_array($result_sub)) {
  159. foreach ($result_sub as $k_sub => $v_sub) {
  160. if ($tmp_sub == "") {
  161. $tmp_sub = "['" . $v_sub['area_id'] . "','" . $v_sub['area_name'] . "']";
  162. } else {
  163. $tmp_sub .= ",['" . $v_sub['area_id'] . "','" . $v_sub['area_name'] . "']";
  164. }
  165. }
  166. }
  167. $tmp.=$tmp_sub;
  168. $tmp.="];\n";
  169. }
  170. }
  171. $fp = fopen($cache_file, "wb+");
  172. fwrite($fp, $tmp);
  173. fclose($fp);
  174. }
  175. //自动更新“area_datas.php”
  176. public static function updateAreaPhp() {
  177. $cache_file = PUBLIC_PATH . DIRECTORY_SEPARATOR . "static" . DIRECTORY_SEPARATOR . "plugins" . DIRECTORY_SEPARATOR . "area_datas.php";
  178. $order = "area_deep asc ,area_sort ASC,area_id ASC";
  179. $area_mod = model('area');
  180. $result = $area_mod->getAreaList(array(), '*', '', 0, $order);
  181. $tmp = "<?php \r\n";
  182. $tmp.= "\$area_array = array(\r\n";
  183. if (is_array($result)) {
  184. foreach ($result as $k => $v) {
  185. $tmp.= "\t" . $v['area_id'] . " => array ( 'area_name' => '" . $v['area_name'] . "', 'area_parent_id' => '" . $v['area_parent_id'] . "', ),";
  186. }
  187. }
  188. $tmp.= ");";
  189. $fp = fopen($cache_file, "wb+");
  190. fwrite($fp, $tmp);
  191. fclose($fp);
  192. }
  193. }
  194. ?>