Api2021Controller.php 47KB

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