Api2021Controller.php 48KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. <?php
  2. namespace App\Http\Controllers\Web;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use App\Http\Services\Web\Api2021Service;
  6. use App\Http\Services\CheckParamService;
  7. use Illuminate\Support\Facades\Validator;
  8. use App\Http\Services\ConstDef\GeneralConst;
  9. use Illuminate\Support\Facades\Redis;
  10. use App\Http\Services\Backend\DataManagement\SettingManagementService;
  11. use Illuminate\Support\Facades\Cookie;
  12. class Api2021Controller extends Controller
  13. {
  14. private $aSv;
  15. private $cSv;
  16. protected $sisLog;
  17. protected $redis;
  18. private $settingManagementSv;
  19. public function __construct()
  20. {
  21. $this->aSv = new Api2021Service();
  22. $this->cSv = new CheckParamService();
  23. $this->settingManagementSv = new SettingManagementService();
  24. $this->sisLog = [
  25. 'type' => GeneralConst::LOG_USER,
  26. 'cdate' => date("Y-m-d H:i:s"),
  27. ];
  28. }
  29. public function __destruct()
  30. {
  31. $this->aSv->syslog($this->sisLog);
  32. }
  33. // 申報系統(已廢棄)
  34. public function getinfo($hash)
  35. {
  36. // 業務驗證(解析HASH/是否封存/是否上架中)
  37. $hash = $this->aSv->hashParse($hash);
  38. if (!$this->aSv->hashCheck($hash)) return response()->json(["succ" => false, "err" => '代碼錯誤']);
  39. // 取得渲染用資訊
  40. $info = $this->aSv->getinfo($hash);
  41. // 返回
  42. return response()->json(["succ" => true, "err" => '', "info" => $info]);
  43. }
  44. public function saveimg($hash, Request $request)
  45. {
  46. // 參數驗證
  47. $messages = [
  48. 'type.required' => 'type 必填',
  49. 'type.regex' => 'type 格式錯誤',
  50. ];
  51. $validate = Validator::make($request->all(), [
  52. 'type' => ['required', 'regex:/^(' . implode('|', GeneralConst::$appendixValidateMap) . ')$/'],
  53. ], $messages);
  54. if ($validate->fails()) {
  55. $err = $validate->errors();
  56. $err_msg = "";
  57. foreach ($err->all() as $item) {
  58. $err_msg .= $item . "\n";
  59. }
  60. return response()->json(["succ" => false, "err" => $err_msg]);
  61. } else {
  62. $type = $request->input('type', '');
  63. }
  64. // 業務驗證(解析HASH/是否封存/是否上架中)
  65. $hash = $this->aSv->hashParse($hash);
  66. if (!$this->aSv->hashCheck($hash)) return response()->json(["succ" => false, "err" => '代碼錯誤']);
  67. // 路徑命名
  68. $dirname = 'material';
  69. $date = date("Ymd");
  70. if (!file_exists($dirname)) mkdir($dirname, 0777, true);
  71. if (!file_exists($dirname . '/' . GeneralConst::$appendixMap[ $type ]['web_path'])) mkdir($dirname . '/' . GeneralConst::$appendixMap[ $type ]['web_path'], 0777, true);
  72. if (!file_exists($dirname . '/' . GeneralConst::$appendixMap[ $type ]['web_path'] . '/' . $date)) mkdir($dirname . '/' . GeneralConst::$appendixMap[ $type ]['web_path'] . '/' . $date, 0777, true);
  73. $basepath = $dirname . '/' . GeneralConst::$appendixMap[ $type ]['web_path'] . '/' . $date . '/';
  74. // 保存圖片
  75. $time = md5(bcrypt(microtime()));
  76. $err = '';
  77. $url = '';
  78. if ($request->hasFile('img')) {
  79. if ($request->file('img')->isValid()) {
  80. if (!in_array($request->file('img')->getMimeType(), ['image/jpeg'])) $err = '只接受 jpg 圖片';
  81. if ($request->file('img')->getSize() > 5242880) $err = '圖片檔案太大'; // 5MB
  82. if ($err == '') {
  83. $request->file('img')->move($basepath, $time . GeneralConst::$appendixMap[ $type ]['ext']);
  84. $url = $basepath . $time . GeneralConst::$appendixMap[ $type ]['ext'];
  85. }
  86. } else {
  87. $err = '圖片格式有誤';
  88. }
  89. } else {
  90. $err = '圖片檔案太大';
  91. }
  92. if ($err != '') return response()->json(["succ" => false, "err" => $err]);
  93. // 返回
  94. return response()->json(["succ" => true, "err" => '', "url" => $url]);
  95. }
  96. public function savedata($hash, Request $request)
  97. {
  98. // 參數驗證
  99. $messages = [
  100. 'name.required' => '姓名必填',
  101. 'name.min' => '姓名字數限制為1-50',
  102. 'name.max' => '姓名字數限制為1-50',
  103. 'identity.required' => '身分證號必填',
  104. 'identity.min' => '身分證號格式錯誤',
  105. 'identity.max' => '身分證號格式錯誤',
  106. 'add_host.required' => '戶籍地址必填',
  107. 'add_host.min' => '戶籍地址字數限制為1-200',
  108. 'add_host.max' => '戶籍地址字數限制為1-200',
  109. 'add_contact.required' => '通訊地址必填',
  110. 'add_contact.min' => '通訊地址字數限制為1-200',
  111. 'add_contact.max' => '通訊地址字數限制為1-200',
  112. 'tel.required' => '電話必填',
  113. 'tel.min' => '電話字數限制為1-20',
  114. 'tel.max' => '電話字數限制為1-20',
  115. ];
  116. $validate = Validator::make($request->all(), [
  117. 'name' => 'required|min:1|max:50',
  118. 'identity' => 'required|min:10|max:10',
  119. 'add_host' => 'required|min:1|max:200',
  120. 'add_contact' => 'required|min:1|max:200',
  121. 'tel' => 'required|min:1|max:20',
  122. ], $messages);
  123. if ($validate->fails()) {
  124. $err = $validate->errors();
  125. $err_msg = "";
  126. foreach ($err->all() as $item) {
  127. $err_msg .= $item . "\n";
  128. }
  129. return response()->json(["succ" => false, "err" => $err_msg]);
  130. } else {
  131. $name = $request->input('name', '');
  132. $identity = $request->input('identity', '');
  133. $add_host = $request->input('add_host', '');
  134. $add_contact = $request->input('add_contact', '');
  135. $tel = $request->input('tel', '');
  136. }
  137. // 業務驗證(解析HASH/是否封存/是否上架中)
  138. $hash = $this->aSv->hashParse($hash);
  139. if (!$this->aSv->hashCheck($hash)) return response()->json(["succ" => false, "err" => '代碼錯誤']);
  140. // 業務驗證(身分證字號)
  141. if (!$this->cSv->isIdentity($identity)) return response()->json(["succ" => false, "err" => '身分證格式有誤']);
  142. // 業務驗證(是否有本獎項需要的素材)
  143. $info = $this->aSv->getinfo($hash);
  144. ${GeneralConst::APPENDIX_IDENTITY_FRONT} = '';
  145. ${GeneralConst::APPENDIX_IDENTITY_BACK} = '';
  146. ${GeneralConst::APPENDIX_PASSBOOK} = '';
  147. if ($info['is_ide'] == GeneralConst::PHOTO_YES) {
  148. ${GeneralConst::APPENDIX_IDENTITY_FRONT} = $request->input(GeneralConst::APPENDIX_IDENTITY_FRONT, '');
  149. ${GeneralConst::APPENDIX_IDENTITY_BACK} = $request->input(GeneralConst::APPENDIX_IDENTITY_BACK, '');
  150. if (!file_exists(${GeneralConst::APPENDIX_IDENTITY_FRONT})) {
  151. return response()->json(["succ" => false, "err" => '請重新上傳身分證正面']);
  152. } else {
  153. if (!preg_match('/^material\/' . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_FRONT ]['web_path'] . '\/[0-9]{8}\/[a-zA-Z0-9]{32}' . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_FRONT ]['ext'] . '$/', ${GeneralConst::APPENDIX_IDENTITY_FRONT})) {
  154. return response()->json(["succ" => false, "err" => '身分證正面路徑有問題']);
  155. }
  156. }
  157. if (!file_exists(${GeneralConst::APPENDIX_IDENTITY_BACK})) {
  158. return response()->json(["succ" => false, "err" => '請重新上傳身分證背面']);
  159. } else {
  160. if (!preg_match('/^material\/' . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_BACK ]['web_path'] . '\/[0-9]{8}\/[a-zA-Z0-9]{32}' . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_BACK ]['ext'] . '$/', ${GeneralConst::APPENDIX_IDENTITY_BACK})) {
  161. return response()->json(["succ" => false, "err" => '身分證背面路徑有問題']);
  162. }
  163. }
  164. }
  165. if ($info['id_acc'] == GeneralConst::PHOTO_YES) {
  166. ${GeneralConst::APPENDIX_PASSBOOK} = $request->input(GeneralConst::APPENDIX_PASSBOOK, '');
  167. if (!file_exists(${GeneralConst::APPENDIX_PASSBOOK})) {
  168. return response()->json(["succ" => false, "err" => '請重新上傳存摺']);
  169. } else {
  170. if (!preg_match('/^material\/' . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_PASSBOOK ]['web_path'] . '\/[0-9]{8}\/[a-zA-Z0-9]{32}' . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_PASSBOOK ]['ext'] . '$/', ${GeneralConst::APPENDIX_PASSBOOK})) {
  171. return response()->json(["succ" => false, "err" => '存摺路徑有問題']);
  172. }
  173. }
  174. }
  175. // 業務邏輯(製作PDF)
  176. $pdf_path = $this->aSv->makePDF($info, $name, $identity, $add_host, $add_contact, $tel, ${GeneralConst::APPENDIX_IDENTITY_FRONT}, ${GeneralConst::APPENDIX_IDENTITY_BACK}, ${GeneralConst::APPENDIX_PASSBOOK});
  177. // 業務邏輯(存入資料庫)
  178. $this->aSv->savedata($hash[1], $name, $identity, $add_host, $add_contact, $tel, ${GeneralConst::APPENDIX_IDENTITY_FRONT}, ${GeneralConst::APPENDIX_IDENTITY_BACK}, ${GeneralConst::APPENDIX_PASSBOOK}, $pdf_path);
  179. // 返回
  180. return response()->json(["succ" => true, "err" => '', "url" => $pdf_path]);
  181. }
  182. // 小遊戲 0518
  183. public function info($lineId)
  184. {
  185. // testonly 等待 N 秒
  186. // $now = time();
  187. // while(time() - $now < 6) {}
  188. // return response()->json(["succ" => true, "err" => '', "info" => []]);
  189. // testonly
  190. // $this->aSv->debuglog('server', [
  191. // 'SERVER_ADDR' => $_SERVER['SERVER_ADDR'],
  192. // 'lineUserId' => $lineId,
  193. // ]);
  194. $this->sisLog['func'] = __FUNCTION__;
  195. $this->sisLog['k'] = $lineId;
  196. $this->sisLog['memoIn'] = '';
  197. $this->sisLog['memoOut'] = '';
  198. // testonly
  199. // $this->aSv->debuglog([
  200. // 'hashLineId' => $hashLineId,
  201. // 'cookieGamecc' => Cookie::get('gamecc')
  202. // ]);
  203. // 業務驗證(是否加入LINE帳號的好友) [不存在 cookie 的時候就要進直通查詢,造成每天只要最多一次查詢就好]
  204. if (!Cookie::get('gamecc')) {
  205. // if (false) { // testonly
  206. // if (!$this->aSv->isAuthUserExists($lineId)) { // testonly 用資料庫檢查
  207. if (!$this->aSv->lineFriendCheck($lineId)) {
  208. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  209. $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
  210. return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
  211. }
  212. // else { // testonly 用資料庫檢查
  213. // $this->aSv->addAuthUser($lineId);
  214. // }
  215. }
  216. // 業務邏輯(檢查有無本地資料,沒有就新增,順便取得個人資訊包含吉點)
  217. $playerinfo = $this->aSv->playerinfo($lineId);
  218. // 業務邏輯(取得當天所屬於的回合日期資訊,包含回合起訖日、本回合簽到狀態)
  219. // 業務邏輯(取得當回合的兌獎狀態,包含所有獎項以及可換與否)
  220. $roundinfo = $this->aSv->roundinfo($lineId);
  221. $goodinfo = $this->aSv->goodinfo($lineId);
  222. if (!$roundinfo && !$goodinfo) {
  223. // 遊戲時間已過 && 兌獎時間已過
  224. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  225. $this->sisLog['memoOut'] = json_encode(['msg' => '活動已結束'], JSON_UNESCAPED_UNICODE);
  226. return response()->json(["succ" => false, "err" => '活動已結束']);
  227. } else {
  228. // 要嘛都在期限內,要嘛一定至少兌獎在期限內因為他的時間範圍較大,這時候就要把回合簽到資訊清空
  229. if (!$roundinfo) $roundinfo = [];
  230. }
  231. // 業務邏輯(整理實際可兌換的獎項)
  232. foreach ($goodinfo as &$g) if ($playerinfo['gp'] < $g['gp']) $g['canuse'] = false;
  233. // 資料整理
  234. $info = [
  235. 'playerinfo' => $playerinfo,
  236. 'roundinfo' => $roundinfo,
  237. 'goodinfo' => $goodinfo,
  238. ];
  239. $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
  240. $this->sisLog['memoOut'] = json_encode(['info' => $info], JSON_UNESCAPED_UNICODE);
  241. // 返回
  242. if (Cookie::get('gamecc')) {
  243. return response()->json(["succ" => true, "err" => '', "info" => $info]);
  244. } else {
  245. return response()->json(["succ" => true, "err" => '', "info" => $info])->cookie('gamecc', time(), 1440);
  246. }
  247. }
  248. public function game($lineId, Request $request)
  249. {
  250. // testonly 等待 N 秒
  251. // $now = time();
  252. // while(time() - $now < 6) {}
  253. $this->sisLog['func'] = __FUNCTION__;
  254. $this->sisLog['k'] = $lineId;
  255. $this->sisLog['memoIn'] = '';
  256. $this->sisLog['memoOut'] = '';
  257. // 業務驗證(是否加入LINE帳號的好友) [不存在 cookie 的時候就要進直通查詢,造成每天只要最多一次查詢就好]
  258. // if (false) { // testonly
  259. if (!Cookie::get('gamecc')) {
  260. if (!$this->aSv->lineFriendCheck($lineId)) {
  261. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  262. $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
  263. return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
  264. }
  265. }
  266. // 業務驗證(是否在本地有資料)
  267. if (!$this->aSv->isPlayer($lineId)) {
  268. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  269. $this->sisLog['memoOut'] = json_encode(['msg' => '請從首頁進入'], JSON_UNESCAPED_UNICODE);
  270. return response()->json(["succ" => false, "err" => '請從首頁進入']);
  271. }
  272. // 業務驗證(是否今日已玩過遊戲與簽到)
  273. // if (false) { // testonly for 壓測
  274. // if ($lineId != 'Ud2534d6e1eed9cee2c919a993443352b') {
  275. if ($lineId != $this->settingManagementSv->getSetting()['GAME_TESTING_LINE_ID']) {
  276. if (!$this->aSv->canPlay($lineId)) {
  277. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  278. $this->sisLog['memoOut'] = json_encode(['msg' => '今日已玩過遊戲或者活動結束'], JSON_UNESCAPED_UNICODE);
  279. return response()->json(["succ" => false, "err" => '今日已玩過遊戲或者活動結束']);
  280. }
  281. }
  282. // 參數驗證
  283. $name = '';
  284. $messages = [
  285. 'name.required' => '姓名必填',
  286. 'name.max' => '姓名最多100字',
  287. ];
  288. $validate = Validator::make($request->all(), [
  289. 'name' => 'required|max:100',
  290. ], $messages);
  291. if ($validate->fails()) {
  292. $err = $validate->errors();
  293. $err_msg = "";
  294. foreach ($err->all() as $item) {
  295. $err_msg .= $item . "\n";
  296. }
  297. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  298. $this->sisLog['memoOut'] = json_encode(['msg' => $err_msg], JSON_UNESCAPED_UNICODE);
  299. return response()->json(["succ" => false, "err" => $err_msg]);
  300. } else {
  301. $name = $request->input('name', '');
  302. }
  303. // 業務邏輯(玩遊戲簽到)
  304. $res = $this->aSv->playGameAndCheckin($lineId, $name);
  305. // 資料整理
  306. $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
  307. $this->sisLog['memoOut'] = json_encode(['res' => $res], JSON_UNESCAPED_UNICODE);
  308. // 返回
  309. if (Cookie::get('gamecc')) {
  310. return response()->json(["succ" => true, "err" => '', "currentGameGp" => $res['currentGameGp'], "currentCheckinGp" => $res['currentCheckinGp'], "gpAll" => $res['gp']]);
  311. } else {
  312. return response()->json(["succ" => true, "err" => '', "currentGameGp" => $res['currentGameGp'], "currentCheckinGp" => $res['currentCheckinGp'], "gpAll" => $res['gp']])->cookie('gamecc', time(), 1440);
  313. }
  314. }
  315. public function redeem($lineId, $gid, Request $request)
  316. {
  317. $this->sisLog['func'] = __FUNCTION__;
  318. $this->sisLog['k'] = $lineId;
  319. $this->sisLog['memoIn'] = '';
  320. $this->sisLog['memoOut'] = '';
  321. // 打點數量限制
  322. // 作用範圍: ALL
  323. $redis = new Redis();
  324. $redis::connect('localhost', 6379);
  325. $rlimitkey = $this->settingManagementSv->getSetting()['GAME_ESI_TEST_EVENT_KEY'];
  326. if (!$redis::exists($rlimitkey)) {
  327. $redis::set($rlimitkey, 1);
  328. } else {
  329. $redis::incr($rlimitkey);
  330. }
  331. if ($redis::get($rlimitkey) > $this->settingManagementSv->getSetting()['GAME_ESI_TEST_EVENT_LIMIT']) {
  332. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  333. $this->sisLog['memoOut'] = json_encode(['msg' => '測試點數不能再打了'], JSON_UNESCAPED_UNICODE);
  334. return response()->json(["succ" => false, "err" => '測試點數不能再打了']);
  335. }
  336. // \Log::debug($rlimitkey);
  337. // \Log::debug($redis::get($rlimitkey));
  338. // return response()->json(["succ" => true, "err" => 'okok']);
  339. // 業務驗證(防重)
  340. // 作用範圍: 以人為單位
  341. $rkey = $this->settingManagementSv->getSetting()['GAME_ESI_TEST_EVENT_LIMIT'] . '_' . $lineId;
  342. if (!$redis::setnx($rkey, 'true')) {
  343. $redis::expire($rkey, 15); // 15 秒防重(這樣就算業務在其他地方掛了,他也會自動失效)
  344. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  345. $this->sisLog['memoOut'] = json_encode(['msg' => '您操作過快,請稍後再試'], JSON_UNESCAPED_UNICODE);
  346. return response()->json(["succ" => false, "err" => '您操作過快,請稍後再試']);
  347. }
  348. // 業務驗證(是否加入LINE帳號的好友)
  349. if (!$this->aSv->lineFriendCheck($lineId)) {
  350. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  351. $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
  352. return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
  353. }
  354. // 業務驗證(是否在本地有資料)
  355. if (!$this->aSv->isPlayer($lineId)) {
  356. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  357. $this->sisLog['memoOut'] = json_encode(['msg' => '請從首頁進入'], JSON_UNESCAPED_UNICODE);
  358. return response()->json(["succ" => false, "err" => '請從首頁進入']);
  359. }
  360. // 業務驗證(取得當回合的兌獎狀態,包含所有獎項以及可換與否,拿來比對驗證)
  361. $canuse = false;
  362. foreach ($this->aSv->goodinfo($lineId) as $g) if ($g['id'] == $gid) if ($g['canuse'] == true) $canuse = true;
  363. if (!$canuse) {
  364. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  365. $this->sisLog['memoOut'] = json_encode(['msg' => '獎項不存在或沒有兌獎所需吉點'], JSON_UNESCAPED_UNICODE);
  366. return response()->json(["succ" => false, "err" => '獎項不存在或沒有兌獎所需吉點']);
  367. }
  368. // 參數驗證
  369. $name = '';
  370. $messages = [
  371. 'name.required' => '姓名必填',
  372. 'name.max' => '姓名最多100字',
  373. ];
  374. $validate = Validator::make($request->all(), [
  375. 'name' => 'required|max:100',
  376. ], $messages);
  377. if ($validate->fails()) {
  378. $err = $validate->errors();
  379. $err_msg = "";
  380. foreach ($err->all() as $item) {
  381. $err_msg .= $item . "\n";
  382. }
  383. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  384. $this->sisLog['memoOut'] = json_encode(['msg' => $err_msg], JSON_UNESCAPED_UNICODE);
  385. return response()->json(["succ" => false, "err" => $err_msg]);
  386. } else {
  387. $name = $request->input('name', '');
  388. }
  389. // 業務邏輯(兌點)
  390. $redeeminfo = $this->aSv->redeemGood($lineId, $gid, $name);
  391. if (!$redeeminfo) {
  392. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  393. $this->sisLog['memoOut'] = json_encode(['msg' => '兌換失敗'], JSON_UNESCAPED_UNICODE);
  394. return response()->json(["succ" => false, "err" => '兌換失敗']);
  395. }
  396. // 資料整理
  397. $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId, 'gid' => $gid], JSON_UNESCAPED_UNICODE);
  398. $this->sisLog['memoOut'] = json_encode(['redeeminfo' => $redeeminfo], JSON_UNESCAPED_UNICODE);
  399. // 防重解鎖
  400. $redis::del($rkey);
  401. return response()->json(["succ" => true, "err" => '']);
  402. }
  403. public function test()
  404. {
  405. // if (Cookie::get('ttcc')) {
  406. // return response()->json(["succ" => true, "err" => '', "info" => 'I have cookie: ' . Cookie::get('ttcc')]);
  407. // } else {
  408. // return response()->json(["succ" => true, "err" => '', "info" => 'no cookie'])->cookie('ttcc', '123', 1440);
  409. // }
  410. return true;
  411. // return $this->aSv->esiFlowBak();
  412. //return $_SERVER['SERVER_ADDR'] ?? 'no';
  413. }
  414. // 類發票
  415. public function friendchk($lineId)
  416. {
  417. $this->sisLog['func'] = __FUNCTION__;
  418. $this->sisLog['k'] = $lineId;
  419. $this->sisLog['kk'] = 'activity';
  420. $this->sisLog['memoIn'] = '';
  421. $this->sisLog['memoOut'] = '';
  422. // 業務驗證(是否加入LINE帳號的好友)
  423. if (!$this->aSv->lineFriendCheck($lineId, 'activity')) {
  424. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  425. $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
  426. return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
  427. }
  428. // 資料整理
  429. $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
  430. $this->sisLog['memoOut'] = json_encode(['res' => ''], JSON_UNESCAPED_UNICODE);
  431. // 返回
  432. return response()->json(["succ" => true, "err" => '']);
  433. }
  434. public function activities($lineId)
  435. {
  436. $this->sisLog['func'] = __FUNCTION__;
  437. $this->sisLog['k'] = $lineId;
  438. $this->sisLog['kk'] = 'activity';
  439. $this->sisLog['memoIn'] = '';
  440. $this->sisLog['memoOut'] = '';
  441. // 業務驗證(是否加入LINE帳號的好友)
  442. if (!$this->aSv->lineFriendCheck($lineId, 'activity')) {
  443. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  444. $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
  445. return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
  446. }
  447. // 業務邏輯(取得活動時間資訊)
  448. $activities = $this->aSv->activities();
  449. // 資料整理
  450. $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
  451. $this->sisLog['memoOut'] = json_encode(['res' => $activities], JSON_UNESCAPED_UNICODE);
  452. // 返回
  453. return response()->json(["succ" => true, "err" => '', "data" => $activities]);
  454. }
  455. public function setdoc($lineId, Request $request)
  456. {
  457. $this->sisLog['func'] = __FUNCTION__;
  458. $this->sisLog['k'] = $lineId;
  459. $this->sisLog['kk'] = 'activity';
  460. $this->sisLog['memoIn'] = '';
  461. $this->sisLog['memoOut'] = '';
  462. // 業務驗證(是否加入LINE帳號的好友)
  463. // if (!$this->aSv->lineFriendCheck($lineId, 'activity')) {
  464. // $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  465. // $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
  466. //
  467. // return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
  468. // }
  469. // 參數驗證
  470. $messages = [
  471. 'doc.required' => '圖片必填',
  472. ];
  473. $validate = Validator::make($request->all(), [
  474. 'doc' => 'required',
  475. ], $messages);
  476. if ($validate->fails()) {
  477. $err = $validate->errors();
  478. $err_msg = "";
  479. foreach ($err->all() as $item) {
  480. $err_msg .= $item . "\n";
  481. }
  482. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  483. $this->sisLog['memoOut'] = json_encode(['msg' => $err_msg], JSON_UNESCAPED_UNICODE);
  484. return response()->json(["succ" => false, "err" => $err_msg]);
  485. } else {
  486. $doc = $request->input('doc', '');
  487. $doc = explode(',', $doc);
  488. $doc = base64_decode($doc[1]);
  489. }
  490. // 業務邏輯(圖片辨識)
  491. $data = $this->aSv->setdoc($doc);
  492. // 資料整理
  493. $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
  494. $this->sisLog['memoOut'] = json_encode(['res' => $data], JSON_UNESCAPED_UNICODE);
  495. // 返回
  496. return response()->json(["succ" => true, "err" => '', "data" => $data]);
  497. }
  498. public function reqreceipt($lineId, Request $request)
  499. {
  500. $this->sisLog['func'] = __FUNCTION__;
  501. $this->sisLog['k'] = $lineId;
  502. $this->sisLog['kk'] = 'activity';
  503. $this->sisLog['memoIn'] = '';
  504. $this->sisLog['memoOut'] = '';
  505. // 業務驗證(是否加入LINE帳號的好友)
  506. if (!$this->aSv->lineFriendCheck($lineId, 'activity')) {
  507. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  508. $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
  509. return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
  510. }
  511. // 參數驗證
  512. $messages = [
  513. 'name.required' => '姓名必填',
  514. 'name.max' => '姓名最多64字',
  515. 'order_date.required' => '登錄日期必填',
  516. 'order_date.date_format' => '登錄日期格式錯誤',
  517. 'account_bank.required' => '銀行代碼必填',
  518. 'account_bank.regex' => '銀行代碼格式錯誤',
  519. 'account_no.required' => '銀行帳號必填',
  520. 'account_no.regex' => '銀行帳號格式錯誤',
  521. 'order_no.required' => '訂單編號必填',
  522. 'order_no.regex' => '訂單編號格式錯誤',
  523. ];
  524. $validate = Validator::make($request->all(), [
  525. 'name' => 'required|max:64',
  526. 'order_date' => 'required|date_format:Y-m-d',
  527. 'account_bank' => ['required', 'regex:/^[0-9]{1,10}$/'],
  528. 'account_no' => ['required', 'regex:/^[0-9]{1,30}$/'],
  529. 'order_no' => ['required', 'regex:/^[A-Za-z0-9]{1,30}$/'],
  530. ], $messages);
  531. if ($validate->fails()) {
  532. $err = $validate->errors();
  533. $err_msg = "";
  534. foreach ($err->all() as $item) {
  535. $err_msg .= $item . "\n";
  536. }
  537. $this->sisLog['memoIn'] = json_encode(['error' => 'logical', 'param' => $_POST], JSON_UNESCAPED_UNICODE);
  538. $this->sisLog['memoOut'] = json_encode(['msg' => $err_msg], JSON_UNESCAPED_UNICODE);
  539. return response()->json(["succ" => false, "err" => $err_msg]);
  540. } else {
  541. $info = [
  542. 'name' => $request->input('name', ''),
  543. 'order_date' => $request->input('order_date', ''),
  544. 'account_bank' => $request->input('account_bank', ''),
  545. 'account_no' => $request->input('account_no', ''),
  546. 'order_no' => $request->input('order_no', ''),
  547. ];
  548. }
  549. // 業務驗證(取得當前的登錄區間)
  550. $activityinfo = $this->aSv->activityinfo();
  551. if (!$activityinfo) {
  552. // 尚未到達登錄時間
  553. $this->sisLog['memoIn'] = json_encode(['error' => 'logical', 'param' => $_POST], JSON_UNESCAPED_UNICODE);
  554. $this->sisLog['memoOut'] = json_encode(['msg' => '尚未到達登錄時間'], JSON_UNESCAPED_UNICODE);
  555. return response()->json(["succ" => false, "err" => '尚未到達登錄時間']);
  556. }
  557. // 業務驗證(收據時間也要在活動時間內)
  558. if ($activityinfo[0]['reqTimeBegin'] > $info['order_date'] || $activityinfo[0]['reqTimeFinal'] < $info['order_date']) {
  559. // 尚未到達登錄時間
  560. $this->sisLog['memoIn'] = json_encode(['error' => 'logical', 'param' => $_POST], JSON_UNESCAPED_UNICODE);
  561. $this->sisLog['memoOut'] = json_encode(['msg' => '收據日期錯誤'], JSON_UNESCAPED_UNICODE);
  562. return response()->json(["succ" => false, "err" => '收據日期錯誤']);
  563. }
  564. // 業務邏輯(登錄)
  565. $info['lineId'] = $lineId;
  566. $info['aid'] = $activityinfo[0]['id'];
  567. $id = $this->aSv->reqreceipt($info);
  568. if ($id == 0) {
  569. // 完全相同的資料不能登錄
  570. $this->sisLog['memoIn'] = json_encode(['error' => 'logical', 'param' => $_POST], JSON_UNESCAPED_UNICODE);
  571. $this->sisLog['memoOut'] = json_encode(['msg' => '您已登錄過這筆資料'], JSON_UNESCAPED_UNICODE);
  572. return response()->json(["succ" => false, "err" => '您已登錄過這筆資料']);
  573. }
  574. // 資料整理
  575. $this->sisLog['memoIn'] = json_encode(['info' => $info], JSON_UNESCAPED_UNICODE);
  576. $this->sisLog['memoOut'] = json_encode(['res' => $id], JSON_UNESCAPED_UNICODE);
  577. // 返回
  578. return response()->json(["succ" => true, "err" => '']);
  579. }
  580. public function lstreceipt($lineId)
  581. {
  582. $this->sisLog['func'] = __FUNCTION__;
  583. $this->sisLog['k'] = $lineId;
  584. $this->sisLog['kk'] = 'activity';
  585. $this->sisLog['memoIn'] = '';
  586. $this->sisLog['memoOut'] = '';
  587. // 業務驗證(是否加入LINE帳號的好友)
  588. if (!$this->aSv->lineFriendCheck($lineId, 'activity')) {
  589. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  590. $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
  591. return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
  592. }
  593. // 業務邏輯(取得列表)
  594. $data = $this->aSv->lstreceipt($lineId);
  595. // 資料整理
  596. $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
  597. $this->sisLog['memoOut'] = json_encode(['res' => $data], JSON_UNESCAPED_UNICODE);
  598. // 返回
  599. return response()->json(["succ" => true, "err" => '', "data" => $data]);
  600. }
  601. public function redeemreceipt($lineId, $rid, Request $request)
  602. {
  603. $this->sisLog['func'] = __FUNCTION__;
  604. $this->sisLog['k'] = $lineId;
  605. $this->sisLog['kk'] = 'activity';
  606. $this->sisLog['memoIn'] = '';
  607. $this->sisLog['memoOut'] = '';
  608. // 業務驗證(是否加入LINE帳號的好友)
  609. if (!$this->aSv->lineFriendCheck($lineId, 'activity')) {
  610. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  611. $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
  612. return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
  613. }
  614. // 業務驗證(該紀錄狀態是否可執行本動作/該紀錄的所屬活動是否在登錄的時間範圍內)
  615. if (!$this->aSv->redeemCheck($lineId, (integer)$rid)) {
  616. $this->sisLog['memoIn'] = json_encode(['error' => 'logical', 'rid' => $rid], JSON_UNESCAPED_UNICODE);
  617. $this->sisLog['memoOut'] = json_encode(['msg' => '權限錯誤'], JSON_UNESCAPED_UNICODE);
  618. return response()->json(["succ" => false, "err" => '權限錯誤']);
  619. }
  620. // 業務邏輯(更改狀態)
  621. $this->aSv->redeemExecute($lineId, (integer)$rid);
  622. // 資料整理
  623. $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
  624. $this->sisLog['memoOut'] = json_encode(['res' => ''], JSON_UNESCAPED_UNICODE);
  625. // 返回
  626. return response()->json(["succ" => true, "err" => '']);
  627. }
  628. public function getreceipt($lineId, $rid, Request $request)
  629. {
  630. $this->sisLog['func'] = __FUNCTION__;
  631. $this->sisLog['k'] = $lineId;
  632. $this->sisLog['kk'] = 'activity';
  633. $this->sisLog['memoIn'] = '';
  634. $this->sisLog['memoOut'] = '';
  635. // 業務驗證(防重)
  636. // 作用範圍: 以人為單位
  637. $redis = new Redis();
  638. $redis::connect('localhost', 6379);
  639. $rkey = $this->settingManagementSv->getSetting()['RECEIPT_ESI_TEST_EVENT_KEY'] . '_' . $lineId;
  640. if (!$redis::setnx($rkey, 'true')) {
  641. $redis::expire($rkey, 15); // 15 秒防重(這樣就算業務在其他地方掛了,他也會自動失效)
  642. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  643. $this->sisLog['memoOut'] = json_encode(['msg' => '您操作過快,請稍後再試'], JSON_UNESCAPED_UNICODE);
  644. return response()->json(["succ" => false, "err" => '您操作過快,請稍後再試']);
  645. }
  646. // 業務驗證(是否加入LINE帳號的好友)
  647. if (!$this->aSv->lineFriendCheck($lineId, 'activity')) {
  648. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  649. $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
  650. return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
  651. }
  652. // 業務驗證(該紀錄狀態是否可執行本動作/該紀錄的所屬活動是否在登錄的時間範圍內)
  653. if (!$this->aSv->getCheck($lineId, (integer)$rid)) {
  654. $this->sisLog['memoIn'] = json_encode(['error' => 'logical', 'rid' => $rid], JSON_UNESCAPED_UNICODE);
  655. $this->sisLog['memoOut'] = json_encode(['msg' => '權限錯誤'], JSON_UNESCAPED_UNICODE);
  656. return response()->json(["succ" => false, "err" => '權限錯誤']);
  657. }
  658. // 業務邏輯(更改狀態)
  659. $getinfo = $this->aSv->getExecute($lineId, (integer)$rid);
  660. if (!$getinfo) {
  661. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  662. $this->sisLog['memoOut'] = json_encode(['msg' => '兌換失敗'], JSON_UNESCAPED_UNICODE);
  663. return response()->json(["succ" => false, "err" => '兌換失敗']);
  664. }
  665. // 資料整理
  666. $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
  667. $this->sisLog['memoOut'] = json_encode(['res' => ''], JSON_UNESCAPED_UNICODE);
  668. // 防重解鎖
  669. $redis::del($rkey);
  670. // 返回
  671. return response()->json(["succ" => true, "err" => '']);
  672. }
  673. // 串門子
  674. public function tinfo($lineId, Request $request)
  675. {
  676. $this->sisLog['func'] = __FUNCTION__;
  677. $this->sisLog['k'] = $lineId;
  678. $this->sisLog['kk'] = 'tmz';
  679. $this->sisLog['memoIn'] = '';
  680. $this->sisLog['memoOut'] = '';
  681. // 業務驗證(是否加入LINE帳號的好友) [不存在 cookie 的時候就要進直通查詢,造成每天只要最多一次查詢就好]
  682. if (!Cookie::get('tgamecc')) {
  683. if (!$this->aSv->lineFriendCheck($lineId, 'tmz')) {
  684. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  685. $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
  686. return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
  687. }
  688. }
  689. // 業務邏輯(檢查有無本地資料,沒有就新增,順便取得個人資訊包含吉點)
  690. $playerinfo = $this->aSv->tplayerinfo($lineId);
  691. // 參數驗證
  692. $name = '';
  693. $messages = [
  694. 'name.required' => '姓名必填',
  695. 'name.max' => '姓名最多100字',
  696. ];
  697. $validate = Validator::make($request->all(), [
  698. 'name' => 'required|max:100',
  699. ], $messages);
  700. if ($validate->fails()) {
  701. $err = $validate->errors();
  702. $err_msg = "";
  703. foreach ($err->all() as $item) {
  704. $err_msg .= $item . "\n";
  705. }
  706. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  707. $this->sisLog['memoOut'] = json_encode(['msg' => $err_msg], JSON_UNESCAPED_UNICODE);
  708. return response()->json(["succ" => false, "err" => $err_msg]);
  709. } else {
  710. $name = $request->input('name', '');
  711. }
  712. // 業務邏輯(今日簽到)
  713. $this->aSv->tcheckin($lineId, $name);
  714. // 業務邏輯(取得當天所屬於的回合日期資訊,包含回合起訖日、本回合簽到狀態)
  715. $roundinfo = $this->aSv->troundinfo($lineId);
  716. if (!$roundinfo) {
  717. // 遊戲時間已過 && 兌獎時間已過
  718. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  719. $this->sisLog['memoOut'] = json_encode(['msg' => '活動已結束'], JSON_UNESCAPED_UNICODE);
  720. return response()->json(["succ" => false, "err" => '活動已結束']);
  721. } else {
  722. // 要嘛都在期限內,要嘛一定至少兌獎在期限內因為他的時間範圍較大,這時候就要把回合簽到資訊清空
  723. if (!$roundinfo) $roundinfo = [];
  724. }
  725. // 業務邏輯(取得是否可以轉蛋的資格)
  726. $redeeminfo = $this->aSv->tredeeminfo($lineId);
  727. // 資料整理
  728. $info = [
  729. 'playerinfo' => $playerinfo,
  730. 'roundinfo' => $roundinfo,
  731. 'redeeminfo' => $redeeminfo,
  732. ];
  733. $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
  734. $this->sisLog['memoOut'] = json_encode(['info' => $info], JSON_UNESCAPED_UNICODE);
  735. // 返回
  736. if (Cookie::get('tgamecc')) {
  737. return response()->json(["succ" => true, "err" => '', "info" => $info]);
  738. } else {
  739. return response()->json(["succ" => true, "err" => '', "info" => $info])->cookie('tgamecc', time(), 1440);
  740. }
  741. }
  742. public function tgame($lineId, Request $request)
  743. {
  744. $this->sisLog['func'] = __FUNCTION__;
  745. $this->sisLog['k'] = $lineId;
  746. $this->sisLog['kk'] = 'tmz';
  747. $this->sisLog['memoIn'] = '';
  748. $this->sisLog['memoOut'] = '';
  749. // 業務驗證(是否加入LINE帳號的好友) [不存在 cookie 的時候就要進直通查詢,造成每天只要最多一次查詢就好]
  750. if (!Cookie::get('tgamecc')) {
  751. if (!$this->aSv->lineFriendCheck($lineId, 'tmz')) {
  752. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  753. $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
  754. return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
  755. }
  756. }
  757. // 業務驗證(是否在本地有資料)
  758. if (!$this->aSv->isTPlayer($lineId)) {
  759. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  760. $this->sisLog['memoOut'] = json_encode(['msg' => '請從首頁進入'], JSON_UNESCAPED_UNICODE);
  761. return response()->json(["succ" => false, "err" => '請從首頁進入']);
  762. }
  763. // 參數驗證
  764. $name = '';
  765. $c = 0;
  766. $messages = [
  767. 'name.required' => '姓名必填',
  768. 'name.max' => '姓名最多100字',
  769. 'c.required' => '九宮格必填',
  770. ];
  771. $validate = Validator::make($request->all(), [
  772. 'name' => 'required|max:100',
  773. 'c' => 'required|numeric|min:1|max:9',
  774. ], $messages);
  775. if ($validate->fails()) {
  776. $err = $validate->errors();
  777. $err_msg = "";
  778. foreach ($err->all() as $item) {
  779. $err_msg .= $item . "\n";
  780. }
  781. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  782. $this->sisLog['memoOut'] = json_encode(['msg' => $err_msg], JSON_UNESCAPED_UNICODE);
  783. return response()->json(["succ" => false, "err" => $err_msg]);
  784. } else {
  785. $name = $request->input('name', '');
  786. $c = $request->input('c', 0);
  787. }
  788. // 業務邏輯(玩遊戲)
  789. $res = $this->aSv->playTGame($lineId, $name, $c);
  790. if (!$res) {
  791. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  792. $this->sisLog['memoOut'] = json_encode(['msg' => '已回答過囉'], JSON_UNESCAPED_UNICODE);
  793. return response()->json(["succ" => false, "err" => '已回答過囉']);
  794. }
  795. // 資料整理
  796. $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId, 'c' => $c], JSON_UNESCAPED_UNICODE);
  797. $this->sisLog['memoOut'] = json_encode(['res' => $res], JSON_UNESCAPED_UNICODE);
  798. // 返回
  799. if (Cookie::get('tgamecc')) {
  800. return response()->json(["succ" => true, "err" => '', "currentGameGp" => $res['currentGameGp'], "playerinfo" => $res['gp']]);
  801. } else {
  802. return response()->json(["succ" => true, "err" => '', "currentGameGp" => $res['currentGameGp'], "playerinfo" => $res['gp']])->cookie('tgamecc', time(), 1440);
  803. }
  804. }
  805. public function tredeem($lineId, Request $request)
  806. {
  807. $this->sisLog['func'] = __FUNCTION__;
  808. $this->sisLog['k'] = $lineId;
  809. $this->sisLog['kk'] = 'tmz';
  810. $this->sisLog['memoIn'] = '';
  811. $this->sisLog['memoOut'] = '';
  812. // 打點數量限制
  813. // 作用範圍: ALL
  814. $redis = new Redis();
  815. $redis::connect('localhost', 6379);
  816. $rlimitkey = $this->settingManagementSv->getSetting()['TMZ_ESI_TEST_EVENT_KEY'];
  817. if (!$redis::exists($rlimitkey)) {
  818. $redis::set($rlimitkey, 1);
  819. } else {
  820. $redis::incr($rlimitkey);
  821. }
  822. \Log::debug($rlimitkey);
  823. if ($redis::get($rlimitkey) > $this->settingManagementSv->getSetting()['TMZ_ESI_TEST_EVENT_LIMIT']) {
  824. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  825. $this->sisLog['memoOut'] = json_encode(['msg' => '測試點數不能再打了'], JSON_UNESCAPED_UNICODE);
  826. return response()->json(["succ" => false, "err" => '測試點數不能再打了']);
  827. }
  828. // 業務驗證(防重)
  829. // 作用範圍: 以人為單位
  830. $rkey = $this->settingManagementSv->getSetting()['TMZ_ESI_TEST_EVENT_LIMIT'] . '_' . $lineId;
  831. \Log::debug($rkey);
  832. if (!$redis::setnx($rkey, 'true')) {
  833. $redis::expire($rkey, 15); // 15 秒防重(這樣就算業務在其他地方掛了,他也會自動失效)
  834. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  835. $this->sisLog['memoOut'] = json_encode(['msg' => '您操作過快,請稍後再試'], JSON_UNESCAPED_UNICODE);
  836. return response()->json(["succ" => false, "err" => '您操作過快,請稍後再試']);
  837. }
  838. // 業務驗證(是否加入LINE帳號的好友)
  839. if (!$this->aSv->lineFriendCheck($lineId, 'tmz')) {
  840. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  841. $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
  842. return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
  843. }
  844. // 業務驗證(是否在本地有資料)
  845. if (!$this->aSv->isTPlayer($lineId)) {
  846. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  847. $this->sisLog['memoOut'] = json_encode(['msg' => '請從首頁進入'], JSON_UNESCAPED_UNICODE);
  848. return response()->json(["succ" => false, "err" => '請從首頁進入']);
  849. }
  850. // 參數驗證
  851. $name = '';
  852. $messages = [
  853. 'name.required' => '姓名必填',
  854. 'name.max' => '姓名最多100字',
  855. ];
  856. $validate = Validator::make($request->all(), [
  857. 'name' => 'required|max:100',
  858. ], $messages);
  859. if ($validate->fails()) {
  860. $err = $validate->errors();
  861. $err_msg = "";
  862. foreach ($err->all() as $item) {
  863. $err_msg .= $item . "\n";
  864. }
  865. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  866. $this->sisLog['memoOut'] = json_encode(['msg' => $err_msg], JSON_UNESCAPED_UNICODE);
  867. return response()->json(["succ" => false, "err" => $err_msg]);
  868. } else {
  869. $name = $request->input('name', '');
  870. }
  871. // 業務邏輯(可否轉蛋再次確認)
  872. if (!$this->aSv->tredeeminfo($lineId)) {
  873. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  874. $this->sisLog['memoOut'] = json_encode(['msg' => '吉點不夠不能轉蛋'], JSON_UNESCAPED_UNICODE);
  875. return response()->json(["succ" => false, "err" => '吉點不夠不能轉蛋']);
  876. }
  877. // 業務邏輯(轉蛋兌點)
  878. $redeeminfo = $this->aSv->redeemTGood($lineId, $name);
  879. if (!$redeeminfo) {
  880. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  881. $this->sisLog['memoOut'] = json_encode(['msg' => '銘謝惠顧'], JSON_UNESCAPED_UNICODE);
  882. return response()->json(["succ" => false, "err" => '銘謝惠顧']);
  883. }
  884. // 資料整理
  885. $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
  886. $this->sisLog['memoOut'] = json_encode(['redeeminfo' => $redeeminfo], JSON_UNESCAPED_UNICODE);
  887. unset($redeeminfo['playerinfo']['lineId']);
  888. unset($redeeminfo['redeeminfo']['fromIssuedQty']);
  889. unset($redeeminfo['redeeminfo']['toIssuedQty']);
  890. unset($redeeminfo['session']);
  891. // 防重解鎖
  892. $redis::del($rkey);
  893. return response()->json(["succ" => true, "err" => '', 'info' => $redeeminfo]);
  894. }
  895. }