RiddleActive.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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\Riddle\Active as Model;
  5. use App\Models\City;
  6. use App\Models\WeiBan\Tags as WeiBanTags;
  7. use Intervention\Image\Facades\Image;
  8. /**
  9. * 灯谜活动规则
  10. *
  11. * @author 刘相欣
  12. *
  13. */
  14. class RiddleActive 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:riddle:active:urllink:'.$id);
  53. // 不存在数据
  54. if ( is_null($link) ) {
  55. // 从数据库获取数据
  56. $link = Mini::getUrlLink('pages/activity/index','id='.$id);
  57. // 存起来
  58. cache(['admin:riddle:active: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['rule'] = request('rule','');
  75. $data['freq'] = request('freq',0);
  76. $data['join_total'] = request('join_total',0);
  77. $data['join_share'] = request('join_share',0);
  78. $data['lottery_id'] = request('lottery_id',0);
  79. $data['start_time'] = request('start_time','');
  80. $data['end_time'] = request('end_time','');
  81. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
  82. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
  83. $cityIds = request('city_ids',[]);
  84. $tagScope = request('tag_scope',[]);
  85. $data['city_ids'] = implode(',',$cityIds);
  86. $data['tag_scope'] = implode(',',$tagScope);
  87. $data['status'] = 1;
  88. // 写入数据表
  89. $id = $Model->add($data);
  90. // 如果操作失败
  91. if( !$id ) return json_send(['code'=>'error','msg'=>'新增失败']);
  92. // 记录行为
  93. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  94. // 告知结果
  95. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  96. }
  97. // 获取列表
  98. $cityList = $City->getCityList();
  99. // 标签列表
  100. $tagData = $WeiBanTags->query()->groupBy('group')->groupBy('name')->get(['group','name'])->toArray();
  101. // 标签列表
  102. $tagList = [];
  103. // 循环数据
  104. foreach ($tagData as $value) {
  105. $tagList[$value['group']][] = $value['name'];
  106. }
  107. // 分配数据
  108. $this->assign('cityList',$cityList);
  109. $this->assign('tagList',$tagList);
  110. $this->assign('crumbs','新增');
  111. // 加载模板
  112. return $this->fetch();
  113. }
  114. /**
  115. * 修改
  116. *
  117. * */
  118. public function edit(Request $request,Model $Model,City $City,WeiBanTags $WeiBanTags){
  119. // 接收参数
  120. $id = request('id',0);
  121. // 查询用户
  122. $oldData = $Model->where(['id'=>$id])->first();
  123. // 修改
  124. if(request()->isMethod('post')){
  125. // 验证参数
  126. $request->scene('edit')->validate();
  127. // 接收数据
  128. $data['logo'] = request('logo','');
  129. $data['name'] = request('name','');
  130. $data['rule'] = request('rule','');
  131. $data['freq'] = request('freq',0);
  132. $data['join_total'] = request('join_total',0);
  133. $data['join_share'] = request('join_share',0);
  134. $data['lottery_id'] = request('lottery_id',0);
  135. $data['start_time'] = request('start_time','');
  136. $data['end_time'] = request('end_time','');
  137. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
  138. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
  139. $cityIds = request('city_ids',[]);
  140. $tagScope = request('tag_scope',[]);
  141. $data['city_ids'] = implode(',',$cityIds);
  142. $data['tag_scope'] = implode(',',$tagScope);
  143. // 写入数据表
  144. $result = $Model->edit($id,$data);
  145. // 如果操作失败
  146. if( !$result ) return json_send(['code'=>'error','msg'=>'修改失败']);
  147. // 记录行为
  148. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,$data);
  149. // 告知结果
  150. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  151. }
  152. // 错误告知
  153. if( !$oldData ) return $this->error('查无数据');
  154. // 获取城市ID
  155. $oldData['city_ids'] = explode(',',$oldData['city_ids']);
  156. $oldData['tag_scope'] = explode(',',$oldData['tag_scope']);
  157. // 获取列表
  158. $cityList = $City->getCityList();
  159. // 标签列表
  160. $tagData = $WeiBanTags->query()->groupBy('group')->groupBy('name')->get(['group','name'])->toArray();
  161. // 标签列表
  162. $tagList = [];
  163. // 循环数据
  164. foreach ($tagData as $value) {
  165. $tagList[$value['group']][] = $value['name'];
  166. }
  167. // 分配数据
  168. $this->assign('cityList',$cityList);
  169. $this->assign('tagList',$tagList);
  170. $this->assign('oldData',$oldData);
  171. $this->assign('crumbs','修改');
  172. // 加载模板
  173. return $this->fetch();
  174. }
  175. /**
  176. * 修改状态
  177. *
  178. * */
  179. public function set_status(Request $request,Model $Model){
  180. // 验证参数
  181. $request->scene('set_status')->validate();
  182. // 设置状态
  183. $id = request('id',0);
  184. $status = request('status',0);
  185. // 查询用户
  186. $oldData = $Model->where(['id'=>$id])->first();
  187. // 如果用户不存在
  188. if( !$oldData ) return json_send(['code'=>'error','msg'=>'数据不存在']);
  189. // 执行修改
  190. $result = $Model->edit($id,['status'=>$status]);
  191. // 提示新增失败
  192. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  193. // 记录行为
  194. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
  195. // 告知结果
  196. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  197. }
  198. }