LotteryRiddle.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Facades\Servers\WechatMini\Mini;
  3. use App\Http\Requests\Admin\Riddle\Active as Request;
  4. use App\Models\Lottery\Riddle as Model;
  5. use App\Models\City;
  6. use App\Models\Lottery\RiddleReward;
  7. use App\Models\WeiBan\Tags as WeiBanTags;
  8. use Intervention\Image\Facades\Image;
  9. /**
  10. * 优惠券自动发放规则
  11. *
  12. * @author 刘相欣
  13. *
  14. */
  15. class LotteryRiddle extends Auth{
  16. protected function _initialize(){
  17. parent::_initialize();
  18. $this->assign('breadcrumb1','抽奖管理');
  19. $this->assign('breadcrumb2','答题抽奖');
  20. }
  21. /**
  22. * 列表页
  23. *
  24. * */
  25. public function index(Model $Model){
  26. // 接收参数
  27. $name = request('name','');
  28. // 查询条件
  29. $map = [];
  30. // 组合条件
  31. if( $name ) $map[] = ['name','=',$name];
  32. // 查询数据
  33. $list = $Model->query()->where($map)->orderByDesc('id')->paginate(config('page_num',10))->appends(request()->all());
  34. // 循环处理数据
  35. foreach ($list as $key => $value) {
  36. // 小程序链接
  37. $value['mp_urllink']= $this->getUrlLink($value['id']);
  38. // 重组
  39. $list[$key] = $value;
  40. }
  41. // 分配数据
  42. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  43. $this->assign('list',$list);
  44. // 加载模板
  45. return $this->fetch();
  46. }
  47. /**
  48. * 获取小程序链接
  49. *
  50. */
  51. private function getUrlLink($id){
  52. // 结果数据
  53. $link = cache('admin:lottery:riddle:urllink:'.$id);
  54. // 不存在数据
  55. if ( is_null($link) ) {
  56. // 从数据库获取数据
  57. $link = Mini::getUrlLink('pages/activity/lottery','id='.$id);
  58. // 存起来
  59. cache(['admin:lottery:riddle:urllink:'.$id=>$link],$link ? now()->addDays(28) : now()->addMinutes(3));
  60. }
  61. // 返回结果
  62. return $link;
  63. }
  64. /**
  65. * 添加
  66. *
  67. * */
  68. public function add(Request $request,Model $Model){
  69. if( request()->isMethod('post') ){
  70. // 验证参数
  71. $request->scene('add')->validate();
  72. // 接收数据
  73. $data['logo'] = request('logo','');
  74. $data['name'] = request('name','');
  75. $data['rule'] = request('rule','');
  76. $data['freq'] = request('freq',0);
  77. $data['max_reward'] = request('max_reward',0);
  78. $data['start_time'] = request('start_time','');
  79. $data['end_time'] = request('end_time','');
  80. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
  81. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
  82. $data['status'] = 1;
  83. // 写入数据表
  84. $id = $Model->add($data);
  85. // 如果操作失败
  86. if( !$id ) return json_send(['code'=>'error','msg'=>'新增失败']);
  87. // 记录行为
  88. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  89. // 告知结果
  90. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  91. }
  92. // 分配数据
  93. $this->assign('crumbs','新增');
  94. // 加载模板
  95. return $this->fetch();
  96. }
  97. /**
  98. * 修改
  99. *
  100. * */
  101. public function edit(Request $request,Model $Model){
  102. // 接收参数
  103. $id = request('id',0);
  104. // 查询用户
  105. $oldData = $Model->where(['id'=>$id])->first();
  106. // 修改
  107. if(request()->isMethod('post')){
  108. // 验证参数
  109. $request->scene('edit')->validate();
  110. // 接收数据
  111. $data['logo'] = request('logo','');
  112. $data['name'] = request('name','');
  113. $data['rule'] = request('rule','');
  114. $data['freq'] = request('freq',0);
  115. $data['max_reward'] = request('max_reward',0);
  116. $data['start_time'] = request('start_time','');
  117. $data['end_time'] = request('end_time','');
  118. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
  119. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
  120. // 写入数据表
  121. $result = $Model->edit($id,$data);
  122. // 如果操作失败
  123. if( !$result ) return json_send(['code'=>'error','msg'=>'修改失败']);
  124. // 记录行为
  125. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,$data);
  126. // 告知结果
  127. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  128. }
  129. // 错误告知
  130. if( !$oldData ) return $this->error('查无数据');
  131. $this->assign('oldData',$oldData);
  132. $this->assign('crumbs','修改');
  133. // 加载模板
  134. return $this->fetch();
  135. }
  136. /**
  137. * 修改状态
  138. *
  139. * */
  140. public function set_status(Request $request,Model $Model,RiddleReward $RiddleReward){
  141. // 验证参数
  142. $request->scene('set_status')->validate();
  143. // 设置状态
  144. $id = request('id',0);
  145. $status = request('status',0);
  146. // 查询用户
  147. $oldData = $Model->where(['id'=>$id])->first();
  148. // 如果用户不存在
  149. if( !$oldData ) return json_send(['code'=>'error','msg'=>'数据不存在']);
  150. // 如果设置上架
  151. if( !$status ) {
  152. // 判断奖品个数
  153. $count = $RiddleReward->query()->where([['lottery_id','=',$id]])->count();
  154. // 获取统计数量
  155. if( $count < 3 || $count > 7 ) return json_send(['code'=>'error','msg'=>'奖项请设置3~7个再启用']);
  156. }
  157. // 执行修改
  158. $result = $Model->edit($id,['status'=>$status]);
  159. // 提示新增失败
  160. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  161. // 记录行为
  162. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
  163. // 告知结果
  164. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  165. }
  166. }