Pointcart.php 8.6 KB

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