Pointvoucher.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Lang;
  4. /**
  5. *
  6. *
  7. * ----------------------------------------------------------------------------
  8. *
  9. * 控制器
  10. */
  11. class Pointvoucher extends MobileMall
  12. {
  13. public function initialize()
  14. {
  15. parent::initialize(); // TODO: Change the autogenerated stub
  16. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/voucher.lang.php');
  17. //判断系统是否开启积分兑换功能
  18. if (config('ds_config.points_isuse') != 1 || config('ds_config.pointprod_isuse') != 1) {
  19. ds_json_encode(10001, lang('pointshop_unavailable'));
  20. }
  21. if (config('ds_config.voucher_allow') != 1) {
  22. ds_json_encode(10001, lang('voucher_pointunavailable'));
  23. }
  24. }
  25. public function index()
  26. {
  27. $this->pointvoucher();
  28. }
  29. /**
  30. * 代金券列表
  31. */
  32. public function pointvoucher()
  33. {
  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. $page_count = $voucher_model->page_info;
  96. //查询代金券面额
  97. $pricelist = $voucher_model->getVoucherPriceList();
  98. //查询店铺分类
  99. $store_class = rkcache('storeclass', true);
  100. $result = array_merge(array('voucherlist' => $voucherlist, 'pricelist' => $pricelist, 'store_class' => $store_class), mobile_page($page_count));
  101. ds_json_encode(10000, '', $result);
  102. }
  103. /**
  104. * 兑换代金券保存信息
  105. *
  106. */
  107. public function voucherexchange_save()
  108. {
  109. $member_id = $this->getMemberIdIfExists();
  110. if (!$member_id) {
  111. ds_json_encode(10001, lang('param_error'));
  112. }
  113. $vid = intval(input('post.vid'));
  114. if ($vid <= 0) {
  115. ds_json_encode(10001, lang('param_error'));
  116. }
  117. $seller_model = model('seller');
  118. $seller_info = $seller_model->getSellerInfo(array('member_id' => $member_id));
  119. $voucher_model = model('voucher');
  120. //验证是否可以兑换代金券
  121. $data = $voucher_model->getCanChangeTemplateInfo($vid, intval($member_id), $seller_info['store_id']);
  122. if ($data['state'] == false) {
  123. ds_json_encode(10001, $data['msg']);
  124. }
  125. //添加代金券信息
  126. $data = $voucher_model->exchangeVoucher($data['info'], $member_id);
  127. if ($data['state'] == true) {
  128. ds_json_encode(10000, $data['msg']);
  129. } else {
  130. ds_json_encode(10001, $data['msg']);
  131. }
  132. }
  133. }