Pointcart.php 8.4 KB

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