Bonus.php 9.6 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. class Bonus extends AdminControl
  14. {
  15. public function initialize()
  16. {
  17. parent::initialize();
  18. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/bonus.lang.php');
  19. }
  20. public function index()
  21. {
  22. $condition = array();
  23. $bonus_name = input('param.bonus_name');
  24. if (!empty($bonus_name)) {
  25. $condition[] = array('bonus_name', 'like', '%' . $bonus_name . '%');
  26. }
  27. //红包是否有效
  28. $bonus_state = intval(input('get.bonus_state'));
  29. if ($bonus_state) {
  30. $condition[] = array('bonus_state', '=', $bonus_state);
  31. }
  32. //红包类型
  33. $bonus_type = intval(input('get.bonus_type'));
  34. if ($bonus_type) {
  35. $condition[] = array('bonus_type', '=', $bonus_type);
  36. }
  37. $bonus_model = model('bonus');
  38. $bonus_list = $bonus_model->getBonusList($condition, 10);
  39. //红包类型
  40. View::assign('bonus_type_list', $bonus_model->bonus_type_list());
  41. //红包状态
  42. View::assign('bonus_state_list', $bonus_model->bonus_state_list());
  43. View::assign('bonus_list', $bonus_list);
  44. View::assign('show_page', $bonus_model->page_info->render());
  45. $this->setAdminCurItem('index');
  46. return View::fetch();
  47. }
  48. /**
  49. * 添加吸粉红包
  50. */
  51. public function add()
  52. {
  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. {
  142. $bonus_id = intval(input('param.bonus_id'));
  143. if ($bonus_id < 0) {
  144. ds_json_encode(10000, lang('param_error'));
  145. }
  146. $bonus_model = model('bonus');
  147. $condition = array();
  148. $condition[] = array('bonus_id', '=', $bonus_id);
  149. if (!request()->isPost()) {
  150. $bonus = $bonus_model->getOneBonus($condition);
  151. View::assign('bonus', $bonus);
  152. //红包类型
  153. View::assign('bonus_type_list', $bonus_model->bonus_type_list());
  154. return View::fetch('form');
  155. } else {
  156. $data_bonus = array(
  157. 'bonus_name' => input('param.bonus_name'),
  158. 'bonus_remark' => input('param.bonus_remark'),
  159. 'bonus_blessing' => input('param.bonus_blessing'),
  160. 'bonus_begintime' => strtotime(input('param.bonus_begintime')),
  161. 'bonus_endtime' => strtotime(input('param.bonus_endtime')),
  162. );
  163. $bonus_model->editBonus($condition, $data_bonus);
  164. $this->log(lang('ds_edit') . lang('ds_bonus') . '[ID' . $bonus_id . ']', 1);
  165. dsLayerOpenSuccess(lang('ds_common_save_succ'));
  166. }
  167. }
  168. /**
  169. * 设置红包失效 1正在进行 2过期 3失效
  170. */
  171. public function invalid()
  172. {
  173. $bonus_id = intval(input('param.bonus_id'));
  174. if ($bonus_id < 0) {
  175. ds_json_encode(10000, lang('param_error'));
  176. }
  177. $bonus_model = model('bonus');
  178. $condition = array();
  179. $condition[] = array('bonus_id', '=', $bonus_id);
  180. $data['bonus_state'] = 3;
  181. $bonus_model->editBonus($condition, $data);
  182. $this->log(lang('ds_edit') . lang('ds_bonus') . '[ID' . $bonus_id . ']', 1);
  183. ds_json_encode(10000, lang('ds_common_op_succ'));
  184. }
  185. /**
  186. * 领取列表
  187. */
  188. public function receive()
  189. {
  190. $bonus_id = intval(input('param.bonus_id'));
  191. if ($bonus_id < 0) {
  192. $this->error(lang('param_error'));
  193. }
  194. $condition = array();
  195. $condition[] = array('bonus_id', '=', $bonus_id);
  196. $bonus_model = model('bonus');
  197. $bonusreceive_list = $bonus_model->getBonusreceiveList($condition, 10);
  198. View::assign('bonusreceive_list', $bonusreceive_list);
  199. View::assign('show_page', $bonus_model->page_info->render());
  200. return View::fetch();
  201. }
  202. //链接信息
  203. public function link()
  204. {
  205. $bonus_id = intval(input('param.bonus_id'));
  206. if ($bonus_id < 0) {
  207. $this->error(lang('param_error'));
  208. }
  209. $condition = array();
  210. $condition[] = array('bonus_id', '=', $bonus_id);
  211. $bonus_model = model('bonus');
  212. $bonus = $bonus_model->getOneBonus($condition);
  213. View::assign('bonus', $bonus);
  214. $bonus_url = config('ds_config.h5_site_url') . "/pages/home/bonus/Detail?bonus_id=" . $bonus['bonus_id'];
  215. View::assign('bonus_url', $bonus_url);
  216. return View::fetch();
  217. }
  218. protected function getAdminItemList()
  219. {
  220. $menu_array = array(
  221. array(
  222. 'name' => 'index',
  223. 'text' => lang('ds_manage'),
  224. 'url' => (string)url('Bonus/index')
  225. ),
  226. array(
  227. 'name' => 'add',
  228. 'text' => lang('ds_add'),
  229. 'url' => "javascript:dsLayerOpen('" . (string)url('Bonus/add') . "','" . lang('ds_add') . "')"
  230. ),
  231. );
  232. return $menu_array;
  233. }
  234. }