Bonus.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. * DSMall多用户商城
  9. * ============================================================================
  10. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  11. * 网站地址: http://www.csdeshang.com
  12. * ----------------------------------------------------------------------------
  13. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  14. * 不允许对程序代码以任何形式任何目的的再发布。
  15. * ============================================================================
  16. * 平台红包 控制器
  17. */
  18. class Bonus extends AdminControl {
  19. public function initialize() {
  20. parent::initialize();
  21. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/bonus.lang.php');
  22. }
  23. public function index() {
  24. $condition = array();
  25. $bonus_name = input('param.bonus_name');
  26. if (!empty($bonus_name)) {
  27. $condition[]=array('bonus_name','like', '%' . $bonus_name . '%');
  28. }
  29. //红包是否有效
  30. $bonus_state = intval(input('get.bonus_state'));
  31. if ($bonus_state) {
  32. $condition[]=array('bonus_state','=',$bonus_state);
  33. }
  34. //红包类型
  35. $bonus_type = intval(input('get.bonus_type'));
  36. if ($bonus_type) {
  37. $condition[]=array('bonus_type','=',$bonus_type);
  38. }
  39. $bonus_model = model('bonus');
  40. $bonus_list = $bonus_model->getBonusList($condition, 10);
  41. //红包类型
  42. View::assign('bonus_type_list', $bonus_model->bonus_type_list());
  43. //红包状态
  44. View::assign('bonus_state_list', $bonus_model->bonus_state_list());
  45. View::assign('bonus_list', $bonus_list);
  46. View::assign('show_page', $bonus_model->page_info->render());
  47. $this->setAdminCurItem('index');
  48. return View::fetch();
  49. }
  50. /**
  51. * 添加吸粉红包
  52. */
  53. public function add() {
  54. $bonus_model = model('bonus');
  55. if (!request()->isPost()) {
  56. $bonus = array(
  57. 'bonus_type' => 1,
  58. 'bonus_begintime' => TIMESTAMP,
  59. 'bonus_endtime' => TIMESTAMP+3600*24*7,
  60. );
  61. //红包类型
  62. View::assign('bonus_type_list', $bonus_model->bonus_type_list());
  63. View::assign('bonus', $bonus);
  64. return View::fetch('form');
  65. } else {
  66. $bonus_totalprice = floatval(input('param.bonus_totalprice'));
  67. $bonus_pricetype = intval(input('param.bonus_pricetype'));
  68. $bonus_fixedprice = floatval(input('param.bonus_fixedprice'));
  69. $bonus_randomprice_start = floatval(input('param.bonus_randomprice_start'));
  70. $bonus_randomprice_end = floatval(input('param.bonus_randomprice_end'));
  71. //计算写入吸粉红包领取记录表
  72. $data_bonusreceive = array(); //红包领取记录
  73. if ($bonus_pricetype == 1) {
  74. //固定金额
  75. if ($bonus_fixedprice == 0 || $bonus_fixedprice > $bonus_totalprice) {
  76. $this->error(lang('bonus_fixedprice_error'));
  77. }
  78. if (($bonus_totalprice*100) % ($bonus_fixedprice*100) != 0) {
  79. $this->error(lang('bonus_fixedprice_error'));
  80. }
  81. //生成红包领取记录-固定金额
  82. for ($i = 0; $i < $bonus_totalprice / $bonus_fixedprice; $i++) {
  83. $data_bonusreceive[] = array(
  84. 'bonusreceive_price' => $bonus_fixedprice
  85. );
  86. }
  87. $bonus_randomprice_start = 0;
  88. $bonus_randomprice_end = 0;
  89. } else {
  90. if ($bonus_randomprice_start == 0 || $bonus_randomprice_start > $bonus_totalprice || $bonus_randomprice_end > $bonus_totalprice || $bonus_randomprice_start >= $bonus_randomprice_end) {
  91. $this->error(lang('bonus_randomprice_error'));
  92. }
  93. //生成红包领取记录-随机金额
  94. $surplus_price = $bonus_totalprice; //剩余未计算金额
  95. while (true) {
  96. if ($surplus_price <= $bonus_randomprice_end) {
  97. $bonusreceive_price = $surplus_price;
  98. } else {
  99. $bonusreceive_price = rand($bonus_randomprice_start * 100, $bonus_randomprice_end * 100) / 100;
  100. }
  101. $surplus_price -= $bonusreceive_price;
  102. $data_bonusreceive[] = array(
  103. 'bonusreceive_price' => $bonusreceive_price
  104. );
  105. if ($surplus_price == 0) {
  106. break;
  107. }
  108. }
  109. $bonus_fixedprice = 0;
  110. }
  111. $data_bonus = array(
  112. 'bonus_type' => input('param.bonus_type'),
  113. 'bonus_name' => input('param.bonus_name'),
  114. 'bonus_remark' => input('param.bonus_remark'),
  115. 'bonus_blessing' => input('param.bonus_blessing'),
  116. 'bonus_begintime' => strtotime(input('param.bonus_begintime')),
  117. 'bonus_endtime' => strtotime(input('param.bonus_endtime')),
  118. 'bonus_state' => 1,
  119. 'bonus_totalprice' => $bonus_totalprice,
  120. 'bonus_pricetype' => $bonus_pricetype,
  121. 'bonus_fixedprice' => $bonus_fixedprice,
  122. 'bonus_randomprice_start' => $bonus_randomprice_start,
  123. 'bonus_randomprice_end' => $bonus_randomprice_end,
  124. );
  125. $bonus_id = $bonus_model->addBonus($data_bonus);
  126. if ($bonus_id > 0) {
  127. foreach ($data_bonusreceive as $key => $bonusreceive) {
  128. $data_bonusreceive[$key]['bonus_id'] = $bonus_id;
  129. }
  130. Db::name('bonusreceive')->insertAll($data_bonusreceive);
  131. $this->log(lang('ds_add') . lang('ds_bonus') . '[ID' . $bonus_id . ']', 1);
  132. dsLayerOpenSuccess(lang('ds_common_save_succ'));
  133. } else {
  134. $this->error(lang('ds_common_save_fail'));
  135. }
  136. }
  137. }
  138. /**
  139. * 编辑吸粉红包 不可以对金额以及红包类型进行编辑。
  140. */
  141. public function edit() {
  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. $bonus_id = intval(input('param.bonus_id'));
  173. if ($bonus_id < 0) {
  174. ds_json_encode(10000, lang('param_error'));
  175. }
  176. $bonus_model = model('bonus');
  177. $condition = array();
  178. $condition[] = array('bonus_id','=',$bonus_id);
  179. $data['bonus_state'] = 3;
  180. $bonus_model->editBonus($condition, $data);
  181. $this->log(lang('ds_edit') . lang('ds_bonus') . '[ID' . $bonus_id . ']', 1);
  182. ds_json_encode(10000, lang('ds_common_op_succ'));
  183. }
  184. /**
  185. * 领取列表
  186. */
  187. public function receive() {
  188. $bonus_id = intval(input('param.bonus_id'));
  189. if ($bonus_id < 0) {
  190. $this->error(lang('param_error'));
  191. }
  192. $condition = array();
  193. $condition[] = array('bonus_id','=',$bonus_id);
  194. $bonus_model = model('bonus');
  195. $bonusreceive_list = $bonus_model->getBonusreceiveList($condition, 10);
  196. View::assign('bonusreceive_list', $bonusreceive_list);
  197. View::assign('show_page', $bonus_model->page_info->render());
  198. return View::fetch();
  199. }
  200. //链接信息
  201. public function link()
  202. {
  203. $bonus_id = intval(input('param.bonus_id'));
  204. if ($bonus_id < 0) {
  205. $this->error(lang('param_error'));
  206. }
  207. $condition = array();
  208. $condition[] = array('bonus_id','=',$bonus_id);
  209. $bonus_model = model('bonus');
  210. $bonus = $bonus_model->getOneBonus($condition);
  211. View::assign('bonus', $bonus);
  212. $bonus_url = config('ds_config.h5_site_url')."/pages/home/bonus/Detail?bonus_id=".$bonus['bonus_id'];
  213. View::assign('bonus_url', $bonus_url);
  214. return View::fetch();
  215. }
  216. protected function getAdminItemList() {
  217. $menu_array = array(
  218. array(
  219. 'name' => 'index',
  220. 'text' => lang('ds_manage'),
  221. 'url' => (string)url('Bonus/index')
  222. ),
  223. array(
  224. 'name' => 'add',
  225. 'text' => lang('ds_add'),
  226. 'url' => "javascript:dsLayerOpen('" . (string)url('Bonus/add') . "','".lang('ds_add')."')"
  227. ),
  228. );
  229. return $menu_array;
  230. }
  231. }