Compare.php 16 KB

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