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

GeneralInnerServiceController.php 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use chillerlan\QRCode\QRCode;
  4. use chillerlan\QRCode\QROptions;
  5. use Illuminate\Http\Request;
  6. use App\Http\Services\Api\GeneralInnerServiceService;
  7. use App\Http\Services\Backend\DataManagement\UrlManagementService;
  8. use App\Http\Controllers\Controller;
  9. use App\Http\Services\ConstDef\GeneralConst;
  10. use App\Http\Services\CheckParamService;
  11. class GeneralInnerServiceController extends Controller
  12. {
  13. private $apicode;
  14. private $oid;
  15. private $generalInnerServiceSv;
  16. private $urlManagementSv;
  17. private $checkParamSv;
  18. public function __construct(Request $request)
  19. {
  20. $this->generalInnerServiceSv = new GeneralInnerServiceService();
  21. $this->urlManagementSv = new UrlManagementService();
  22. $this->checkParamSv = new CheckParamService();
  23. // 鑒權
  24. $this->oid = $this->generalInnerServiceSv->validate($request->header('userToken'));
  25. $this->apicode = ($this->oid == 0) ? GeneralConst::API_TOKEN_ERR : GeneralConst::API_OK;
  26. }
  27. public function store(Request $request)
  28. {
  29. // 鑒權
  30. if ($this->oid == 0) return json_decode(json_encode(['code' => $this->apicode, 'msg' => GeneralConst::$apiMap[ $this->apicode ], 'data' => []], JSON_NUMERIC_CHECK), true);
  31. // 參數設置
  32. $urlid = '';
  33. $mode = $request->mode;
  34. $id = ($request->mode == 'insert') ? '' : $request->serno; // 只有更新用的到
  35. $title = $request->title;
  36. $url = ($request->mode == 'insert') ? $request->url : ''; // 只有新增用的到
  37. $memo = $request->memo;
  38. $valid = GeneralConst::URL_OK; // 預設都可用
  39. // 參數驗證
  40. if (!in_array($mode, ['insert', 'update'])) return json_decode(json_encode(['code' => GeneralConst::API_PARAM_ERR, 'msg' => GeneralConst::$apiMap[ GeneralConst::API_PARAM_ERR ], 'data' => []], JSON_NUMERIC_CHECK), true);
  41. if ($mode == 'update') {
  42. if (!$this->checkParamSv->isnum($id)) return json_decode(json_encode(['code' => GeneralConst::API_PARAM_ERR, 'msg' => GeneralConst::$apiMap[ GeneralConst::API_PARAM_ERR ], 'data' => []], JSON_NUMERIC_CHECK), true);
  43. }
  44. if ($title != filter_var($title, FILTER_SANITIZE_SPECIAL_CHARS)) return json_decode(json_encode(['code' => GeneralConst::API_PARAM_ERR, 'msg' => GeneralConst::$apiMap[ GeneralConst::API_PARAM_ERR ], 'data' => []], JSON_NUMERIC_CHECK), true);
  45. if (!$this->checkParamSv->LenMToN($title, 1, 20)) return json_decode(json_encode(['code' => GeneralConst::API_PARAM_ERR, 'msg' => GeneralConst::$apiMap[ GeneralConst::API_PARAM_ERR ], 'data' => []], JSON_NUMERIC_CHECK), true);
  46. if ($mode == 'insert') {
  47. if (filter_var($url, FILTER_VALIDATE_URL) === false) return json_decode(json_encode(['code' => GeneralConst::API_PARAM_ERR, 'msg' => GeneralConst::$apiMap[ GeneralConst::API_PARAM_ERR ], 'data' => []], JSON_NUMERIC_CHECK), true);
  48. if (!$this->checkParamSv->LenMToN($url, 1, 1024)) return json_decode(json_encode(['code' => GeneralConst::API_PARAM_ERR, 'msg' => GeneralConst::$apiMap[ GeneralConst::API_PARAM_ERR ], 'data' => []], JSON_NUMERIC_CHECK), true);
  49. }
  50. if ($memo != filter_var($memo, FILTER_SANITIZE_SPECIAL_CHARS)) return json_decode(json_encode(['code' => GeneralConst::API_PARAM_ERR, 'msg' => GeneralConst::$apiMap[ GeneralConst::API_PARAM_ERR ], 'data' => []], JSON_NUMERIC_CHECK), true);
  51. if (!$this->checkParamSv->LenMToN($memo, 0, 20)) return json_decode(json_encode(['code' => GeneralConst::API_PARAM_ERR, 'msg' => GeneralConst::$apiMap[ GeneralConst::API_PARAM_ERR ], 'data' => []], JSON_NUMERIC_CHECK), true);
  52. // 服務層
  53. if ($mode == "insert") {
  54. \Log::info('start getCode');
  55. $code = $this->urlManagementSv->getCode();
  56. \Log::info($code);
  57. // 新增模式
  58. $urlid = $this->urlManagementSv->insertUrl($title, $url, $code, $memo, $valid, $this->oid);
  59. } else {
  60. // 編輯模式
  61. $this->urlManagementSv->modifyUrl($id, $title, $url, $memo, $valid);
  62. }
  63. // 返回
  64. return json_decode(json_encode(['code' => GeneralConst::API_OK, 'msg' => GeneralConst::$apiMap[ GeneralConst::API_OK ], 'data' => $urlid], JSON_NUMERIC_CHECK), true);
  65. }
  66. public function fetch(Request $request)
  67. {
  68. // 鑒權
  69. if ($this->oid == 0) return json_decode(json_encode(['code' => $this->apicode, 'msg' => GeneralConst::$apiMap[ $this->apicode ], 'data' => []], JSON_NUMERIC_CHECK), true);
  70. // 服務層
  71. $recordsTotal = 0;
  72. $result = $this->urlManagementSv->getUrls(
  73. $recordsTotal,
  74. 1,
  75. 'asc',
  76. 0,
  77. 1048576, // 由於不分頁,內部單一用戶或事業體不可能用到百萬等級,所以不想額外做功能
  78. '',
  79. '',
  80. '',
  81. "1900-01-01",
  82. "2050-12-31",
  83. GeneralConst::URL_OK, // 只有返回有效的
  84. $this->oid
  85. );
  86. // 整理返回資料
  87. $data = array();
  88. for ($i = 0; $i < count($result); $i++) {
  89. $data[] = [
  90. 'id' => $result[ $i ]["serno"],
  91. 'code' => $result[ $i ]["api_code"],
  92. 'url' => env('ORL_URL') . $result[ $i ]["api_code"],
  93. 'pv' => $result[ $i ]["pv"],
  94. 'qrcode' => $result[ $i ]["api_qrcode"],
  95. ];
  96. }
  97. // 返回
  98. return json_decode(json_encode(['code' => GeneralConst::API_OK, 'msg' => GeneralConst::$apiMap[ GeneralConst::API_OK ], 'data' => $data], JSON_NUMERIC_CHECK), true);
  99. }
  100. public function fetchById(Request $request)
  101. {
  102. // 鑒權
  103. if ($this->oid == 0) return json_decode(json_encode(['code' => $this->apicode, 'msg' => GeneralConst::$apiMap[ $this->apicode ], 'data' => []], JSON_NUMERIC_CHECK), true);
  104. // 服務層
  105. $orlId = $request->input('orlId');
  106. $result = $this->urlManagementSv->getUrlById($orlId, $this->oid);
  107. // 整理返回資料
  108. $data = [];
  109. if (!empty($result)) {
  110. $data = [
  111. 'id' => $result["serno"],
  112. 'url' => $result["code"],
  113. 'origUrl' => $result["url"],
  114. 'title' => $result["title"]
  115. ];
  116. }
  117. // 返回
  118. return json_decode(json_encode(['code' => GeneralConst::API_OK, 'msg' => GeneralConst::$apiMap[ GeneralConst::API_OK ], 'data' => $data], JSON_NUMERIC_CHECK), true);
  119. }
  120. public function dateseg(Request $request)
  121. {
  122. // 鑒權
  123. if ($this->oid == 0) return json_decode(json_encode(['code' => $this->apicode, 'msg' => GeneralConst::$apiMap[ $this->apicode ], 'data' => []], JSON_NUMERIC_CHECK), true);
  124. // 服務層
  125. $result = $this->urlManagementSv->getDateseg($this->oid);
  126. // 整理返回資料
  127. $data = array();
  128. $tmp = '';
  129. for ($i = 0; $i < count($result); $i++) {
  130. if ($tmp != $result[ $i ]["code"]) {
  131. $tmp = $result[ $i ]["code"];
  132. }
  133. $data[ $tmp ][] = [
  134. 'date' => $result[ $i ]["date"],
  135. 'cnt' => $result[ $i ]["cnt"],
  136. ];
  137. }
  138. // 返回
  139. return json_decode(json_encode(['code' => GeneralConst::API_OK, 'msg' => GeneralConst::$apiMap[ GeneralConst::API_OK ], 'data' => $data], JSON_NUMERIC_CHECK), true);
  140. }
  141. public function qrcode($code)
  142. {
  143. header("Cache-Control: no-cache, must-revalidate");
  144. header("Pragma: no-cache");
  145. header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
  146. header('Content-Description: File Transfer');
  147. header('Content-Disposition: attachment; filename="' . $code . '.png"');
  148. header('Content-Transfer-Encoding: binary');
  149. header('Content-type: image/png');
  150. $options = new QROptions([
  151. 'version' => 2,
  152. 'outputType' => QRCode::OUTPUT_IMAGE_PNG,
  153. 'imageBase64' => false,
  154. ]);
  155. echo (new QRCode($options))->render(env('ORL_URL') . $code);
  156. }
  157. public function takeOrlPV(Request $request){
  158. // 鑒權
  159. if ($this->oid == 0) return json_decode(json_encode(['code' => $this->apicode, 'msg' => GeneralConst::$apiMap[ $this->apicode ], 'data' => []], JSON_NUMERIC_CHECK), true);
  160. // 服務層
  161. $orlId = $request->input('orlId');
  162. $result = $this->urlManagementSv->takeOrlPV($orlId, $this->oid);
  163. // 整理返回資料
  164. $data = [];
  165. if (!empty($result)) {
  166. $data = [
  167. 'id' => $result["serno"],
  168. 'pv' => $result["pv"],
  169. ];
  170. }
  171. // 返回
  172. return json_decode(json_encode(['code' => GeneralConst::API_OK, 'msg' => GeneralConst::$apiMap[ GeneralConst::API_OK ], 'data' => $data], JSON_NUMERIC_CHECK), true);
  173. }
  174. public function datesegById(Request $request){
  175. // 鑒權
  176. if ($this->oid == 0) return json_decode(json_encode(['code' => $this->apicode, 'msg' => GeneralConst::$apiMap[ $this->apicode ], 'data' => []], JSON_NUMERIC_CHECK), true);
  177. // 服務層
  178. $orlId = $request->input('orlId');
  179. $result = $this->urlManagementSv->getDatesegById($orlId, $this->oid);
  180. // 整理返回資料
  181. $data = array();
  182. $tmp = '';
  183. for ($i = 0; $i < count($result); $i++) {
  184. $data[] = [
  185. 'date' => $result[ $i ]["date"],
  186. 'cnt' => $result[ $i ]["cnt"],
  187. ];
  188. }
  189. // 返回
  190. return json_decode(json_encode(['code' => GeneralConst::API_OK, 'msg' => GeneralConst::$apiMap[ GeneralConst::API_OK ], 'data' => $data], JSON_NUMERIC_CHECK), true);
  191. }
  192. }