Bonus.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\View;
  4. use think\facade\Db;
  5. use think\facade\Lang;
  6. /**
  7. * ============================================================================
  8. *
  9. * ============================================================================
  10. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  11. * 网站地址: https://www.valimart.net/
  12. * ----------------------------------------------------------------------------
  13. *
  14. * ============================================================================
  15. * 平台红包 控制器
  16. */
  17. class Bonus extends AdminControl {
  18. public function initialize() {
  19. parent::initialize();
  20. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/bonus.lang.php');
  21. }
  22. public function index() {
  23. $condition = array();
  24. $bonus_name = input('param.bonus_name');
  25. if (!empty($bonus_name)) {
  26. $condition[]=array('bonus_name','like', '%' . $bonus_name . '%');
  27. }
  28. //红包是否有效
  29. $bonus_state = intval(input('get.bonus_state'));
  30. if ($bonus_state) {
  31. $condition[]=array('bonus_state','=',$bonus_state);
  32. }
  33. //红包类型
  34. $bonus_type = intval(input('get.bonus_type'));
  35. if ($bonus_type) {
  36. $condition[]=array('bonus_type','=',$bonus_type);
  37. }
  38. $bonus_model = model('bonus');
  39. $bonus_list = $bonus_model->getBonusList($condition, 10);
  40. //红包类型
  41. View::assign('bonus_type_list', $bonus_model->bonus_type_list());
  42. //红包状态
  43. View::assign('bonus_state_list', $bonus_model->bonus_state_list());
  44. View::assign('bonus_list', $bonus_list);
  45. View::assign('show_page', $bonus_model->page_info->render());
  46. $this->setAdminCurItem('index');
  47. return View::fetch();
  48. }
  49. /**
  50. * 添加吸粉红包
  51. */
  52. public function add() {
  53. $bonus_model = model('bonus');
  54. if (!request()->isPost()) {
  55. $bonus = array(
  56. 'bonus_type' => 1,
  57. 'bonus_begintime' => TIMESTAMP,
  58. 'bonus_endtime' => TIMESTAMP+3600*24*7,
  59. );
  60. //红包类型
  61. View::assign('bonus_type_list', $bonus_model->bonus_type_list());
  62. View::assign('bonus', $bonus);
  63. return View::fetch('form');
  64. } else {
  65. $bonus_totalprice = floatval(input('param.bonus_totalprice'));
  66. $bonus_pricetype = intval(input('param.bonus_pricetype'));
  67. $bonus_fixedprice = floatval(input('param.bonus_fixedprice'));
  68. $bonus_randomprice_start = floatval(input('param.bonus_randomprice_start'));
  69. $bonus_randomprice_end = floatval(input('param.bonus_randomprice_end'));
  70. //计算写入吸粉红包领取记录表
  71. $data_bonusreceive = array(); //红包领取记录
  72. if ($bonus_pricetype == 1) {
  73. //固定金额
  74. if ($bonus_fixedprice == 0 || $bonus_fixedprice > $bonus_totalprice) {
  75. $this->error(lang('bonus_fixedprice_error'));
  76. }
  77. if (($bonus_totalprice*100) % ($bonus_fixedprice*100) != 0) {
  78. $this->error(lang('bonus_fixedprice_error'));
  79. }
  80. //生成红包领取记录-固定金额
  81. for ($i = 0; $i < $bonus_totalprice / $bonus_fixedprice; $i++) {
  82. $data_bonusreceive[] = array(
  83. 'bonusreceive_price' => $bonus_fixedprice
  84. );
  85. }
  86. $bonus_randomprice_start = 0;
  87. $bonus_randomprice_end = 0;
  88. } else {
  89. if ($bonus_randomprice_start == 0 || $bonus_randomprice_start > $bonus_totalprice || $bonus_randomprice_end > $bonus_totalprice || $bonus_randomprice_start >= $bonus_randomprice_end) {
  90. $this->error(lang('bonus_randomprice_error'));
  91. }
  92. //生成红包领取记录-随机金额
  93. $surplus_price = $bonus_totalprice; //剩余未计算金额
  94. while (true) {
  95. if ($surplus_price <= $bonus_randomprice_end) {
  96. $bonusreceive_price = $surplus_price;
  97. } else {
  98. $bonusreceive_price = rand($bonus_randomprice_start * 100, $bonus_randomprice_end * 100) / 100;
  99. }
  100. $surplus_price -= $bonusreceive_price;
  101. $data_bonusreceive[] = array(
  102. 'bonusreceive_price' => $bonusreceive_price
  103. );
  104. if ($surplus_price == 0) {
  105. break;
  106. }
  107. }
  108. $bonus_fixedprice = 0;
  109. }
  110. $data_bonus = array(
  111. 'bonus_type' => input('param.bonus_type'),
  112. 'bonus_name' => input('param.bonus_name'),
  113. 'bonus_remark' => input('param.bonus_remark'),
  114. 'bonus_blessing' => input('param.bonus_blessing'),
  115. 'bonus_begintime' => strtotime(input('param.bonus_begintime')),
  116. 'bonus_endtime' => strtotime(input('param.bonus_endtime')),
  117. 'bonus_state' => 1,
  118. 'bonus_totalprice' => $bonus_totalprice,
  119. 'bonus_pricetype' => $bonus_pricetype,
  120. 'bonus_fixedprice' => $bonus_fixedprice,
  121. 'bonus_randomprice_start' => $bonus_randomprice_start,
  122. 'bonus_randomprice_end' => $bonus_randomprice_end,
  123. );
  124. $bonus_id = $bonus_model->addBonus($data_bonus);
  125. if ($bonus_id > 0) {
  126. foreach ($data_bonusreceive as $key => $bonusreceive) {
  127. $data_bonusreceive[$key]['bonus_id'] = $bonus_id;
  128. }
  129. Db::name('bonusreceive')->insertAll($data_bonusreceive);
  130. $this->log(lang('ds_add') . lang('ds_bonus') . '[ID' . $bonus_id . ']', 1);
  131. dsLayerOpenSuccess(lang('ds_common_save_succ'));
  132. } else {
  133. $this->error(lang('ds_common_save_fail'));
  134. }
  135. }
  136. }
  137. /**
  138. * 编辑吸粉红包 不可以对金额以及红包类型进行编辑。
  139. */
  140. public function edit() {
  141. $bonus_id = intval(input('param.bonus_id'));
  142. if ($bonus_id < 0) {
  143. ds_json_encode(10000, lang('param_error'));
  144. }
  145. $bonus_model = model('bonus');
  146. $condition = array();
  147. $condition[] = array('bonus_id','=',$bonus_id);
  148. if (!request()->isPost()) {
  149. $bonus = $bonus_model->getOneBonus($condition);
  150. View::assign('bonus', $bonus);
  151. //红包类型
  152. View::assign('bonus_type_list', $bonus_model->bonus_type_list());
  153. return View::fetch('form');
  154. } else {
  155. $data_bonus = array(
  156. 'bonus_name' => input('param.bonus_name'),
  157. 'bonus_remark' => input('param.bonus_remark'),
  158. 'bonus_blessing' => input('param.bonus_blessing'),
  159. 'bonus_begintime' => strtotime(input('param.bonus_begintime')),
  160. 'bonus_endtime' => strtotime(input('param.bonus_endtime')),
  161. );
  162. $bonus_model->editBonus($condition, $data_bonus);
  163. $this->log(lang('ds_edit') . lang('ds_bonus') . '[ID' . $bonus_id . ']', 1);
  164. dsLayerOpenSuccess(lang('ds_common_save_succ'));
  165. }
  166. }
  167. /**
  168. * 设置红包失效 1正在进行 2过期 3失效
  169. */
  170. public function invalid() {
  171. $bonus_id = intval(input('param.bonus_id'));
  172. if ($bonus_id < 0) {
  173. ds_json_encode(10000, lang('param_error'));
  174. }
  175. $bonus_model = model('bonus');
  176. $condition = array();
  177. $condition[] = array('bonus_id','=',$bonus_id);
  178. $data['bonus_state'] = 3;
  179. $bonus_model->editBonus($condition, $data);
  180. $this->log(lang('ds_edit') . lang('ds_bonus') . '[ID' . $bonus_id . ']', 1);
  181. ds_json_encode(10000, lang('ds_common_op_succ'));
  182. }
  183. /**
  184. * 领取列表
  185. */
  186. public function receive() {
  187. $bonus_id = intval(input('param.bonus_id'));
  188. if ($bonus_id < 0) {
  189. $this->error(lang('param_error'));
  190. }
  191. $condition = array();
  192. $condition[] = array('bonus_id','=',$bonus_id);
  193. $bonus_model = model('bonus');
  194. $bonusreceive_list = $bonus_model->getBonusreceiveList($condition, 10);
  195. View::assign('bonusreceive_list', $bonusreceive_list);
  196. View::assign('show_page', $bonus_model->page_info->render());
  197. return View::fetch();
  198. }
  199. //链接信息
  200. public function link()
  201. {
  202. $bonus_id = intval(input('param.bonus_id'));
  203. if ($bonus_id < 0) {
  204. $this->error(lang('param_error'));
  205. }
  206. $condition = array();
  207. $condition[] = array('bonus_id','=',$bonus_id);
  208. $bonus_model = model('bonus');
  209. $bonus = $bonus_model->getOneBonus($condition);
  210. View::assign('bonus', $bonus);
  211. $bonus_url = config('ds_config.h5_site_url')."/pages/home/bonus/Detail?bonus_id=".$bonus['bonus_id'];
  212. View::assign('bonus_url', $bonus_url);
  213. return View::fetch();
  214. }
  215. protected function getAdminItemList() {
  216. $menu_array = array(
  217. array(
  218. 'name' => 'index',
  219. 'text' => lang('ds_manage'),
  220. 'url' => (string)url('Bonus/index')
  221. ),
  222. array(
  223. 'name' => 'add',
  224. 'text' => lang('ds_add'),
  225. 'url' => "javascript:dsLayerOpen('" . (string)url('Bonus/add') . "','".lang('ds_add')."')"
  226. ),
  227. );
  228. return $menu_array;
  229. }
  230. }