CouponController.php 5.8 KB

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