mb_special.model.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. <?php
  2. /**
  3. * 手机专题模型
  4. *
  5. *
  6. *
  7. *
  8. */
  9. defined('InShopNC') or exit('Access Invalid!');
  10. class mb_specialModel extends Model
  11. {
  12. //专题项目不可用状态
  13. const SPECIAL_ITEM_UNUSABLE = 0;
  14. //专题项目可用状态
  15. const SPECIAL_ITEM_USABLE = 1;
  16. //首页特殊专题编号
  17. const INDEX_SPECIAL_ID = 0;
  18. public function __construct()
  19. {
  20. parent::__construct('mb_special');
  21. }
  22. /**
  23. * 读取专题列表
  24. * @param array $condition
  25. *
  26. */
  27. public function getMbSpecialList($condition, $page = '', $order = 'special_id desc', $field = '*')
  28. {
  29. $list = $this->table('mb_special')->field($field)->where($condition)->page($page)->order($order)->select();
  30. return $list;
  31. }
  32. public function getMbSpecialByID($special_id,$field='*',$lock = false)
  33. {
  34. return $this->table('mb_special')->field($field)->lock($lock)->find($special_id);
  35. }
  36. /*
  37. * 增加专题
  38. * @param array $param
  39. * @return bool
  40. *
  41. */
  42. public function addMbSpecial($param)
  43. {
  44. return $this->table('mb_special')->insert($param);
  45. }
  46. public function editUserSpecial($special_id,$cond, $update)
  47. {
  48. $result = $this->table('mb_special')->where($cond)->update($update);
  49. if ($result) {
  50. //删除缓存
  51. $this->_delMbSpecialCache($special_id);
  52. return $special_id;
  53. } else {
  54. return false;
  55. }
  56. }
  57. /*
  58. * 更新专题
  59. * @param array $update
  60. * @param array $condition
  61. * @return bool
  62. *
  63. */
  64. public function editMbSpecial($update, $special_id)
  65. {
  66. $special_id = intval($special_id);
  67. if ($special_id <= 0) {
  68. return false;
  69. }
  70. $condition = array();
  71. $condition['special_id'] = $special_id;
  72. $result = $this->table('mb_special')->where($condition)->update($update);
  73. if ($result) {
  74. //删除缓存
  75. $this->_delMbSpecialCache($special_id);
  76. return $special_id;
  77. } else {
  78. return false;
  79. }
  80. }
  81. /*
  82. * 删除专题
  83. * @param int $special_id
  84. * @return bool
  85. *
  86. */
  87. public function delMbSpecialByID($special_id)
  88. {
  89. $special_id = intval($special_id);
  90. if ($special_id <= 0) {
  91. return false;
  92. }
  93. $condition = array();
  94. $condition['special_id'] = $special_id;
  95. $this->delMbSpecialItem($condition, $special_id);
  96. return $this->table('mb_special')->where($condition)->delete();
  97. }
  98. /**
  99. * 专题项目列表(用于后台编辑显示所有项目)
  100. * @param int $special_id
  101. *
  102. */
  103. public function getMbSpecialItemListByID($special_id)
  104. {
  105. $condition = array();
  106. $condition['special_id'] = $special_id;
  107. return $this->_getMbSpecialItemList($condition);
  108. }
  109. private function gen_tabs($tab_words)
  110. {
  111. if(empty($tab_words)) return false;
  112. $result = [];
  113. $items = preg_split("/#/",$tab_words);
  114. foreach ($items as $item)
  115. {
  116. $values = explode(":",$item);
  117. $item_id = intval($values[0]);
  118. if(count($values) != 2 || $item_id <= 0) {
  119. continue;
  120. } else {
  121. $result[$item_id] = ['pos' => 0,'title' => trim($values[1])];
  122. }
  123. }
  124. return empty($result) ? false : $result;
  125. }
  126. private function format_tabs($tabs) {
  127. }
  128. ////ugc_types
  129. public function getUgcTypesList()
  130. {
  131. $result = $this->table('ugc_types')->field('*')->limit(false)->order('type_sort ASC')->select();
  132. return $result;
  133. }
  134. public function getOneUgcTypeById($id){
  135. return $this->table('ugc_types')->field('*')->where(['ugcat_id'=>$id])->find();
  136. }
  137. public function addUgcTypes($item){
  138. return $this->table('ugc_types')->insert($item);
  139. }
  140. public function editUgcTypes($condition,$item){
  141. return $this->table('ugc_types')->where($condition)->update($item);
  142. }
  143. public function delUgcTypes($id){
  144. return $this->table('ugc_types')->where(['ugcat_id'=>$id])->delete();
  145. }
  146. ////ugc_types
  147. public function getMbSpecialItemUsableListByID($special_id)
  148. {
  149. $prefix = 'mb_special';
  150. $item_list = rcache($special_id, $prefix);
  151. //缓存有效
  152. if (!empty($item_list)) {
  153. return unserialize($item_list['special']);
  154. }
  155. //缓存无效查库并缓存
  156. $condition = array();
  157. $condition['special_id'] = $special_id;
  158. $condition['item_usable'] = self::SPECIAL_ITEM_USABLE;
  159. $item_list = $this->_getMbSpecialItemList($condition);
  160. $spinfo = $this->getMbSpecialByID($special_id,'from_user,tabs');
  161. $from_user = $spinfo['from_user'] == 1 ? true : false;
  162. $tabs = $this->gen_tabs($spinfo['tabs']);
  163. $item_sort = 0;
  164. $blocks = [];
  165. foreach ($item_list as $value)
  166. {
  167. $block = [];
  168. $block['item_type'] = $value['item_type'];
  169. $block['bg_image'] = $value['bg_image'];
  170. $block['bg_type'] = $value['bg_type'];
  171. $block['bg_data'] = $value['bg_data'];
  172. $has_margin = intval($value['has_margin']);
  173. $block['has_margin']= $has_margin === 0 ? false : true;
  174. $item_id = intval($value['item_id']);
  175. if($tabs != false && array_key_exists($item_id,$tabs)) {
  176. $tabs[$item_id]['pos'] = $item_sort;
  177. }
  178. $block['item_sort'] = $item_sort;
  179. $item_sort++;
  180. $item_type = $value['item_type'];
  181. if($item_type == 'divider') {
  182. $blocks[] = $block;
  183. continue;
  184. }
  185. $item_data = $value['item_data'];
  186. if($item_type == 'home1') {
  187. $block['title'] = empty($item_data['home1_title']) ? '' : $item_data['home1_title'];
  188. }
  189. else {
  190. $block['title'] = empty($item_data['title']) ? '' : $item_data['title'];
  191. }
  192. if($from_user) {
  193. $items = $this->_formatUserSpecialData($item_data, $value['item_type']);
  194. } else {
  195. $items = $this->_formatMbSpecialData($item_data, $value['item_type']);
  196. }
  197. if($item_type == 'home1' || $item_type == 'home2' || $item_type == 'home4' || $item_type == 'home_ugc') {
  198. $block['items'] = $items;
  199. }
  200. else {
  201. $block['items'] = $items['item'];
  202. }
  203. if(empty($block['items'])) {
  204. continue;
  205. }
  206. $blocks[] = $block;
  207. }
  208. if($tabs == false) {
  209. $tab_items = [];
  210. }
  211. else {
  212. $tab_items = [];
  213. foreach ($tabs as $key => $val) {
  214. $tab_items[] = $val;
  215. }
  216. }
  217. $cache = array('blocks' => $blocks,'tabs' => $tab_items);
  218. wcache($special_id, array('special' => serialize($cache)), $prefix);
  219. return $cache;
  220. }
  221. /**
  222. * 首页专题
  223. */
  224. // public function getMbSpecialIndex()
  225. // {
  226. // return $this->getMbSpecialItemUsableListByID(self::INDEX_SPECIAL_ID);
  227. // }
  228. /**
  229. * 处理专题数据,拼接图片URL
  230. */
  231. private function _formatMbSpecialData($item_data, $item_type)
  232. {
  233. switch ($item_type) {
  234. case 'home1':
  235. $item_data['image'] = getMbSpecialImageUrl($item_data['image']);
  236. if($item_data['show_type'] == 'image') {
  237. $item_data['show_data'] = getMbSpecialImageUrl($item_data['show_data']);
  238. }
  239. elseif($item_data['show_type'] == 'audio') {
  240. $item_data['show_data'] = $this->getMbSpecialAudioUrl($item_data['show_data']);
  241. }
  242. break;
  243. case 'home2':
  244. case 'home4':
  245. $item_data['square_image'] = getMbSpecialImageUrl($item_data['square_image']);
  246. if($item_data['square_show_type'] == 'image') {
  247. $item_data['square_show_data'] = getMbSpecialImageUrl($item_data['square_show_data']);
  248. }
  249. elseif($item_data['square_show_type'] == 'audio') {
  250. $item_data['square_show_data'] = $this->getMbSpecialAudioUrl($item_data['show_data']);
  251. }
  252. $item_data['rectangle1_image'] = getMbSpecialImageUrl($item_data['rectangle1_image']);
  253. if($item_data['rectangle1_show_type'] == 'image') {
  254. $item_data['rectangle1_show_data'] = getMbSpecialImageUrl($item_data['rectangle1_show_data']);
  255. }
  256. elseif($item_data['rectangle1_show_type'] == 'audio') {
  257. $item_data['rectangle1_show_data'] = $this->getMbSpecialAudioUrl($item_data['show_data']);
  258. }
  259. $item_data['rectangle2_image'] = getMbSpecialImageUrl($item_data['rectangle2_image']);
  260. if($item_data['rectangle2_show_type'] == 'image') {
  261. $item_data['rectangle2_show_data'] = getMbSpecialImageUrl($item_data['rectangle2_show_data']);
  262. }
  263. elseif($item_data['rectangle2_show_type'] == 'audio') {
  264. $item_data['rectangle2_show_data'] = $this->getMbSpecialAudioUrl($item_data['show_data']);
  265. }
  266. break;
  267. default:
  268. $new_item = array();
  269. foreach ((array)$item_data['item'] as $key => $value)
  270. {
  271. $value['image'] = getMbSpecialImageUrl($value['image']);
  272. if($value['show_type'] == 'image') {
  273. $value['show_data'] = getMbSpecialImageUrl($value['show_data']);
  274. }
  275. elseif($item_data['show_type'] == 'audio') {
  276. $value['show_data'] = $this->getMbSpecialAudioUrl($value['show_data']);
  277. }
  278. $new_item[] = $value;
  279. }
  280. $item_data['item'] = $new_item;
  281. }
  282. return $item_data;
  283. }
  284. private function _formatUserSpecialData($item_data, $item_type)
  285. {
  286. switch ($item_type) {
  287. case 'home1':
  288. break;
  289. case 'home2':
  290. case 'home4':
  291. break;
  292. case 'home_ugc':
  293. break;
  294. default:
  295. $new_item = array();
  296. foreach ((array)$item_data['item'] as $key => $value)
  297. {
  298. $new_item[] = $value;
  299. }
  300. $item_data['item'] = $new_item;
  301. }
  302. return $item_data;
  303. }
  304. private function getMbSpecialAudioUrl($file)
  305. {
  306. $path = BASE_SITE_URL . "/data/upload/audio/{$file}";
  307. return $path;
  308. }
  309. /**
  310. * 查询专题项目列表
  311. */
  312. private function _getMbSpecialItemList($condition, $order = 'item_sort asc')
  313. {
  314. $item_list = $this->table('mb_special_item')->where($condition)->order($order)->select();
  315. foreach ($item_list as $key => $value) {
  316. $item_list[$key]['item_data'] = $this->_initMbSpecialItemData($value['item_data'], $value['item_type']);
  317. if ($value['item_usable'] == self::SPECIAL_ITEM_USABLE) {
  318. $item_list[$key]['usable_class'] = 'usable';
  319. $item_list[$key]['usable_text'] = '禁用';
  320. } else {
  321. $item_list[$key]['usable_class'] = 'unusable';
  322. $item_list[$key]['usable_text'] = '启用';
  323. }
  324. }
  325. return $item_list;
  326. }
  327. /**
  328. * 检查专题项目是否存在
  329. * @param array $condition
  330. *
  331. */
  332. public function isMbSpecialItemExist($condition)
  333. {
  334. $item_list = $this->table('mb_special_item')->where($condition)->select();
  335. if ($item_list) {
  336. return true;
  337. } else {
  338. return false;
  339. }
  340. }
  341. /**
  342. * 获取项目详细信息
  343. * @param int $item_id
  344. *
  345. */
  346. public function getMbSpecialItemInfoByID($item_id)
  347. {
  348. $item_id = intval($item_id);
  349. if ($item_id <= 0) {
  350. return false;
  351. }
  352. $condition = array();
  353. $condition['item_id'] = $item_id;
  354. $item_info = $this->table('mb_special_item')->where($condition)->find();
  355. $item_info['item_data'] = $this->_initMbSpecialItemData($item_info['item_data'], $item_info['item_type']);
  356. return $item_info;
  357. }
  358. /**
  359. * 整理项目内容
  360. *
  361. */
  362. private function _initMbSpecialItemData($item_data, $item_type)
  363. {
  364. if (!empty($item_data)) {
  365. $item_data = unserialize($item_data);
  366. } else {
  367. $item_data = $this->_initMbSpecialItemNullData($item_type);
  368. }
  369. return $item_data;
  370. }
  371. /**
  372. * 初始化空项目内容
  373. */
  374. private function _initMbSpecialItemNullData($item_type)
  375. {
  376. $item_data = array();
  377. switch ($item_type) {
  378. case 'home1':
  379. $item_data = array(
  380. 'title' => '',
  381. 'image' => '',
  382. 'type' => '',
  383. 'data' => '',
  384. );
  385. break;
  386. case 'home2':
  387. case 'home4':
  388. $item_data = array(
  389. 'title' => '',
  390. 'square_image' => '',
  391. 'square_type' => '',
  392. 'square_data' => '',
  393. 'rectangle1_image' => '',
  394. 'rectangle1_type' => '',
  395. 'rectangle1_data' => '',
  396. 'rectangle2_image' => '',
  397. 'rectangle2_type' => '',
  398. 'rectangle2_data' => '',
  399. );
  400. break;
  401. default:
  402. }
  403. return $item_data;
  404. }
  405. /*
  406. * 增加专题项目
  407. * @param array $param
  408. * @return array $item_info
  409. *
  410. */
  411. public function addMbSpecialItem($param)
  412. {
  413. $param['item_usable'] = self::SPECIAL_ITEM_UNUSABLE;
  414. if(!isset($param['item_sort']))
  415. $param['item_sort'] = 255;
  416. $result = $this->table('mb_special_item')->insert($param);
  417. //删除缓存
  418. if ($result) {
  419. //删除缓存
  420. $this->_delMbSpecialCache($param['special_id']);
  421. $param['item_id'] = $result;
  422. return $param;
  423. } else {
  424. return false;
  425. }
  426. }
  427. /**
  428. * 编辑专题项目
  429. * @param array $update
  430. * @param int $item_id
  431. * @param int $special_id
  432. * @return bool
  433. *
  434. */
  435. public function editMbSpecialItemByID($update, $item_id, $special_id)
  436. {
  437. if (isset($update['item_data'])) {
  438. $update['item_data'] = serialize($update['item_data']);
  439. }
  440. $condition = array();
  441. $condition['item_id'] = $item_id;
  442. //删除缓存
  443. $this->_delMbSpecialCache($special_id);
  444. return $this->table('mb_special_item')->where($condition)->update($update);
  445. }
  446. public function addUserSpecialItem($param)
  447. {
  448. if (isset($param['item_data'])) {
  449. $param['item_data'] = serialize($param['item_data']);
  450. }
  451. $param['item_usable'] = self::SPECIAL_ITEM_USABLE;
  452. $result = $this->table('mb_special_item')->insert($param);
  453. //删除缓存
  454. if ($result) {
  455. //删除缓存
  456. $this->_delMbSpecialCache($param['special_id']);
  457. $param['item_id'] = $result;
  458. return $param;
  459. } else {
  460. return false;
  461. }
  462. }
  463. public function editUserSpecialItem($update, $item_id)
  464. {
  465. if (isset($update['item_data'])) {
  466. $update['item_data'] = serialize($update['item_data']);
  467. }
  468. $condition = array();
  469. $condition['item_id'] = $item_id;
  470. return $this->table('mb_special_item')->where($condition)->update($update);
  471. }
  472. /**
  473. * 编辑专题项目启用状态
  474. * @param string usable-启用/unsable-不启用
  475. * @param int $item_id
  476. * @param int $special_id
  477. *
  478. */
  479. public function editMbSpecialItemUsableByID($usable, $item_id, $special_id)
  480. {
  481. $update = array();
  482. if ($usable == 'usable') {
  483. $update['item_usable'] = self::SPECIAL_ITEM_USABLE;
  484. } else {
  485. $update['item_usable'] = self::SPECIAL_ITEM_UNUSABLE;
  486. }
  487. return $this->editMbSpecialItemByID($update, $item_id, $special_id);
  488. }
  489. /*
  490. * 删除
  491. * @param array $condition
  492. * @return bool
  493. *
  494. */
  495. public function delMbSpecialItem($condition, $special_id)
  496. {
  497. //删除缓存
  498. $this->_delMbSpecialCache($special_id);
  499. return $this->table('mb_special_item')->where($condition)->delete();
  500. }
  501. /**
  502. * 获取专题URL地址
  503. * @param int $special_id
  504. *
  505. */
  506. public function getMbSpecialHtmlUrl($special_id)
  507. {
  508. return UPLOAD_SITE_URL . DS . ATTACH_MOBILE . DS . 'special_html' . DS . md5('special' . $special_id) . '.html';
  509. }
  510. /**
  511. * 获取专题静态文件路径
  512. * @param int $special_id
  513. *
  514. */
  515. public function getMbSpecialHtmlPath($special_id)
  516. {
  517. return BASE_UPLOAD_PATH . DS . ATTACH_MOBILE . DS . 'special_html' . DS . md5('special' . $special_id) . '.html';
  518. }
  519. /**
  520. * 获取专题模块类型列表
  521. * @return array
  522. *
  523. */
  524. public function getMbSpecialModuleList()
  525. {
  526. $module_list = array();
  527. $module_list['adv_list'] = array('name' => 'adv_list', 'desc' => '广告条版块');
  528. $module_list['home1'] = array('name' => 'home1', 'desc' => '模型版块布局A');
  529. $module_list['home2'] = array('name' => 'home2', 'desc' => '模型版块布局B');
  530. $module_list['home3'] = array('name' => 'home3', 'desc' => '模型版块布局C');
  531. $module_list['home4'] = array('name' => 'home4', 'desc' => '模型版块布局D');
  532. $module_list['home5'] = array('name' => 'home5', 'desc' => '模型版块布局E');
  533. $module_list['goods'] = array('name' => 'goods', 'desc' => '商品版块');
  534. $module_list['home6'] = array('name' => 'home6', 'desc' => '模型版块布局G');
  535. $module_list['home7'] = array('name' => 'home7', 'desc' => '模型版块布局F');
  536. $module_list['divider'] = array('name' => 'divider', 'desc' => '分割线');
  537. return $module_list;
  538. }
  539. /**
  540. * 清理缓存
  541. */
  542. private function _delMbSpecialCache($special_id)
  543. {
  544. //清理缓存
  545. dcache($special_id, 'mb_special');
  546. //删除静态文件
  547. $html_path = $this->getMbSpecialHtmlPath($special_id);
  548. if (is_file($html_path)) {
  549. @unlink($html_path);
  550. }
  551. }
  552. }