CouponController.php 5.8 KB

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