CouponController.php 5.9 KB

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