Orl 短網址,供三星、福斯使用

UrlManagementController.php 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace App\Http\Controllers\Backend\DataManagement;
  3. use App\Http\Services\CheckParamService;
  4. use Illuminate\Http\Request;
  5. use App\Http\Services\Backend\DataManagement\UrlManagementService;
  6. use App\Http\Controllers\Controller;
  7. use App\Http\Services\ConstDef\GeneralConst;
  8. use chillerlan\QRCode\QRCode;
  9. use chillerlan\QRCode\QROptions;
  10. class UrlManagementController extends Controller
  11. {
  12. // 相關私有服務層調用器宣告
  13. private $urlManagementSv;
  14. private $checkParamSv;
  15. public function __construct()
  16. {
  17. // 建構服務層調用器
  18. $this->urlManagementSv = new UrlManagementService();
  19. $this->checkParamSv = new CheckParamService();
  20. // 時區調整
  21. date_default_timezone_set("Asia/Taipei");
  22. }
  23. public function index(Request $request)
  24. {
  25. // 渲染
  26. return view('admin.DataManagement.UrlManagement', [
  27. 'titles' => $this->urlManagementSv->getTitles($request->user()->id),
  28. 'valids' => $this->urlManagementSv->getValids(),
  29. ]);
  30. }
  31. public function grid(Request $request)
  32. {
  33. // 取得參數
  34. $param = $_GET;
  35. if ($param == null) exit();
  36. $draw = $param["draw"]; //客戶端傳來的查詢次數,無條件回傳用以核對
  37. $orderColumn = $param["order"][0]["column"] + 1; //前端從 0 開始送,但 mysql 從 1 開始算
  38. $orderDir = $param["order"][0]["dir"];
  39. $start = $param["start"]; // 頁碼
  40. $length = $param["length"]; // 一頁多大
  41. $searchValue = $param["search"]["value"];
  42. //客製化搜尋欄位
  43. $title = $param["columns"][1]["search"]["value"];
  44. $memo = $param["columns"][2]["search"]["value"];
  45. $createDate = explode("\n", $param["columns"][3]["search"]["value"]);
  46. $createDateStart = $createDate[0] ?? null;
  47. $createDateFinal = $createDate[1] ?? null;
  48. $valid = $param["columns"][4]["search"]["value"];
  49. // 驗證
  50. if ($title != filter_var($title, FILTER_SANITIZE_SPECIAL_CHARS)) $title = "___CANNOT_FIND_STRING___";
  51. if (!$this->checkParamSv->LenMToN($title, 0, 20)) $title = "___CANNOT_FIND_STRING___";
  52. if ($memo != filter_var($memo, FILTER_SANITIZE_SPECIAL_CHARS)) $memo = "___CANNOT_FIND_STRING___";
  53. if (!$this->checkParamSv->LenMToN($memo, 0, 20)) $memo = "___CANNOT_FIND_STRING___";
  54. if ($createDateStart == "") $createDateStart = "1900-01-01";
  55. if ($createDateFinal == "") $createDateFinal = "2050-12-31";
  56. if (!$this->checkParamSv->validateDate($createDateStart, 'Y-m-d')) $createDateStart = "1900-01-01";
  57. if (!$this->checkParamSv->validateDate($createDateFinal, 'Y-m-d')) $createDateFinal = "2050-12-31";
  58. if (!isset(GeneralConst::$urlMap[ $valid ])) $valid = GeneralConst::URL_OK;
  59. //資料庫
  60. $recordsTotal = 0;
  61. $result = $this->urlManagementSv->getUrls(
  62. $recordsTotal,
  63. $orderColumn,
  64. $orderDir,
  65. $start,
  66. $length,
  67. $searchValue,
  68. $title,
  69. $memo,
  70. $createDateStart,
  71. $createDateFinal,
  72. $valid,
  73. $request->user()->id
  74. );
  75. // 整理返回資料
  76. $data = array();
  77. for ($i = 0; $i < count($result); $i++) {
  78. $data[] = array(
  79. //一般資料
  80. $result[ $i ]["serno"],
  81. $result[ $i ]["title"],
  82. $result[ $i ]["url"],
  83. $result[ $i ]["code"],
  84. $result[ $i ]["memo"],
  85. $result[ $i ]["pv"],
  86. $result[ $i ]["valid"],
  87. $result[ $i ]["edit"],
  88. $result[ $i ]["qrcode"],
  89. );
  90. }
  91. $json = array(
  92. "draw" => $draw,
  93. "recordsTotal" => $recordsTotal,
  94. "recordsFiltered" => $recordsTotal, //其實還是填入所有筆數,本次筆數可從陣列取得
  95. "data" => $data,
  96. );
  97. // 返回
  98. return json_decode(json_encode($json, JSON_NUMERIC_CHECK), true);
  99. }
  100. public function qrcode($code)
  101. {
  102. header("Cache-Control: no-cache, must-revalidate");
  103. header("Pragma: no-cache");
  104. header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
  105. header('Content-Description: File Transfer');
  106. header('Content-Disposition: attachment; filename="' . $code . '.png"');
  107. header('Content-Transfer-Encoding: binary');
  108. header('Content-type: image/png');
  109. $options = new QROptions([
  110. 'version' => 2,
  111. 'outputType' => QRCode::OUTPUT_IMAGE_PNG,
  112. 'imageBase64' => false,
  113. ]);
  114. echo (new QRCode($options))->render(env('ORL_URL') . $code);
  115. }
  116. public function create(Request $request)
  117. {
  118. // 服務層取得資料(以及實現各種業務上的邏輯)
  119. $titles = $this->urlManagementSv->getTitles($request->user()->id);
  120. // 渲染
  121. return view('admin.DataManagement.UrlManagementEdit', [
  122. 'titles' => $titles,
  123. 'operdata' => "",
  124. ]);
  125. }
  126. public function edit($id, Request $request)
  127. {
  128. // 取得參數與驗證
  129. // 服務層取得資料(以及實現各種業務上的邏輯)
  130. $operdata = $this->urlManagementSv->getUrlById($id, $request->user()->id);
  131. $titles = $this->urlManagementSv->getTitles($request->user()->id);
  132. // 渲染
  133. return view('admin.DataManagement.UrlManagementEdit', [
  134. 'titles' => $titles,
  135. 'operdata' => $operdata,
  136. ]);
  137. }
  138. public function store(Request $request)
  139. {
  140. // 取得參數與驗證
  141. $mode = $request->mode;
  142. $id = ($request->mode == 'insert') ? '' : $request->serno;
  143. $title = $request->title;
  144. $url = $request->url;
  145. $memo = $request->memo;
  146. $valid = ($request->valid == 'on') ? GeneralConst::URL_OK : GeneralConst::URL_NO;
  147. // 服務層設置(以及實現各種業務上的邏輯)
  148. if ($mode == "insert") {
  149. $code = $this->urlManagementSv->getCode();
  150. // 新增模式
  151. $id = $this->urlManagementSv->insertUrl($title, $url, $code, $memo, $valid, $request->user()->id);
  152. } else {
  153. // 編輯模式
  154. $this->urlManagementSv->modifyUrl($id, $title, $url, $memo, $valid);
  155. }
  156. // 跳轉
  157. return redirect('/backend/dataManagement/urlManagement');
  158. }
  159. public function export(Request $request)
  160. {
  161. // 取得參數
  162. $param = $_POST;
  163. if ($param == null) exit();
  164. // 驗證
  165. if ($param['title'] != filter_var($param['title'], FILTER_SANITIZE_SPECIAL_CHARS)) $param['title'] = "___CANNOT_FIND_STRING___";
  166. if (!$this->checkParamSv->LenMToN($param['title'], 0, 20)) $param['title'] = "___CANNOT_FIND_STRING___";
  167. if ($param['memo'] != filter_var($param['memo'], FILTER_SANITIZE_SPECIAL_CHARS)) $param['memo'] = "___CANNOT_FIND_STRING___";
  168. if (!$this->checkParamSv->LenMToN($param['memo'], 0, 20)) $param['memo'] = "___CANNOT_FIND_STRING___";
  169. if ($param['createDateStart'] == "") $param['createDateStart'] = "1900-01-01";
  170. if ($param['createDateFinal'] == "") $param['createDateFinal'] = "2050-12-31";
  171. if (!$this->checkParamSv->validateDate($param['createDateStart'], 'Y-m-d')) $param['createDateStart'] = "1900-01-01";
  172. if (!$this->checkParamSv->validateDate($param['createDateFinal'], 'Y-m-d')) $param['createDateFinal'] = "2050-12-31";
  173. if (!isset(GeneralConst::$urlMap[ $param['valid'] ])) $param['valid'] = GeneralConst::URL_OK;
  174. // 製作
  175. $title = [
  176. '網址',
  177. '短網址',
  178. '備註',
  179. '有效否',
  180. '造訪次',
  181. ];
  182. $urls = $this->urlManagementSv->getExportUrls($param, $request->user()->id);
  183. $this->urlManagementSv->downloadExcel($title, $urls, date("YmdHis") . '-REPORT');
  184. }
  185. }