Pointvoucher.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Lang;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. *
  9. * ----------------------------------------------------------------------------
  10. *
  11. * ============================================================================
  12. * 控制器
  13. */
  14. class Pointvoucher extends MobileMall
  15. {
  16. public function initialize()
  17. {
  18. parent::initialize(); // TODO: Change the autogenerated stub
  19. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/voucher.lang.php');
  20. //判断系统是否开启积分兑换功能
  21. if (config('ds_config.points_isuse') != 1 || config('ds_config.pointprod_isuse') != 1) {
  22. ds_json_encode(10001, lang('pointshop_unavailable'));
  23. }
  24. if (config('ds_config.voucher_allow') != 1) {
  25. ds_json_encode(10001, lang('voucher_pointunavailable'));
  26. }
  27. }
  28. public function index()
  29. {
  30. $this->pointvoucher();
  31. }
  32. /**
  33. * 代金券列表
  34. */
  35. public function pointvoucher()
  36. {
  37. $voucher_model = model('voucher');
  38. //代金券模板状态
  39. $templatestate_arr = $voucher_model->getTemplateState();
  40. //查询会员信息
  41. $member_info = model('member')->getMemberInfoByID(session('member_id'));
  42. //查询代金券列表
  43. $where = array();
  44. $where[] = array('vouchertemplate_if_private', '=', 0);
  45. $where[] = array('vouchertemplate_state', '=', $templatestate_arr['usable'][0]);
  46. $where[] = array('vouchertemplate_enddate', '>', TIMESTAMP);
  47. if (intval(input('storeclass_id')) > 0) {
  48. $where[] = array('vouchertemplate_sc_id', '=', intval(input('storeclass_id')));
  49. }
  50. if (intval(input('price')) > 0) {
  51. $where[] = array('vouchertemplate_price', '=', intval(input('price')));
  52. }
  53. //查询仅我能兑换和所需积分
  54. $points_filter = array();
  55. if (intval(input('isable')) == 1) {
  56. $points_filter['isable'] = $member_info['member_points'];
  57. }
  58. if (intval(input('points_min')) > 0) {
  59. $points_filter['min'] = intval(input('points_min'));
  60. }
  61. if (intval(input('points_max')) > 0) {
  62. $points_filter['max'] = intval(input('points_max'));
  63. }
  64. if (count($points_filter) > 0) {
  65. asort($points_filter);
  66. if (count($points_filter) > 1) {
  67. $points_filter = array_values($points_filter);
  68. $where[] = array('vouchertemplate_points', 'between', array($points_filter[0], $points_filter[1]));
  69. } else {
  70. if ($points_filter['min']) {
  71. $where[] = array('vouchertemplate_points', '>=', $points_filter['min']);
  72. } elseif ($points_filter['max']) {
  73. $where[] = array('vouchertemplate_points', '<=', $points_filter['max']);
  74. } elseif ($points_filter['isable']) {
  75. $where[] = array('vouchertemplate_points', '<=', $points_filter['isable']);
  76. }
  77. }
  78. }
  79. //排序
  80. switch (input('orderby')) {
  81. case 'exchangenumdesc':
  82. $orderby = 'vouchertemplate_giveout desc,';
  83. break;
  84. case 'exchangenumasc':
  85. $orderby = 'vouchertemplate_giveout asc,';
  86. break;
  87. case 'pointsdesc':
  88. $orderby = 'vouchertemplate_points desc,';
  89. break;
  90. case 'pointsasc':
  91. $orderby = 'vouchertemplate_points asc,';
  92. break;
  93. default:
  94. $orderby = '';
  95. }
  96. $orderby .= 'vouchertemplate_id desc';
  97. $voucherlist = $voucher_model->getVouchertemplateList($where, '*', 0, 18, $orderby);
  98. $page_count = $voucher_model->page_info;
  99. //查询代金券面额
  100. $pricelist = $voucher_model->getVoucherPriceList();
  101. //查询店铺分类
  102. $store_class = rkcache('storeclass', true);
  103. $result = array_merge(array('voucherlist' => $voucherlist, 'pricelist' => $pricelist, 'store_class' => $store_class), mobile_page($page_count));
  104. ds_json_encode(10000, '', $result);
  105. }
  106. /**
  107. * 兑换代金券保存信息
  108. *
  109. */
  110. public function voucherexchange_save()
  111. {
  112. $member_id = $this->getMemberIdIfExists();
  113. if (!$member_id) {
  114. ds_json_encode(10001, lang('param_error'));
  115. }
  116. $vid = intval(input('post.vid'));
  117. if ($vid <= 0) {
  118. ds_json_encode(10001, lang('param_error'));
  119. }
  120. $seller_model = model('seller');
  121. $seller_info = $seller_model->getSellerInfo(array('member_id' => $member_id));
  122. $voucher_model = model('voucher');
  123. //验证是否可以兑换代金券
  124. $data = $voucher_model->getCanChangeTemplateInfo($vid, intval($member_id), $seller_info['store_id']);
  125. if ($data['state'] == false) {
  126. ds_json_encode(10001, $data['msg']);
  127. }
  128. //添加代金券信息
  129. $data = $voucher_model->exchangeVoucher($data['info'], $member_id);
  130. if ($data['state'] == true) {
  131. ds_json_encode(10000, $data['msg']);
  132. } else {
  133. ds_json_encode(10001, $data['msg']);
  134. }
  135. }
  136. }