Pointcart.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 Pointcart extends BasePointShop
  17. {
  18. public function initialize()
  19. {
  20. parent::initialize(); // TODO: Change the autogenerated stub
  21. Lang::load(base_path().'home/lang/'.config('lang.default_lang').'/pointcart.lang.php');
  22. //判断系统是否开启积分和积分兑换功能
  23. if (config('ds_config.pointprod_isuse') != 1){
  24. $this->error(lang('pointcart_unavailable'),HOME_SITE_URL);
  25. }
  26. if (session('is_login') != 1){
  27. $ref_url = request_uri();
  28. session('ref_url',$ref_url);
  29. $this->redirect(HOME_SITE_URL.'/Login/login.html');
  30. }
  31. }
  32. /**
  33. * 积分礼品购物车首页
  34. */
  35. public function index() {
  36. $cart_goods = array();
  37. $pointcart_model = model('pointcart');
  38. $data = $pointcart_model->getPCartListAndAmount(array('pmember_id'=>session('member_id')));
  39. View::assign('pgoods_pointall',$data['data']['cartgoods_pointall']);
  40. View::assign('cart_array',$data['data']['cartgoods_list']);
  41. return View::fetch($this->template_dir.'pointcart_list');
  42. }
  43. /**
  44. * 购物车添加礼品
  45. */
  46. public function add() {
  47. $pgid = intval(input('pgid'));
  48. $quantity = intval(input('quantity'));
  49. if($pgid <= 0 || $quantity <= 0) {
  50. echo json_encode(array('done'=>false,'msg'=>lang('pointcart_cart_addcart_fail'))); die;
  51. }
  52. //验证积分礼品是否存在购物车中
  53. $pointcart_model = model('pointcart');
  54. $check_cart = $pointcart_model->getPointcartInfo(array('pgoods_id'=>$pgid,'pmember_id'=>session('member_id')));
  55. if(!empty($check_cart)) {
  56. echo json_encode(array('done'=>true)); die;
  57. }
  58. //验证是否能兑换
  59. $data = $pointcart_model->checkExchange($pgid, $quantity, session('member_id'));
  60. if (!$data['state']){
  61. switch ($data['error']){
  62. case 'ParameterError':
  63. echo json_encode(array('done'=>false,'msg'=>$data['msg'],'url'=>(string)url('Pointprod/plist'))); die;
  64. break;
  65. default:
  66. echo json_encode(array('done'=>false,'msg'=>$data['msg'])); die;
  67. break;
  68. }
  69. }
  70. $prod_info = $data['data']['prod_info'];
  71. $insert_arr = array();
  72. $insert_arr['pmember_id'] = session('member_id');
  73. $insert_arr['pgoods_id'] = $prod_info['pgoods_id'];
  74. $insert_arr['pgoods_name'] = $prod_info['pgoods_name'];
  75. $insert_arr['pgoods_points'] = $prod_info['pgoods_points'];
  76. $insert_arr['pgoods_choosenum'] = $prod_info['quantity'];
  77. $insert_arr['pgoods_image'] = $prod_info['pgoods_image_old'];
  78. $cart_state = $pointcart_model->addPointcart($insert_arr);
  79. echo json_encode(array('done'=>true));
  80. die;
  81. }
  82. /**
  83. * 积分礼品购物车更新礼品数量
  84. */
  85. public function update() {
  86. $pcart_id = intval(input('get.pc_id'));
  87. $quantity = intval(input('get.quantity'));
  88. //兑换失败提示
  89. $msg = lang('pointcart_cart_modcart_fail');
  90. if($pcart_id <= 0 || $quantity <= 0) {
  91. echo json_encode(array('msg'=>$msg));
  92. die;
  93. }
  94. //验证礼品购物车信息是否存在
  95. $pointcart_model = model('pointcart');
  96. $cart_info = $pointcart_model->getPointcartInfo(array('pcart_id'=>$pcart_id,'pmember_id'=>session('member_id')));
  97. if (!$cart_info){
  98. echo json_encode(array('msg'=>$msg)); die;
  99. }
  100. //验证是否能兑换
  101. $data = $pointcart_model->checkExchange($cart_info['pgoods_id'], $quantity, session('member_id'));
  102. if (!$data['state']){
  103. echo json_encode(array('msg'=>$data['msg'])); die;
  104. }
  105. $prod_info = $data['data']['prod_info'];
  106. $quantity = $prod_info['quantity'];
  107. $cart_state = true;
  108. //如果数量发生变化则更新礼品购物车内单个礼品数量
  109. if ($cart_info['pgoods_choosenum'] != $quantity){
  110. $cart_state = $pointcart_model->editPointcart(array('pcart_id'=>$pcart_id,'pmember_id'=>session('member_id')),array('pgoods_choosenum'=>$quantity));
  111. }
  112. if ($cart_state) {
  113. //计算总金额
  114. $amount= $pointcart_model->getPointcartAmount(session('member_id'));
  115. echo json_encode(array('done'=>'true','subtotal'=>$prod_info['pointsamount'],'amount'=>$amount,'quantity'=>$quantity));
  116. die;
  117. }
  118. }
  119. /**
  120. * 积分礼品购物车删除单个礼品
  121. */
  122. public function drop() {
  123. $pcart_id = intval(input('get.pc_id'));
  124. if($pcart_id <= 0) {
  125. echo json_encode(array('done'=>false,'msg'=> lang('ds_common_del_fail'))); die;
  126. }
  127. $pointcart_model = model('pointcart');
  128. $drop_state = $pointcart_model->delPointcartById($pcart_id,session('member_id'));
  129. if ($drop_state){
  130. echo json_encode(array('done'=>true)); die;
  131. } else {
  132. echo json_encode(array('done'=>false,'msg'=>lang('ds_common_del_fail'))); die;
  133. }
  134. }
  135. /**
  136. * 兑换订单流程第一步
  137. */
  138. public function step1(){
  139. //获取符合条件的兑换礼品和总积分
  140. $data = model('pointcart')->getCartGoodsList(session('member_id'));
  141. if (!$data['state']){
  142. $this->error($data['msg'],(string)url('Pointprod/index'));
  143. }
  144. View::assign('pointprod_arr',$data['data']);
  145. //实例化收货地址模型(不显示门店地址)
  146. $address_list = model('address')->getAddressList(array('member_id'=>session('member_id'),'chain_id'=>0), 'address_is_default desc,address_id desc');
  147. View::assign('address_list',$address_list);
  148. return View::fetch($this->template_dir.'pointcart_step1');
  149. }
  150. /**
  151. * 兑换订单流程第二步
  152. */
  153. public function step2() {
  154. $pointcart_model = model('pointcart');
  155. //获取符合条件的兑换礼品和总积分
  156. $data = $pointcart_model->getCartGoodsList(session('member_id'));
  157. if (!$data['state']){
  158. $this->error($data['msg'],(string)url('Pointcart/index'));
  159. }
  160. $pointprod_arr = $data['data'];
  161. unset($data);
  162. //验证积分数是否足够
  163. $data = $pointcart_model->checkPointEnough($pointprod_arr['pgoods_pointall'], session('member_id'));
  164. if (!$data['state']){
  165. $this->error($data['msg'],(string)url('Pointcart/index'));
  166. }
  167. unset($data);
  168. //创建兑换订单
  169. $data = model('pointorder')->createOrder(input('post.'), $pointprod_arr, array('member_id'=>session('member_id'),'member_name'=>session('member_name'),'member_email'=>session('member_email')));
  170. if (!$data['state']){
  171. $this->error($data['msg'],(string)url('Pointcart/step1'));
  172. }
  173. $order_id = $data['data']['order_id'];
  174. $this->redirect('pointcart/step3',['order_id'=>$order_id]);
  175. }
  176. /**
  177. * 流程第三步
  178. */
  179. public function step3($order_arr=array()) {
  180. $order_id = intval(input('order_id'));
  181. if ($order_id <= 0){
  182. $this->error(lang('pointcart_record_error'),(string)url('Index/index'));
  183. }
  184. $condition = array();
  185. $condition[] = array('point_orderid','=',$order_id);
  186. $condition[] = array('point_buyerid','=',session('member_id'));
  187. $order_info = model('pointorder')->getPointorderInfo($condition);
  188. if (!$order_info){
  189. $this->error(lang('pointcart_record_error'),(string)url('Index/index'));
  190. }
  191. View::assign('order_info',$order_info);
  192. return View::fetch($this->template_dir.'pointcart_step2');
  193. }
  194. }