CouponController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\Admin\CouponRequest;
  5. use App\Models\Coupon;
  6. use Exception;
  7. use Illuminate\Http\JsonResponse;
  8. use Illuminate\Http\Request;
  9. use Log;
  10. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  11. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  12. use Redirect;
  13. use Response;
  14. use Str;
  15. /**
  16. * 优惠券控制器.
  17. *
  18. * Class CouponController
  19. */
  20. class CouponController extends Controller
  21. {
  22. // 优惠券列表
  23. public function index(Request $request)
  24. {
  25. $sn = $request->input('sn');
  26. $type = $request->input('type');
  27. $status = $request->input('status');
  28. $query = Coupon::query();
  29. if (isset($sn)) {
  30. $query->where('sn', 'like', '%'.$sn.'%');
  31. }
  32. if (isset($type)) {
  33. $query->whereType($type);
  34. }
  35. if (isset($status)) {
  36. $query->whereStatus($status);
  37. }
  38. return view('admin.coupon.index', ['couponList' => $query->latest()->paginate(15)->appends($request->except('page'))]);
  39. }
  40. // 添加优惠券页面
  41. public function create()
  42. {
  43. return view('admin.coupon.create');
  44. }
  45. // 添加优惠券
  46. public function store(CouponRequest $request)
  47. {
  48. // 优惠卷LOGO
  49. $logo = null;
  50. if ($request->hasFile('logo')) {
  51. $file = $request->file('logo');
  52. $fileName = Str::random(8).time().'.'.$file->getClientOriginalExtension();
  53. $path = $file->storeAs('public', $fileName);
  54. if (! $path) {
  55. return Redirect::back()->withInput()->withErrors('LOGO不合法');
  56. }
  57. $logo = 'upload/'.$fileName;
  58. }
  59. try {
  60. $num = (int) $request->input('num');
  61. $data = $request->only(['name', 'type', 'usable_times', 'value', 'rule', 'start_time', 'end_time']);
  62. $data['logo'] = $logo;
  63. for ($i = 0; $i < $num; $i++) {
  64. $data['sn'] = $num === 1 && $request->input('sn') ? $request->input('sn') : Str::random(8);
  65. Coupon::create($data);
  66. }
  67. return Redirect::route('admin.coupon.index')->with('successMsg', '生成成功');
  68. } catch (Exception $e) {
  69. Log::error('生成优惠券失败:'.$e->getMessage());
  70. return Redirect::back()->withInput()->withInput()->withErrors('生成优惠券失败:'.$e->getMessage());
  71. }
  72. }
  73. // 删除优惠券
  74. public function destroy(Coupon $coupon): JsonResponse
  75. {
  76. try {
  77. if ($coupon->delete()) {
  78. return Response::json(['status' => 'success', 'message' => '删除成功']);
  79. }
  80. } catch (Exception $e) {
  81. Log::error('删除优惠券失败:'.$e->getMessage());
  82. return Response::json(['status' => 'success', 'message' => '删除优惠券失败:'.$e->getMessage()]);
  83. }
  84. return Response::json(['status' => 'fail', 'message' => '删除失败']);
  85. }
  86. // 导出卡券
  87. public function exportCoupon(): void
  88. {
  89. $voucherList = Coupon::type(1)->whereStatus(0)->get();
  90. $discountCouponList = Coupon::type(2)->whereStatus(0)->get();
  91. $refillList = Coupon::type(3)->whereStatus(0)->get();
  92. try {
  93. $filename = '卡券'.date('Ymd').'.xlsx';
  94. $spreadsheet = new Spreadsheet();
  95. $spreadsheet->getProperties()
  96. ->setCreator('ProxyPanel')
  97. ->setLastModifiedBy('ProxyPanel')
  98. ->setTitle('邀请码')
  99. ->setSubject('邀请码');
  100. // 抵用券
  101. $spreadsheet->setActiveSheetIndex(0);
  102. $sheet = $spreadsheet->getActiveSheet();
  103. $sheet->setTitle('抵用券');
  104. $sheet->fromArray(['名称', '使用次数', '有效期', '券码', '金额(元)', '使用限制(元)'], null);
  105. foreach ($voucherList as $k => $vo) {
  106. $dateRange = $vo->start_time.' ~ '.$vo->end_time;
  107. $sheet->fromArray([$vo->name, $vo->usable_times ?? '无限制', $dateRange, $vo->sn, $vo->value, $vo->rule], null, 'A'.($k + 2));
  108. }
  109. // 折扣券
  110. $spreadsheet->createSheet(1);
  111. $spreadsheet->setActiveSheetIndex(1);
  112. $sheet = $spreadsheet->getActiveSheet();
  113. $sheet->setTitle('折扣券');
  114. $sheet->fromArray(['名称', '使用次数', '有效期', '券码', '折扣(折)', '使用限制(元)'], null);
  115. foreach ($discountCouponList as $k => $vo) {
  116. $dateRange = $vo->start_time.' ~ '.$vo->end_time;
  117. $sheet->fromArray([$vo->name, $vo->usable_times ?? '无限制', $dateRange, $vo->sn, $vo->value, $vo->rule], null, 'A'.($k + 2));
  118. }
  119. // 充值券
  120. $spreadsheet->createSheet(2);
  121. $spreadsheet->setActiveSheetIndex(2);
  122. $sheet = $spreadsheet->getActiveSheet();
  123. $sheet->setTitle('充值券');
  124. $sheet->fromArray(['名称', '有效期', '券码', '金额(元)'], null);
  125. foreach ($refillList as $k => $vo) {
  126. $dateRange = $vo->start_time.' ~ '.$vo->end_time;
  127. $sheet->fromArray([$vo->name, $dateRange, $vo->sn, $vo->value], null, 'A'.($k + 2));
  128. }
  129. // 指针切换回第一个sheet
  130. $spreadsheet->setActiveSheetIndex(0);
  131. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); // 输出07Excel文件
  132. //header('Content-Type:application/vnd.ms-excel'); // 输出Excel03版本文件
  133. header('Content-Disposition: attachment;filename="'.$filename.'"');
  134. header('Cache-Control: max-age=0');
  135. $writer = new Xlsx($spreadsheet);
  136. $writer->save('php://output');
  137. } catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
  138. Log::error('导出优惠券时报错:'.$e->getMessage());
  139. }
  140. }
  141. }