LotteryScore.php 6.7 KB

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