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