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