Pointcart.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Lang;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  9. * 网站地址: https://www.valimart.net/
  10. * ----------------------------------------------------------------------------
  11. *
  12. * ============================================================================
  13. * 积分兑换购物车控制器
  14. */
  15. class Pointcart extends MobileMember {
  16. public function initialize() {
  17. parent::initialize();
  18. Lang::load(base_path().'home/lang/'.config('lang.default_lang').'/pointcart.lang.php');
  19. //判断系统是否开启积分和积分兑换功能
  20. if (config('ds_config.pointprod_isuse') != 1) {
  21. ds_json_encode(10001,lang('pointcart_unavailable'));
  22. }
  23. }
  24. /**
  25. * @api {POST} api/Pointcart/cart_list 购物车礼品列表
  26. * @apiVersion 1.0.0
  27. * @apiGroup Pointcart
  28. *
  29. * @apiHeader {String} X-DS-KEY 用户授权token
  30. *
  31. *
  32. * @apiSuccess {String} code 返回码,10000为成功
  33. * @apiSuccess {String} message 返回消息
  34. * @apiSuccess {Object} result 返回数据
  35. * @apiSuccess {Int} result.pgoods_pointall 所需总积分 (返回字段参考pointscart表)
  36. * @apiSuccess {Object[]} result.cart_array 返回数据
  37. */
  38. public function cart_list() {
  39. $cart_goods = array();
  40. $pointcart_model = model('pointcart');
  41. $data = $pointcart_model->getPCartListAndAmount(array('pmember_id'=>$this->member_info['member_id']));
  42. ds_json_encode(10000, lang('ds_common_op_succ'),array('pgoods_pointall' => $data['data']['cartgoods_pointall'],'cart_array'=>$data['data']['cartgoods_list']));
  43. return View::fetch($this->template_dir.'pointcart_list');
  44. }
  45. /**
  46. * @api {POST} api/Pointcart/cart_del 购物车删除
  47. * @apiVersion 1.0.0
  48. * @apiGroup Pointcart
  49. *
  50. * @apiHeader {String} X-DS-KEY 用户授权token
  51. *
  52. * @apiParam {String} pcart_id 购物车主键ID 例3,5,8
  53. *
  54. * @apiSuccess {String} code 返回码,10000为成功
  55. * @apiSuccess {String} message 返回消息
  56. * @apiSuccess {Object} result 返回数据
  57. */
  58. public function cart_del() {
  59. $pcart_id = intval(input('param.pcart_id'));
  60. if($pcart_id <= 0) {
  61. ds_json_encode(10001,lang('ds_common_del_fail'));
  62. }
  63. $pointcart_model = model('pointcart');
  64. $drop_state = $pointcart_model->delPointcartById($pcart_id,$this->member_info['member_id']);
  65. if ($drop_state){
  66. ds_json_encode(10000,'');
  67. } else {
  68. ds_json_encode(10001,lang('ds_common_del_fail'));
  69. }
  70. }
  71. /**
  72. * @api {POST} api/Pointcart/cart_edit_quantity 更新购物车购买数量
  73. * @apiVersion 1.0.0
  74. * @apiGroup Pointcart
  75. *
  76. * @apiHeader {String} X-DS-KEY 用户授权token
  77. *
  78. * @apiParam {Int} pcart_id 购物车主键ID
  79. * @apiParam {Int} quantity 修改数量
  80. *
  81. * @apiSuccess {String} code 返回码,10000为成功
  82. * @apiSuccess {String} message 返回消息
  83. * @apiSuccess {Object} result 返回数据
  84. * @apiSuccess {Int} result.subtotal 小计
  85. * @apiSuccess {Int} result.amount 总积分
  86. * @apiSuccess {Int} result.quantity 积分商品数量
  87. */
  88. public function cart_edit_quantity() {
  89. $pcart_id = intval(input('param.pcart_id'));
  90. $quantity = intval(input('param.quantity'));
  91. //兑换失败提示
  92. $msg = lang('pointcart_cart_modcart_fail');
  93. if($pcart_id <= 0 || $quantity <= 0) {
  94. ds_json_encode(10001,$msg);
  95. }
  96. //验证礼品购物车信息是否存在
  97. $pointcart_model = model('pointcart');
  98. $cart_info = $pointcart_model->getPointcartInfo(array('pcart_id'=>$pcart_id,'pmember_id'=>$this->member_info['member_id']));
  99. if (!$cart_info){
  100. ds_json_encode(10001,$msg);
  101. }
  102. //验证是否能兑换
  103. $data = $pointcart_model->checkExchange($cart_info['pgoods_id'], $quantity, $this->member_info['member_id']);
  104. if (!$data['state']){
  105. ds_json_encode(10001,$data['msg']);
  106. }
  107. $prod_info = $data['data']['prod_info'];
  108. $quantity = $prod_info['quantity'];
  109. $cart_state = true;
  110. //如果数量发生变化则更新礼品购物车内单个礼品数量
  111. if ($cart_info['pgoods_choosenum'] != $quantity){
  112. $cart_state = $pointcart_model->editPointcart(array('pcart_id'=>$pcart_id,'pmember_id'=>$this->member_info['member_id']),array('pgoods_choosenum'=>$quantity));
  113. }
  114. if ($cart_state) {
  115. //计算总金额
  116. $amount= $pointcart_model->getPointcartAmount($this->member_info['member_id']);
  117. ds_json_encode(10000,'',array('subtotal'=>$prod_info['pointsamount'],'amount'=>$amount,'quantity'=>$quantity));
  118. }
  119. }
  120. /**
  121. * @api {POST} api/Pointcart/add 购物车添加礼品
  122. * @apiVersion 1.0.0
  123. * @apiGroup Pointcart
  124. *
  125. * @apiHeader {String} X-DS-KEY 用户授权token
  126. *
  127. * @apiParam {String} pgid 积分商品id
  128. * @apiParam {String} quantity 数量
  129. *
  130. * @apiSuccess {String} code 返回码,10000为成功
  131. * @apiSuccess {String} message 返回消息
  132. * @apiSuccess {Object} result 返回数据
  133. */
  134. public function add() {
  135. $pgid = intval(input('post.pgid'));
  136. $quantity = intval(input('post.quantity'));
  137. if ($pgid <= 0 || $quantity <= 0) {
  138. ds_json_encode(10001,lang('param_error'));
  139. }
  140. $pointcart_model = model('pointcart');
  141. //$pointcart_model->delPointcart(array('pmember_id' => $this->member_info['member_id']));
  142. //验证是否能兑换
  143. $data = $pointcart_model->checkExchange($pgid, $quantity, $this->member_info['member_id']);
  144. if (!$data['state']) {
  145. ds_json_encode(10001,$data['msg']);
  146. }
  147. //验证积分礼品是否存在购物车中
  148. $check_cart = $pointcart_model->getPointcartInfo(array('pgoods_id' => $pgid, 'pmember_id' => $this->member_info['member_id']));
  149. if (!empty($check_cart)) {
  150. $cart_state = $pointcart_model->editPointcart(array('pcart_id'=>$check_cart['pcart_id'],'pmember_id'=>$this->member_info['member_id']),array('pgoods_choosenum'=>$quantity));
  151. ds_json_encode(10000, lang('ds_common_op_succ'),array('done' => 'ok1'));
  152. }
  153. $prod_info = $data['data']['prod_info'];
  154. $insert_arr = array();
  155. $insert_arr['pmember_id'] = $this->member_info['member_id'];
  156. $insert_arr['pgoods_id'] = $prod_info['pgoods_id'];
  157. $insert_arr['pgoods_name'] = $prod_info['pgoods_name'];
  158. $insert_arr['pgoods_points'] = $prod_info['pgoods_points'];
  159. $insert_arr['pgoods_choosenum'] = $prod_info['quantity'];
  160. $insert_arr['pgoods_image'] = $prod_info['pgoods_image_old'];
  161. $cart_state = $pointcart_model->addPointcart($insert_arr);
  162. if($cart_state){
  163. ds_json_encode(10000, lang('ds_common_op_succ'),array('done' => 'ok'));
  164. }else{
  165. ds_json_encode(10001,$data['msg']);
  166. }
  167. }
  168. /**
  169. * @api {POST} api/Pointcart/step1 兑换订单流程第一步
  170. * @apiVersion 1.0.0
  171. * @apiGroup Pointcart
  172. *
  173. * @apiHeader {String} X-DS-KEY 用户授权token
  174. *
  175. * @apiParam {String} cart_id 购买信息 商品id|数量
  176. * @apiParam {String} ifcart 是否从购物车获取商品 0否1是
  177. *
  178. * @apiSuccess {String} code 返回码,10000为成功
  179. * @apiSuccess {String} message 返回消息
  180. * @apiSuccess {Object} result 返回数据
  181. * @apiSuccess {Object} result.pointprod_arr 积分购物车信息
  182. * @apiSuccess {Object} result.pointprod_arr.pgoods_pointall 所需总积分
  183. * @apiSuccess {Object[]} result.pointprod_arr.pointprod_list 积分商品列表 (返回字段参考pointsgoods表)
  184. * @apiSuccess {Int} result.pointprod_arr.pointprod_list.quantity 购买数量
  185. * @apiSuccess {Int} result.pointprod_arr.pointprod_list.onepoints 所需积分
  186. * @apiSuccess {Int} result.pointprod_arr.pointprod_list.pgoods_limitgradename 所需用户等级
  187. * @apiSuccess {String} result.pointprod_arr.pointprod_list.ex_state 兑换状态代码 end不可兑换willbe即将开始going进行中
  188. * @apiSuccess {Object} result.address_info 用户收货地址信息 (返回字段参考address表)
  189. */
  190. public function step1() {
  191. //获取符合条件的兑换礼品和总积分
  192. $data = model('pointcart')->getCartGoodsList($this->member_info['member_id'],input('post.'));
  193. if (!$data['state']) {
  194. ds_json_encode(10001,$data['msg']);
  195. }
  196. //实例化收货地址模型(不显示门店地址)
  197. $address_list = model('address')->getAddressList(array('member_id' => $this->member_info['member_id'], 'chain_id' => 0), 'address_is_default desc,address_id desc');
  198. //收货地址为空 返回兑换里面信息
  199. ds_json_encode(10000, '', array('pointprod_arr' => $data['data'], 'address_info' => $address_list?$address_list[0]:false));
  200. }
  201. /**
  202. * @api {POST} api/Pointcart/step2 兑换订单流程第二步
  203. * @apiVersion 1.0.0
  204. * @apiGroup Pointcart
  205. *
  206. * @apiHeader {String} X-DS-KEY 用户授权token
  207. *
  208. * @apiParam {String} cart_id 购买信息 商品id|数量
  209. * @apiParam {String} ifcart 是否从购物车获取商品 0否1是
  210. * @apiParam {String} address_options 地址id
  211. * @apiParam {String} pcart_message 用户备注
  212. *
  213. * @apiSuccess {String} code 返回码,10000为成功
  214. * @apiSuccess {String} message 返回消息
  215. * @apiSuccess {Object} result 返回数据
  216. * @apiSuccess {Int} result.pointprod_arr 订单ID
  217. */
  218. public function step2() {
  219. $pointcart_model = model('pointcart');
  220. //获取符合条件的兑换礼品和总积分
  221. $data = $pointcart_model->getCartGoodsList($this->member_info['member_id'],input('post.'));
  222. if (!$data['state']) {
  223. ds_json_encode(10001,$data['msg']);
  224. }
  225. $pointprod_arr = $data['data'];
  226. unset($data);
  227. //验证积分数是否足够
  228. $data = $pointcart_model->checkPointEnough($pointprod_arr['pgoods_pointall'], $this->member_info['member_id']);
  229. if (!$data['state']) {
  230. ds_json_encode(10001,$data['msg']);
  231. }
  232. unset($data);
  233. //创建兑换订单
  234. $data = model('pointorder')->createOrder(input('post.'), $pointprod_arr, array('member_id' => $this->member_info['member_id'], 'member_name' => $this->member_info['member_name'], 'member_email' => $this->member_info['member_email']));
  235. if (!$data['state']) {
  236. ds_json_encode(10001,$data['msg']);
  237. }
  238. $order_id = $data['data']['order_id'];
  239. ds_json_encode(10000, '',array('pointprod_arr' => $order_id));
  240. }
  241. /**
  242. * @api {POST} api/Pointcart/cart_count 检查购物车数量
  243. * @apiVersion 1.0.0
  244. * @apiGroup Pointcart
  245. *
  246. * @apiHeader {String} X-DS-KEY 用户授权token
  247. *
  248. * @apiSuccess {String} code 返回码,10000为成功
  249. * @apiSuccess {String} message 返回消息
  250. * @apiSuccess {Object} result 返回数据
  251. * @apiSuccess {Int} result.cart_count 购物车数量
  252. */
  253. public function cart_count() {
  254. $pointcart_model = model('pointcart');
  255. $count = $pointcart_model->getPointcartCount($this->member_info['member_id']);
  256. $data['cart_count'] = $count;
  257. ds_json_encode(10000,'',$data);
  258. }
  259. }
  260. ?>