Compare.php 16 KB

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