category_helper.php 7.1 KB

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