BonusController.class.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. /**
  3. * 小梦科技资源nanodreamtech.com
  4. *
  5. * ==========================================================================
  6. * @link https://www.nanodreamtech.com/
  7. * @copyright Copyright (c) 2015 liofis.com.
  8. * @license https://www.nanodreamtech.com/license.html License
  9. * ==========================================================================
  10. * 拼团模块
  11. * @author fish
  12. *
  13. */
  14. namespace Home\Controller;
  15. class BonusController extends CommonController {
  16. protected function _initialize(){
  17. parent::_initialize();
  18. define('UID',is_login());
  19. $this->member_id = UID;
  20. }
  21. function index(){
  22. $id = I('get.id', 0);
  23. $this->id = $id;
  24. $quan_info = $this->get_voucher_info_do($id,0);
  25. if($quan_info['can_get'] == 0 )
  26. {
  27. $url = C('SITE_URL')."index.php?s=/Bonus/bonus/id/{$id}.html";//U('Bonus/bonus', array('id' => $id));
  28. header('Location: '.$url);
  29. //$this->redirect($url);
  30. }else{
  31. $this->display();
  32. }
  33. }
  34. /**
  35. * 优惠券活动页面
  36. */
  37. function bonus(){
  38. $id = I('get.id', 0);
  39. $type = I('get.type', 0);
  40. $this->id = $id;
  41. $this->type = $type;
  42. $this->display();
  43. }
  44. /**
  45. * 优惠券过期页面
  46. */
  47. function overdue(){
  48. $this->display();
  49. }
  50. private function get_voucher_info_do($voucher_id,$is_ajax = 1)
  51. {
  52. $voucher_info = M('voucher')->where( array('id' => $voucher_id) )->find();
  53. if( empty($voucher_info) )
  54. {
  55. echo json_encode( array('code' => 1) );
  56. die();
  57. } else{
  58. $voucher_info['share_logo'] = C('SITE_URL').'Uploads/image/'.$voucher_info['share_logo'];
  59. unset($voucher_info['store_id']);
  60. unset($voucher_info['type']);
  61. unset($voucher_info['is_index_show']);
  62. unset($voucher_info['is_limit_goods_buy']);
  63. unset($voucher_info['add_time']);
  64. $activity_over = 0; //券的活动时间是否结束了
  65. $now_time = time(); //end_time
  66. if( $voucher_info['end_time'] < $now_time)
  67. {
  68. $activity_over =1;
  69. }
  70. $has_get_voucher = 0;
  71. //0、计算已经领取了几张
  72. $has_get_count = M('voucher_list')->where( array('voucher_id' =>$voucher_id, 'user_id' => UID) )->count();
  73. //获取限制商品的分类:如果是全场商品。那么就调用出推荐的分类,如果是部分商品,就调用出部分商品的分类
  74. $category_list = array();
  75. $cate_ids = array();
  76. if( $voucher_info['is_limit_goods_buy'] == 1)
  77. {
  78. //部分商品
  79. $goods_list = M('voucher_goods')->where( array('voucher_id' => $voucher_id) )->select();
  80. foreach( $goods_list as $goods )
  81. {
  82. $cate_tmp = M('goods_to_category')->where( array('goods_id' => $goods['goods_id']) )->find();
  83. if( !empty($cate_tmp) )
  84. {
  85. if( empty($cate_ids) || !in_array($cate_tmp['class_id1'], $cate_ids) )
  86. {
  87. $cate_ids[] = $cate_tmp['class_id1'];
  88. }
  89. }
  90. }
  91. if( !empty( $cate_ids ) )
  92. {
  93. $category_list = M('goods_category')->field('id,name')->where( array('id'=> array('in',$cate_ids ) ) )->order('sort_order desc')->select();
  94. }
  95. }else{
  96. //全场商品
  97. $category_list = M('goods_category')->field('id,name')->where( array('is_hot'=> 1 ) )->order('sort_order desc')->select();
  98. }
  99. $result = array();
  100. $result['code'] = 0;
  101. $result['activity_over'] = $activity_over;//活动是否结束,1已结束,0 未结束
  102. $result['can_get'] = $activity_over == 1 ? 0 : 1;
  103. //$result['has_get_count'] = $has_get_count;//这个券,该会员获取了几张,这个数字大于0表示会员有券,如果活动已经结束,还需要借助这个字段判断是否还有要展示的领取过的券
  104. $result['voucher_info'] = $voucher_info;//券的详细信息
  105. $result['category_list'] = $category_list;
  106. $result['cur_time'] = time();//服务器的当前时间戳,倒计时的时候可能会用到
  107. $result['get_voucher_info'] = array();//需要展示的券信息
  108. if( $has_get_count == 0 )
  109. {
  110. //一张都没有领过, 活动已经结束
  111. }else{
  112. //检测是否还有未使用的券 end_time
  113. $get_voucher_info = M('voucher_list')->where( array('voucher_id' =>$voucher_id, 'user_id' => UID,'consume' => 'N') )->order('id desc')->find();
  114. if( !empty($get_voucher_info) )
  115. {
  116. $result['get_voucher_info'] = $get_voucher_info;
  117. $result['can_get'] = 0;
  118. }else{
  119. //判断一个人可以领几张
  120. if($activity_over == 0 && ( $voucher_info['person_limit_count'] ==0 || $voucher_info['person_limit_count'] > $has_get_count) )
  121. {
  122. $result['can_get'] = 1;
  123. }else if($activity_over == 0){
  124. $result['can_get'] = 0;
  125. $get_voucher_info = M('voucher_list')->where( array('voucher_id' =>$voucher_id, 'user_id' => UID,'consume' => 'Y') )->order('id desc')->find();
  126. $result['get_voucher_info'] = $get_voucher_info;//已经使用过了。领取的券数量已经大于等于 可领的券数量,
  127. }
  128. }
  129. }
  130. //total_count send_count
  131. if( $voucher_info['total_count'] <= $voucher_info['send_count'] )
  132. {
  133. $result['activity_over'] =2;//优惠券已经被抢光
  134. $result['can_get'] = 0;
  135. }
  136. if($is_ajax == 1)
  137. {
  138. echo json_encode($result);
  139. die();
  140. }else{
  141. return $result;
  142. }
  143. }
  144. }
  145. /**
  146. 获取优惠券活动信息
  147. **/
  148. public function load_voucher_info($is_ajax = 1)
  149. {
  150. $voucher_id = I('get.voucher_id', 0);
  151. $this->get_voucher_info_do($voucher_id,1);
  152. }
  153. /**
  154. 会员抢券
  155. **/
  156. public function get_bonus_voucher()
  157. {
  158. $voucher_id = I('get.voucher_id', 0);
  159. $is_double = I('get.is_double', 0);
  160. if($is_double == 1)
  161. {
  162. $is_double = true;
  163. }else{
  164. $is_double = false;
  165. }
  166. $voucher_model = D('Home/Voucher');
  167. $vocher_detail_id = $voucher_model->send_user_voucher_byId_frombonus($voucher_id,UID,true,$is_double);
  168. //-1 被抢光了, -2 已领过
  169. $result = array('code' => $vocher_detail_id,'cur_time' => time() );
  170. if($vocher_detail_id > 0)
  171. {
  172. $get_voucher_info = M('voucher_list')->where( array('id' =>$vocher_detail_id, 'user_id' => UID) )->find();
  173. if( empty($get_voucher_info) )
  174. {
  175. $get_voucher_info = array();
  176. }
  177. $result['code'] = 0;
  178. $result['get_voucher_info'] = $get_voucher_info;//已经使用过了。领取的券数量已经大于等于 可领的券数量,
  179. }
  180. echo json_encode( $result );
  181. die();
  182. }
  183. /**
  184. 搜索优惠券商品
  185. **/
  186. public function get_voucher_goods_list()
  187. {
  188. $pre_page = 10;
  189. $voucher_id = I('get.voucher_id', 0);
  190. $page = I('get.page',1);
  191. $id = I('get.gid',0);
  192. $voucher_info = M('voucher')->where( array('id' => $voucher_id) )->find();
  193. if( empty($voucher_info) )
  194. {
  195. echo json_encode( array('code' =>1) );
  196. die();
  197. }
  198. if( $id > 0 )
  199. {
  200. //is_limit_goods_buy
  201. if( $voucher_info['is_limit_goods_buy'] == 1)
  202. {
  203. $goods_list_tmp = M('voucher_goods')->where( array('voucher_id' => $voucher_id) )->order('goods_id desc')->select();
  204. $need_goods_ids = array();
  205. foreach( $goods_list_tmp as $val )
  206. {
  207. $need_goods_ids[] = $val['goods_id'];
  208. }
  209. $goods_ids_arr = M('goods_to_category')->where("class_id1 ={$id} or class_id2 ={$id} or class_id3 = {$id} ")->field('goods_id')->select();
  210. $ids_arr = array();
  211. foreach($goods_ids_arr as $val){
  212. if( in_array($val['goods_id'], $need_goods_ids) )
  213. {
  214. $ids_arr[] = $val['goods_id'];
  215. }
  216. }
  217. if( empty($ids_arr) )
  218. {
  219. $ids_arr = array('a');
  220. }
  221. $ids_str = implode(',',$ids_arr);
  222. $condition = array('goods_id' => array('in',$ids_str), 'status'=>1,'quantity' =>array('gt',0) );
  223. $condition['_string'] = ' type="normal" or type="pintuan" ';
  224. }else{
  225. $goods_ids_arr = M('goods_to_category')->where("class_id1 ={$id} or class_id2 ={$id} or class_id3 = {$id} ")->field('goods_id')->select();
  226. $ids_arr = array();
  227. foreach($goods_ids_arr as $val){
  228. $ids_arr[] = $val['goods_id'];
  229. }
  230. $ids_str = implode(',',$ids_arr);
  231. $condition = array('goods_id' => array('in',$ids_str), 'status'=>1,'quantity' =>array('gt',0) );
  232. $condition['_string'] = ' type="normal" or type="pintuan" ';
  233. }
  234. }else{
  235. if( $voucher_info['is_limit_goods_buy'] == 1)
  236. {
  237. $goods_list_tmp = M('voucher_goods')->where( array('voucher_id' => $voucher_id) )->order('goods_id desc')->select();
  238. $need_goods_ids = array();
  239. foreach( $goods_list_tmp as $val )
  240. {
  241. $need_goods_ids[] = $val['goods_id'];
  242. }
  243. $ids_arr = $need_goods_ids;
  244. $ids_str = implode(',',$ids_arr);
  245. $condition = array('goods_id' => array('in',$ids_str), 'status'=>1,'quantity' =>array('gt',0) );
  246. $condition['_string'] = ' type="normal" or type="pintuan" ';
  247. }else{
  248. $condition = array( 'status'=>1,'quantity' =>array('gt',0) );
  249. $condition['_string'] = ' type="normal" or type="pintuan" ';
  250. }
  251. }
  252. $offset = ($page -1) * $pre_page;
  253. $list = M('goods')->field('goods_id,name,seller_count,virtual_count,quantity,image')->where($condition)->order('seller_count+virtual_count desc,goods_id asc')->limit($offset,$pre_page)->select();
  254. $goods_model = D('Home/goods');
  255. if(!empty($list)) {
  256. foreach($list as $key => $v){
  257. if(empty($v['fan_image'])){
  258. $list[$key]['image']= resize($v['image'], 480, 480);
  259. }
  260. }
  261. }
  262. foreach($list as $key => $val)
  263. {
  264. $val['seller_count'] += $val['virtual_count'];
  265. $price_arr = $goods_model->get_goods_price($val['goods_id']);
  266. $val['pinprice'] = $price_arr['price'];
  267. //credit
  268. $val['quan_after_price'] = $price_arr['price'] - $voucher_info['credit'];
  269. if($val['quan_after_price'] < 0)
  270. {
  271. $val['quan_after_price'] = 0;
  272. }
  273. $val['url'] = U('Goods/gshow', array('id' => $val['goods_id'], 'voucher_id' => $voucher_id));
  274. $list[$key] = $val;
  275. }
  276. //https://mall.shiziyu888.com/dan/index.php?s=/goods/99.html
  277. if( !empty($list) )
  278. {
  279. echo json_encode( array('code' => 0, 'data' => $list) );
  280. die();
  281. }else{
  282. echo json_encode( array('code' => 1) );
  283. die();
  284. }
  285. }
  286. }