Compare.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. namespace app\home\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. * ============================================================================
  7. *
  8. * ============================================================================
  9. *
  10. * ----------------------------------------------------------------------------
  11. *
  12. * ============================================================================
  13. * 控制器
  14. */
  15. class Compare extends BaseMall
  16. {
  17. const MAXNUM = 4; //最大的商品比较数量
  18. public function initialize()
  19. {
  20. parent::initialize(); // TODO: Change the autogenerated stub
  21. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/compare.lang.php');
  22. }
  23. /**
  24. * 商品对比详细页面
  25. */
  26. public function index()
  27. {
  28. $gid_arr = array();
  29. $gids = trim(input('param.gids'));
  30. if (!$gids) {
  31. $gids = trim(Cookie('comparegoods'));
  32. }
  33. $gid_arrtmp = $gids ? explode(',', $gids) : array();
  34. if (!empty($gid_arrtmp)) {
  35. foreach ($gid_arrtmp as $k => $v) {
  36. $gid_arr[] = intval($v);
  37. }
  38. }
  39. $goods_model = model('goods');
  40. $compare_list = array(); //对比商品数组
  41. if ($gid_arr) {
  42. //查询商品信息
  43. $goods_list = $gcommon_id_arr = array();
  44. $goods_listtmp = $goods_model->getGoodsList(array(
  45. array('goods_id', 'in', $gid_arr), array('goods_state', '=', 1),
  46. array('goods_verify', '=', 1)
  47. ));
  48. if (!empty($goods_listtmp)) {
  49. foreach ($goods_listtmp as $k => $v) {
  50. $v['goods_spec_arr'] = ($t = $v['goods_spec']) ? unserialize($t) : array();
  51. $goods_list[$v['goods_id']] = $v;
  52. $gcommon_id_arr[] = $v['goods_commonid'];
  53. }
  54. }
  55. if (!empty($gcommon_id_arr)) {
  56. $gcommon_listtmp = $goods_model->getGoodsCommonList(array(
  57. array(
  58. 'goods_commonid', 'in', $gcommon_id_arr
  59. )
  60. ), 'goods_commonid,brand_id,brand_name,goods_attr,spec_name,spec_value');
  61. if (!empty($gcommon_listtmp)) {
  62. foreach ($gcommon_listtmp as $k => $v) {
  63. $v['goods_attr_arr'] = ($t = trim($v['goods_attr'])) ? unserialize($t) : array();
  64. $v['spec_name_arr'] = ($t = trim($v['spec_name'])) ? unserialize($t) : array();
  65. $v['spec_value_arr'] = ($t = trim($v['spec_value'])) ? unserialize($t) : array();
  66. $gcommon_list[$v['goods_commonid']] = $v;
  67. }
  68. }
  69. }
  70. if ($goods_list && $gcommon_list) {
  71. foreach ($goods_list as $k => $v) {
  72. $goods_list[$k] = array_merge($goods_list[$k], $gcommon_list[$v['goods_commonid']]);
  73. }
  74. }
  75. //整理对比数组
  76. $compare_listtmp = array();
  77. foreach ($gid_arr as $k => $v) {
  78. $compare_listtmp[] = $goods_list[$v];
  79. }
  80. //商品图片及名称
  81. $i = 0;
  82. $compare_list[$i]['key'] = 'goodsinfo';
  83. $compare_list[$i]['name'] = lang('commodity_picture');
  84. for ($j = 0; $j < self::MAXNUM; $j++) {
  85. $compare_list[$i][] = isset($compare_listtmp[$j]) ? array(
  86. 'goods_id' => $compare_listtmp[$j]['goods_id'], 'goods_name' => $compare_listtmp[$j]['goods_name'],
  87. 'goods_image' => $compare_listtmp[$j]['goods_image'], 'store_id' => $compare_listtmp[$j]['store_id']
  88. ) : '';
  89. }
  90. $i++;
  91. //商品价格
  92. $compare_list[$i]['key'] = 'goodsprice';
  93. $compare_list[$i]['name'] = lang('commodity_price');
  94. for ($j = 0; $j < self::MAXNUM; $j++) {
  95. $compare_list[$i][] = isset($compare_listtmp[$j]) ? $compare_listtmp[$j]['goods_promotion_price'] : '';
  96. }
  97. $i++;
  98. //品牌
  99. $compare_list[$i]['key'] = 'brand';
  100. $compare_list[$i]['name'] = lang('brand');
  101. for ($j = 0; $j < self::MAXNUM; $j++) {
  102. $compare_list[$i][] = isset($compare_listtmp[$j]) ? ($compare_listtmp[$j]['brand_id'] ? array(
  103. 'brand_id' => $compare_listtmp[$j]['brand_id'], 'brand_name' => $compare_listtmp[$j]['brand_name']
  104. ) : '-') : '';
  105. }
  106. $i++;
  107. //所在地
  108. $area_name_array = model('area')->getAreaNames();
  109. $compare_list[$i]['key'] = '';
  110. $compare_list[$i]['name'] = lang('location');
  111. for ($j = 0; $j < self::MAXNUM; $j++) {
  112. $compare_list[$i][] = isset($compare_listtmp[$j]) ? @$area_name_array[$compare_listtmp[$j]['areaid_1']] . ' ' . @$area_name_array[$compare_listtmp[$j]['areaid_2']] : '';
  113. }
  114. $tmp = $compare_list[$i];
  115. unset($tmp['key'], $tmp['name']);
  116. $tmp = array_filter($tmp);
  117. $difftmp = array_diff($tmp, array($tmp[0]));
  118. if ($difftmp) {
  119. $compare_list[$i]['isdiff'] = 1;
  120. } else {
  121. $compare_list[$i]['isdiff'] = 0;
  122. }
  123. unset($tmp, $difftmp);
  124. $i++;
  125. //发票信息
  126. $compare_list[$i]['key'] = '';
  127. $compare_list[$i]['name'] = lang('vat_invoice');
  128. for ($j = 0; $j < self::MAXNUM; $j++) {
  129. $compare_list[$i][] = isset($compare_listtmp[$j]) ? ($compare_listtmp[$j]['goods_vat'] == 1 ? lang('ds_yes') : lang('ds_no')) : '';
  130. }
  131. $tmp = $compare_list[$i];
  132. unset($tmp['key'], $tmp['name']);
  133. $tmp = array_filter($tmp);
  134. $difftmp = array_diff($tmp, array($tmp[0]));
  135. if ($difftmp) {
  136. $compare_list[$i]['isdiff'] = 1;
  137. } else {
  138. $compare_list[$i]['isdiff'] = 0;
  139. }
  140. unset($tmp, $difftmp);
  141. $i++;
  142. //规格处理
  143. if ($compare_listtmp[0]['spec_name_arr'] && $compare_listtmp[0]['spec_value_arr']) {
  144. foreach ($compare_listtmp[0]['spec_name_arr'] as $k => $v) {
  145. $compare_list[$i]['key'] = '';
  146. $compare_list[$i]['name'] = $v;
  147. for ($j = 0; $j < self::MAXNUM; $j++) {
  148. //对比商品记录是否存在
  149. if (isset($compare_listtmp[$j])) {
  150. //处理规格值
  151. if (!empty($compare_listtmp[0]['spec_value_arr'][$k]) && $compare_listtmp[$j]['goods_spec_arr']) {
  152. $tmp = array_values(array_intersect_assoc($compare_listtmp[0]['spec_value_arr'][$k], $compare_listtmp[$j]['goods_spec_arr']));
  153. if ($tmp) {
  154. $compare_list[$i][] = $tmp[0];
  155. } else {
  156. $compare_list[$i][] = '-';
  157. }
  158. } else {
  159. $compare_list[$i][] = '-';
  160. }
  161. unset($tmp);
  162. } else {
  163. $compare_list[$i][] = '';
  164. }
  165. }
  166. $tmp = $compare_list[$i];
  167. unset($tmp['key'], $tmp['name']);
  168. $tmp = array_filter($tmp);
  169. $difftmp = array_diff($tmp, array($tmp[0]));
  170. if ($difftmp) {
  171. $compare_list[$i]['isdiff'] = 1;
  172. } else {
  173. $compare_list[$i]['isdiff'] = 0;
  174. }
  175. unset($tmp, $difftmp);
  176. $i++;
  177. }
  178. }
  179. //属性处理
  180. if ($compare_listtmp[0]['goods_attr_arr'] && $compare_listtmp[0]['goods_attr_arr']) {
  181. foreach ($compare_listtmp[0]['goods_attr_arr'] as $k => $v) {
  182. $compare_list[$i]['key'] = '';
  183. $compare_list[$i]['name'] = $v['name'];
  184. for ($j = 0; $j < self::MAXNUM; $j++) {
  185. //对比商品记录是否存在
  186. if (isset($compare_listtmp[$j])) {
  187. if ($compare_listtmp[$j]['goods_attr_arr'][$k]) {
  188. $tmp = array_values($compare_listtmp[$j]['goods_attr_arr'][$k]);
  189. $compare_list[$i][] = $tmp[1];
  190. unset($tmp);
  191. } else {
  192. $compare_list[$i][] = '-';
  193. }
  194. } else {
  195. $compare_list[$i][] = '';
  196. }
  197. }
  198. $tmp = $compare_list[$i];
  199. unset($tmp['key'], $tmp['name']);
  200. $tmp = array_filter($tmp);
  201. $difftmp = array_diff($tmp, array($tmp[0]));
  202. if ($difftmp) {
  203. $compare_list[$i]['isdiff'] = 1;
  204. } else {
  205. $compare_list[$i]['isdiff'] = 0;
  206. }
  207. unset($tmp, $difftmp);
  208. $i++;
  209. }
  210. }
  211. }
  212. //处理商品所属的最小分类
  213. if (isset($compare_listtmp[0]['gc_id_3']) && $compare_listtmp[0]['gc_id_3']) {
  214. $cate_id = $compare_listtmp[0]['gc_id_3'];
  215. } elseif (isset($compare_listtmp[0]['gc_id_2']) && $compare_listtmp[0]['gc_id_2']) {
  216. $cate_id = $compare_listtmp[0]['gc_id_2'];
  217. } else {
  218. $cate_id = @$compare_listtmp[0]['gc_id_1'];
  219. }
  220. View::assign('compare_list', $compare_list);
  221. View::assign('cate_id', $cate_id);
  222. //隐藏右侧工具条中的对比功能
  223. View::assign('hidden_rtoolbar_compare', 1);
  224. View::assign('maxnum', self::MAXNUM);
  225. return View::fetch($this->template_dir . 'compare');
  226. }
  227. /**
  228. * 增加对比商品
  229. */
  230. public function addcompare()
  231. {
  232. $gid = intval(input('param.id'));
  233. if ($gid <= 0) {
  234. exit(json_encode(array('done' => false, 'msg' => lang('param_error'))));
  235. }
  236. if (Cookie('comparegoods')) { //如果cookie已经存在
  237. $comparegoods_str = Cookie('comparegoods');
  238. if (get_magic_quotes_gpc()) {
  239. $comparegoods_str = stripslashes($comparegoods_str); //去除斜杠
  240. }
  241. if ($comparegoods_str) {
  242. $comparegoods_arr = explode(',', $comparegoods_str);
  243. if (count($comparegoods_arr) >= self::MAXNUM) {
  244. exit(json_encode(array('done' => false, 'msg' => lang('comparison_bar_full'))));
  245. }
  246. $comparegoods_arr[] = $gid;
  247. $comparegoods_arr = array_unique($comparegoods_arr);
  248. }
  249. } else {
  250. $comparegoods_arr[] = $gid;
  251. }
  252. $comparegoods_str = $comparegoods_arr ? implode(',', $comparegoods_arr) : '';
  253. Cookie('comparegoods', $comparegoods_str);
  254. return json(array('done' => true, 'msg' => ''));
  255. }
  256. /**
  257. * 显示对比商品
  258. */
  259. public function showcompare()
  260. {
  261. $gids = Cookie('comparegoods');
  262. $gid_arr = array();
  263. $goods_list = array();
  264. $goodsid_arr = array();
  265. $gid_arrtmp = $gids ? explode(',', $gids) : array();
  266. if (!empty($gid_arrtmp)) {
  267. foreach ($gid_arrtmp as $k => $v) {
  268. $gid_arr[] = intval($v);
  269. }
  270. }
  271. $goods_model = model('goods');
  272. $compare_list = array(); //对比商品数组
  273. if ($gid_arr) {
  274. //查询商品信息
  275. $goods_listtmp = $goods_model->getGoodsList(array(
  276. array('goods_id', 'in', $gid_arr), array('goods_state', '=', 1),
  277. array('goods_verify', '=', 1)
  278. ), 'goods_id,goods_commonid,goods_name,store_id,gc_id,gc_id_1,gc_id_2,gc_id_3,goods_promotion_price,goods_promotion_type,goods_image');
  279. if ($goods_listtmp) {
  280. foreach ($goods_listtmp as $k => $v) {
  281. $goods_list[$v['goods_id']] = $v;
  282. }
  283. foreach ($gid_arr as $v) {
  284. if ($goods_list[$v]) {
  285. $compare_list[] = $goods_list[$v];
  286. $goodsid_arr[] = $v;
  287. }
  288. }
  289. //更新cookie数据
  290. Cookie('comparegoods', $goodsid_arr ? implode(',', $goodsid_arr) : '');
  291. } else {
  292. //更新cookie数据
  293. Cookie('comparegoods', '', -3600);
  294. }
  295. }
  296. View::assign('goodsid_str', $goodsid_arr ? implode(',', $goodsid_arr) : '');
  297. View::assign('compare_list', $compare_list);
  298. View::assign('freemaxnum', self::MAXNUM - count($compare_list));
  299. echo View::fetch($this->template_dir . 'compare_mini');
  300. }
  301. /**
  302. * 获得已加入对比的商品ID
  303. */
  304. public function checkcompare()
  305. {
  306. $gid_arr = array();
  307. $gids = Cookie('comparegoods');
  308. $gid_arrtmp = $gids ? explode(',', $gids) : array();
  309. if (!empty($gid_arrtmp)) {
  310. foreach ($gid_arrtmp as $k => $v) {
  311. $gid_arr[] = intval($v);
  312. }
  313. }
  314. $goods_model = model('goods');
  315. $goodsid_arr = array(); //对比商品ID数组
  316. if ($gid_arr) {
  317. //查询商品信息
  318. $goods_list = array();
  319. $goods_list = $goods_model->getGoodsList(array(
  320. array('goods_id', 'in', $gid_arr), array('goods_state', '=', 1),
  321. array('goods_verify', '=', 1)
  322. ), 'goods_id,goods_commonid,goods_name,store_id,gc_id,gc_id_1,gc_id_2,gc_id_3,goods_promotion_price,goods_promotion_type,goods_image');
  323. if ($goods_list) {
  324. foreach ($goods_list as $k => $v) {
  325. $goodsid_arr[] = $v['goods_id'];
  326. }
  327. }
  328. }
  329. echo json_encode($goodsid_arr);
  330. }
  331. /**
  332. * 清除对比栏
  333. */
  334. public function delcompare()
  335. {
  336. if (trim(input('param.gid')) == 'all') {
  337. $gid_arr = array();
  338. } else {
  339. $gids = cookie('comparegoods');
  340. $gid_arr = $gids ? explode(',', $gids) : array();
  341. $gid = intval(input('param.gid'));
  342. if ($gid > 0 && $gid_arr) {
  343. unset($gid_arr[array_search($gid, $gid_arr)]);
  344. }
  345. }
  346. $gid_str = $gid_arr ? implode(',', $gid_arr) : '';
  347. //更新cookie数据
  348. if ($gid_str) {
  349. cookie('comparegoods', $gid_str);
  350. } else {
  351. cookie('comparegoods', null);
  352. }
  353. return json(array('done' => true, 'gid_str' => $gid_str));
  354. }
  355. }