LotteryRiddle.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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){
  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['freq'] = request('freq',0);
  129. $data['max_reward'] = request('max_reward',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. $data['status'] = 1;
  135. // 写入数据表
  136. $id = $Model->add($data);
  137. // 如果操作失败
  138. if( !$id ) return json_send(['code'=>'error','msg'=>'新增失败']);
  139. // 记录行为
  140. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  141. // 告知结果
  142. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  143. }
  144. // 分配数据
  145. $this->assign('crumbs','新增');
  146. // 加载模板
  147. return $this->fetch();
  148. }
  149. /**
  150. * 修改
  151. *
  152. * */
  153. public function edit(Request $request,Model $Model){
  154. // 接收参数
  155. $id = request('id',0);
  156. // 查询用户
  157. $oldData = $Model->where(['id'=>$id])->first();
  158. // 修改
  159. if(request()->isMethod('post')){
  160. // 验证参数
  161. $request->scene('edit')->validate();
  162. // 接收数据
  163. $data['logo'] = request('logo','');
  164. $data['name'] = request('name','');
  165. $data['rule'] = request('rule','');
  166. $data['freq'] = request('freq',0);
  167. $data['max_reward'] = request('max_reward',0);
  168. $data['start_time'] = request('start_time','');
  169. $data['end_time'] = request('end_time','');
  170. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
  171. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
  172. // 写入数据表
  173. $result = $Model->edit($id,$data);
  174. // 如果操作失败
  175. if( !$result ) return json_send(['code'=>'error','msg'=>'修改失败']);
  176. // 记录行为
  177. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,$data);
  178. // 告知结果
  179. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  180. }
  181. // 错误告知
  182. if( !$oldData ) return $this->error('查无数据');
  183. $this->assign('oldData',$oldData);
  184. $this->assign('crumbs','修改');
  185. // 加载模板
  186. return $this->fetch();
  187. }
  188. /**
  189. * 修改状态
  190. *
  191. * */
  192. public function set_status(Request $request,Model $Model,RiddleReward $RiddleReward){
  193. // 验证参数
  194. $request->scene('set_status')->validate();
  195. // 设置状态
  196. $id = request('id',0);
  197. $status = request('status',0);
  198. // 查询用户
  199. $oldData = $Model->where(['id'=>$id])->first();
  200. // 如果用户不存在
  201. if( !$oldData ) return json_send(['code'=>'error','msg'=>'数据不存在']);
  202. // 如果设置上架
  203. if( !$status ) {
  204. // 判断奖品个数
  205. $count = $RiddleReward->query()->where([['lottery_id','=',$id]])->count();
  206. // 获取统计数量
  207. if( $count < 3 || $count > 7 ) return json_send(['code'=>'error','msg'=>'奖项请设置3~7个再启用']);
  208. }
  209. // 执行修改
  210. $result = $Model->edit($id,['status'=>$status]);
  211. // 提示新增失败
  212. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  213. // 记录行为
  214. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
  215. // 告知结果
  216. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  217. }
  218. }