category_helper.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/5/9
  6. * Time: 下午9:06
  7. */
  8. require_once(BASE_ROOT_PATH . '/helper/img_helper.php');
  9. class category_helper
  10. {
  11. const img_path = '/mobile/category';
  12. private $mCategories = NULL;
  13. private $mCids = NULL;
  14. static private $stInstance = NULL;
  15. static public function instance()
  16. {
  17. if(self::$stInstance == NULL) {
  18. self::$stInstance = new category_helper();
  19. }
  20. return self::$stInstance;
  21. }
  22. private function __construct()
  23. {
  24. $this->mCids = array();
  25. self::load_xml();
  26. }
  27. private function load_xml()
  28. {
  29. libxml_use_internal_errors(true);
  30. $xml = simplexml_load_string($this->content());
  31. if($xml === false) {
  32. $err_msg = json_encode(libxml_get_errors(),JSON_UNESCAPED_UNICODE);
  33. Log::record("{$err_msg}", Log::ERR);
  34. libxml_clear_errors();
  35. throw new Exception("无法解析XML.");
  36. }
  37. $url = RESOURCE_SITE_URL . self::img_path;
  38. $this->mCategories = array();
  39. foreach($xml->children() as $xitem)
  40. {
  41. $item = array();
  42. $attrs = $xitem->attributes();
  43. $title = $attrs['title']->__toString();
  44. $pid = $attrs['id']->__toString();
  45. $cids = $attrs['cids']->__toString();
  46. $img = $attrs['img']->__toString();
  47. $item['name'] = $title;
  48. $item['hot_id'] = $pid;
  49. $item['img'] = $url . '/' . $img;
  50. $item['subitem'] = array();
  51. $this->add_cids($pid,$cids);
  52. foreach($xitem->children() as $xsubitem) {
  53. $subitem = array();
  54. $sattrs = $xsubitem->attributes();
  55. $title = $sattrs['title']->__toString();
  56. $sid = $sattrs['id']->__toString();
  57. $cids = $sattrs['cids']->__toString();
  58. $img = $sattrs['img']->__toString();
  59. $subitem['name'] = $title;
  60. $subitem['hot_id'] = $sid;
  61. $subitem['img'] = $url . '/' . $img;
  62. array_push($item['subitem'],$subitem);
  63. $this->add_cids($sid,$cids);
  64. $this->add_cids($pid,$cids);
  65. }
  66. array_push($this->mCategories,$item);
  67. }
  68. }
  69. private function add_cids($id,$cids)
  70. {
  71. if(!array_key_exists($id,$this->mCids)) {
  72. $this->mCids[$id] = array();
  73. }
  74. $cids = explode(',',$cids);
  75. foreach($cids as $val)
  76. {
  77. $cur_cids = &$this->mCids[$id];
  78. if(!empty($val) && !in_array($val,$cur_cids)) {
  79. array_push($cur_cids,$val);
  80. }
  81. }
  82. }
  83. public function categories()
  84. {
  85. try {
  86. return $this->mCategories;
  87. } catch (Exception $ex) {
  88. return array();
  89. }
  90. }
  91. public function cids($id)
  92. {
  93. try
  94. {
  95. if(!array_key_exists($id,$this->mCids)) {
  96. return array();
  97. } else {
  98. return $this->mCids[$id];
  99. }
  100. } catch (Exception $ex) {
  101. return array();
  102. }
  103. }
  104. private function content()
  105. {
  106. $content = <<<EOD
  107. <?xml version="1.0" encoding="utf-8"?>
  108. <category>
  109. <item title="护肤" id ="1" cids="" img="hf.png">
  110. <subitem title="洁面" id ="10" cids="50024975,50011977,50448025" img="jm.png" />
  111. <subitem title="化妆水" id ="11" cids="50012002,50011978,121368014" img="hzs.png" />
  112. <subitem title="面部精华" id ="12" cids="50011979,121756003" img="mbjh.png" />
  113. <subitem title="乳液/面霜" id ="13" cids="50011996,50011980,121410035" img="ry.png" />
  114. <subitem title="眼霜" id ="14" cids="121410013,121408040,121422013,121454013,121472009,121484013,121848006" img="ys.png" />
  115. <subitem title="面膜" id ="15" cids="121390006,121366015,121426033,121454014,121460030,121468012,121474010" img="mm.png" />
  116. <subitem title="防晒/隔离" id ="16" cids="121426007,50011982,121434025,121458034" img="fs.png" />
  117. <subitem title="去角质" id ="17" cids="121384013,50011997,121408009" img="qjz.png" />
  118. </item>
  119. <item title="彩妆" id ="2" cids="" img="cz.png">
  120. <subitem title="卸妆" id ="20" cids="50011990" img="xz.png" />
  121. <subitem title="BB霜" id ="21" cids="50013794" img="bbs.png" />
  122. <subitem title="粉底液" id ="22" cids="50010789" img="fdy.png" />
  123. <subitem title="散粉" id ="23" cids="50010792" img="sf.png" />
  124. <subitem title="粉饼" id ="24" cids="5001079,50010792" img="fb.png" />
  125. <subitem title="腮红" id ="25" cids="50010805" img="sh.png" />
  126. <subitem title="遮瑕" id ="26" cids="50010803" img="zx.png" />
  127. <subitem title="睫毛膏" id ="27" cids="50010794" img="jmg.png" />
  128. <subitem title="眼线" id ="28" cids="50010797" img="yx.png" />
  129. <subitem title="眼影" id ="29" cids="50010796" img="yy.png" />
  130. <subitem title="眉笔/眉粉" id ="290" cids="50010798" img="mb.png" />
  131. <subitem title="唇膏/唇彩" id ="291" cids="50010801,50010807,50010808,121392016,121418013,121448009,121456011,121466011,121476007" img="cg.png" />
  132. </item>
  133. <item title="个人护理" id ="3" cids="" img="grhl.png">
  134. <subitem title="洗发水" id ="30" cids="121396029,213202,121404024,121426020,121476023" img="xfs.png" />
  135. <subitem title="美发护发" id ="31" cids="50023282,50016883,50024999,121410029" img="mfhf.png" />
  136. <subitem title="头发造型" id ="32" cids="50023294,50023293,50023326" img="tfzx.png" />
  137. <subitem title="身体护理" id ="33" cids="50012000,50011999,50012001,50012006,50014257,121396013,121412016,121422012,121466009,121478012,121484012" img="sthl.png" />
  138. <subitem title="手足护理" id ="34" cids="121402008,121390007,121424012,121452007,121456010,121472008,122430002" img="szhl.png" />
  139. <subitem title="止汗露" id ="35" cids="121392036,121392015" img="zhl.png" />
  140. <subitem title="沐浴乳/露" id ="36" cids="50014254,213205,50023724,50014248,50014259,50014260,50458018,121386011" img="myr.png" />
  141. <subitem title="香氛" id ="37" cids="50010815" img="xf.png" />
  142. </item>
  143. <item title="男士护肤" id ="4" cids="" img="nshf.png">
  144. <subitem title="男士洁面" id ="40" cids="121452027,121410035" img="nsjm.png" />
  145. <subitem title="男士爽肤水" id ="41" cids="121392037" img="nssfs.png" />
  146. <subitem title="男士乳霜" id ="42" cids="121410035" img="nsrs.png" />
  147. <subitem title="男士护理" id ="43" cids="121366036,50022680,121368010,121386035,121392036,121398029,121404031,121416019,121450036,121470041,121482026" img="nshl.png" />
  148. <subitem title="男士其它" id ="44" cids="121410035" img="nsjm.png" />
  149. </item>
  150. </category>
  151. EOD;
  152. return $content;
  153. }
  154. }
  155. class brand_helper
  156. {
  157. static function brands()
  158. {
  159. $prefix = 'mb_';
  160. $ret = rcache('brand', $prefix);
  161. if(empty($ret))
  162. {
  163. $model = Model();
  164. $brand_area_list = $model->table('brand_area')->order('area_sort asc')->limit(false)->select();
  165. $filed = 'brand_id,brand_name,brand_pic,brand_img_bg,brand_img_logo,brand_area_id';
  166. $brand_c_list = $model->table('brand')->field($filed)->where(array('brand_apply' => '1'))->order('brand_sort asc')->limit(false)->select();
  167. $brands = array();
  168. foreach ($brand_area_list as $brand_area)
  169. {
  170. foreach ($brand_c_list as $brand) {
  171. $brand['brand_pic'] = image_helper::format_brand_img_rect($brand['brand_pic']);
  172. $brand['brand_img_bg'] = image_helper::format_brand_img_bg($brand['brand_img_bg']);
  173. $brand['brand_img_logo'] = image_helper::format_brand_img_circle($brand['brand_img_logo']);
  174. if ($brand['brand_area_id'] == $brand_area['area_id']) {
  175. if (empty($brand_area['data'])) {
  176. $brand_area['data'] = array();
  177. }
  178. array_push($brand_area['data'], $brand);
  179. }
  180. }
  181. if (!empty($brand_area['data'])) {
  182. array_push($brands, $brand_area);
  183. }
  184. }
  185. wcache("brand", array("brands" => serialize($brands)), $prefix);
  186. } else {
  187. $brands = unserialize($ret['brands']);
  188. }
  189. return $brands;
  190. }
  191. }