Bonus.php 9.9 KB

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