Pointcart.php 12 KB

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