Api2021Controller.php 48KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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 (!$this->aSv->canPlay($lineId)) {
  276. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  277. $this->sisLog['memoOut'] = json_encode(['msg' => '今日已玩過遊戲或者活動結束'], JSON_UNESCAPED_UNICODE);
  278. return response()->json(["succ" => false, "err" => '今日已玩過遊戲或者活動結束']);
  279. }
  280. }
  281. // 參數驗證
  282. $name = '';
  283. $messages = [
  284. 'name.required' => '姓名必填',
  285. 'name.max' => '姓名最多100字',
  286. ];
  287. $validate = Validator::make($request->all(), [
  288. 'name' => 'required|max:100',
  289. ], $messages);
  290. if ($validate->fails()) {
  291. $err = $validate->errors();
  292. $err_msg = "";
  293. foreach ($err->all() as $item) {
  294. $err_msg .= $item . "\n";
  295. }
  296. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  297. $this->sisLog['memoOut'] = json_encode(['msg' => $err_msg], JSON_UNESCAPED_UNICODE);
  298. return response()->json(["succ" => false, "err" => $err_msg]);
  299. } else {
  300. $name = $request->input('name', '');
  301. }
  302. // 業務邏輯(玩遊戲簽到)
  303. $res = $this->aSv->playGameAndCheckin($lineId, $name);
  304. // 資料整理
  305. $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
  306. $this->sisLog['memoOut'] = json_encode(['res' => $res], JSON_UNESCAPED_UNICODE);
  307. // 返回
  308. if (Cookie::get('gamecc')) {
  309. return response()->json(["succ" => true, "err" => '', "currentGameGp" => $res['currentGameGp'], "currentCheckinGp" => $res['currentCheckinGp'], "gpAll" => $res['gp']]);
  310. } else {
  311. return response()->json(["succ" => true, "err" => '', "currentGameGp" => $res['currentGameGp'], "currentCheckinGp" => $res['currentCheckinGp'], "gpAll" => $res['gp']])->cookie('gamecc', time(), 1440);
  312. }
  313. }
  314. public function redeem($lineId, $gid, Request $request)
  315. {
  316. $this->sisLog['func'] = __FUNCTION__;
  317. $this->sisLog['k'] = $lineId;
  318. $this->sisLog['memoIn'] = '';
  319. $this->sisLog['memoOut'] = '';
  320. // 打點數量限制
  321. // 作用範圍: ALL
  322. $redis = new Redis();
  323. $redis::connect('localhost', 6379);
  324. $rlimitkey = $this->settingManagementSv->getSetting()['GAME_ESI_TEST_EVENT_KEY'];
  325. if (!$redis::exists($rlimitkey)) {
  326. $redis::set($rlimitkey, 1);
  327. } else {
  328. $redis::incr($rlimitkey);
  329. }
  330. if ($redis::get($rlimitkey) > $this->settingManagementSv->getSetting()['GAME_ESI_TEST_EVENT_LIMIT']) {
  331. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  332. $this->sisLog['memoOut'] = json_encode(['msg' => '測試點數不能再打了'], JSON_UNESCAPED_UNICODE);
  333. return response()->json(["succ" => false, "err" => '測試點數不能再打了']);
  334. }
  335. // \Log::debug($rlimitkey);
  336. // \Log::debug($redis::get($rlimitkey));
  337. // return response()->json(["succ" => true, "err" => 'okok']);
  338. // 業務驗證(防重)
  339. // 作用範圍: 以人為單位
  340. $rkey = $this->settingManagementSv->getSetting()['GAME_ESI_TEST_EVENT_LIMIT'] . '_' . $lineId;
  341. if (!$redis::setnx($rkey, 'true')) {
  342. $redis::expire($rkey, 15); // 15 秒防重(這樣就算業務在其他地方掛了,他也會自動失效)
  343. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  344. $this->sisLog['memoOut'] = json_encode(['msg' => '您操作過快,請稍後再試'], JSON_UNESCAPED_UNICODE);
  345. return response()->json(["succ" => false, "err" => '您操作過快,請稍後再試']);
  346. }
  347. // 業務驗證(是否加入LINE帳號的好友)
  348. if (!$this->aSv->lineFriendCheck($lineId)) {
  349. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  350. $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
  351. return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
  352. }
  353. // 業務驗證(是否在本地有資料)
  354. if (!$this->aSv->isPlayer($lineId)) {
  355. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  356. $this->sisLog['memoOut'] = json_encode(['msg' => '請從首頁進入'], JSON_UNESCAPED_UNICODE);
  357. return response()->json(["succ" => false, "err" => '請從首頁進入']);
  358. }
  359. // 業務驗證(取得當回合的兌獎狀態,包含所有獎項以及可換與否,拿來比對驗證)
  360. $canuse = false;
  361. foreach ($this->aSv->goodinfo($lineId) as $g) if ($g['id'] == $gid) if ($g['canuse'] == true) $canuse = true;
  362. if (!$canuse) {
  363. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  364. $this->sisLog['memoOut'] = json_encode(['msg' => '獎項不存在或沒有兌獎所需吉點'], JSON_UNESCAPED_UNICODE);
  365. return response()->json(["succ" => false, "err" => '獎項不存在或沒有兌獎所需吉點']);
  366. }
  367. // 參數驗證
  368. $name = '';
  369. $messages = [
  370. 'name.required' => '姓名必填',
  371. 'name.max' => '姓名最多100字',
  372. ];
  373. $validate = Validator::make($request->all(), [
  374. 'name' => 'required|max:100',
  375. ], $messages);
  376. if ($validate->fails()) {
  377. $err = $validate->errors();
  378. $err_msg = "";
  379. foreach ($err->all() as $item) {
  380. $err_msg .= $item . "\n";
  381. }
  382. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  383. $this->sisLog['memoOut'] = json_encode(['msg' => $err_msg], JSON_UNESCAPED_UNICODE);
  384. return response()->json(["succ" => false, "err" => $err_msg]);
  385. } else {
  386. $name = $request->input('name', '');
  387. }
  388. // 業務邏輯(兌點)
  389. $redeeminfo = $this->aSv->redeemGood($lineId, $gid, $name);
  390. if (!$redeeminfo) {
  391. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  392. $this->sisLog['memoOut'] = json_encode(['msg' => '兌換失敗'], JSON_UNESCAPED_UNICODE);
  393. return response()->json(["succ" => false, "err" => '兌換失敗']);
  394. }
  395. // 資料整理
  396. $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId, 'gid' => $gid], JSON_UNESCAPED_UNICODE);
  397. $this->sisLog['memoOut'] = json_encode(['redeeminfo' => $redeeminfo], JSON_UNESCAPED_UNICODE);
  398. // 防重解鎖
  399. $redis::del($rkey);
  400. return response()->json(["succ" => true, "err" => '']);
  401. }
  402. public function test()
  403. {
  404. // if (Cookie::get('ttcc')) {
  405. // return response()->json(["succ" => true, "err" => '', "info" => 'I have cookie: ' . Cookie::get('ttcc')]);
  406. // } else {
  407. // return response()->json(["succ" => true, "err" => '', "info" => 'no cookie'])->cookie('ttcc', '123', 1440);
  408. // }
  409. return true;
  410. // return $this->aSv->esiFlowBak();
  411. //return $_SERVER['SERVER_ADDR'] ?? 'no';
  412. }
  413. // 類發票
  414. public function friendchk($lineId)
  415. {
  416. $this->sisLog['func'] = __FUNCTION__;
  417. $this->sisLog['k'] = $lineId;
  418. $this->sisLog['kk'] = 'activity';
  419. $this->sisLog['memoIn'] = '';
  420. $this->sisLog['memoOut'] = '';
  421. // 業務驗證(是否加入LINE帳號的好友)
  422. if (!$this->aSv->lineFriendCheck($lineId, 'activity')) {
  423. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  424. $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
  425. return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
  426. }
  427. // 資料整理
  428. $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
  429. $this->sisLog['memoOut'] = json_encode(['res' => ''], JSON_UNESCAPED_UNICODE);
  430. // 返回
  431. return response()->json(["succ" => true, "err" => '']);
  432. }
  433. public function activities($lineId)
  434. {
  435. $this->sisLog['func'] = __FUNCTION__;
  436. $this->sisLog['k'] = $lineId;
  437. $this->sisLog['kk'] = 'activity';
  438. $this->sisLog['memoIn'] = '';
  439. $this->sisLog['memoOut'] = '';
  440. // 業務驗證(是否加入LINE帳號的好友)
  441. if (!$this->aSv->lineFriendCheck($lineId, 'activity')) {
  442. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  443. $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
  444. return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
  445. }
  446. // 業務邏輯(取得活動時間資訊)
  447. $activities = $this->aSv->activities();
  448. // 資料整理
  449. $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
  450. $this->sisLog['memoOut'] = json_encode(['res' => $activities], JSON_UNESCAPED_UNICODE);
  451. // 返回
  452. return response()->json(["succ" => true, "err" => '', "data" => $activities]);
  453. }
  454. public function setdoc($lineId, Request $request)
  455. {
  456. $this->sisLog['func'] = __FUNCTION__;
  457. $this->sisLog['k'] = $lineId;
  458. $this->sisLog['kk'] = 'activity';
  459. $this->sisLog['memoIn'] = '';
  460. $this->sisLog['memoOut'] = '';
  461. // 業務驗證(是否加入LINE帳號的好友)
  462. // if (!$this->aSv->lineFriendCheck($lineId, 'activity')) {
  463. // $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  464. // $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
  465. //
  466. // return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
  467. // }
  468. // 參數驗證
  469. $messages = [
  470. 'doc.required' => '圖片必填',
  471. ];
  472. $validate = Validator::make($request->all(), [
  473. 'doc' => 'required',
  474. ], $messages);
  475. if ($validate->fails()) {
  476. $err = $validate->errors();
  477. $err_msg = "";
  478. foreach ($err->all() as $item) {
  479. $err_msg .= $item . "\n";
  480. }
  481. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  482. $this->sisLog['memoOut'] = json_encode(['msg' => $err_msg], JSON_UNESCAPED_UNICODE);
  483. return response()->json(["succ" => false, "err" => $err_msg]);
  484. } else {
  485. $doc = $request->input('doc', '');
  486. $doc = explode(',', $doc);
  487. $doc = base64_decode($doc[1]);
  488. }
  489. // 業務邏輯(圖片辨識)
  490. $data = $this->aSv->setdoc($doc);
  491. // 資料整理
  492. $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
  493. $this->sisLog['memoOut'] = json_encode(['res' => $data], JSON_UNESCAPED_UNICODE);
  494. // 返回
  495. return response()->json(["succ" => true, "err" => '', "data" => $data]);
  496. }
  497. public function reqreceipt($lineId, Request $request)
  498. {
  499. $this->sisLog['func'] = __FUNCTION__;
  500. $this->sisLog['k'] = $lineId;
  501. $this->sisLog['kk'] = 'activity';
  502. $this->sisLog['memoIn'] = '';
  503. $this->sisLog['memoOut'] = '';
  504. // 業務驗證(是否加入LINE帳號的好友)
  505. if (!$this->aSv->lineFriendCheck($lineId, 'activity')) {
  506. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  507. $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
  508. return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
  509. }
  510. // 參數驗證
  511. $messages = [
  512. 'name.required' => '姓名必填',
  513. 'name.max' => '姓名最多64字',
  514. 'order_date.required' => '登錄日期必填',
  515. 'order_date.date_format' => '登錄日期格式錯誤',
  516. 'account_bank.required' => '銀行代碼必填',
  517. 'account_bank.regex' => '銀行代碼格式錯誤',
  518. 'account_no.required' => '銀行帳號必填',
  519. 'account_no.regex' => '銀行帳號格式錯誤',
  520. 'order_no.required' => '訂單編號必填',
  521. 'order_no.regex' => '訂單編號格式錯誤',
  522. ];
  523. $validate = Validator::make($request->all(), [
  524. 'name' => 'required|max:64',
  525. 'order_date' => 'required|date_format:Y-m-d',
  526. 'account_bank' => ['required', 'regex:/^[0-9]{1,10}$/'],
  527. 'account_no' => ['required', 'regex:/^[0-9]{1,30}$/'],
  528. 'order_no' => ['required', 'regex:/^[A-Za-z0-9]{1,30}$/'],
  529. ], $messages);
  530. if ($validate->fails()) {
  531. $err = $validate->errors();
  532. $err_msg = "";
  533. foreach ($err->all() as $item) {
  534. $err_msg .= $item . "\n";
  535. }
  536. $this->sisLog['memoIn'] = json_encode(['error' => 'logical', 'param' => $_POST], JSON_UNESCAPED_UNICODE);
  537. $this->sisLog['memoOut'] = json_encode(['msg' => $err_msg], JSON_UNESCAPED_UNICODE);
  538. return response()->json(["succ" => false, "err" => $err_msg]);
  539. } else {
  540. $info = [
  541. 'name' => $request->input('name', ''),
  542. 'order_date' => $request->input('order_date', ''),
  543. 'account_bank' => $request->input('account_bank', ''),
  544. 'account_no' => $request->input('account_no', ''),
  545. 'order_no' => $request->input('order_no', ''),
  546. ];
  547. }
  548. // 業務驗證(取得當前的登錄區間)
  549. $activityinfo = $this->aSv->activityinfo();
  550. if (!$activityinfo) {
  551. // 尚未到達登錄時間
  552. $this->sisLog['memoIn'] = json_encode(['error' => 'logical', 'param' => $_POST], JSON_UNESCAPED_UNICODE);
  553. $this->sisLog['memoOut'] = json_encode(['msg' => '尚未到達登錄時間'], JSON_UNESCAPED_UNICODE);
  554. return response()->json(["succ" => false, "err" => '尚未到達登錄時間']);
  555. }
  556. // 業務驗證(收據時間也要在活動時間內)
  557. if ($activityinfo[0]['reqTimeBegin'] > $info['order_date'] || $activityinfo[0]['reqTimeFinal'] < $info['order_date']) {
  558. // 尚未到達登錄時間
  559. $this->sisLog['memoIn'] = json_encode(['error' => 'logical', 'param' => $_POST], JSON_UNESCAPED_UNICODE);
  560. $this->sisLog['memoOut'] = json_encode(['msg' => '收據日期錯誤'], JSON_UNESCAPED_UNICODE);
  561. return response()->json(["succ" => false, "err" => '收據日期錯誤']);
  562. }
  563. // 業務邏輯(登錄)
  564. $info['lineId'] = $lineId;
  565. $info['aid'] = $activityinfo[0]['id'];
  566. $id = $this->aSv->reqreceipt($info);
  567. if ($id == 0) {
  568. // 完全相同的資料不能登錄
  569. $this->sisLog['memoIn'] = json_encode(['error' => 'logical', 'param' => $_POST], JSON_UNESCAPED_UNICODE);
  570. $this->sisLog['memoOut'] = json_encode(['msg' => '您已登錄過這筆資料'], JSON_UNESCAPED_UNICODE);
  571. return response()->json(["succ" => false, "err" => '您已登錄過這筆資料']);
  572. }
  573. // 資料整理
  574. $this->sisLog['memoIn'] = json_encode(['info' => $info], JSON_UNESCAPED_UNICODE);
  575. $this->sisLog['memoOut'] = json_encode(['res' => $id], JSON_UNESCAPED_UNICODE);
  576. // 返回
  577. return response()->json(["succ" => true, "err" => '']);
  578. }
  579. public function lstreceipt($lineId)
  580. {
  581. $this->sisLog['func'] = __FUNCTION__;
  582. $this->sisLog['k'] = $lineId;
  583. $this->sisLog['kk'] = 'activity';
  584. $this->sisLog['memoIn'] = '';
  585. $this->sisLog['memoOut'] = '';
  586. // 業務驗證(是否加入LINE帳號的好友)
  587. if (!$this->aSv->lineFriendCheck($lineId, 'activity')) {
  588. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  589. $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
  590. return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
  591. }
  592. // 業務邏輯(取得列表)
  593. $data = $this->aSv->lstreceipt($lineId);
  594. // 資料整理
  595. $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
  596. $this->sisLog['memoOut'] = json_encode(['res' => $data], JSON_UNESCAPED_UNICODE);
  597. // 返回
  598. return response()->json(["succ" => true, "err" => '', "data" => $data]);
  599. }
  600. public function redeemreceipt($lineId, $rid, Request $request)
  601. {
  602. $this->sisLog['func'] = __FUNCTION__;
  603. $this->sisLog['k'] = $lineId;
  604. $this->sisLog['kk'] = 'activity';
  605. $this->sisLog['memoIn'] = '';
  606. $this->sisLog['memoOut'] = '';
  607. // 業務驗證(是否加入LINE帳號的好友)
  608. if (!$this->aSv->lineFriendCheck($lineId, 'activity')) {
  609. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  610. $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
  611. return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
  612. }
  613. // 業務驗證(該紀錄狀態是否可執行本動作/該紀錄的所屬活動是否在登錄的時間範圍內)
  614. if (!$this->aSv->redeemCheck($lineId, (integer)$rid)) {
  615. $this->sisLog['memoIn'] = json_encode(['error' => 'logical', 'rid' => $rid], JSON_UNESCAPED_UNICODE);
  616. $this->sisLog['memoOut'] = json_encode(['msg' => '權限錯誤'], JSON_UNESCAPED_UNICODE);
  617. return response()->json(["succ" => false, "err" => '權限錯誤']);
  618. }
  619. // 業務邏輯(更改狀態)
  620. $this->aSv->redeemExecute($lineId, (integer)$rid);
  621. // 資料整理
  622. $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
  623. $this->sisLog['memoOut'] = json_encode(['res' => ''], JSON_UNESCAPED_UNICODE);
  624. // 返回
  625. return response()->json(["succ" => true, "err" => '']);
  626. }
  627. public function getreceipt($lineId, $rid, Request $request)
  628. {
  629. $this->sisLog['func'] = __FUNCTION__;
  630. $this->sisLog['k'] = $lineId;
  631. $this->sisLog['kk'] = 'activity';
  632. $this->sisLog['memoIn'] = '';
  633. $this->sisLog['memoOut'] = '';
  634. // 業務驗證(防重)
  635. // 作用範圍: 以人為單位
  636. $redis = new Redis();
  637. $redis::connect('localhost', 6379);
  638. $rkey = $this->settingManagementSv->getSetting()['RECEIPT_ESI_TEST_EVENT_KEY'] . '_' . $lineId;
  639. if (!$redis::setnx($rkey, 'true')) {
  640. $redis::expire($rkey, 15); // 15 秒防重(這樣就算業務在其他地方掛了,他也會自動失效)
  641. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  642. $this->sisLog['memoOut'] = json_encode(['msg' => '您操作過快,請稍後再試'], JSON_UNESCAPED_UNICODE);
  643. return response()->json(["succ" => false, "err" => '您操作過快,請稍後再試']);
  644. }
  645. // 業務驗證(是否加入LINE帳號的好友)
  646. if (!$this->aSv->lineFriendCheck($lineId, 'activity')) {
  647. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  648. $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
  649. return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
  650. }
  651. // 業務驗證(該紀錄狀態是否可執行本動作/該紀錄的所屬活動是否在登錄的時間範圍內)
  652. if (!$this->aSv->getCheck($lineId, (integer)$rid)) {
  653. $this->sisLog['memoIn'] = json_encode(['error' => 'logical', 'rid' => $rid], JSON_UNESCAPED_UNICODE);
  654. $this->sisLog['memoOut'] = json_encode(['msg' => '權限錯誤'], JSON_UNESCAPED_UNICODE);
  655. return response()->json(["succ" => false, "err" => '權限錯誤']);
  656. }
  657. // 業務邏輯(更改狀態)
  658. $getinfo = $this->aSv->getExecute($lineId, (integer)$rid);
  659. if (!$getinfo) {
  660. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  661. $this->sisLog['memoOut'] = json_encode(['msg' => '兌換失敗'], JSON_UNESCAPED_UNICODE);
  662. return response()->json(["succ" => false, "err" => '兌換失敗']);
  663. }
  664. // 資料整理
  665. $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
  666. $this->sisLog['memoOut'] = json_encode(['res' => ''], JSON_UNESCAPED_UNICODE);
  667. // 防重解鎖
  668. $redis::del($rkey);
  669. // 返回
  670. return response()->json(["succ" => true, "err" => '']);
  671. }
  672. // 串門子
  673. public function tinfo($lineId, Request $request)
  674. {
  675. $this->sisLog['func'] = __FUNCTION__;
  676. $this->sisLog['k'] = $lineId;
  677. $this->sisLog['kk'] = 'tmz';
  678. $this->sisLog['memoIn'] = '';
  679. $this->sisLog['memoOut'] = '';
  680. // 業務驗證(是否加入LINE帳號的好友) [不存在 cookie 的時候就要進直通查詢,造成每天只要最多一次查詢就好]
  681. if (!Cookie::get('tgamecc')) {
  682. if (!$this->aSv->lineFriendCheck($lineId, 'tmz')) {
  683. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  684. $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
  685. return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
  686. }
  687. }
  688. // 業務邏輯(檢查有無本地資料,沒有就新增,順便取得個人資訊包含吉點)
  689. $playerinfo = $this->aSv->tplayerinfo($lineId);
  690. // 參數驗證
  691. $name = '';
  692. $messages = [
  693. 'name.required' => '姓名必填',
  694. 'name.max' => '姓名最多100字',
  695. ];
  696. $validate = Validator::make($request->all(), [
  697. 'name' => 'required|max:100',
  698. ], $messages);
  699. if ($validate->fails()) {
  700. $err = $validate->errors();
  701. $err_msg = "";
  702. foreach ($err->all() as $item) {
  703. $err_msg .= $item . "\n";
  704. }
  705. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  706. $this->sisLog['memoOut'] = json_encode(['msg' => $err_msg], JSON_UNESCAPED_UNICODE);
  707. return response()->json(["succ" => false, "err" => $err_msg]);
  708. } else {
  709. $name = $request->input('name', '');
  710. }
  711. // 業務邏輯(今日簽到)
  712. $this->aSv->tcheckin($lineId, $name);
  713. // 業務邏輯(取得當天所屬於的回合日期資訊,包含回合起訖日、本回合簽到狀態)
  714. $roundinfo = $this->aSv->troundinfo($lineId);
  715. if (!$roundinfo) {
  716. // 遊戲時間已過 && 兌獎時間已過
  717. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  718. $this->sisLog['memoOut'] = json_encode(['msg' => '活動已結束'], JSON_UNESCAPED_UNICODE);
  719. return response()->json(["succ" => false, "err" => '活動已結束']);
  720. } else {
  721. // 要嘛都在期限內,要嘛一定至少兌獎在期限內因為他的時間範圍較大,這時候就要把回合簽到資訊清空
  722. if (!$roundinfo) $roundinfo = [];
  723. }
  724. // 業務邏輯(取得是否可以轉蛋的資格)
  725. $redeeminfo = $this->aSv->tredeeminfo($lineId);
  726. // 資料整理
  727. $info = [
  728. 'playerinfo' => $playerinfo,
  729. 'roundinfo' => $roundinfo,
  730. 'redeeminfo' => $redeeminfo,
  731. ];
  732. $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
  733. $this->sisLog['memoOut'] = json_encode(['info' => $info], JSON_UNESCAPED_UNICODE);
  734. // 返回
  735. if (Cookie::get('tgamecc')) {
  736. return response()->json(["succ" => true, "err" => '', "info" => $info]);
  737. } else {
  738. return response()->json(["succ" => true, "err" => '', "info" => $info])->cookie('tgamecc', time(), 1440);
  739. }
  740. }
  741. public function tgame($lineId, Request $request)
  742. {
  743. $this->sisLog['func'] = __FUNCTION__;
  744. $this->sisLog['k'] = $lineId;
  745. $this->sisLog['kk'] = 'tmz';
  746. $this->sisLog['memoIn'] = '';
  747. $this->sisLog['memoOut'] = '';
  748. // 業務驗證(是否加入LINE帳號的好友) [不存在 cookie 的時候就要進直通查詢,造成每天只要最多一次查詢就好]
  749. if (!Cookie::get('tgamecc')) {
  750. if (!$this->aSv->lineFriendCheck($lineId, 'tmz')) {
  751. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  752. $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
  753. return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
  754. }
  755. }
  756. // 業務驗證(是否在本地有資料)
  757. if (!$this->aSv->isTPlayer($lineId)) {
  758. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  759. $this->sisLog['memoOut'] = json_encode(['msg' => '請從首頁進入'], JSON_UNESCAPED_UNICODE);
  760. return response()->json(["succ" => false, "err" => '請從首頁進入']);
  761. }
  762. // 參數驗證
  763. $name = '';
  764. $c = 0;
  765. $messages = [
  766. 'name.required' => '姓名必填',
  767. 'name.max' => '姓名最多100字',
  768. 'c.required' => '九宮格必填',
  769. ];
  770. $validate = Validator::make($request->all(), [
  771. 'name' => 'required|max:100',
  772. 'c' => 'required|numeric|min:1|max:9',
  773. ], $messages);
  774. if ($validate->fails()) {
  775. $err = $validate->errors();
  776. $err_msg = "";
  777. foreach ($err->all() as $item) {
  778. $err_msg .= $item . "\n";
  779. }
  780. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  781. $this->sisLog['memoOut'] = json_encode(['msg' => $err_msg], JSON_UNESCAPED_UNICODE);
  782. return response()->json(["succ" => false, "err" => $err_msg]);
  783. } else {
  784. $name = $request->input('name', '');
  785. $c = $request->input('c', 0);
  786. }
  787. // 業務邏輯(玩遊戲)
  788. $res = $this->aSv->playTGame($lineId, $name, $c);
  789. if (!$res) {
  790. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  791. $this->sisLog['memoOut'] = json_encode(['msg' => '已回答過囉'], JSON_UNESCAPED_UNICODE);
  792. return response()->json(["succ" => false, "err" => '已回答過囉']);
  793. }
  794. // 資料整理
  795. $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId, 'c' => $c], JSON_UNESCAPED_UNICODE);
  796. $this->sisLog['memoOut'] = json_encode(['res' => $res], JSON_UNESCAPED_UNICODE);
  797. // 返回
  798. if (Cookie::get('tgamecc')) {
  799. return response()->json(["succ" => true, "err" => '', "currentGameGp" => $res['currentGameGp'], "playerinfo" => $res['gp']]);
  800. } else {
  801. return response()->json(["succ" => true, "err" => '', "currentGameGp" => $res['currentGameGp'], "playerinfo" => $res['gp']])->cookie('tgamecc', time(), 1440);
  802. }
  803. }
  804. public function tredeem($lineId, Request $request)
  805. {
  806. $this->sisLog['func'] = __FUNCTION__;
  807. $this->sisLog['k'] = $lineId;
  808. $this->sisLog['kk'] = 'tmz';
  809. $this->sisLog['memoIn'] = '';
  810. $this->sisLog['memoOut'] = '';
  811. // 打點數量限制
  812. // 作用範圍: ALL
  813. $redis = new Redis();
  814. $redis::connect('localhost', 6379);
  815. $rlimitkey = $this->settingManagementSv->getSetting()['TMZ_ESI_TEST_EVENT_KEY'];
  816. if (!$redis::exists($rlimitkey)) {
  817. $redis::set($rlimitkey, 1);
  818. } else {
  819. $redis::incr($rlimitkey);
  820. }
  821. if ($redis::get($rlimitkey) > $this->settingManagementSv->getSetting()['TMZ_ESI_TEST_EVENT_LIMIT']) {
  822. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  823. $this->sisLog['memoOut'] = json_encode(['msg' => '測試點數不能再打了'], JSON_UNESCAPED_UNICODE);
  824. return response()->json(["succ" => false, "err" => '測試點數不能再打了']);
  825. }
  826. // 業務驗證(防重)
  827. // 作用範圍: 以人為單位
  828. $rkey = $this->settingManagementSv->getSetting()['TMZ_ESI_TEST_EVENT_LIMIT'] . '_' . $lineId;
  829. if (!$redis::setnx($rkey, 'true')) {
  830. $redis::expire($rkey, 15); // 15 秒防重(這樣就算業務在其他地方掛了,他也會自動失效)
  831. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  832. $this->sisLog['memoOut'] = json_encode(['msg' => '您操作過快,請稍後再試'], JSON_UNESCAPED_UNICODE);
  833. return response()->json(["succ" => false, "err" => '您操作過快,請稍後再試']);
  834. }
  835. // 業務驗證(是否加入LINE帳號的好友)
  836. if (!$this->aSv->lineFriendCheck($lineId, 'tmz')) {
  837. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  838. $this->sisLog['memoOut'] = json_encode(['msg' => '請先加入 LINE 好友'], JSON_UNESCAPED_UNICODE);
  839. return response()->json(["succ" => false, "err" => '請先加入 LINE 好友']);
  840. }
  841. // 業務驗證(是否在本地有資料)
  842. if (!$this->aSv->isTPlayer($lineId)) {
  843. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  844. $this->sisLog['memoOut'] = json_encode(['msg' => '請從首頁進入'], JSON_UNESCAPED_UNICODE);
  845. return response()->json(["succ" => false, "err" => '請從首頁進入']);
  846. }
  847. // 參數驗證
  848. $name = '';
  849. $messages = [
  850. 'name.required' => '姓名必填',
  851. 'name.max' => '姓名最多100字',
  852. ];
  853. $validate = Validator::make($request->all(), [
  854. 'name' => 'required|max:100',
  855. ], $messages);
  856. if ($validate->fails()) {
  857. $err = $validate->errors();
  858. $err_msg = "";
  859. foreach ($err->all() as $item) {
  860. $err_msg .= $item . "\n";
  861. }
  862. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  863. $this->sisLog['memoOut'] = json_encode(['msg' => $err_msg], JSON_UNESCAPED_UNICODE);
  864. return response()->json(["succ" => false, "err" => $err_msg]);
  865. } else {
  866. $name = $request->input('name', '');
  867. }
  868. // 業務邏輯(可否轉蛋再次確認)
  869. if (!$this->aSv->tredeeminfo($lineId)) {
  870. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  871. $this->sisLog['memoOut'] = json_encode(['msg' => '吉點不夠不能轉蛋'], JSON_UNESCAPED_UNICODE);
  872. return response()->json(["succ" => false, "err" => '吉點不夠不能轉蛋']);
  873. }
  874. // 業務邏輯(轉蛋兌點)
  875. $redeeminfo = $this->aSv->redeemTGood($lineId, $name);
  876. if (!$redeeminfo) {
  877. $this->sisLog['memoIn'] = json_encode(['error' => 'logical'], JSON_UNESCAPED_UNICODE);
  878. $this->sisLog['memoOut'] = json_encode(['msg' => '銘謝惠顧'], JSON_UNESCAPED_UNICODE);
  879. return response()->json(["succ" => false, "err" => '銘謝惠顧']);
  880. }
  881. // 資料整理
  882. $this->sisLog['memoIn'] = json_encode(['lineId' => $lineId], JSON_UNESCAPED_UNICODE);
  883. $this->sisLog['memoOut'] = json_encode(['redeeminfo' => $redeeminfo], JSON_UNESCAPED_UNICODE);
  884. unset($redeeminfo['playerinfo']['lineId']);
  885. unset($redeeminfo['redeeminfo']['fromIssuedQty']);
  886. unset($redeeminfo['redeeminfo']['toIssuedQty']);
  887. unset($redeeminfo['session']);
  888. // 防重解鎖
  889. $redis::del($rkey);
  890. return response()->json(["succ" => true, "err" => '', 'info' => $redeeminfo]);
  891. }
  892. }