Pointcart.php 12 KB

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