Pointvoucher.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace app\home\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. * ============================================================================
  7. *
  8. * ============================================================================
  9. *
  10. * ----------------------------------------------------------------------------
  11. *
  12. * ============================================================================
  13. * 控制器
  14. */
  15. class Pointvoucher extends BasePointShop
  16. {
  17. public function initialize()
  18. {
  19. parent::initialize(); // TODO: Change the autogenerated stub
  20. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/voucher.lang.php');
  21. if (config('ds_config.voucher_allow') != 1) {
  22. $this->error(lang('voucher_pointunavailable'), HOME_SITE_URL);
  23. }
  24. }
  25. public function index()
  26. {
  27. $this->pointvoucher();
  28. return View::fetch($this->template_dir . 'pointvoucher');
  29. }
  30. /**
  31. * 代金券列表
  32. */
  33. public function pointvoucher()
  34. {
  35. //查询会员及其附属信息
  36. parent::pointshopMInfo();
  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. View::assign('voucherlist', $voucherlist);
  99. View::assign('show_page', $voucher_model->page_info->render());
  100. //查询代金券面额
  101. $pricelist = $voucher_model->getVoucherPriceList();
  102. View::assign('pricelist', $pricelist);
  103. //查询店铺分类
  104. $store_class = rkcache('storeclass', true);
  105. View::assign('store_class', $store_class);
  106. //分类导航
  107. $nav_link = array(
  108. 0 => array('title' => lang('homepage'), 'link' => HOME_SITE_URL),
  109. 1 => array('title' => lang('integral_center'), 'link' => (string)url('Pointshop/index')),
  110. 2 => array('title' => lang('voucher_list'))
  111. );
  112. View::assign('nav_link_list', $nav_link);
  113. }
  114. /**
  115. * 兑换代金券
  116. */
  117. public function voucherexchange()
  118. {
  119. $vid = intval(input('param.vid'));
  120. $result = true;
  121. $message = "";
  122. if ($vid <= 0) {
  123. $result = false;
  124. }
  125. if ($result) {
  126. //查询可兑换代金券模板信息
  127. $template_info = model('voucher')->getCanChangeTemplateInfo($vid, intval(session('member_id')), intval(session('store_id')));
  128. if ($template_info['state'] == false) {
  129. $result = false;
  130. $message = $template_info['msg'];
  131. } else {
  132. //查询会员信息
  133. $member_info = model('member')->getMemberInfoByID(session('member_id'));
  134. View::assign('member_info', $member_info);
  135. View::assign('template_info', $template_info['info']);
  136. }
  137. }
  138. View::assign('message', $message);
  139. View::assign('result', $result);
  140. echo View::fetch($this->template_dir . 'exchange');
  141. exit;
  142. }
  143. /**
  144. * 兑换代金券保存信息
  145. *
  146. */
  147. public function voucherexchange_save()
  148. {
  149. if (session('is_login') != '1') {
  150. ds_json_encode(10001, lang('param_error'));
  151. }
  152. $vid = intval(input('post.vid'));
  153. if ($vid <= 0) {
  154. ds_json_encode(10001, lang('param_error'));
  155. }
  156. $voucher_model = model('voucher');
  157. //验证是否可以兑换代金券
  158. $data = $voucher_model->getCanChangeTemplateInfo($vid, intval(session('member_id')), intval(session('store_id')));
  159. if ($data['state'] == false) {
  160. ds_json_encode(10001, $data['msg']);
  161. }
  162. //添加代金券信息
  163. $data = $voucher_model->exchangeVoucher($data['info'], session('member_id'), session('member_name'));
  164. if ($data['state'] == true) {
  165. ds_json_encode(10000, $data['msg']);
  166. } else {
  167. ds_json_encode(10001, $data['msg']);
  168. }
  169. }
  170. }