Pointvoucher.php 6.5 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. class Pointvoucher extends BasePointShop
  13. {
  14. public function initialize()
  15. {
  16. parent::initialize(); // TODO: Change the autogenerated stub
  17. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/voucher.lang.php');
  18. if (config('ds_config.voucher_allow') != 1) {
  19. $this->error(lang('voucher_pointunavailable'), HOME_SITE_URL);
  20. }
  21. }
  22. public function index()
  23. {
  24. $this->pointvoucher();
  25. return View::fetch($this->template_dir . 'pointvoucher');
  26. }
  27. /**
  28. * 代金券列表
  29. */
  30. public function pointvoucher()
  31. {
  32. //查询会员及其附属信息
  33. parent::pointshopMInfo();
  34. $voucher_model = model('voucher');
  35. //代金券模板状态
  36. $templatestate_arr = $voucher_model->getTemplateState();
  37. //查询会员信息
  38. $member_info = model('member')->getMemberInfoByID(session('member_id'));
  39. //查询代金券列表
  40. $where = array();
  41. $where[] = array('vouchertemplate_if_private', '=', 0);
  42. $where[] = array('vouchertemplate_state', '=', $templatestate_arr['usable'][0]);
  43. $where[] = array('vouchertemplate_enddate', '>', TIMESTAMP);
  44. if (intval(input('storeclass_id')) > 0) {
  45. $where[] = array('vouchertemplate_sc_id', '=', intval(input('storeclass_id')));
  46. }
  47. if (intval(input('price')) > 0) {
  48. $where[] = array('vouchertemplate_price', '=', intval(input('price')));
  49. }
  50. //查询仅我能兑换和所需积分
  51. $points_filter = array();
  52. if (intval(input('isable')) == 1) {
  53. $points_filter['isable'] = $member_info['member_points'];
  54. }
  55. if (intval(input('points_min')) > 0) {
  56. $points_filter['min'] = intval(input('points_min'));
  57. }
  58. if (intval(input('points_max')) > 0) {
  59. $points_filter['max'] = intval(input('points_max'));
  60. }
  61. if (count($points_filter) > 0) {
  62. asort($points_filter);
  63. if (count($points_filter) > 1) {
  64. $points_filter = array_values($points_filter);
  65. $where[] = array('vouchertemplate_points', 'between', array($points_filter[0], $points_filter[1]));
  66. } else {
  67. if ($points_filter['min']) {
  68. $where[] = array('vouchertemplate_points', '>=', $points_filter['min']);
  69. } elseif ($points_filter['max']) {
  70. $where[] = array('vouchertemplate_points', '<=', $points_filter['max']);
  71. } elseif ($points_filter['isable']) {
  72. $where[] = array('vouchertemplate_points', '<=', $points_filter['isable']);
  73. }
  74. }
  75. }
  76. //排序
  77. switch (input('orderby')) {
  78. case 'exchangenumdesc':
  79. $orderby = 'vouchertemplate_giveout desc,';
  80. break;
  81. case 'exchangenumasc':
  82. $orderby = 'vouchertemplate_giveout asc,';
  83. break;
  84. case 'pointsdesc':
  85. $orderby = 'vouchertemplate_points desc,';
  86. break;
  87. case 'pointsasc':
  88. $orderby = 'vouchertemplate_points asc,';
  89. break;
  90. default:
  91. $orderby = '';
  92. }
  93. $orderby .= 'vouchertemplate_id desc';
  94. $voucherlist = $voucher_model->getVouchertemplateList($where, '*', 0, 18, $orderby);
  95. View::assign('voucherlist', $voucherlist);
  96. View::assign('show_page', $voucher_model->page_info->render());
  97. //查询代金券面额
  98. $pricelist = $voucher_model->getVoucherPriceList();
  99. View::assign('pricelist', $pricelist);
  100. //查询店铺分类
  101. $store_class = rkcache('storeclass', true);
  102. View::assign('store_class', $store_class);
  103. //分类导航
  104. $nav_link = array(
  105. 0 => array('title' => lang('homepage'), 'link' => HOME_SITE_URL),
  106. 1 => array('title' => lang('integral_center'), 'link' => (string)url('Pointshop/index')),
  107. 2 => array('title' => lang('voucher_list'))
  108. );
  109. View::assign('nav_link_list', $nav_link);
  110. }
  111. /**
  112. * 兑换代金券
  113. */
  114. public function voucherexchange()
  115. {
  116. $vid = intval(input('param.vid'));
  117. $result = true;
  118. $message = "";
  119. if ($vid <= 0) {
  120. $result = false;
  121. }
  122. if ($result) {
  123. //查询可兑换代金券模板信息
  124. $template_info = model('voucher')->getCanChangeTemplateInfo($vid, intval(session('member_id')), intval(session('store_id')));
  125. if ($template_info['state'] == false) {
  126. $result = false;
  127. $message = $template_info['msg'];
  128. } else {
  129. //查询会员信息
  130. $member_info = model('member')->getMemberInfoByID(session('member_id'));
  131. View::assign('member_info', $member_info);
  132. View::assign('template_info', $template_info['info']);
  133. }
  134. }
  135. View::assign('message', $message);
  136. View::assign('result', $result);
  137. echo View::fetch($this->template_dir . 'exchange');
  138. exit;
  139. }
  140. /**
  141. * 兑换代金券保存信息
  142. *
  143. */
  144. public function voucherexchange_save()
  145. {
  146. if (session('is_login') != '1') {
  147. ds_json_encode(10001, lang('param_error'));
  148. }
  149. $vid = intval(input('post.vid'));
  150. if ($vid <= 0) {
  151. ds_json_encode(10001, lang('param_error'));
  152. }
  153. $voucher_model = model('voucher');
  154. //验证是否可以兑换代金券
  155. $data = $voucher_model->getCanChangeTemplateInfo($vid, intval(session('member_id')), intval(session('store_id')));
  156. if ($data['state'] == false) {
  157. ds_json_encode(10001, $data['msg']);
  158. }
  159. //添加代金券信息
  160. $data = $voucher_model->exchangeVoucher($data['info'], session('member_id'), session('member_name'));
  161. if ($data['state'] == true) {
  162. ds_json_encode(10000, $data['msg']);
  163. } else {
  164. ds_json_encode(10001, $data['msg']);
  165. }
  166. }
  167. }