LotteryRiddle.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Facades\Servers\WechatMini\Mini;
  3. use App\Http\Requests\Admin\Lottery\Order 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 Illuminate\Support\Facades\DB;
  9. use Intervention\Image\Facades\Image;
  10. /**
  11. * 优惠券自动发放规则
  12. *
  13. * @author 刘相欣
  14. *
  15. */
  16. class LotteryRiddle extends Auth{
  17. protected function _initialize(){
  18. parent::_initialize();
  19. $this->assign('breadcrumb1','抽奖管理');
  20. $this->assign('breadcrumb2','答题抽奖');
  21. }
  22. /**
  23. * 列表页
  24. *
  25. * */
  26. public function index(Model $Model){
  27. // 接收参数
  28. $name = request('name','');
  29. // 查询条件
  30. $map = [];
  31. // 组合条件
  32. if( $name ) $map[] = ['name','=',$name];
  33. // 查询数据
  34. $list = $Model->query()->where($map)->orderByDesc('id')->paginate(config('page_num',10));
  35. // 循环处理数据
  36. foreach ($list as $key => $value) {
  37. // 小程序链接
  38. $value['mp_urllink']= $this->getUrlLink($value['id']);
  39. // 重组
  40. $list[$key] = $value;
  41. }
  42. // 分配数据
  43. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  44. $this->assign('list',$list);
  45. // 加载模板
  46. return $this->fetch();
  47. }
  48. /**
  49. * 获取小程序链接
  50. *
  51. */
  52. private function getUrlLink($id){
  53. // 结果数据
  54. $link = cache('admin:lottery:riddle:urllink:'.$id);
  55. // 不存在数据
  56. if ( is_null($link) ) {
  57. // 从数据库获取数据
  58. $link = Mini::getUrlLink('pages/activity/lottery','id='.$id);
  59. // 存起来
  60. cache(['admin:lottery:riddle:urllink:'.$id=>$link],$link ? now()->addDays(28) : now()->addMinutes(3));
  61. }
  62. // 返回结果
  63. return $link;
  64. }
  65. /**
  66. * 获取小程序海报
  67. *
  68. */
  69. public function get_poster(Model $Model){
  70. // 接收参数
  71. $id = request('id',0);
  72. // 查询用户
  73. $oldData = $Model->where(['id'=>$id])->first();
  74. // 错误告知
  75. if( !$oldData ) return $this->error('查无数据');
  76. // 获取分享海报图片
  77. $result = $this->getShareImage('id='.$id);
  78. // 错误提示
  79. if( isset($result['error']) ) return $this->error($result['error']);
  80. // 分配数据
  81. $this->assign('image',$result);
  82. $this->assign('oldData',$oldData);
  83. $this->assign('crumbs','海报');
  84. // 加载模板
  85. return $this->fetch();
  86. }
  87. /**
  88. * 获取分享海报图片
  89. * @param int $scene 场景值
  90. *
  91. */
  92. private function getShareImage($scene){
  93. // 尝试执行
  94. try {
  95. // 加载图片
  96. $image = Image::make(public_path('uploads/images/poster/').'score_reward.png');
  97. // 生成小程序二维码
  98. $qrcode = Mini::getUnlimit($scene,['page'=>'pages/orders/lottery','width'=>280,'is_hyaline'=>true]);
  99. // 错误提示
  100. if( isset($qrcode['error']) ) return $qrcode;
  101. // 加载图片
  102. $qrcode = Image::make($qrcode)->resize(200,200);
  103. // 插入图片
  104. $image->insert($qrcode,'bottom-left',60,60);
  105. // 转码成字符串
  106. $image = $image->encode('jpg', 90)->__toString();
  107. // 转base64
  108. $base64 = 'data:image/jpg;base64,' . base64_encode($image);
  109. // 返回结果
  110. return $base64;
  111. } catch (\Throwable $th) {
  112. // 错误提示
  113. return ['error'=>$th->getMessage()];
  114. }
  115. }
  116. /**
  117. * 添加
  118. *
  119. * */
  120. public function add(Request $request,Model $Model,City $City,WeiBanTags $WeiBanTags){
  121. if( request()->isMethod('post') ){
  122. // 验证参数
  123. $request->scene('add')->validate();
  124. // 接收数据
  125. $data['logo'] = request('logo','');
  126. $data['name'] = request('name','');
  127. $data['rule'] = request('rule','');
  128. $data['start_time'] = request('start_time','');
  129. $data['end_time'] = request('end_time','');
  130. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
  131. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
  132. $data['status'] = 1;
  133. // 写入数据表
  134. $id = $Model->add($data);
  135. // 如果操作失败
  136. if( !$id ) return json_send(['code'=>'error','msg'=>'新增失败']);
  137. // 记录行为
  138. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  139. // 告知结果
  140. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  141. }
  142. // 分配数据
  143. $this->assign('crumbs','新增');
  144. // 加载模板
  145. return $this->fetch();
  146. }
  147. /**
  148. * 修改
  149. *
  150. * */
  151. public function edit(Request $request,Model $Model,City $City,WeiBanTags $WeiBanTags){
  152. // 接收参数
  153. $id = request('id',0);
  154. // 查询用户
  155. $oldData = $Model->where(['id'=>$id])->first();
  156. // 修改
  157. if(request()->isMethod('post')){
  158. // 验证参数
  159. $request->scene('edit')->validate();
  160. // 接收数据
  161. $data['logo'] = request('logo','');
  162. $data['name'] = request('name','');
  163. $data['rule'] = request('rule','');
  164. $data['start_time'] = request('start_time','');
  165. $data['end_time'] = request('end_time','');
  166. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
  167. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
  168. // 写入数据表
  169. $result = $Model->edit($id,$data);
  170. // 如果操作失败
  171. if( !$result ) return json_send(['code'=>'error','msg'=>'修改失败']);
  172. // 记录行为
  173. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,$data);
  174. // 告知结果
  175. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  176. }
  177. // 错误告知
  178. if( !$oldData ) return $this->error('查无数据');
  179. $this->assign('oldData',$oldData);
  180. $this->assign('crumbs','修改');
  181. // 加载模板
  182. return $this->fetch();
  183. }
  184. /**
  185. * 修改状态
  186. *
  187. * */
  188. public function set_status(Request $request,Model $Model,RiddleReward $RiddleReward){
  189. // 验证参数
  190. $request->scene('set_status')->validate();
  191. // 设置状态
  192. $id = request('id',0);
  193. $status = request('status',0);
  194. // 查询用户
  195. $oldData = $Model->where(['id'=>$id])->first();
  196. // 如果用户不存在
  197. if( !$oldData ) return json_send(['code'=>'error','msg'=>'数据不存在']);
  198. // 如果设置上架
  199. if( !$status ) {
  200. // 判断奖品个数
  201. $count = $RiddleReward->query()->where([['lottery_id','=',$id]])->count();
  202. // 获取统计数量
  203. if( $count < 3 || $count > 7 ) return json_send(['code'=>'error','msg'=>'奖项请设置3~7个再启用']);
  204. }
  205. // 执行修改
  206. $result = $Model->edit($id,['status'=>$status]);
  207. // 提示新增失败
  208. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  209. // 记录行为
  210. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
  211. // 告知结果
  212. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  213. }
  214. }