LotteryOrder.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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\Order as Model;
  5. use App\Models\City;
  6. use App\Models\Lottery\OrderReward;
  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 LotteryOrder 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:order:urllink:'.$id);
  55. // 不存在数据
  56. if ( is_null($link) ) {
  57. // 从数据库获取数据
  58. $link = Mini::getUrlLink('pages/orders/lottery','id='.$id);
  59. // 存起来
  60. cache(['admin:lottery:order: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. $cityIds = request('city_ids',[]);
  133. $tagScope = request('tag_scope',[]);
  134. $data['city_ids'] = implode(',',$cityIds);
  135. $data['tag_scope'] = implode(',',$tagScope);
  136. $data['status'] = 1;
  137. // 写入数据表
  138. $id = $Model->add($data);
  139. // 如果操作失败
  140. if( !$id ) return json_send(['code'=>'error','msg'=>'新增失败']);
  141. // 记录行为
  142. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  143. // 告知结果
  144. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  145. }
  146. // 获取列表
  147. $cityList = $City->getCityList();
  148. // 标签列表
  149. $tagData = $WeiBanTags->query()->groupBy('group')->groupBy('name')->get(['group','name'])->toArray();
  150. // 标签列表
  151. $tagList = [];
  152. // 循环数据
  153. foreach ($tagData as $value) {
  154. $tagList[$value['group']][] = $value['name'];
  155. }
  156. // 分配数据
  157. $this->assign('cityList',$cityList);
  158. $this->assign('tagList',$tagList);
  159. $this->assign('crumbs','新增');
  160. // 加载模板
  161. return $this->fetch();
  162. }
  163. /**
  164. * 修改
  165. *
  166. * */
  167. public function edit(Request $request,Model $Model,City $City,WeiBanTags $WeiBanTags){
  168. // 接收参数
  169. $id = request('id',0);
  170. // 查询用户
  171. $oldData = $Model->where(['id'=>$id])->first();
  172. // 修改
  173. if(request()->isMethod('post')){
  174. // 验证参数
  175. $request->scene('edit')->validate();
  176. // 接收数据
  177. $data['logo'] = request('logo','');
  178. $data['name'] = request('name','');
  179. $data['rule'] = request('rule','');
  180. $data['start_time'] = request('start_time','');
  181. $data['end_time'] = request('end_time','');
  182. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
  183. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
  184. $cityIds = request('city_ids',[]);
  185. $tagScope = request('tag_scope',[]);
  186. $data['city_ids'] = implode(',',$cityIds);
  187. $data['tag_scope'] = implode(',',$tagScope);
  188. // 写入数据表
  189. $result = $Model->edit($id,$data);
  190. // 如果操作失败
  191. if( !$result ) return json_send(['code'=>'error','msg'=>'修改失败']);
  192. // 记录行为
  193. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,$data);
  194. // 告知结果
  195. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  196. }
  197. // 错误告知
  198. if( !$oldData ) return $this->error('查无数据');
  199. // 获取城市ID
  200. $oldData['city_ids'] = explode(',',$oldData['city_ids']);
  201. $oldData['tag_scope'] = explode(',',$oldData['tag_scope']);
  202. // 获取列表
  203. $cityList = $City->getCityList();
  204. // 标签列表
  205. $tagData = $WeiBanTags->query()->groupBy('group')->groupBy('name')->get(['group','name'])->toArray();
  206. // 标签列表
  207. $tagList = [];
  208. // 循环数据
  209. foreach ($tagData as $value) {
  210. $tagList[$value['group']][] = $value['name'];
  211. }
  212. // 分配数据
  213. $this->assign('cityList',$cityList);
  214. $this->assign('tagList',$tagList);
  215. $this->assign('oldData',$oldData);
  216. $this->assign('crumbs','修改');
  217. // 加载模板
  218. return $this->fetch();
  219. }
  220. /**
  221. * 修改状态
  222. *
  223. * */
  224. public function set_status(Request $request,Model $Model,OrderReward $OrderReward){
  225. // 验证参数
  226. $request->scene('set_status')->validate();
  227. // 设置状态
  228. $id = request('id',0);
  229. $status = request('status',0);
  230. // 查询用户
  231. $oldData = $Model->where(['id'=>$id])->first();
  232. // 如果用户不存在
  233. if( !$oldData ) return json_send(['code'=>'error','msg'=>'数据不存在']);
  234. // 如果设置上架
  235. if( !$status ) {
  236. // 判断奖品个数
  237. $count = $OrderReward->query()->where([['lottery_id','=',$id]])->count();
  238. // 获取统计数量
  239. if( $count < 3 || $count > 7 ) return json_send(['code'=>'error','msg'=>'奖项请设置3~7个再启用']);
  240. }
  241. // 执行修改
  242. $result = $Model->edit($id,['status'=>$status]);
  243. // 提示新增失败
  244. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  245. // 记录行为
  246. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
  247. // 告知结果
  248. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  249. }
  250. }