RiddleActive.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 get_poster(Model $Model){
  68. // 接收参数
  69. $id = request('id',0);
  70. // 查询用户
  71. $oldData = $Model->where(['id'=>$id])->first();
  72. // 错误告知
  73. if( !$oldData ) return $this->error('查无数据');
  74. // 获取分享海报图片
  75. $result = $this->getShareImage('id='.$id);
  76. // 错误提示
  77. if( isset($result['error']) ) return $this->error($result['error']);
  78. // 分配数据
  79. $this->assign('image',$result);
  80. $this->assign('oldData',$oldData);
  81. $this->assign('crumbs','海报');
  82. // 加载模板
  83. return $this->fetch();
  84. }
  85. /**
  86. * 获取分享海报图片
  87. * @param int $scene 场景值
  88. *
  89. */
  90. private function getShareImage($scene){
  91. // 尝试执行
  92. try {
  93. // 加载图片
  94. $image = Image::make(public_path('uploads/images/poster/').'score_reward.png');
  95. // 生成小程序二维码
  96. $qrcode = Mini::getUnlimit($scene,['page'=>'pages/activity/index','width'=>280,'is_hyaline'=>true]);
  97. // 错误提示
  98. if( isset($qrcode['error']) ) return $qrcode;
  99. // 加载图片
  100. $qrcode = Image::make($qrcode)->resize(200,200);
  101. // 插入图片
  102. $image->insert($qrcode,'bottom-left',60,60);
  103. // 转码成字符串
  104. $image = $image->encode('jpg', 90)->__toString();
  105. // 转base64
  106. $base64 = 'data:image/jpg;base64,' . base64_encode($image);
  107. // 返回结果
  108. return $base64;
  109. } catch (\Throwable $th) {
  110. // 错误提示
  111. return ['error'=>$th->getMessage()];
  112. }
  113. }
  114. /**
  115. * 添加
  116. *
  117. * */
  118. public function add(Request $request,Model $Model,City $City,WeiBanTags $WeiBanTags){
  119. if( request()->isMethod('post') ){
  120. // 验证参数
  121. $request->scene('add')->validate();
  122. // 接收数据
  123. $data['logo'] = request('logo','');
  124. $data['name'] = request('name','');
  125. $data['rule'] = request('rule','');
  126. $data['freq'] = request('freq',0);
  127. $data['join_total'] = request('join_total',0);
  128. $data['join_share'] = request('join_share',0);
  129. $data['lottery_id'] = request('lottery_id',0);
  130. $data['start_time'] = request('start_time','');
  131. $data['end_time'] = request('end_time','');
  132. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
  133. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
  134. $cityIds = request('city_ids',[]);
  135. $tagScope = request('tag_scope',[]);
  136. $data['city_ids'] = implode(',',$cityIds);
  137. $data['tag_scope'] = implode(',',$tagScope);
  138. $data['status'] = 1;
  139. // 写入数据表
  140. $id = $Model->add($data);
  141. // 如果操作失败
  142. if( !$id ) return json_send(['code'=>'error','msg'=>'新增失败']);
  143. // 记录行为
  144. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  145. // 告知结果
  146. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  147. }
  148. // 获取列表
  149. $cityList = $City->getCityList();
  150. // 标签列表
  151. $tagData = $WeiBanTags->query()->groupBy('group')->groupBy('name')->get(['group','name'])->toArray();
  152. // 标签列表
  153. $tagList = [];
  154. // 循环数据
  155. foreach ($tagData as $value) {
  156. $tagList[$value['group']][] = $value['name'];
  157. }
  158. // 分配数据
  159. $this->assign('cityList',$cityList);
  160. $this->assign('tagList',$tagList);
  161. $this->assign('crumbs','新增');
  162. // 加载模板
  163. return $this->fetch();
  164. }
  165. /**
  166. * 修改
  167. *
  168. * */
  169. public function edit(Request $request,Model $Model,City $City,WeiBanTags $WeiBanTags){
  170. // 接收参数
  171. $id = request('id',0);
  172. // 查询用户
  173. $oldData = $Model->where(['id'=>$id])->first();
  174. // 修改
  175. if(request()->isMethod('post')){
  176. // 验证参数
  177. $request->scene('edit')->validate();
  178. // 接收数据
  179. $data['logo'] = request('logo','');
  180. $data['name'] = request('name','');
  181. $data['rule'] = request('rule','');
  182. $data['freq'] = request('freq',0);
  183. $data['join_total'] = request('join_total',0);
  184. $data['join_share'] = request('join_share',0);
  185. $data['lottery_id'] = request('lottery_id',0);
  186. $data['start_time'] = request('start_time','');
  187. $data['end_time'] = request('end_time','');
  188. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
  189. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
  190. $cityIds = request('city_ids',[]);
  191. $tagScope = request('tag_scope',[]);
  192. $data['city_ids'] = implode(',',$cityIds);
  193. $data['tag_scope'] = implode(',',$tagScope);
  194. // 写入数据表
  195. $result = $Model->edit($id,$data);
  196. // 如果操作失败
  197. if( !$result ) return json_send(['code'=>'error','msg'=>'修改失败']);
  198. // 记录行为
  199. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,$data);
  200. // 告知结果
  201. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  202. }
  203. // 错误告知
  204. if( !$oldData ) return $this->error('查无数据');
  205. // 获取城市ID
  206. $oldData['city_ids'] = explode(',',$oldData['city_ids']);
  207. $oldData['tag_scope'] = explode(',',$oldData['tag_scope']);
  208. // 获取列表
  209. $cityList = $City->getCityList();
  210. // 标签列表
  211. $tagData = $WeiBanTags->query()->groupBy('group')->groupBy('name')->get(['group','name'])->toArray();
  212. // 标签列表
  213. $tagList = [];
  214. // 循环数据
  215. foreach ($tagData as $value) {
  216. $tagList[$value['group']][] = $value['name'];
  217. }
  218. // 分配数据
  219. $this->assign('cityList',$cityList);
  220. $this->assign('tagList',$tagList);
  221. $this->assign('oldData',$oldData);
  222. $this->assign('crumbs','修改');
  223. // 加载模板
  224. return $this->fetch();
  225. }
  226. /**
  227. * 修改状态
  228. *
  229. * */
  230. public function set_status(Request $request,Model $Model){
  231. // 验证参数
  232. $request->scene('set_status')->validate();
  233. // 设置状态
  234. $id = request('id',0);
  235. $status = request('status',0);
  236. // 查询用户
  237. $oldData = $Model->where(['id'=>$id])->first();
  238. // 如果用户不存在
  239. if( !$oldData ) return json_send(['code'=>'error','msg'=>'数据不存在']);
  240. // 执行修改
  241. $result = $Model->edit($id,['status'=>$status]);
  242. // 提示新增失败
  243. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  244. // 记录行为
  245. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
  246. // 告知结果
  247. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  248. }
  249. }