Api2021Service.php 121KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002
  1. <?php
  2. namespace App\Http\Services\Web;
  3. use Exception;
  4. use App\Models\Web\Event;
  5. use App\Models\Web\Item;
  6. use App\Models\Web\Submit;
  7. use Google\Cloud\Vision\V1\Feature\Type;
  8. use Google\Cloud\Vision\V1\ImageAnnotatorClient;
  9. use GuzzleHttp\Client;
  10. use App\Http\Services\ConstDef\GeneralConst;
  11. use Illuminate\Support\Facades\DB;
  12. use Dompdf\Dompdf;
  13. use Dompdf\Options;
  14. use App\Models\Web\Syslog;
  15. use App\Models\Web\Syslogact;
  16. use App\Models\Web\Syslogt;
  17. use App\Models\Web\Player;
  18. use App\Models\Web\Round;
  19. use App\Models\Web\Good;
  20. use App\Models\Web\Session;
  21. use App\Models\Web\CheckinGpAlloc;
  22. use App\Models\Web\GameGpRatio;
  23. use App\Models\Web\Activity;
  24. use App\Models\Web\Receipt;
  25. use App\Models\Web\TPlayer;
  26. use App\Models\Web\TRound;
  27. use App\Models\Web\TGood;
  28. use App\Models\Web\TSession;
  29. use App\Models\Web\TCheckinGpAlloc;
  30. use App\Models\Web\AuthLineUsers;
  31. use App\Http\Services\Backend\DataManagement\SettingManagementService;
  32. class Api2021Service
  33. {
  34. private $c;
  35. private $eventManagementDb;
  36. private $itemManagementDb;
  37. private $submitManagementDb;
  38. private $syslogManagementDb;
  39. private $syslogactManagementDb;
  40. private $syslogtManagementDb;
  41. private $playerManagementDb;
  42. private $roundManagementDb;
  43. private $goodManagementDb;
  44. private $sessionManagementDb;
  45. private $checkinManagementDb;
  46. private $gameManagementDb;
  47. private $activityManagementDb;
  48. private $receiptManagementDb;
  49. private $tplayerManagementDb;
  50. private $troundManagementDb;
  51. private $tgoodManagementDb;
  52. private $tsessionManagementDb;
  53. private $tcheckinManagementDb;
  54. private $authLineUsersDb;
  55. private $settingManagementSv;
  56. private $imgC;
  57. private $basedir;
  58. public function __construct()
  59. {
  60. date_default_timezone_set("Asia/Taipei");
  61. $this->c = new Client();
  62. // 建構 model 調用器
  63. $this->eventManagementDb = new Event();
  64. $this->itemManagementDb = new Item();
  65. $this->submitManagementDb = new Submit();
  66. $this->syslogManagementDb = new Syslog();
  67. $this->syslogactManagementDb = new Syslogact();
  68. $this->syslogtManagementDb = new Syslogt();
  69. $this->playerManagementDb = new Player();
  70. $this->roundManagementDb = new Round();
  71. $this->goodManagementDb = new Good();
  72. $this->sessionManagementDb = new Session();
  73. $this->checkinManagementDb = new CheckinGpAlloc();
  74. $this->gameManagementDb = new GameGpRatio();
  75. $this->activityManagementDb = new Activity();
  76. $this->receiptManagementDb = new Receipt();
  77. $this->tplayerManagementDb = new TPlayer();
  78. $this->troundManagementDb = new TRound();
  79. $this->tgoodManagementDb = new TGood();
  80. $this->tcheckinManagementDb = new TCheckinGpAlloc();
  81. $this->authLineUsersDb = new AuthLineUsers();
  82. $this->settingManagementSv = new SettingManagementService();
  83. $this->settingManagementSv = new SettingManagementService();
  84. $this->basedir = preg_replace('/\/app\/.*/', '/public/', __DIR__);
  85. $this->imgC = new ImageAnnotatorClient([
  86. 'credentials' => $this->basedir . 'private/google.json',
  87. ]);
  88. }
  89. // 申報系統(已廢棄)
  90. public function hashParse($hash)
  91. {
  92. $hash = \DB::select("SELECT FROM_BASE64(AES_DECRYPT(UNHEX(\"" . $hash . "\"), \"" . env('KK') . "\")) as str;");
  93. return explode(',', $hash[0]->str);
  94. }
  95. public function hashCheck($hash)
  96. {
  97. // 格式判斷
  98. if (count($hash) != 2) return false;
  99. // 業務邏輯判斷
  100. $items = $this->eventManagementDb
  101. ->leftJoin('items', 'events.id', '=', 'items.eid')
  102. ->select([
  103. 'events.archive',
  104. ])
  105. ->where('events.id', $hash[0])
  106. ->where('items.id', $hash[1])
  107. ->where('events.date_begin', '<=', date("Y-m-d"))
  108. ->where('events.date_final', '>=', date("Y-m-d"))
  109. ->get()
  110. ->toArray();
  111. if (count($items) == 0) return false;
  112. return ($items[0]['archive'] == GeneralConst::ARCHIVE_NO) ? true : false;
  113. }
  114. public function getinfo($hash)
  115. {
  116. // 業務邏輯判斷
  117. $info = $this->eventManagementDb
  118. ->leftJoin('items', 'events.id', '=', 'items.eid')
  119. ->select([
  120. 'events.name',
  121. 'events.kv',
  122. 'events.email',
  123. 'events.tel',
  124. \DB::raw("items.name as item_name"),
  125. 'items.is_ide',
  126. 'items.id_acc',
  127. ])
  128. ->where('events.id', $hash[0])
  129. ->where('items.id', $hash[1])
  130. ->where('events.date_begin', '<=', date("Y-m-d"))
  131. ->where('events.date_final', '>=', date("Y-m-d"))
  132. ->first()
  133. ->toArray();
  134. return $info;
  135. }
  136. public function makePDF($info, $name, $identity, $add_host, $add_contact, $tel, $ida, $idb, $acc)
  137. {
  138. // 初期設定
  139. $options = new Options();
  140. $options->set('isRemoteEnabled', true);
  141. $options->set('chroot', '/');
  142. $dompdf = new Dompdf();
  143. $dompdf->setOptions($options);
  144. // 內容拼湊
  145. $html =
  146. '<!doctype html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><style> .font-zh { font-family: "msjh" } </style></head><body>' .
  147. '<span class="font-zh">申報資料</span><hr>' .
  148. '<span class="font-zh">姓名: ' . $name . '</span><br>' .
  149. '<span class="font-zh">身分證字號: ' . $identity . '</span><br>' .
  150. '<span class="font-zh">戶籍地址: ' . $add_host . '</span><br>' .
  151. '<span class="font-zh">通訊地址: ' . $add_contact . '</span><br>' .
  152. '<span class="font-zh">電話: ' . $tel . '</span><br>' .
  153. '<span class="font-zh"><br>' .
  154. '<span class="font-zh">檢附文件:</span><hr>' .
  155. '[is_ide]' .
  156. '[id_acc]' .
  157. '</body></html>';
  158. if ($info['is_ide'] == GeneralConst::PHOTO_YES) {
  159. $html = str_replace('[is_ide]',
  160. '<div class="page_break"><span class="font-zh">身分證正面</span><br>' .
  161. '<img src="' . env('LOCAL_PATH') . 'public/' . $ida . '" /></div>' .
  162. '<div class="page_break"><span class="font-zh">身分證背面</span><br>' .
  163. '<img src="' . env('LOCAL_PATH') . 'public/' . $idb . '" /></div>'
  164. , $html);
  165. } else {
  166. $html = str_replace('[is_ide]', '', $html);
  167. }
  168. if ($info['id_acc'] == GeneralConst::PHOTO_YES) {
  169. $html = str_replace('[id_acc]',
  170. '<div class="page_break"><span class="font-zh">存摺</span><br>' .
  171. '<img src="' . env('LOCAL_PATH') . 'public/' . $acc . '" /></div>'
  172. , $html);
  173. } else {
  174. $html = str_replace('[id_acc]', '', $html);
  175. }
  176. $dompdf->setPaper('A4');
  177. $dompdf->loadHtml($html, 'UTF-8');
  178. $dompdf->render();
  179. $output = $dompdf->output();
  180. // 路徑命名
  181. $dirname = 'material';
  182. $date = date("Ymd");
  183. if (!file_exists($dirname)) mkdir($dirname, 0777, true);
  184. if (!file_exists($dirname . '/' . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['web_path'])) mkdir($dirname . '/' . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['web_path'], 0777, true);
  185. if (!file_exists($dirname . '/' . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['web_path'] . '/' . $date)) mkdir($dirname . '/' . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['web_path'] . '/' . $date, 0777, true);
  186. $basepath = $dirname . '/' . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['web_path'] . '/' . $date . '/';
  187. $time = md5(bcrypt(microtime()));
  188. file_put_contents($basepath . $time . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['ext'], $output);
  189. return $basepath . $time . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['ext'];
  190. }
  191. public function savedata($iid, $name, $identity, $add_host, $add_contact, $tel, $ida, $idb, $acc, $pdf)
  192. {
  193. // 取得參數
  194. // 調用資料庫(或者其他業務邏輯)
  195. $this->submitManagementDb
  196. ->insert([
  197. 'iid' => $iid,
  198. 'name' => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $name . "'), \"" . env('KK') . "\"))"),
  199. 'identity' => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $identity . "'), \"" . env('KK') . "\"))"),
  200. 'add_host' => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $add_host . "'), \"" . env('KK') . "\"))"),
  201. 'add_contact' => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $add_contact . "'), \"" . env('KK') . "\"))"),
  202. 'tel' => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $tel . "'), \"" . env('KK') . "\"))"),
  203. GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_FRONT ]['sql'] => $ida,
  204. GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_BACK ]['sql'] => $idb,
  205. GeneralConst::$appendixMap[ GeneralConst::APPENDIX_PASSBOOK ]['sql'] => $acc,
  206. GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['sql'] => $pdf,
  207. 'cdate' => date('Y-m-d H:i:s'),
  208. ]);
  209. $id = \DB::getPdo()->lastInsertId();
  210. // 整理返回值並返回
  211. return $id;
  212. }
  213. // 小遊戲
  214. // LINE 好友判斷 (實作介接直通部分)
  215. public function lineFriendCheck($lineId, $from = null)
  216. {
  217. try {
  218. // 本次調用的參數整理
  219. $uid = $lineId;
  220. $rqid = $this->getRqId();
  221. // 取得 seedKey
  222. $seedKey = $this->getSeedKey();
  223. // 隨機產生多樣性資料
  224. $divData = $this->getDivData();
  225. // 產生本次調用的 sessionKey
  226. $sessionKey = $this->getSessionKey($seedKey, $divData);
  227. // 整理押碼用的資料欄內容
  228. $msgRaw = $this->getMsgRaw([
  229. 'RqClient' => env('RQ_CLIENT'),
  230. 'RqId' => $rqid,
  231. 'UID' => $uid,
  232. ]);
  233. $msgHash = $this->getMsgHash($msgRaw);
  234. // 建立ICV
  235. $icv = $this->getICV();
  236. // 建立押碼
  237. $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
  238. $mac = $this->getMAC($macRaw);
  239. // 參數打包
  240. $param = [
  241. 'RqClient' => env('RQ_CLIENT'),
  242. 'RqId' => $rqid,
  243. 'ICV' => $icv,
  244. 'DivData' => $divData,
  245. 'MAC' => $mac,
  246. 'UID' => $uid,
  247. ];
  248. // 相關中繼資料打包(LOG 用)
  249. $logdata = [
  250. 'sessionKey' => $sessionKey,
  251. 'msgRaw' => $msgRaw,
  252. 'msgHash' => $msgHash,
  253. 'macRaw' => $macRaw,
  254. ];
  255. // 憑證參數整理
  256. $curlparam = [
  257. CURLOPT_SSLCERTTYPE => 'PEM',
  258. CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
  259. CURLOPT_SSLKEYTYPE => 'PEM',
  260. CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
  261. CURLOPT_SSL_VERIFYHOST => 0,
  262. CURLOPT_SSL_VERIFYPEER => 0,
  263. ];
  264. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
  265. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
  266. // 調用
  267. $req = [
  268. 'curl' => $curlparam,
  269. 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
  270. 'headers' => [
  271. 'Content-Type' => 'application/json',
  272. 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
  273. 'Accept' => '*/*',
  274. ],
  275. 'timeout' => 15, // Response timeout
  276. 'connect_timeout' => 10, // Connection timeout
  277. ];
  278. if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197'])) $req['proxy'] = env('CCH_PROXY');
  279. $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/contact/QueryQualification', $req);
  280. $res = $response->getBody();
  281. // 直通紀錄
  282. $data = [
  283. 'type' => GeneralConst::LOG_ESI,
  284. 'func' => 'QueryQualification',
  285. 'k' => $uid,
  286. 'cdate' => date("Y-m-d H:i:s"),
  287. ];
  288. if ($from) $data['kk'] = $from; // 區分來源(紀錄的 log 表不同)
  289. // 經由計算取得的完整 request JSON
  290. $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
  291. // 調用 API 取得返回
  292. $data['memoOut'] = $res;
  293. // 紀錄
  294. $this->syslog($data);
  295. // 返回值判斷
  296. $res = json_decode($res);
  297. $rcode = $res->RCode ?? '';
  298. if ($rcode != '0000') return false;
  299. $isfollow = $res->Result->IsFollow ?? false;
  300. $ismember = $res->Result->IsTWPMember ?? false;
  301. if (!$isfollow || !$ismember) return false;
  302. } catch (Exception $ex) {
  303. // 錯誤可能發生在 memoIn 或是 out
  304. $data['memoIn'] = json_encode(['error' => 'exception', 'param' => array_merge($param, $logdata)], JSON_UNESCAPED_UNICODE);
  305. $data['memoOut'] = json_encode(['code' => $ex->getCode(), 'msg' => $ex->getMessage()], JSON_UNESCAPED_UNICODE);
  306. // $this->syslog($data);
  307. $this->debuglog('lineFriendCheck', [
  308. 'rqid' => $rqid ?? 0
  309. ], [
  310. 'errCode' => $ex->getCode(),
  311. 'errMsg' => $ex->getMessage(),
  312. ]);
  313. return false;
  314. }
  315. return true;
  316. }
  317. public function isAuthUserExists($lineUserId)
  318. {
  319. return $this->authLineUsersDb->where('lineUserId', $lineUserId)->exists();
  320. }
  321. public function addAuthUser($lineUserId)
  322. {
  323. $this->authLineUsersDb->insert([
  324. 'lineUserId' => $lineUserId
  325. ]);
  326. }
  327. // LINE 取得個資(建立在好友判斷後才可以執行)
  328. public function lineProfile($lineId)
  329. {
  330. $response = $this->c->request('GET', env('LINE_API_BASE') . 'bot/profile/' . $lineId, [
  331. 'headers' => [
  332. 'Content-Type' => 'application/json; charset=utf-8',
  333. 'Accept' => '*/*',
  334. 'Authorization' => 'Bearer ' . env('LINE_CHANNEL_TOKEN'),
  335. 'Accept-Encoding' => 'gzip, deflate, br',
  336. ]
  337. ]);
  338. $profile = json_decode($response->getBody());
  339. return $profile;
  340. }
  341. // 新增或取得用戶資訊
  342. public function playerinfo($lineId)
  343. {
  344. // 取得用戶在本地的資訊
  345. $playerinfo = $this->playerManagementDb
  346. ->select(['id'])
  347. ->where('lineId', $lineId)
  348. ->get()
  349. ->toArray();
  350. // 取得用戶在 LINE 上的 profile
  351. // $profile = $this->lineProfile($lineId);
  352. if (count($playerinfo) == 0) {
  353. // 用戶不在本地資料庫,新增
  354. // 寫入資料表
  355. $d = [
  356. 'lineId' => $lineId,
  357. 'userName' => '',
  358. 'userPhoto' => '',
  359. 'gp' => 0,
  360. 'cdate' => date("Y-m-d H:i:s"),
  361. 'mdate' => date("Y-m-d H:i:s"),
  362. ];
  363. $this->playerManagementDb
  364. ->insert($d);
  365. $id = \DB::getPdo()->lastInsertId();
  366. // 替用互打一次性標籤
  367. $this->playerTag($lineId);
  368. } else {
  369. // 用戶的 LINE 資訊可能會更新,所以這個 API 還身兼更新資訊的功能
  370. // $d = [
  371. // 'userName' => $profile->displayName,
  372. // 'userPhoto' => $profile->pictureUrl,
  373. // ];
  374. // $this->playerManagementDb
  375. // ->where('lineId', $lineId)
  376. // ->update($d);
  377. }
  378. // 再重新查一遍
  379. $playerinfo = $this->playerManagementDb
  380. ->select([
  381. // 'id',
  382. 'lineId',
  383. // 'userName',
  384. // 'userPhoto',
  385. 'gp',
  386. // 'cdate',
  387. // 'mdate',
  388. ])
  389. ->where('lineId', $lineId)
  390. ->first()
  391. ->toArray();
  392. return $playerinfo;
  393. }
  394. // 檢查是否在本地有資料
  395. public function isPlayer($lineId)
  396. {
  397. $playerinfo = $this->playerManagementDb->select(['id'])->where('lineId', $lineId)->get()->toArray();
  398. return (count($playerinfo) > 0);
  399. }
  400. // 取得回合資訊
  401. public function roundinfo($lineId)
  402. {
  403. // 回合資訊
  404. $roundinfo = $this->roundManagementDb
  405. ->select(['id', 'dateBegin', 'dateFinal'])
  406. ->where('dateFinal', '>=', date("Y-m-d"))
  407. ->where('dateBegin', '<=', date("Y-m-d"))
  408. ->where('active', '=', GeneralConst::ACTIVE_YES)
  409. ->get();
  410. if (count($roundinfo) == 0) {
  411. return false;
  412. } else {
  413. $roundinfo = $roundinfo->toArray();
  414. }
  415. // 簽到資訊
  416. $checkininfo = $this->sessionManagementDb
  417. ->select(['eventDate', 'currentGameGp'])
  418. ->where('lineId', $lineId)
  419. ->where('rid', $roundinfo[0]['id'])
  420. ->where('gid', '<>', 0) // 僅篩選簽到或遊戲
  421. ->orderby('id', 'asc') // 確保按照時間排
  422. ->get()
  423. ->toArray();
  424. // 整理
  425. $res = [
  426. 'dateBegin' => $roundinfo[0]['dateBegin'],
  427. 'dateFinal' => $roundinfo[0]['dateFinal'],
  428. 'today' => date("Y-m-d"),
  429. 'checkins' => [],
  430. ];
  431. foreach ($checkininfo as $c) {
  432. // 相同天可能會有重新再玩,因此會有超過1筆資料的可能,不想使用 SQL group by 耗能
  433. $res['checkins'][ $c['eventDate'] ] = [
  434. 'date' => $c['eventDate'],
  435. 'canplay' => ($c['eventDate'] != date("Y-m-d")) ? false : ($c['currentGameGp'] < 0) ? true : false,
  436. ];
  437. }
  438. $res['checkins'] = array_values($res['checkins']);
  439. return $res;
  440. }
  441. // 取得獎項兌換資訊
  442. public function goodinfo($lineId)
  443. {
  444. // 獎項資訊
  445. $goodinfo = $this->goodManagementDb
  446. ->select(['id', 'lp', 'gp', 'active', 'totalQty', 'issuedQty'])
  447. ->get()
  448. ->toArray();
  449. // 一定有獎項所以不防呆
  450. // 回合資訊 (注意用於兌獎資訊的日期區間判定與玩遊戲不同)
  451. $roundinfo = $this->roundManagementDb
  452. ->select(['id'])
  453. ->where(\DB::raw("DATE_ADD(dateFinal, INTERVAL +redeemExtra DAY)"), '>=', date("Y-m-d"))
  454. ->where('dateBegin', '<=', date("Y-m-d"))
  455. ->where('active', '=', GeneralConst::ACTIVE_YES)
  456. ->get();
  457. // 若由 INFO 調用,則會先調用回合資訊確保來到這邊時一定拿到數值
  458. // 若由 REDEEM 調用,由於回合的日期限制比兌換的日期限制來的窄,不能優先於函數執行
  459. // 因此這邊有可能拿不到值,需要防呆
  460. if (count($roundinfo) == 0) {
  461. return false;
  462. } else {
  463. $roundinfo = $roundinfo->toArray();
  464. }
  465. // player 準備資料
  466. $playerinfo = $this->playerManagementDb->select(['gp'])->where('lineId', $lineId)->get();
  467. if (count($playerinfo) == 0) {
  468. return false;
  469. } else {
  470. $playerinfo = $playerinfo->toArray();
  471. }
  472. $mygp = $playerinfo[0]['gp'];
  473. // 兌獎資訊
  474. $redeeminfo = $this->sessionManagementDb
  475. ->select(['cid']) // 獎項 ID
  476. ->where('lineId', $lineId)
  477. ->where('rid', $roundinfo[0]['id'])
  478. ->where('cid', '<>', 0) // 僅篩選兌獎
  479. ->get()
  480. ->toArray();
  481. $redeemIds = [];
  482. foreach ($redeeminfo as $r) $redeemIds[] = $r['cid'];
  483. // 整理
  484. $res = [];
  485. foreach ($goodinfo as $g) {
  486. // 稽核用,如果已核發數量 >= 可發數量,就不顯示
  487. if ($g['issuedQty'] >= $g['totalQty'] && $g['totalQty'] > 0) {
  488. continue;
  489. }
  490. // 可用性限制
  491. $issoldout = false;
  492. $isredeemed = false;
  493. $isnomoney = false;
  494. $canuse = false;
  495. if ($g['totalQty'] - $g['issuedQty'] <= $this->settingManagementSv->getSetting()['GAME_ISSUE_BUFFER']) $issoldout = true;
  496. if (in_array($g['id'], $redeemIds)) $isredeemed = true;
  497. if ($mygp < $g['gp']) $isnomoney = true;
  498. if (
  499. ($g['totalQty'] - $g['issuedQty'] > $this->settingManagementSv->getSetting()['GAME_ISSUE_BUFFER']) // 核發數量限制還夠
  500. && (!in_array($g['id'], $redeemIds)) // 本回合沒有兌換過
  501. && ($mygp >= $g['gp']) // 自己的吉點足夠
  502. ) {
  503. $canuse = true;
  504. }
  505. // 最後一道防線就是新添加的 active 欄位(一般來說如果其他三個偵測有任何一個是 true 的話,canuse 就是 false ;但是當其他三個偵測都是 false 的情況如果 canuse 也是 false 就是該獎項被關閉)
  506. if ($g['active'] == GeneralConst::ACTIVE_NO) $canuse = false;
  507. $res[] = [
  508. 'id' => $g['id'],
  509. 'lp' => $g['lp'], // 獎項名
  510. 'gp' => $g['gp'], // 所需吉點
  511. 'issoldout' => $issoldout, // 是否全數兌換完畢
  512. 'isredeemed' => $isredeemed, // 是否已兌換過
  513. 'isnomoney' => $isnomoney, // 是否吉點還夠
  514. 'canuse' => $canuse, // 綜合顯示
  515. ];
  516. }
  517. return $res;
  518. }
  519. // 是否今日可玩遊戲
  520. public function canPlay($lineId)
  521. {
  522. // 回合資訊
  523. $roundinfo = $this->roundManagementDb
  524. ->select(['id'])
  525. ->where('dateFinal', '>=', date("Y-m-d"))
  526. ->where('dateBegin', '<=', date("Y-m-d"))
  527. ->where('active', '=', GeneralConst::ACTIVE_YES)
  528. ->get();
  529. if (count($roundinfo) == 0) {
  530. return false;
  531. } else {
  532. $roundinfo = $roundinfo->toArray();
  533. }
  534. $playinfo = $this->sessionManagementDb
  535. ->select(['currentGameGp'])
  536. ->where('lineId', $lineId)
  537. ->where('rid', $roundinfo[0]['id'])
  538. ->where('gid', '<>', 0) // 僅篩選簽到或遊戲
  539. ->where('eventDate', date("Y-m-d"))
  540. ->get()
  541. ->toArray();
  542. // 可用性限制
  543. $canplay = true;
  544. foreach ($playinfo as $p) {
  545. // 目前只有在玩一次的狀態是 -1
  546. if ($p['currentGameGp'] >= 0) $canplay = false;
  547. }
  548. return $canplay;
  549. }
  550. // 玩遊戲簽到(建立在可玩的狀態下才會進來)
  551. public function playGameAndCheckin($lineId, $name)
  552. {
  553. // 回合資訊
  554. $roundinfo = $this->roundManagementDb
  555. ->select(['id'])
  556. ->where('dateFinal', '>=', date("Y-m-d"))
  557. ->where('dateBegin', '<=', date("Y-m-d"))
  558. ->where('active', '=', GeneralConst::ACTIVE_YES)
  559. ->get()
  560. ->toArray();
  561. // 一定有回合所以不防呆(參考本函數註解)
  562. // 依照比例決定返回的轉盤結果
  563. $gameinfo = $this->gameManagementDb
  564. ->select(['id', 'ratio', 'gp'])
  565. ->get()
  566. ->toArray();
  567. $gameArr = [];
  568. foreach ($gameinfo as $g) for ($i = 0; $i < $g['ratio']; $i++) $gameArr[] = ['id' => $g['id'], 'gp' => $g['gp']];
  569. shuffle($gameArr);
  570. // 依照簽到狀態決定需不需要簽到 & 決定簽到吉點
  571. $playinfo = $this->sessionManagementDb
  572. ->select(['currentCheckinGp', 'eventDate'])
  573. ->where('lineId', $lineId)
  574. ->where('rid', $roundinfo[0]['id'])
  575. ->where('gid', '<>', 0) // 僅篩選簽到或遊戲
  576. ->get()
  577. ->toArray();
  578. $cancheckin = true;
  579. $checkins = 0; // 本回合不含這一次,共簽到過幾次
  580. foreach ($playinfo as $p) {
  581. // 檢驗當天是否有簽到
  582. if ($p['eventDate'] == date("Y-m-d")) {
  583. // 目前只有在玩一次的狀態是 -1
  584. if ($p['currentCheckinGp'] >= 0) $cancheckin = false;
  585. }
  586. // 統計本回合已簽到過幾次
  587. if ($p['currentCheckinGp'] > 0) {
  588. $checkins++;
  589. }
  590. }
  591. if ($lineId == $this->settingManagementSv->getSetting()['GAME_TESTING_LINE_ID']) $checkins = 0;
  592. $currentCheckinGp = ($cancheckin) ? $this->getGpByCheckins($checkins + 1) : 0;
  593. // 拼湊 session 紀錄
  594. $currentGp = ((($gameArr[0]['gp'] < 0) ? 0 : $gameArr[0]['gp']) + ($currentCheckinGp)); // 將遊戲吉點小於 0 的部分歸零
  595. $res = [
  596. 'pid' => $this->getPidBylineId($lineId),
  597. 'lineId' => $lineId,
  598. 'userName' => $name,
  599. 'rid' => $roundinfo[0]['id'],
  600. 'gid' => $gameArr[0]['id'],
  601. 'cid' => 0,
  602. 'eventDate' => date("Y-m-d"),
  603. 'currentGp' => $currentGp,
  604. 'currentGameGp' => $gameArr[0]['gp'],
  605. 'currentCheckinGp' => $currentCheckinGp,
  606. 'cdate' => date('Y-m-d H:i:s'),
  607. ];
  608. $this->sessionManagementDb->insert($res);
  609. $id = \DB::getPdo()->lastInsertId();
  610. $res['id'] = $id;
  611. // 上檔玩家吉點計算
  612. $res['gp'] = $this->addGpToPlayer($lineId, $currentGp);
  613. return $res;
  614. }
  615. public function getPidBylineId($lineId)
  616. {
  617. $info = $this->playerManagementDb->select(['id'])->where('lineId', $lineId)->first()->toArray();
  618. return $info['id'];
  619. }
  620. public function getGpByCheckins($checkins)
  621. {
  622. $info = $this->checkinManagementDb->select(['gp'])->where('day', $checkins)->first()->toArray();
  623. return $info['gp'];
  624. }
  625. public function addGpToPlayer($lineId, $gp)
  626. {
  627. $info = $this->playerManagementDb->select(['gp'])->where('lineId', $lineId)->first()->toArray();
  628. $gp += $info['gp'];
  629. $this->playerManagementDb->where('lineId', $lineId)->update(['gp' => $gp]);
  630. return $gp;
  631. }
  632. // 兌點(建立在 goodinfo 執行過確認兌獎資格的情況)
  633. // 採取策略是我們資料庫先改動 > 調用直通 > 如果直通有問題我們就會加註 MEMO > 但本地的資料就不更改了,避免重複讓用戶調用
  634. public function redeemGood($lineId, $gid, $name)
  635. {
  636. $redeeminfo = [];
  637. try {
  638. // 回合資訊 (注意用於兌獎資訊的日期區間判定與玩遊戲不同)
  639. $roundinfo = $this->roundManagementDb
  640. ->select(['id'])
  641. ->where(\DB::raw("DATE_ADD(dateFinal, INTERVAL +redeemExtra DAY)"), '>=', date("Y-m-d"))
  642. ->where('dateBegin', '<=', date("Y-m-d"))
  643. ->where('active', '=', GeneralConst::ACTIVE_YES)
  644. ->get()
  645. ->toArray();
  646. // 來到這邊代表一定有資料
  647. // good 準備資料
  648. $goodinfo = $this->goodManagementDb->select(['issuedQty', 'gp', 'lp'])->where('id', $gid)->first()->toArray();
  649. $fromIssuedQty = $goodinfo['issuedQty'];
  650. $toIssuedQty = $fromIssuedQty + 1;
  651. // player 準備資料
  652. $playerinfo = $this->playerManagementDb->select(['gp'])->where('lineId', $lineId)->first()->toArray();
  653. $fromGp = $playerinfo['gp'];
  654. $toGp = $playerinfo['gp'] - $goodinfo['gp'];
  655. // 直通打點與確認 (先打點,依照返回正確與否再決定要不要後續操作本地資料)
  656. if ($this->settingManagementSv->getSetting()['GAME_ESI_LINEPOINT'] == 'true') {
  657. // 共用變數
  658. $uid = $lineId;
  659. $rqidA = $this->getRqId();
  660. $rqidB = $this->getRqId();
  661. $seedKey = $this->getSeedKey();
  662. $divDataA = $this->getDivData();
  663. $divDataB = $this->getDivData();
  664. //
  665. //
  666. // 1/2 打點
  667. //
  668. //
  669. // 產生本次調用的 sessionKey
  670. $sessionKey = $this->getSessionKey($seedKey, $divDataA);
  671. // 整理押碼用的資料欄內容
  672. $msgRaw = $this->getMsgRaw([
  673. 'RqClient' => env('RQ_CLIENT'),
  674. 'RqId' => $rqidA,
  675. 'UID' => $uid,
  676. 'Amount' => $goodinfo['lp'],
  677. ]);
  678. $msgHash = $this->getMsgHash($msgRaw);
  679. // 建立ICV
  680. $icv = $this->getICV();
  681. // 建立押碼
  682. $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
  683. $mac = $this->getMAC($macRaw);
  684. // 參數打包
  685. $param = [
  686. 'RqClient' => env('RQ_CLIENT'),
  687. 'RqId' => $rqidA,
  688. 'ICV' => $icv,
  689. 'DivData' => $divDataA,
  690. 'MAC' => $mac,
  691. 'UID' => $uid,
  692. 'Amount' => $goodinfo['lp'],
  693. ];
  694. // 相關中繼資料打包(LOG 用)
  695. $logdata = [
  696. 'sessionKey' => $sessionKey,
  697. 'msgRaw' => $msgRaw,
  698. 'msgHash' => $msgHash,
  699. 'macRaw' => $macRaw,
  700. ];
  701. // 憑證參數整理
  702. $curlparam = [
  703. CURLOPT_SSLCERTTYPE => 'PEM',
  704. CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
  705. CURLOPT_SSLKEYTYPE => 'PEM',
  706. CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
  707. CURLOPT_SSL_VERIFYHOST => 0,
  708. CURLOPT_SSL_VERIFYPEER => 0,
  709. ];
  710. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
  711. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
  712. // 直通紀錄
  713. $data = [
  714. 'type' => GeneralConst::LOG_ESI,
  715. 'func' => 'LinepointIssue',
  716. 'k' => $uid,
  717. 'cdate' => date("Y-m-d H:i:s"),
  718. ];
  719. // 調用
  720. $req = [
  721. 'curl' => $curlparam,
  722. 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
  723. 'headers' => [
  724. 'Content-Type' => 'application/json',
  725. 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
  726. 'Accept' => '*/*',
  727. ]
  728. ];
  729. if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.16.196', '172.31.1.169', '172.31.9.46'])) $req['proxy'] = env('CCH_PROXY');
  730. $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/linepoint/issue', $req);
  731. $resA = json_decode($response->getBody());
  732. $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
  733. $data['memoOut'] = json_encode($resA, JSON_UNESCAPED_UNICODE);
  734. $this->syslog($data);
  735. //
  736. //
  737. // 2/2 打點確認
  738. //
  739. //
  740. // 產生本次調用的 sessionKey
  741. $sessionKey = $this->getSessionKey($seedKey, $divDataB);
  742. // 整理押碼用的資料欄內容
  743. $msgRaw = $this->getMsgRaw([
  744. 'RqClient' => env('RQ_CLIENT'),
  745. 'RqId' => $rqidB,
  746. 'QueryRqid' => $rqidA, // 剛才的RQID
  747. 'UID' => $uid,
  748. ]);
  749. $msgHash = $this->getMsgHash($msgRaw);
  750. // 建立ICV
  751. $icv = $this->getICV();
  752. // 建立押碼
  753. $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
  754. $mac = $this->getMAC($macRaw);
  755. // 參數打包
  756. $param = [
  757. 'RqClient' => env('RQ_CLIENT'),
  758. 'RqId' => $rqidB,
  759. 'ICV' => $icv,
  760. 'DivData' => $divDataB,
  761. 'MAC' => $mac,
  762. 'QueryRqid' => $rqidA, // 剛才的RQID
  763. 'UID' => $uid,
  764. ];
  765. // 相關中繼資料打包(LOG 用)
  766. $logdata = [
  767. 'sessionKey' => $sessionKey,
  768. 'msgRaw' => $msgRaw,
  769. 'msgHash' => $msgHash,
  770. 'macRaw' => $macRaw,
  771. ];
  772. // 憑證參數整理
  773. $curlparam = [
  774. CURLOPT_SSLCERTTYPE => 'PEM',
  775. CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
  776. CURLOPT_SSLKEYTYPE => 'PEM',
  777. CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
  778. CURLOPT_SSL_VERIFYHOST => 0,
  779. CURLOPT_SSL_VERIFYPEER => 0,
  780. ];
  781. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
  782. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
  783. // 直通紀錄
  784. $data = [
  785. 'type' => GeneralConst::LOG_ESI,
  786. 'func' => 'LinepointQuery',
  787. 'k' => $uid,
  788. 'cdate' => date("Y-m-d H:i:s"),
  789. ];
  790. // 調用
  791. $req = [
  792. 'curl' => $curlparam,
  793. 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
  794. 'headers' => [
  795. 'Content-Type' => 'application/json',
  796. 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
  797. 'Accept' => '*/*',
  798. ]
  799. ];
  800. if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.16.196', '172.31.1.169', '172.31.9.46'])) $req['proxy'] = env('CCH_PROXY');
  801. $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/linepoint/query', $req);
  802. $resB = json_decode($response->getBody());
  803. $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
  804. $data['memoOut'] = json_encode($resB, JSON_UNESCAPED_UNICODE);
  805. $this->syslog($data);
  806. // 打點返回成功,需要確認
  807. // 確認成功,對用戶返回已打點的資訊,也計入資料庫
  808. // 確認失敗,對用戶返回已打點的資訊,也計入資料庫(差別只在於 LOG 註記的內容不同)
  809. // 打點返回失敗,需要確認
  810. // 確認成功,對用戶返回已打點的資訊,也計入資料庫(差別也只在於 LOG 註記的內容不同)
  811. // 確認失敗,報錯
  812. if ($resA->RCode != '0000' && $resB->RCode != '0000') {
  813. return false;
  814. }
  815. }
  816. // session 插入
  817. $res = [
  818. 'pid' => $this->getPidBylineId($lineId),
  819. 'lineId' => $lineId,
  820. 'userName' => $name,
  821. 'rid' => $roundinfo[0]['id'],
  822. 'gid' => 0, // 注意這是轉盤的獎項 ID 不是兌獎的 ID
  823. 'cid' => $gid,
  824. 'eventDate' => date("Y-m-d"),
  825. 'currentGp' => -$goodinfo['gp'],
  826. 'currentGameGp' => 0,
  827. 'currentCheckinGp' => 0,
  828. 'cdate' => date('Y-m-d H:i:s'),
  829. ];
  830. $this->sessionManagementDb->insert($res);
  831. // good 更新資料
  832. $this->goodManagementDb->where('id', $gid)->update(['issuedQty' => $toIssuedQty]);
  833. // player 更新資料
  834. $this->playerManagementDb->where('lineId', $lineId)->update(['gp' => $toGp]);
  835. // 整理本地紀錄資料
  836. $redeeminfo = [
  837. 'player' => [
  838. 'lineId' => $lineId,
  839. 'fromGp' => $fromGp,
  840. 'toGp' => $toGp,
  841. ],
  842. 'good' => [
  843. 'id' => $gid,
  844. 'fromIssuedQty' => $fromIssuedQty,
  845. 'toIssuedQty' => $toIssuedQty,
  846. ],
  847. 'session' => $res,
  848. ];
  849. } catch (Exception $ex) {
  850. // 錯誤可能發生在 memoIn 或是 out
  851. $data['memoIn'] = json_encode(['error' => 'exception'], JSON_UNESCAPED_UNICODE);
  852. $data['memoOut'] = json_encode(['code' => $ex->getCode(), 'msg' => $ex->getMessage()], JSON_UNESCAPED_UNICODE);
  853. $this->syslog($data);
  854. return false;
  855. }
  856. return $redeeminfo;
  857. }
  858. public function playerTag($lineId)
  859. {
  860. try {
  861. // 本次調用的參數整理
  862. $tid = $this->settingManagementSv->getSetting()['GAME_CURRENT_TAG'];
  863. $uid = $lineId;
  864. $rqid = $this->getRqId();
  865. // 取得 seedKey
  866. $seedKey = $this->getSeedKey();
  867. // 隨機產生多樣性資料
  868. $divData = $this->getDivData();
  869. // 產生本次調用的 sessionKey
  870. $sessionKey = $this->getSessionKey($seedKey, $divData);
  871. // 整理押碼用的資料欄內容
  872. $msgRaw = $this->getMsgRaw([
  873. 'RqClient' => env('RQ_CLIENT'),
  874. 'RqId' => $rqid,
  875. 'TagInfos' => [
  876. [
  877. 'Tagid' => $tid,
  878. 'UIDs' => [$uid]
  879. ],
  880. ],
  881. ]);
  882. $msgHash = $this->getMsgHash($msgRaw);
  883. // 建立ICV
  884. $icv = $this->getICV();
  885. // 建立押碼
  886. $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
  887. $mac = $this->getMAC($macRaw);
  888. // 參數打包
  889. $param = [
  890. 'RqClient' => env('RQ_CLIENT'),
  891. 'RqId' => $rqid,
  892. 'ICV' => $icv,
  893. 'DivData' => $divData,
  894. 'MAC' => $mac,
  895. 'TagInfos' => [
  896. [
  897. 'Tagid' => $tid,
  898. 'UIDs' => [$uid]
  899. ],
  900. ],
  901. ];
  902. // 相關中繼資料打包(LOG 用)
  903. $logdata = [
  904. 'sessionKey' => $sessionKey,
  905. 'msgRaw' => $msgRaw,
  906. 'msgHash' => $msgHash,
  907. 'macRaw' => $macRaw,
  908. ];
  909. // 憑證參數整理
  910. $curlparam = [
  911. CURLOPT_SSLCERTTYPE => 'PEM',
  912. CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
  913. CURLOPT_SSLKEYTYPE => 'PEM',
  914. CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
  915. CURLOPT_SSL_VERIFYHOST => 0,
  916. CURLOPT_SSL_VERIFYPEER => 0,
  917. ];
  918. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
  919. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
  920. // 直通紀錄
  921. $data = [
  922. 'type' => GeneralConst::LOG_ESI,
  923. 'func' => 'TagTagging',
  924. 'k' => $uid,
  925. 'cdate' => date("Y-m-d H:i:s"),
  926. ];
  927. // 調用
  928. $req = [
  929. 'curl' => $curlparam,
  930. 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
  931. 'headers' => [
  932. 'Content-Type' => 'application/json',
  933. 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
  934. 'Accept' => '*/*',
  935. ],
  936. 'timeout' => 15, // Response timeout
  937. 'connect_timeout' => 10, // Connection timeout
  938. ];
  939. if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197'])) $req['proxy'] = env('CCH_PROXY');
  940. $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/tag/Tagging', $req);
  941. $res = $response->getBody();
  942. // 經由計算取得的完整 request JSON
  943. $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
  944. // 調用 API 取得返回
  945. $data['memoOut'] = $res;
  946. // 紀錄
  947. $this->syslog($data);
  948. $this->debuglog('playerTag', [
  949. ''
  950. ], $data);
  951. } catch (Exception $ex) {
  952. // 錯誤可能發生在 memoIn 或是 out
  953. $data['memoIn'] = json_encode(['error' => 'exception'], JSON_UNESCAPED_UNICODE);
  954. $data['memoOut'] = json_encode(['code' => $ex->getCode(), 'msg' => $ex->getMessage()], JSON_UNESCAPED_UNICODE);
  955. // $this->syslog($data);
  956. $this->debuglog('playerTag', [
  957. 'rqid' => $rqid ?? 0
  958. ], [
  959. 'errCode' => $ex->getCode(),
  960. 'errMsg' => $ex->getMessage(),
  961. ]);
  962. return false;
  963. }
  964. return true;
  965. }
  966. public function syslog($sysLog)
  967. {
  968. // 此功能暫時只有小遊戲+類發票有,所以偵測 k 值存在才紀錄
  969. if (isset($sysLog['k'])) {
  970. if (!isset($sysLog['kk'])) {
  971. // 偵測到不存在特殊 kk 鍵的請求就進入小遊戲
  972. $this->syslogManagementDb->insert($sysLog);
  973. } else {
  974. // 偵測到存在特殊 kk 鍵的請求就
  975. if ($sysLog['kk'] == 'activity') {
  976. //進入類發票(需要先移除特殊鍵)
  977. unset($sysLog['kk']);
  978. $this->syslogactManagementDb->insert($sysLog);
  979. }
  980. if ($sysLog['kk'] == 'tmz') {
  981. //進入串門子(需要先移除特殊鍵)
  982. unset($sysLog['kk']);
  983. $this->syslogtManagementDb->insert($sysLog);
  984. }
  985. }
  986. }
  987. return true;
  988. }
  989. public function debuglog($func, $dataIn, $dataOut = '')
  990. {
  991. $this->syslogManagementDb->insert([
  992. 'type' => 'debug',
  993. 'func' => $func,
  994. 'k' => $_SERVER['SERVER_ADDR'],
  995. 'memoIn' => json_encode($dataIn),
  996. 'memoOut' => substr(json_encode($dataOut),0,150),
  997. 'cdate' => date("Y-m-d H:i:s"),
  998. ]);
  999. }
  1000. // 直通 API 串聯流程
  1001. public function esiFlow()
  1002. {
  1003. // 本次調用的參數整理
  1004. $tid = '87B3984D-15EC-480C-8E23-25275F2D2DF2';
  1005. $uid = 'Ud2534d6e1eed9cee2c919a993443352b';
  1006. $rqid = $this->getRqId();
  1007. // 取得 seedKey
  1008. $seedKey = $this->getSeedKey();
  1009. // 隨機產生多樣性資料
  1010. $divData = $this->getDivData();
  1011. // 產生本次調用的 sessionKey
  1012. $sessionKey = $this->getSessionKey($seedKey, $divData);
  1013. // 整理押碼用的資料欄內容
  1014. $msgRaw = $this->getMsgRaw([
  1015. 'RqClient' => env('RQ_CLIENT'),
  1016. 'RqId' => $rqid,
  1017. 'TagInfos' => [
  1018. [
  1019. 'Tagid' => $tid,
  1020. 'UIDs' => [$uid]
  1021. ],
  1022. ],
  1023. ]);
  1024. $msgHash = $this->getMsgHash($msgRaw);
  1025. // 建立ICV
  1026. $icv = $this->getICV();
  1027. // 建立押碼
  1028. $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
  1029. $mac = $this->getMAC($macRaw);
  1030. // 參數打包
  1031. $param = [
  1032. 'RqClient' => env('RQ_CLIENT'),
  1033. 'RqId' => $rqid,
  1034. 'ICV' => $icv,
  1035. 'DivData' => $divData,
  1036. 'MAC' => $mac,
  1037. 'TagInfos' => [
  1038. [
  1039. 'Tagid' => $tid,
  1040. 'UIDs' => [$uid]
  1041. ],
  1042. ],
  1043. ];
  1044. // 相關中繼資料打包(LOG 用)
  1045. $logdata = [
  1046. 'sessionKey' => $sessionKey,
  1047. 'msgRaw' => $msgRaw,
  1048. 'msgHash' => $msgHash,
  1049. 'macRaw' => $macRaw,
  1050. ];
  1051. // 憑證參數整理
  1052. $curlparam = [
  1053. CURLOPT_SSLCERTTYPE => 'PEM',
  1054. CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
  1055. CURLOPT_SSLKEYTYPE => 'PEM',
  1056. CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
  1057. CURLOPT_SSL_VERIFYHOST => 0,
  1058. CURLOPT_SSL_VERIFYPEER => 0,
  1059. ];
  1060. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
  1061. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
  1062. // 調用
  1063. try {
  1064. $req = [
  1065. 'curl' => $curlparam,
  1066. 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
  1067. 'headers' => [
  1068. 'Content-Type' => 'application/json',
  1069. 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
  1070. 'Accept' => '*/*',
  1071. ]
  1072. ];
  1073. if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.16.196', '172.31.1.169', '172.31.9.46'])) $req['proxy'] = env('CCH_PROXY');
  1074. $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/tag/Tagging', $req);
  1075. $res = $response->getBody();
  1076. } catch (Exception $ex) {
  1077. }
  1078. // 直通紀錄
  1079. $data = [
  1080. 'type' => GeneralConst::LOG_ESI,
  1081. 'func' => 'TagTagging',
  1082. 'k' => '',
  1083. 'cdate' => date("Y-m-d H:i:s"),
  1084. ];
  1085. // 經由計算取得的完整 request JSON
  1086. $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
  1087. // 調用 API 取得返回
  1088. $data['memoOut'] = $res;
  1089. // 紀錄
  1090. $this->syslog($data);
  1091. return $res;
  1092. }
  1093. public function esiFlowTagUpdate()
  1094. {
  1095. // 本次調用的參數整理
  1096. $tid = 'f7d27d19-b05e-4ec8-bb49-2c5b0525e095';
  1097. $uid = 'Ud2534d6e1eed9cee2c919a993443352b';
  1098. $rqid = $this->getRqId();
  1099. // 取得 seedKey
  1100. $seedKey = $this->getSeedKey();
  1101. // 隨機產生多樣性資料
  1102. $divData = $this->getDivData();
  1103. // 產生本次調用的 sessionKey
  1104. $sessionKey = $this->getSessionKey($seedKey, $divData);
  1105. // 整理押碼用的資料欄內容
  1106. $msgRaw = $this->getMsgRaw([
  1107. 'RqClient' => env('RQ_CLIENT'),
  1108. 'RqId' => $rqid,
  1109. 'Tags' => [
  1110. [
  1111. 'Tagid' => $tid,
  1112. 'Name' => '奧美測試用標籤' . date("YmdHis"),
  1113. ]
  1114. ],
  1115. ]);
  1116. //$msgRaw = preg_replace('/[\-]/', '', $msgRaw);
  1117. $msgHash = $this->getMsgHash($msgRaw);
  1118. // 建立ICV
  1119. $icv = $this->getICV();
  1120. // 建立押碼
  1121. $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
  1122. $mac = $this->getMAC($macRaw);
  1123. // 參數打包
  1124. $param = [
  1125. 'RqClient' => env('RQ_CLIENT'),
  1126. 'RqId' => $rqid,
  1127. 'ICV' => $icv,
  1128. 'DivData' => $divData,
  1129. 'MAC' => $mac,
  1130. 'Tags' => [
  1131. [
  1132. 'Tagid' => $tid,
  1133. 'Name' => '奧美測試用標籤' . date("YmdHis"),
  1134. ]
  1135. ],
  1136. ];
  1137. // 相關中繼資料打包(LOG 用)
  1138. $logdata = [
  1139. 'sessionKey' => $sessionKey,
  1140. 'msgRaw' => $msgRaw,
  1141. 'msgHash' => $msgHash,
  1142. 'macRaw' => $macRaw,
  1143. ];
  1144. // 憑證參數整理
  1145. $curlparam = [
  1146. CURLOPT_SSLCERTTYPE => 'PEM',
  1147. CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
  1148. CURLOPT_SSLKEYTYPE => 'PEM',
  1149. CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
  1150. CURLOPT_SSL_VERIFYHOST => 0,
  1151. CURLOPT_SSL_VERIFYPEER => 0,
  1152. ];
  1153. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
  1154. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
  1155. // 調用
  1156. $req = [
  1157. 'curl' => $curlparam,
  1158. 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
  1159. 'headers' => [
  1160. 'Content-Type' => 'application/json',
  1161. 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
  1162. 'Accept' => '*/*',
  1163. ]
  1164. ];
  1165. if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.16.196', '172.31.1.169', '172.31.9.46'])) $req['proxy'] = env('CCH_PROXY');
  1166. $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/tag/Update', $req);
  1167. $res = $response->getBody();
  1168. // 直通紀錄
  1169. $data = [
  1170. 'type' => GeneralConst::LOG_ESI,
  1171. 'func' => 'TagUpdate',
  1172. 'k' => '',
  1173. 'cdate' => date("Y-m-d H:i:s"),
  1174. ];
  1175. // 經由計算取得的完整 request JSON
  1176. $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
  1177. // 調用 API 取得返回
  1178. $data['memoOut'] = $res;
  1179. // 紀錄
  1180. $this->syslog($data);
  1181. return [
  1182. 'in' => json_decode($data['memoIn']),
  1183. 'out' => json_decode($data['memoOut']),
  1184. ];
  1185. }
  1186. public function esiFlowTagTagging()
  1187. {
  1188. // 本次調用的參數整理
  1189. $tid = 'f7d27d19-b05e-4ec8-bb49-2c5b0525e095';
  1190. $uid = 'Ud2534d6e1eed9cee2c919a993443352b';
  1191. $rqid = $this->getRqId();
  1192. // 取得 seedKey
  1193. $seedKey = $this->getSeedKey();
  1194. // 隨機產生多樣性資料
  1195. $divData = $this->getDivData();
  1196. // 產生本次調用的 sessionKey
  1197. $sessionKey = $this->getSessionKey($seedKey, $divData);
  1198. // 整理押碼用的資料欄內容
  1199. $msgRaw = $this->getMsgRaw([
  1200. 'RqClient' => env('RQ_CLIENT'),
  1201. 'RqId' => $rqid,
  1202. 'TagInfos' => [
  1203. [
  1204. 'Tagid' => $tid,
  1205. 'UIDs' => [$uid]
  1206. ],
  1207. ],
  1208. ]);
  1209. $msgHash = $this->getMsgHash($msgRaw);
  1210. // 建立ICV
  1211. $icv = $this->getICV();
  1212. // 建立押碼
  1213. $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
  1214. $mac = $this->getMAC($macRaw);
  1215. // 參數打包
  1216. $param = [
  1217. 'RqClient' => env('RQ_CLIENT'),
  1218. 'RqId' => $rqid,
  1219. 'ICV' => $icv,
  1220. 'DivData' => $divData,
  1221. 'MAC' => $mac,
  1222. 'TagInfos' => [
  1223. [
  1224. 'Tagid' => $tid,
  1225. 'UIDs' => [$uid]
  1226. ],
  1227. ],
  1228. ];
  1229. // 相關中繼資料打包(LOG 用)
  1230. $logdata = [
  1231. 'sessionKey' => $sessionKey,
  1232. 'msgRaw' => $msgRaw,
  1233. 'msgHash' => $msgHash,
  1234. 'macRaw' => $macRaw,
  1235. ];
  1236. // 憑證參數整理
  1237. $curlparam = [
  1238. CURLOPT_SSLCERTTYPE => 'PEM',
  1239. CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
  1240. CURLOPT_SSLKEYTYPE => 'PEM',
  1241. CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
  1242. CURLOPT_SSL_VERIFYHOST => 0,
  1243. CURLOPT_SSL_VERIFYPEER => 0,
  1244. ];
  1245. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
  1246. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
  1247. // 調用
  1248. $req = [
  1249. 'curl' => $curlparam,
  1250. 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
  1251. 'headers' => [
  1252. 'Content-Type' => 'application/json',
  1253. 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
  1254. 'Accept' => '*/*',
  1255. ]
  1256. ];
  1257. if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.16.196', '172.31.1.169', '172.31.9.46'])) $req['proxy'] = env('CCH_PROXY');
  1258. $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/tag/Tagging', $req);
  1259. $res = $response->getBody();
  1260. // 直通紀錄
  1261. $data = [
  1262. 'type' => GeneralConst::LOG_ESI,
  1263. 'func' => 'TagTagging',
  1264. 'k' => '',
  1265. 'cdate' => date("Y-m-d H:i:s"),
  1266. ];
  1267. // 經由計算取得的完整 request JSON
  1268. $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
  1269. // 調用 API 取得返回
  1270. $data['memoOut'] = $res;
  1271. // 紀錄
  1272. $this->syslog($data);
  1273. return [
  1274. 'in' => json_decode($data['memoIn']),
  1275. 'out' => json_decode($data['memoOut']),
  1276. ];
  1277. }
  1278. public function esiFlowTagInsert()
  1279. {
  1280. // 本次調用的參數整理
  1281. $tag = '串門子-所有';
  1282. $rqid = $this->getRqId();
  1283. // 取得 seedKey
  1284. $seedKey = $this->getSeedKey();
  1285. // 隨機產生多樣性資料
  1286. $divData = $this->getDivData();
  1287. // 產生本次調用的 sessionKey
  1288. $sessionKey = $this->getSessionKey($seedKey, $divData);
  1289. // 整理押碼用的資料欄內容
  1290. $msgRaw = $this->getMsgRaw([
  1291. 'RqClient' => env('RQ_CLIENT'),
  1292. 'RqId' => $rqid,
  1293. 'Tags' => [['Name' => $tag]],
  1294. ]);
  1295. //$msgRaw = preg_replace('/[\-]/', '', $msgRaw);
  1296. $msgHash = $this->getMsgHash($msgRaw);
  1297. // 建立ICV
  1298. $icv = $this->getICV();
  1299. // 建立押碼
  1300. $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
  1301. $mac = $this->getMAC($macRaw);
  1302. // 參數打包
  1303. $param = [
  1304. 'RqClient' => env('RQ_CLIENT'),
  1305. 'RqId' => $rqid,
  1306. 'ICV' => $icv,
  1307. 'DivData' => $divData,
  1308. 'MAC' => $mac,
  1309. 'Tags' => [['Name' => $tag]],
  1310. ];
  1311. // 相關中繼資料打包(LOG 用)
  1312. $logdata = [
  1313. 'sessionKey' => $sessionKey,
  1314. 'msgRaw' => $msgRaw,
  1315. 'msgHash' => $msgHash,
  1316. 'macRaw' => $macRaw,
  1317. ];
  1318. // 憑證參數整理
  1319. $curlparam = [
  1320. CURLOPT_SSLCERTTYPE => 'PEM',
  1321. CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
  1322. CURLOPT_SSLKEYTYPE => 'PEM',
  1323. CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
  1324. CURLOPT_SSL_VERIFYHOST => 0,
  1325. CURLOPT_SSL_VERIFYPEER => 0,
  1326. ];
  1327. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
  1328. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
  1329. // 調用
  1330. $req = [
  1331. 'curl' => $curlparam,
  1332. 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
  1333. 'headers' => [
  1334. 'Content-Type' => 'application/json',
  1335. 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
  1336. 'Accept' => '*/*',
  1337. ]
  1338. ];
  1339. if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.16.196', '172.31.1.169', '172.31.9.46'])) $req['proxy'] = env('CCH_PROXY');
  1340. $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/tag/Insert', $req);
  1341. $res = $response->getBody();
  1342. // 直通紀錄
  1343. $data = [
  1344. 'type' => GeneralConst::LOG_ESI,
  1345. 'func' => 'TagInsert',
  1346. 'k' => '',
  1347. 'cdate' => date("Y-m-d H:i:s"),
  1348. ];
  1349. // 經由計算取得的完整 request JSON
  1350. $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
  1351. // 調用 API 取得返回
  1352. $data['memoOut'] = $res;
  1353. // 紀錄
  1354. $this->syslog($data);
  1355. return [
  1356. 'in' => json_decode($data['memoIn']),
  1357. 'out' => json_decode($data['memoOut']),
  1358. ];
  1359. }
  1360. public function esiFlowLP()
  1361. {
  1362. // 共用變數
  1363. $uid = "Ub02cb4e598a7d6b3802cc6fb073ce717";
  1364. $rqidA = $this->getRqId();
  1365. $seedKey = $this->getSeedKey();
  1366. $divDataA = $this->getDivData();
  1367. // 產生本次調用的 sessionKey
  1368. $sessionKey = $this->getSessionKey($seedKey, $divDataA);
  1369. // 整理押碼用的資料欄內容
  1370. $msgRaw = $this->getMsgRaw([
  1371. 'RqClient' => env('RQ_CLIENT'),
  1372. 'RqId' => $rqidA,
  1373. 'UID' => $uid,
  1374. 'Amount' => 1,
  1375. ]);
  1376. $msgHash = $this->getMsgHash($msgRaw);
  1377. // 建立ICV
  1378. $icv = $this->getICV();
  1379. // 建立押碼
  1380. $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
  1381. $mac = $this->getMAC($macRaw);
  1382. // 參數打包
  1383. $param = [
  1384. 'RqClient' => env('RQ_CLIENT'),
  1385. 'RqId' => $rqidA,
  1386. 'ICV' => $icv,
  1387. 'DivData' => $divDataA,
  1388. 'MAC' => $mac,
  1389. 'UID' => $uid,
  1390. 'Amount' => 1,
  1391. ];
  1392. // 相關中繼資料打包(LOG 用)
  1393. $logdata = [
  1394. 'sessionKey' => $sessionKey,
  1395. 'msgRaw' => $msgRaw,
  1396. 'msgHash' => $msgHash,
  1397. 'macRaw' => $macRaw,
  1398. ];
  1399. // 憑證參數整理
  1400. $curlparam = [
  1401. CURLOPT_SSLCERTTYPE => 'PEM',
  1402. CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
  1403. CURLOPT_SSLKEYTYPE => 'PEM',
  1404. CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
  1405. CURLOPT_SSL_VERIFYHOST => 0,
  1406. CURLOPT_SSL_VERIFYPEER => 0,
  1407. ];
  1408. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
  1409. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
  1410. // 直通紀錄
  1411. $data = [
  1412. 'type' => GeneralConst::LOG_ESI,
  1413. 'func' => 'LinepointIssue',
  1414. 'k' => $uid,
  1415. 'cdate' => date("Y-m-d H:i:s"),
  1416. ];
  1417. // 調用
  1418. $req = [
  1419. 'curl' => $curlparam,
  1420. 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
  1421. 'headers' => [
  1422. 'Content-Type' => 'application/json',
  1423. 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
  1424. 'Accept' => '*/*',
  1425. ]
  1426. ];
  1427. if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.16.196', '172.31.1.169', '172.31.9.46'])) $req['proxy'] = env('CCH_PROXY');
  1428. $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/linepoint/issue', $req);
  1429. $resA = json_decode($response->getBody());
  1430. $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
  1431. $data['memoOut'] = json_encode($resA, JSON_UNESCAPED_UNICODE);
  1432. $this->syslog($data);
  1433. return [
  1434. 'in' => json_decode($data['memoIn']),
  1435. 'out' => json_decode($data['memoOut']),
  1436. ];
  1437. }
  1438. public function esiFlowBak()
  1439. {
  1440. // 本次調用的參數整理
  1441. $uid = 'Ud2534d6e1eed9cee2c919a993443352b';
  1442. $rqid = $this->getRqId();
  1443. // 取得 seedKey
  1444. $seedKey = $this->getSeedKey();
  1445. // 隨機產生多樣性資料
  1446. $divData = $this->getDivData();
  1447. // 產生本次調用的 sessionKey
  1448. $sessionKey = $this->getSessionKey($seedKey, $divData);
  1449. // 整理押碼用的資料欄內容
  1450. $msgRaw = $this->getMsgRaw([
  1451. 'RqClient' => env('RQ_CLIENT'),
  1452. 'RqId' => $rqid,
  1453. 'UID' => $uid,
  1454. ]);
  1455. $msgHash = $this->getMsgHash($msgRaw);
  1456. // 建立ICV
  1457. $icv = $this->getICV();
  1458. // 建立押碼
  1459. $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
  1460. $mac = $this->getMAC($macRaw);
  1461. // 參數打包
  1462. $param = [
  1463. 'RqClient' => env('RQ_CLIENT'),
  1464. 'RqId' => $rqid,
  1465. 'ICV' => $icv,
  1466. 'DivData' => $divData,
  1467. 'MAC' => $mac,
  1468. 'UID' => $uid,
  1469. ];
  1470. // 相關中繼資料打包(LOG 用)
  1471. $logdata = [
  1472. 'sessionKey' => $sessionKey,
  1473. 'msgRaw' => $msgRaw,
  1474. 'msgHash' => $msgHash,
  1475. 'macRaw' => $macRaw,
  1476. ];
  1477. // 憑證參數整理
  1478. $curlparam = [
  1479. CURLOPT_SSLCERTTYPE => 'PEM',
  1480. CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
  1481. CURLOPT_SSLKEYTYPE => 'PEM',
  1482. CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
  1483. CURLOPT_SSL_VERIFYHOST => 0,
  1484. CURLOPT_SSL_VERIFYPEER => 0,
  1485. ];
  1486. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
  1487. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
  1488. // 調用
  1489. $req = [
  1490. 'curl' => $curlparam,
  1491. 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
  1492. 'headers' => [
  1493. 'Content-Type' => 'application/json',
  1494. 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
  1495. 'Accept' => '*/*',
  1496. ]
  1497. ];
  1498. if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.16.196', '172.31.1.169', '172.31.9.46'])) $req['proxy'] = env('CCH_PROXY');
  1499. $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/contact/QueryQualification', $req);
  1500. $res = $response->getBody();
  1501. // 直通紀錄
  1502. $data = [
  1503. 'type' => GeneralConst::LOG_ESI,
  1504. 'func' => 'QueryQualification',
  1505. 'k' => $uid,
  1506. 'cdate' => date("Y-m-d H:i:s"),
  1507. ];
  1508. // 經由計算取得的完整 request JSON
  1509. $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
  1510. // 調用 API 取得返回
  1511. $data['memoOut'] = $res;
  1512. // 紀錄
  1513. $this->syslog($data);
  1514. // 返回值判斷
  1515. $res = $res . json_encode($req);
  1516. return $res;
  1517. $rcode = $res->RCode ?? '';
  1518. if ($rcode != '0000') return false;
  1519. $isfollow = $res->Result->IsFollow ?? false;
  1520. $ismember = $res->Result->IsTWPMember ?? false;
  1521. if (!$isfollow || !$ismember) return false;
  1522. return true;
  1523. }
  1524. public function getSeedKey()
  1525. {
  1526. // 參照官方文件教學
  1527. // HEX 的 XOR 運算
  1528. $all = strtoupper(bin2hex(pack('H*', env('SEED_KEY_A')) ^ pack('H*', env('SEED_KEY_B'))));
  1529. // 分為前後的 A B 兩段,而不是原先文章定義的 A B 兩個 KEY
  1530. $a = substr($all, 0, strlen($all) / 2);
  1531. $b = substr($all, strlen($all) / 2, strlen($all) / 2);
  1532. // 最後結合的是 A-B-A 段
  1533. return $a . $b . $a;
  1534. }
  1535. public function getSessionKey($seedKey, $divData)
  1536. {
  1537. // 先將 HEX 還原成 BYTE 運算
  1538. $sessionKey = bin2hex(openssl_encrypt(hex2bin($divData), 'DES-EDE3-ECB', hex2bin($seedKey), OPENSSL_RAW_DATA));
  1539. // 按照文件擷取前 24 BYTE (也可以不用)
  1540. $sessionKey = substr($sessionKey, 0, 48);
  1541. // 按照文件轉成大寫 (也可以不用)
  1542. $sessionKey = strtoupper($sessionKey);
  1543. return $sessionKey;
  1544. }
  1545. public function getRqId()
  1546. {
  1547. $data = $data ?? random_bytes(16);
  1548. assert(strlen($data) == 16);
  1549. $data[6] = chr(ord($data[6]) & 0x0f | 0x40);
  1550. $data[8] = chr(ord($data[8]) & 0x3f | 0x80);
  1551. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  1552. }
  1553. public function getDivData()
  1554. {
  1555. return bin2hex(random_bytes(24));
  1556. }
  1557. public function getMsgRaw($msg)
  1558. {
  1559. $ret = '';
  1560. foreach ($msg as $m) {
  1561. $ret .= (is_array($m)) ? json_encode($m, JSON_UNESCAPED_UNICODE) : preg_replace('/[^A-Za-z0-9\-]/', '', $m);
  1562. }
  1563. return $ret;
  1564. }
  1565. public function getMsgHash($msg)
  1566. {
  1567. // 返回可以大寫也可以不用
  1568. return strtoupper(hash('sha256', $msg));
  1569. }
  1570. public function getICV()
  1571. {
  1572. return bin2hex(random_bytes(8));
  1573. }
  1574. public function getMACRaw($msgHash, $sessionKey, $icv)
  1575. {
  1576. // 先將 HEX 還原成 BYTE 運算 (其中 ICV 可以大寫也可以不用)
  1577. $macRaw = bin2hex(openssl_encrypt(hex2bin($msgHash), 'DES-EDE3-CBC', hex2bin($sessionKey), OPENSSL_RAW_DATA, hex2bin($icv)));
  1578. // 返回可以大寫也可以不用
  1579. return strtoupper($macRaw);
  1580. }
  1581. public function getMAC($macRaw)
  1582. {
  1583. // 按照文件擷取前 32 BYTE (必要)
  1584. $macRaw = substr($macRaw, 0, 64);
  1585. // 可以大寫也可以不用
  1586. $macRaw = strtoupper($macRaw);
  1587. return substr($macRaw, strlen($macRaw) - 16, 8);
  1588. }
  1589. // 類發票
  1590. public function setdoc($data)
  1591. {
  1592. // 調用
  1593. $response = $this->imgC->annotateImage(
  1594. $data,
  1595. [Type::TEXT_DETECTION]
  1596. );
  1597. $texts = $response->getTextAnnotations();
  1598. // 整理字串
  1599. $ret = [];
  1600. foreach ($texts as $text) $ret[] = $text->getDescription();
  1601. // 銀行 APP 判別
  1602. $account_bank = '';
  1603. $detect = explode("\n", $ret[0]);
  1604. foreach ($detect as $d) {
  1605. if ($d == '出示退款碼') $account_bank = '007';
  1606. if ($d == '登入/安控') $account_bank = '009';
  1607. if ($d == '繼續台灣 Pay交易') $account_bank = '016';
  1608. if ($d == '我要退款') $account_bank = '017';
  1609. if ($d == '繼續行動支付') $account_bank = '050';
  1610. if ($d == '完成') $account_bank = '806';
  1611. }
  1612. // 整理觸發字串
  1613. $order_no = '';
  1614. $account_no = '';
  1615. $order_date = '';
  1616. switch ($account_bank) {
  1617. case '007':
  1618. for ($i = 0; $i < count($ret); $i++) {
  1619. if ($ret[ $i ] == '訂單') $order_no = $ret[ $i + 2 ] ?? '';
  1620. if ($ret[ $i ] == '帳號') $account_no = $ret[ $i + 1 ] ?? '';
  1621. if (preg_match("/^[0-9]{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
  1622. $order_date = $ret[ $i ] ?? '';
  1623. }
  1624. }
  1625. break;
  1626. case '009':
  1627. for ($i = 0; $i < count($ret); $i++) {
  1628. if ($ret[ $i ] == '訂單') $order_no = $ret[ $i + 2 ] ?? '';
  1629. if ($ret[ $i ] == '帳號') $account_no = $ret[ $i + 1 ] ?? '';
  1630. if (preg_match("/^[0-9]{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
  1631. $order_date = $ret[ $i ] ?? '';
  1632. }
  1633. }
  1634. break;
  1635. case '016':
  1636. for ($i = 0; $i < count($ret); $i++) {
  1637. if ($ret[ $i ] == '訂單') $order_no = $ret[ $i + 2 ] ?? '';
  1638. if ($ret[ $i ] == '帳號') $account_no = $ret[ $i + 1 ] ?? '';
  1639. if (preg_match("/^[0-9]{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
  1640. $order_date = $ret[ $i ] ?? '';
  1641. }
  1642. }
  1643. break;
  1644. case '017':
  1645. for ($i = 0; $i < count($ret); $i++) {
  1646. if ($ret[ $i ] == '訂單') $order_no = $ret[ $i + 2 ] ?? '';
  1647. if ($ret[ $i ] == '方式') $account_no = $ret[ $i + 1 ] ?? '';
  1648. if (preg_match("/^[0-9]{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
  1649. $order_date = $ret[ $i ] ?? '';
  1650. }
  1651. }
  1652. break;
  1653. case '050':
  1654. for ($i = 0; $i < count($ret); $i++) {
  1655. if ($ret[ $i ] == '訂單') $order_no = $ret[ $i + 2 ] ?? '';
  1656. if ($ret[ $i ] == '帳號') $account_no = $ret[ $i + 1 ] ?? '';
  1657. // 沒有時間資訊可取得,這個是虛設
  1658. if (preg_match("/^[0-9]{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
  1659. $order_date = $ret[ $i ] ?? '';
  1660. }
  1661. }
  1662. break;
  1663. case '806':
  1664. for ($i = 0; $i < count($ret); $i++) {
  1665. if ($ret[ $i ] == '編號') $order_no = $ret[ $i + 1 ] ?? '';
  1666. if ($ret[ $i ] == '商戶') $account_no = preg_replace('/-/', "", $ret[ $i - 1 ]) ?? '';
  1667. if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
  1668. $order_date = preg_replace('/-/', "/", $ret[ $i ]) ?? '';
  1669. }
  1670. }
  1671. break;
  1672. case '':
  1673. for ($i = 0; $i < count($ret); $i++) {
  1674. if ($ret[ $i ] == '訂單') $order_no = $ret[ $i + 2 ] ?? '';
  1675. if ($ret[ $i ] == '付款' && $ret[ $i + 1 ] == '帳號') $account_bank = $ret[ $i + 2 ] ?? '';
  1676. if ($ret[ $i ] == '付款' && $ret[ $i + 1 ] == '帳號') $account_no = $ret[ $i + 4 ] ?? '';
  1677. //if ($ret[ $i ] == '時間') $order_date = $ret[ $i + 2 ] ?? '';
  1678. if (preg_match("/^[0-9]{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
  1679. $order_date = $ret[ $i ] ?? '';
  1680. }
  1681. }
  1682. break;
  1683. }
  1684. return [
  1685. 'order_no' => $order_no,
  1686. 'account_bank' => $account_bank,
  1687. 'account_no' => $account_no,
  1688. 'order_date' => $order_date,
  1689. //'debug' => $ret,
  1690. ];
  1691. }
  1692. public function activityinfo()
  1693. {
  1694. // 活動資訊
  1695. $activityinfo = $this->activityManagementDb
  1696. ->select(['id', 'reqTimeBegin', 'reqTimeFinal'])
  1697. ->where('reqTimeFinal', '>=', date("Y-m-d"))
  1698. ->where('reqTimeBegin', '<=', date("Y-m-d"))
  1699. ->where('isDraw', '')
  1700. ->get();
  1701. if (count($activityinfo) == 0) {
  1702. return false;
  1703. } else {
  1704. $activityinfo = $activityinfo->toArray();
  1705. }
  1706. return $activityinfo;
  1707. }
  1708. public function activities()
  1709. {
  1710. // 活動資訊
  1711. $activities = $this->activityManagementDb
  1712. ->select([
  1713. 'id',
  1714. 'activityName',
  1715. 'reqTimeBegin',
  1716. 'reqTimeFinal',
  1717. 'drawTime',
  1718. 'drawNumbers',
  1719. 'isDraw',
  1720. 'redeemTimeBegin',
  1721. 'redeemTimeFinal',
  1722. 'isCheckBegin',
  1723. 'checkTimeBegin',
  1724. 'checkTimeFinal',
  1725. 'getTimeBegin',
  1726. 'getTimeFinal',
  1727. 'isGetFinal',
  1728. ])
  1729. ->get();
  1730. if (count($activities) == 0) {
  1731. return false;
  1732. } else {
  1733. $activities = $activities->toArray();
  1734. }
  1735. return $activities;
  1736. }
  1737. public function reqreceipt($info)
  1738. {
  1739. // 重複登錄資料邏輯判斷
  1740. $r = $this->receiptManagementDb
  1741. ->select(['id'])
  1742. ->where('lineId', $info['lineId'])
  1743. ->where('lineName', $info['name'])
  1744. ->where('tranDate', $info['order_date'])
  1745. ->where('tranBank', \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $info['account_bank'] . "'), \"" . env('KK') . "\"))"))
  1746. ->where('tranAccount', \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $info['account_no'] . "'), \"" . env('KK') . "\"))"))
  1747. ->where('tranOrderNo', \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $info['order_no'] . "'), \"" . env('KK') . "\"))"))
  1748. ->get();
  1749. if (count($r) == 0) return 0;
  1750. // 實際寫入
  1751. $this->receiptManagementDb
  1752. ->insert([
  1753. 'aid' => $info['aid'],
  1754. 'tranDate' => $info['order_date'],
  1755. 'tranBank' => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $info['account_bank'] . "'), \"" . env('KK') . "\"))"),
  1756. 'tranAccount' => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $info['account_no'] . "'), \"" . env('KK') . "\"))"),
  1757. 'tranOrderNo' => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $info['order_no'] . "'), \"" . env('KK') . "\"))"),
  1758. 'lineId' => $info['lineId'],
  1759. 'lineName' => $info['name'],
  1760. 'cdate' => date('Y-m-d H:i:s'),
  1761. 'mdate' => date('Y-m-d H:i:s'),
  1762. 'oid' => 0, // 外部用戶使用0
  1763. ]);
  1764. $id = \DB::getPdo()->lastInsertId();
  1765. return $id;
  1766. }
  1767. public function lstreceipt($lineId)
  1768. {
  1769. // 依照(有限的)不同活動逐次取得該活動的所有紀錄
  1770. $activity = $this->activityManagementDb->select(['id', 'activityName'])->orderby('drawTime', 'desc')->get();
  1771. if (count($activity) == 0) {
  1772. return [];
  1773. } else {
  1774. $activity = $activity->toArray();
  1775. }
  1776. $ret = [];
  1777. // 選欄位
  1778. $rStatusMsg = '';
  1779. $rStatusBtn = '';
  1780. foreach (GeneralConst::$rStatusMap as $k => $v) {
  1781. $rStatusMsg .= ' when \'' . $k . '\' then \'' . $v['front'] . '\'';
  1782. $rStatusBtn .= ' when \'' . $k . '\' then \'' . $v['btn'] . '\'';
  1783. }
  1784. // 逐個活動取得紀錄
  1785. foreach ($activity as $a) {
  1786. $receipt = $this->receiptManagementDb
  1787. ->select([
  1788. 'id',
  1789. \DB::raw("tranDate as order_date"),
  1790. \DB::raw("FROM_BASE64(AES_DECRYPT(UNHEX(tranBank), \"" . env('KK') . "\")) as account_bank"),
  1791. \DB::raw("FROM_BASE64(AES_DECRYPT(UNHEX(tranAccount), \"" . env('KK') . "\")) as account_no"),
  1792. \DB::raw("FROM_BASE64(AES_DECRYPT(UNHEX(tranOrderNo), \"" . env('KK') . "\")) as order_no"),
  1793. 'rStatus',
  1794. \DB::raw("(case rStatus $rStatusMsg end) as rStatusMsg"),
  1795. \DB::raw("(case rStatus $rStatusBtn end) as rStatusBtn"),
  1796. ])
  1797. ->where('lineId', $lineId) // 自己的訂單
  1798. ->where('aid', $a['id']) // 某一期的訂單
  1799. ->orderby('cdate', 'desc')
  1800. ->get();
  1801. if (count($receipt) != 0) {
  1802. $receipt = $receipt->toArray();
  1803. // 依照順序切分(356412978)
  1804. $tmp = [];
  1805. foreach ($receipt as $r) $tmp[ $r['rStatus'] ][] = $r;
  1806. // 整合
  1807. $ret[] = [
  1808. 'activityName' => $a['activityName'],
  1809. 'receipts' => array_merge(
  1810. $tmp[ GeneralConst::RSTATUS_DRAW_DONE ] ?? [],
  1811. $tmp[ GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE ] ?? [],
  1812. $tmp[ GeneralConst::RSTATUS_DRAW_DONE_REDEEM_FAIL ] ?? [],
  1813. $tmp[ GeneralConst::RSTATUS_DRAW_DONE_REDEEM ] ?? [],
  1814. $tmp[ GeneralConst::RSTATUS_INIT ] ?? [],
  1815. $tmp[ GeneralConst::RSTATUS_DRAW_FAIL ] ?? [],
  1816. $tmp[ GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE_GET ] ?? [],
  1817. $tmp[ GeneralConst::RSTATUS_DRAW_DONE_EXPIRED ] ?? [],
  1818. $tmp[ GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE_EXPIRED ] ?? []
  1819. ),
  1820. ];
  1821. }
  1822. }
  1823. return $ret;
  1824. }
  1825. public function redeemCheck($lineId, $rid)
  1826. {
  1827. // 收據資訊
  1828. $receiptinfo = $this->receiptManagementDb
  1829. ->select(['aid'])
  1830. ->where('lineId', $lineId) // 找自己的
  1831. ->where('id', $rid) // 收據
  1832. ->where('rStatus', GeneralConst::RSTATUS_DRAW_DONE) // 可驗證的狀態
  1833. ->get();
  1834. if (count($receiptinfo) == 0) {
  1835. return false;
  1836. } else {
  1837. $receiptinfo = $receiptinfo->toArray();
  1838. }
  1839. // 活動資訊
  1840. $activityinfo = $this->activityManagementDb
  1841. ->select(['id', 'reqTimeBegin', 'reqTimeFinal'])
  1842. ->where('id', $receiptinfo[0]['aid']) // 找該收據活動
  1843. ->where('isDraw', 'Y') // 已經開獎
  1844. ->where('redeemTimeFinal', '>=', date("Y-m-d")) // 且還在可登記的時間範圍內
  1845. ->where('redeemTimeBegin', '<=', date("Y-m-d"))
  1846. ->get();
  1847. if (count($activityinfo) == 0) {
  1848. return false;
  1849. } else {
  1850. return true;
  1851. }
  1852. }
  1853. public function redeemExecute($lineId, $rid)
  1854. {
  1855. $this->receiptManagementDb
  1856. ->where('lineId', $lineId)
  1857. ->where('id', $rid)
  1858. ->where('rStatus', GeneralConst::RSTATUS_DRAW_DONE)
  1859. ->update([
  1860. 'rStatus' => GeneralConst::RSTATUS_DRAW_DONE_REDEEM,
  1861. 'mdate' => date('Y-m-d H:i:s'),
  1862. 'oid' => 0, // 外部用戶使用0
  1863. ]);
  1864. return true;
  1865. }
  1866. public function getCheck($lineId, $rid)
  1867. {
  1868. // 收據資訊
  1869. $receiptinfo = $this->receiptManagementDb
  1870. ->select(['aid'])
  1871. ->where('lineId', $lineId) // 找自己的
  1872. ->where('id', $rid) // 收據
  1873. ->where('rStatus', GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE) // 可領獎的狀態
  1874. ->where('canGet', 'Y') // 可領獎的狀態(輔助狀態)
  1875. ->get();
  1876. if (count($receiptinfo) == 0) {
  1877. return false;
  1878. } else {
  1879. $receiptinfo = $receiptinfo->toArray();
  1880. }
  1881. // 活動資訊
  1882. $activityinfo = $this->activityManagementDb
  1883. ->select(['id', 'reqTimeBegin', 'reqTimeFinal'])
  1884. ->where('id', $receiptinfo[0]['aid']) // 找該收據活動
  1885. ->where('isDraw', 'Y') // 已經開獎
  1886. ->where('getTimeFinal', '>=', date("Y-m-d")) // 且還在可領獎的時間範圍內
  1887. ->where('getTimeBegin', '<=', date("Y-m-d"))
  1888. ->get();
  1889. if (count($activityinfo) == 0) {
  1890. return false;
  1891. } else {
  1892. return true;
  1893. }
  1894. }
  1895. public function getExecute($lineId, $rid)
  1896. {
  1897. try {
  1898. if ($this->settingManagementSv->getSetting()['RECEIPT_ESI_LINEPOINT'] == 'true') {
  1899. // 共用變數
  1900. $uid = $lineId;
  1901. $rqidA = $this->getRqId();
  1902. $rqidB = $this->getRqId();
  1903. $seedKey = $this->getSeedKey();
  1904. $divDataA = $this->getDivData();
  1905. $divDataB = $this->getDivData();
  1906. //
  1907. //
  1908. // 1/2 打點
  1909. //
  1910. //
  1911. // 產生本次調用的 sessionKey
  1912. $sessionKey = $this->getSessionKey($seedKey, $divDataA);
  1913. // 整理押碼用的資料欄內容
  1914. $msgRaw = $this->getMsgRaw([
  1915. 'RqClient' => env('RQ_CLIENT'),
  1916. 'RqId' => $rqidA,
  1917. 'UID' => $uid,
  1918. 'Amount' => 1,
  1919. ]);
  1920. $msgHash = $this->getMsgHash($msgRaw);
  1921. // 建立ICV
  1922. $icv = $this->getICV();
  1923. // 建立押碼
  1924. $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
  1925. $mac = $this->getMAC($macRaw);
  1926. // 參數打包
  1927. $param = [
  1928. 'RqClient' => env('RQ_CLIENT'),
  1929. 'RqId' => $rqidA,
  1930. 'ICV' => $icv,
  1931. 'DivData' => $divDataA,
  1932. 'MAC' => $mac,
  1933. 'UID' => $uid,
  1934. 'Amount' => 1,
  1935. ];
  1936. // 相關中繼資料打包(LOG 用)
  1937. $logdata = [
  1938. 'sessionKey' => $sessionKey,
  1939. 'msgRaw' => $msgRaw,
  1940. 'msgHash' => $msgHash,
  1941. 'macRaw' => $macRaw,
  1942. ];
  1943. // 憑證參數整理
  1944. $curlparam = [
  1945. CURLOPT_SSLCERTTYPE => 'PEM',
  1946. CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
  1947. CURLOPT_SSLKEYTYPE => 'PEM',
  1948. CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
  1949. CURLOPT_SSL_VERIFYHOST => 0,
  1950. CURLOPT_SSL_VERIFYPEER => 0,
  1951. ];
  1952. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
  1953. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
  1954. // 直通紀錄
  1955. $data = [
  1956. 'type' => GeneralConst::LOG_ESI,
  1957. 'func' => 'LinepointIssue',
  1958. 'k' => $uid,
  1959. 'kk' => 'activity',
  1960. 'cdate' => date("Y-m-d H:i:s"),
  1961. ];
  1962. // 調用
  1963. $req = [
  1964. 'curl' => $curlparam,
  1965. 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
  1966. 'headers' => [
  1967. 'Content-Type' => 'application/json',
  1968. 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
  1969. 'Accept' => '*/*',
  1970. ]
  1971. ];
  1972. if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.16.196', '172.31.1.169', '172.31.9.46'])) $req['proxy'] = env('CCH_PROXY');
  1973. $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/linepoint/issue', $req);
  1974. $resA = json_decode($response->getBody());
  1975. $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
  1976. $data['memoOut'] = json_encode($resA, JSON_UNESCAPED_UNICODE);
  1977. $this->syslog($data);
  1978. //
  1979. //
  1980. // 2/2 打點確認
  1981. //
  1982. //
  1983. // 產生本次調用的 sessionKey
  1984. $sessionKey = $this->getSessionKey($seedKey, $divDataB);
  1985. // 整理押碼用的資料欄內容
  1986. $msgRaw = $this->getMsgRaw([
  1987. 'RqClient' => env('RQ_CLIENT'),
  1988. 'RqId' => $rqidB,
  1989. 'QueryRqid' => $rqidA, // 剛才的RQID
  1990. 'UID' => $uid,
  1991. ]);
  1992. $msgHash = $this->getMsgHash($msgRaw);
  1993. // 建立ICV
  1994. $icv = $this->getICV();
  1995. // 建立押碼
  1996. $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
  1997. $mac = $this->getMAC($macRaw);
  1998. // 參數打包
  1999. $param = [
  2000. 'RqClient' => env('RQ_CLIENT'),
  2001. 'RqId' => $rqidB,
  2002. 'ICV' => $icv,
  2003. 'DivData' => $divDataB,
  2004. 'MAC' => $mac,
  2005. 'QueryRqid' => $rqidA, // 剛才的RQID
  2006. 'UID' => $uid,
  2007. ];
  2008. // 相關中繼資料打包(LOG 用)
  2009. $logdata = [
  2010. 'sessionKey' => $sessionKey,
  2011. 'msgRaw' => $msgRaw,
  2012. 'msgHash' => $msgHash,
  2013. 'macRaw' => $macRaw,
  2014. ];
  2015. // 憑證參數整理
  2016. $curlparam = [
  2017. CURLOPT_SSLCERTTYPE => 'PEM',
  2018. CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
  2019. CURLOPT_SSLKEYTYPE => 'PEM',
  2020. CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
  2021. CURLOPT_SSL_VERIFYHOST => 0,
  2022. CURLOPT_SSL_VERIFYPEER => 0,
  2023. ];
  2024. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
  2025. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
  2026. // 直通紀錄
  2027. $data = [
  2028. 'type' => GeneralConst::LOG_ESI,
  2029. 'func' => 'LinepointQuery',
  2030. 'k' => $uid,
  2031. 'kk' => 'activity',
  2032. 'cdate' => date("Y-m-d H:i:s"),
  2033. ];
  2034. // 調用
  2035. $req = [
  2036. 'curl' => $curlparam,
  2037. 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
  2038. 'headers' => [
  2039. 'Content-Type' => 'application/json',
  2040. 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
  2041. 'Accept' => '*/*',
  2042. ]
  2043. ];
  2044. if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.16.196', '172.31.1.169', '172.31.9.46'])) $req['proxy'] = env('CCH_PROXY');
  2045. $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/linepoint/query', $req);
  2046. $resB = json_decode($response->getBody());
  2047. $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
  2048. $data['memoOut'] = json_encode($resB, JSON_UNESCAPED_UNICODE);
  2049. $this->syslog($data);
  2050. // 打點返回成功,需要確認
  2051. // 確認成功,對用戶返回已打點的資訊,也計入資料庫
  2052. // 確認失敗,對用戶返回已打點的資訊,也計入資料庫(差別只在於 LOG 註記的內容不同)
  2053. // 打點返回失敗,需要確認
  2054. // 確認成功,對用戶返回已打點的資訊,也計入資料庫(差別也只在於 LOG 註記的內容不同)
  2055. // 確認失敗,報錯
  2056. if ($resA->RCode != '0000' && $resB->RCode != '0000') {
  2057. return false;
  2058. }
  2059. }
  2060. // 修改資料庫狀態
  2061. $this->receiptManagementDb
  2062. ->where('lineId', $lineId)
  2063. ->where('id', $rid)
  2064. ->where('rStatus', GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE)
  2065. ->update([
  2066. 'rStatus' => GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE_GET,
  2067. 'mdate' => date('Y-m-d H:i:s'),
  2068. 'oid' => 0, // 外部用戶使用0
  2069. ]);
  2070. } catch (Exception $ex) {
  2071. // 錯誤可能發生在 memoIn 或是 out
  2072. $data['memoIn'] = json_encode(['error' => 'exception'], JSON_UNESCAPED_UNICODE);
  2073. $data['memoOut'] = json_encode(['code' => $ex->getCode(), 'msg' => $ex->getMessage()], JSON_UNESCAPED_UNICODE);
  2074. $this->syslog($data);
  2075. return false;
  2076. }
  2077. return true;
  2078. }
  2079. // 串門子
  2080. public function tplayerinfo($lineId)
  2081. {
  2082. // 取得用戶在本地的資訊
  2083. $tplayerinfo = $this->tplayerManagementDb
  2084. ->select(['id'])
  2085. ->where('lineId', $lineId)
  2086. ->get()
  2087. ->toArray();
  2088. // 取得用戶在 LINE 上的 profile
  2089. // $profile = $this->lineProfile($lineId);
  2090. if (count($tplayerinfo) == 0) {
  2091. // 用戶不在本地資料庫,新增
  2092. // 寫入資料表
  2093. $d = [
  2094. 'lineId' => $lineId,
  2095. 'userName' => '',
  2096. 'userPhoto' => '',
  2097. 'c1' => 0,
  2098. 'c2' => 0,
  2099. 'c3' => 0,
  2100. 'c4' => 0,
  2101. 'c5' => 0,
  2102. 'c6' => 0,
  2103. 'c7' => 0,
  2104. 'c8' => 0,
  2105. 'c9' => 0,
  2106. 'gp' => 0,
  2107. 'cdate' => date("Y-m-d H:i:s"),
  2108. 'mdate' => date("Y-m-d H:i:s"),
  2109. ];
  2110. $this->tplayerManagementDb
  2111. ->insert($d);
  2112. $id = \DB::getPdo()->lastInsertId();
  2113. // 替用互打一次性標籤
  2114. $this->tplayerTag($lineId);
  2115. } else {
  2116. // 用戶的 LINE 資訊可能會更新,所以這個 API 還身兼更新資訊的功能
  2117. // $d = [
  2118. // 'userName' => $profile->displayName,
  2119. // 'userPhoto' => $profile->pictureUrl,
  2120. // ];
  2121. // $this->tplayerManagementDb
  2122. // ->where('lineId', $lineId)
  2123. // ->update($d);
  2124. }
  2125. // 再重新查一遍
  2126. $tplayerinfo = $this->tplayerManagementDb
  2127. ->select([
  2128. // 'id',
  2129. 'lineId',
  2130. // 'userName',
  2131. // 'userPhoto',
  2132. 'gp',
  2133. 'c1',
  2134. 'c2',
  2135. 'c3',
  2136. 'c4',
  2137. 'c5',
  2138. 'c6',
  2139. 'c7',
  2140. 'c8',
  2141. 'c9',
  2142. \DB::raw("FLOOR(gp / " . $this->settingManagementSv->getSetting()['TMZ_GP_DRAW'] . ") as draw"),
  2143. // 'cdate',
  2144. // 'mdate',
  2145. ])
  2146. ->where('lineId', $lineId)
  2147. ->first()
  2148. ->toArray();
  2149. return $tplayerinfo;
  2150. }
  2151. public function tplayerTag($lineId)
  2152. {
  2153. try {
  2154. // 本次調用的參數整理
  2155. $tid = $this->settingManagementSv->getSetting()['TMZ_CURRENT_TAG'];
  2156. $uid = $lineId;
  2157. $rqid = $this->getRqId();
  2158. // 取得 seedKey
  2159. $seedKey = $this->getSeedKey();
  2160. // 隨機產生多樣性資料
  2161. $divData = $this->getDivData();
  2162. // 產生本次調用的 sessionKey
  2163. $sessionKey = $this->getSessionKey($seedKey, $divData);
  2164. // 整理押碼用的資料欄內容
  2165. $msgRaw = $this->getMsgRaw([
  2166. 'RqClient' => env('RQ_CLIENT'),
  2167. 'RqId' => $rqid,
  2168. 'TagInfos' => [
  2169. [
  2170. 'Tagid' => $tid,
  2171. 'UIDs' => [$uid]
  2172. ],
  2173. ],
  2174. ]);
  2175. $msgHash = $this->getMsgHash($msgRaw);
  2176. // 建立ICV
  2177. $icv = $this->getICV();
  2178. // 建立押碼
  2179. $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
  2180. $mac = $this->getMAC($macRaw);
  2181. // 參數打包
  2182. $param = [
  2183. 'RqClient' => env('RQ_CLIENT'),
  2184. 'RqId' => $rqid,
  2185. 'ICV' => $icv,
  2186. 'DivData' => $divData,
  2187. 'MAC' => $mac,
  2188. 'TagInfos' => [
  2189. [
  2190. 'Tagid' => $tid,
  2191. 'UIDs' => [$uid]
  2192. ],
  2193. ],
  2194. ];
  2195. // 相關中繼資料打包(LOG 用)
  2196. $logdata = [
  2197. 'sessionKey' => $sessionKey,
  2198. 'msgRaw' => $msgRaw,
  2199. 'msgHash' => $msgHash,
  2200. 'macRaw' => $macRaw,
  2201. ];
  2202. // 憑證參數整理
  2203. $curlparam = [
  2204. CURLOPT_SSLCERTTYPE => 'PEM',
  2205. CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
  2206. CURLOPT_SSLKEYTYPE => 'PEM',
  2207. CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
  2208. CURLOPT_SSL_VERIFYHOST => 0,
  2209. CURLOPT_SSL_VERIFYPEER => 0,
  2210. ];
  2211. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
  2212. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
  2213. // 直通紀錄
  2214. $data = [
  2215. 'type' => GeneralConst::LOG_ESI,
  2216. 'func' => 'TagTagging',
  2217. 'k' => $uid,
  2218. 'kk' => 'tmz',
  2219. 'cdate' => date("Y-m-d H:i:s"),
  2220. ];
  2221. // 調用
  2222. $req = [
  2223. 'curl' => $curlparam,
  2224. 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
  2225. 'headers' => [
  2226. 'Content-Type' => 'application/json',
  2227. 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
  2228. 'Accept' => '*/*',
  2229. ]
  2230. ];
  2231. if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.16.196', '172.31.1.169', '172.31.9.46'])) $req['proxy'] = env('CCH_PROXY');
  2232. $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/tag/Tagging', $req);
  2233. $res = $response->getBody();
  2234. // 經由計算取得的完整 request JSON
  2235. $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
  2236. // 調用 API 取得返回
  2237. $data['memoOut'] = $res;
  2238. // 紀錄
  2239. $this->syslog($data);
  2240. } catch (Exception $ex) {
  2241. // 錯誤可能發生在 memoIn 或是 out
  2242. $data['memoIn'] = json_encode(['error' => 'exception'], JSON_UNESCAPED_UNICODE);
  2243. $data['memoOut'] = json_encode(['code' => $ex->getCode(), 'msg' => $ex->getMessage()], JSON_UNESCAPED_UNICODE);
  2244. $this->syslog($data);
  2245. return false;
  2246. }
  2247. return true;
  2248. }
  2249. public function tcheckin($lineId, $name)
  2250. {
  2251. // 回合資訊
  2252. $troundinfo = $this->troundManagementDb
  2253. ->select(['id'])
  2254. ->where('dateFinal', '>=', date("Y-m-d"))
  2255. ->where('dateBegin', '<=', date("Y-m-d"))
  2256. ->where('active', '=', GeneralConst::ACTIVE_YES)
  2257. ->get()
  2258. ->toArray();
  2259. // 一定有回合所以不防呆(參考本函數註解)
  2260. // 依照簽到狀態決定需不需要簽到 & 決定簽到吉點
  2261. $playinfo = $this->tsessionManagementDb
  2262. ->select(['currentCheckinGp', 'eventDate'])
  2263. ->where('lineId', $lineId)
  2264. ->where('rid', $troundinfo[0]['id'])
  2265. ->where('gid', '=', -1) // 僅篩選簽到
  2266. ->get()
  2267. ->toArray();
  2268. $cancheckin = true;
  2269. $checkins = 0; // 本回合不含這一次,共簽到過幾次
  2270. foreach ($playinfo as $p) {
  2271. // 檢驗當天是否有簽到
  2272. if ($p['eventDate'] == date("Y-m-d")) {
  2273. if ($p['currentCheckinGp'] >= 0) $cancheckin = false;
  2274. }
  2275. // 統計本回合已簽到過幾次
  2276. if ($p['currentCheckinGp'] >= 0) {
  2277. $checkins++;
  2278. }
  2279. }
  2280. //if ($lineId == $this->settingManagementSv->getSetting()['GAME_TESTING_LINE_ID']) $checkins = 0;
  2281. $currentCheckinGp = ($cancheckin) ? $this->getGpByTCheckins($checkins + 1) : 0;
  2282. // 拼湊 tsession 紀錄
  2283. $currentGp = $currentCheckinGp;
  2284. $res = [
  2285. 'pid' => $this->getTPidBylineId($lineId),
  2286. 'lineId' => $lineId,
  2287. 'userName' => $name,
  2288. 'rid' => $troundinfo[0]['id'],
  2289. 'gid' => -1, // 簽到專用
  2290. 'cid' => 0,
  2291. 'eventDate' => date("Y-m-d"),
  2292. 'currentGp' => $currentGp,
  2293. 'currentGameGp' => 0,
  2294. 'currentCheckinGp' => $currentCheckinGp,
  2295. 'cdate' => date('Y-m-d H:i:s'),
  2296. ];
  2297. // 重複簽到不會有點數
  2298. if ($currentGp > 0) {
  2299. $this->tsessionManagementDb->insert($res);
  2300. $id = \DB::getPdo()->lastInsertId();
  2301. $res['id'] = $id;
  2302. // 上檔玩家吉點計算
  2303. $res['gp'] = $this->addGpToTPlayer($lineId, $currentGp);
  2304. }
  2305. return $res;
  2306. }
  2307. public function getGpByTCheckins($checkins)
  2308. {
  2309. $info = $this->tcheckinManagementDb->select(['gp'])->where('day', $checkins)->first()->toArray();
  2310. return $info['gp'];
  2311. }
  2312. public function getTPidBylineId($lineId)
  2313. {
  2314. $info = $this->tplayerManagementDb->select(['id'])->where('lineId', $lineId)->first()->toArray();
  2315. return $info['id'];
  2316. }
  2317. public function troundinfo($lineId)
  2318. {
  2319. // 回合資訊
  2320. $troundinfo = $this->troundManagementDb
  2321. ->select(['id', 'dateBegin', 'dateFinal'])
  2322. ->where('dateFinal', '>=', date("Y-m-d"))
  2323. ->where('dateBegin', '<=', date("Y-m-d"))
  2324. ->where('active', '=', GeneralConst::ACTIVE_YES)
  2325. ->get();
  2326. if (count($troundinfo) == 0) {
  2327. return false;
  2328. } else {
  2329. $troundinfo = $troundinfo->toArray();
  2330. }
  2331. // 簽到資訊
  2332. $checkininfo = $this->tsessionManagementDb
  2333. ->select(['eventDate', 'currentGameGp'])
  2334. ->where('lineId', $lineId)
  2335. ->where('rid', $troundinfo[0]['id'])
  2336. ->where('gid', '=', -1) // 僅篩選簽到
  2337. ->orderby('id', 'asc') // 確保按照時間排
  2338. ->get()
  2339. ->toArray();
  2340. // 整理
  2341. $res = [
  2342. 'dateBegin' => $troundinfo[0]['dateBegin'],
  2343. 'dateFinal' => $troundinfo[0]['dateFinal'],
  2344. 'today' => date("Y-m-d"),
  2345. 'checkins' => [],
  2346. ];
  2347. foreach ($checkininfo as $c) {
  2348. // 相同天可能會有重新再玩,因此會有超過1筆資料的可能,不想使用 SQL group by 耗能
  2349. $res['checkins'][ $c['eventDate'] ] = [
  2350. 'date' => $c['eventDate'],
  2351. ];
  2352. }
  2353. $res['checkins'] = array_values($res['checkins']);
  2354. return $res;
  2355. }
  2356. public function addGpToTPlayer($lineId, $gp)
  2357. {
  2358. $info = $this->tplayerManagementDb->select(['gp'])->where('lineId', $lineId)->first()->toArray();
  2359. $gp += $info['gp'];
  2360. $this->tplayerManagementDb->where('lineId', $lineId)->update(['gp' => $gp]);
  2361. return $gp;
  2362. }
  2363. public function isTPlayer($lineId)
  2364. {
  2365. $tplayerinfo = $this->tplayerManagementDb->select(['id'])->where('lineId', $lineId)->get()->toArray();
  2366. return (count($tplayerinfo) > 0);
  2367. }
  2368. public function playTGame($lineId, $name, $c)
  2369. {
  2370. // 回合資訊
  2371. $troundinfo = $this->troundManagementDb
  2372. ->select(['id'])
  2373. ->where('dateFinal', '>=', date("Y-m-d"))
  2374. ->where('dateBegin', '<=', date("Y-m-d"))
  2375. ->where('active', '=', GeneralConst::ACTIVE_YES)
  2376. ->get()
  2377. ->toArray();
  2378. // 一定有回合所以不防呆(參考本函數註解)
  2379. // 檢查本回合玩過沒(同時看 tsession & tplayer,順便取得上檔九宮格分布)
  2380. $playinfo = $this->tsessionManagementDb
  2381. ->select(['currentCheckinGp', 'eventDate'])
  2382. ->where('lineId', $lineId)
  2383. ->where('rid', $troundinfo[0]['id'])
  2384. ->where('gid', '=', $c) // 僅篩選九宮格的這一格
  2385. ->get();
  2386. if (count($playinfo) > 0) return false; // 本周期玩過了
  2387. $playinfo = $this->tplayerManagementDb
  2388. ->select(['c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9'])
  2389. ->where('lineId', $lineId)
  2390. ->first()
  2391. ->toArray();
  2392. $s = [];
  2393. foreach ($playinfo as $k => $v) {
  2394. if ('c' . $c == $k && $v != 0) return false; // 本周期玩過了
  2395. $s[ $k ] = $v;
  2396. }
  2397. // 依照九宮格分布計算本次應得吉點,以及紀錄九宮格狀態變遷
  2398. $gp = 1; // 答對本來就有1點
  2399. $s[ 'c' . $c ] = 2; // 答對初始狀態
  2400. switch ($c) {
  2401. case 1:
  2402. if ($playinfo['c2'] != 0 && $playinfo['c3'] != 0) {
  2403. $gp += 3;
  2404. $s[ 'c' . $c ] = 3;
  2405. $s['c2'] = 3;
  2406. $s['c3'] = 3;
  2407. }
  2408. if ($playinfo['c4'] != 0 && $playinfo['c7'] != 0) {
  2409. $gp += 3;
  2410. $s[ 'c' . $c ] = 3;
  2411. $s['c4'] = 3;
  2412. $s['c7'] = 3;
  2413. }
  2414. if ($playinfo['c5'] != 0 && $playinfo['c9'] != 0) {
  2415. $gp += 3;
  2416. $s[ 'c' . $c ] = 3;
  2417. $s['c5'] = 3;
  2418. $s['c9'] = 3;
  2419. }
  2420. break;
  2421. case 2:
  2422. if ($playinfo['c1'] != 0 && $playinfo['c3'] != 0) {
  2423. $gp += 3;
  2424. $s[ 'c' . $c ] = 3;
  2425. $s['c1'] = 3;
  2426. $s['c3'] = 3;
  2427. }
  2428. if ($playinfo['c5'] != 0 && $playinfo['c8'] != 0) {
  2429. $gp += 3;
  2430. $s[ 'c' . $c ] = 3;
  2431. $s['c5'] = 3;
  2432. $s['c8'] = 3;
  2433. }
  2434. break;
  2435. case 3:
  2436. if ($playinfo['c1'] != 0 && $playinfo['c2'] != 0) {
  2437. $gp += 3;
  2438. $s[ 'c' . $c ] = 3;
  2439. $s['c1'] = 3;
  2440. $s['c2'] = 3;
  2441. }
  2442. if ($playinfo['c6'] != 0 && $playinfo['c9'] != 0) {
  2443. $gp += 3;
  2444. $s[ 'c' . $c ] = 3;
  2445. $s['c6'] = 3;
  2446. $s['c9'] = 3;
  2447. }
  2448. if ($playinfo['c5'] != 0 && $playinfo['c7'] != 0) {
  2449. $gp += 3;
  2450. $s[ 'c' . $c ] = 3;
  2451. $s['c5'] = 3;
  2452. $s['c7'] = 3;
  2453. }
  2454. break;
  2455. case 4:
  2456. if ($playinfo['c1'] != 0 && $playinfo['c7'] != 0) {
  2457. $gp += 3;
  2458. $s[ 'c' . $c ] = 3;
  2459. $s['c1'] = 3;
  2460. $s['c7'] = 3;
  2461. }
  2462. if ($playinfo['c5'] != 0 && $playinfo['c6'] != 0) {
  2463. $gp += 3;
  2464. $s[ 'c' . $c ] = 3;
  2465. $s['c5'] = 3;
  2466. $s['c6'] = 3;
  2467. }
  2468. break;
  2469. case 5:
  2470. if ($playinfo['c4'] != 0 && $playinfo['c6'] != 0) {
  2471. $gp += 3;
  2472. $s[ 'c' . $c ] = 3;
  2473. $s['c4'] = 3;
  2474. $s['c6'] = 3;
  2475. }
  2476. if ($playinfo['c2'] != 0 && $playinfo['c8'] != 0) {
  2477. $gp += 3;
  2478. $s[ 'c' . $c ] = 3;
  2479. $s['c2'] = 3;
  2480. $s['c8'] = 3;
  2481. }
  2482. if ($playinfo['c1'] != 0 && $playinfo['c9'] != 0) {
  2483. $gp += 3;
  2484. $s[ 'c' . $c ] = 3;
  2485. $s['c1'] = 3;
  2486. $s['c9'] = 3;
  2487. }
  2488. if ($playinfo['c3'] != 0 && $playinfo['c7'] != 0) {
  2489. $gp += 3;
  2490. $s[ 'c' . $c ] = 3;
  2491. $s['c3'] = 3;
  2492. $s['c7'] = 3;
  2493. }
  2494. break;
  2495. case 6:
  2496. if ($playinfo['c3'] != 0 && $playinfo['c9'] != 0) {
  2497. $gp += 3;
  2498. $s[ 'c' . $c ] = 3;
  2499. $s['c3'] = 3;
  2500. $s['c9'] = 3;
  2501. }
  2502. if ($playinfo['c4'] != 0 && $playinfo['c5'] != 0) {
  2503. $gp += 3;
  2504. $s[ 'c' . $c ] = 3;
  2505. $s['c4'] = 3;
  2506. $s['c5'] = 3;
  2507. }
  2508. break;
  2509. case 7:
  2510. if ($playinfo['c8'] != 0 && $playinfo['c9'] != 0) {
  2511. $gp += 3;
  2512. $s[ 'c' . $c ] = 3;
  2513. $s['c8'] = 3;
  2514. $s['c9'] = 3;
  2515. }
  2516. if ($playinfo['c1'] != 0 && $playinfo['c4'] != 0) {
  2517. $gp += 3;
  2518. $s[ 'c' . $c ] = 3;
  2519. $s['c1'] = 3;
  2520. $s['c4'] = 3;
  2521. }
  2522. if ($playinfo['c3'] != 0 && $playinfo['c5'] != 0) {
  2523. $gp += 3;
  2524. $s[ 'c' . $c ] = 3;
  2525. $s['c3'] = 3;
  2526. $s['c5'] = 3;
  2527. }
  2528. break;
  2529. case 8:
  2530. if ($playinfo['c2'] != 0 && $playinfo['c5'] != 0) {
  2531. $gp += 3;
  2532. $s[ 'c' . $c ] = 3;
  2533. $s['c2'] = 3;
  2534. $s['c5'] = 3;
  2535. }
  2536. if ($playinfo['c7'] != 0 && $playinfo['c9'] != 0) {
  2537. $gp += 3;
  2538. $s[ 'c' . $c ] = 3;
  2539. $s['c7'] = 3;
  2540. $s['c9'] = 3;
  2541. }
  2542. break;
  2543. case 9:
  2544. if ($playinfo['c7'] != 0 && $playinfo['c8'] != 0) {
  2545. $gp += 3;
  2546. $s[ 'c' . $c ] = 3;
  2547. $s['c7'] = 3;
  2548. $s['c8'] = 3;
  2549. }
  2550. if ($playinfo['c3'] != 0 && $playinfo['c6'] != 0) {
  2551. $gp += 3;
  2552. $s[ 'c' . $c ] = 3;
  2553. $s['c3'] = 3;
  2554. $s['c6'] = 3;
  2555. }
  2556. if ($playinfo['c1'] != 0 && $playinfo['c5'] != 0) {
  2557. $gp += 3;
  2558. $s[ 'c' . $c ] = 3;
  2559. $s['c1'] = 3;
  2560. $s['c5'] = 3;
  2561. }
  2562. break;
  2563. }
  2564. // 拼湊 tsession 紀錄
  2565. $currentGp = $gp;
  2566. $res = [
  2567. 'pid' => $this->getTPidBylineId($lineId),
  2568. 'lineId' => $lineId,
  2569. 'userName' => $name,
  2570. 'rid' => $troundinfo[0]['id'],
  2571. 'gid' => $c,
  2572. 'cid' => 0,
  2573. 'eventDate' => date("Y-m-d"),
  2574. 'currentGp' => $currentGp,
  2575. 'currentGameGp' => $currentGp,
  2576. 'currentCheckinGp' => 0,
  2577. 'cdate' => date('Y-m-d H:i:s'),
  2578. ];
  2579. $this->tsessionManagementDb->insert($res);
  2580. $id = \DB::getPdo()->lastInsertId();
  2581. $res['id'] = $id;
  2582. // 上檔玩家吉點計算+九宮格布局修改
  2583. $res['gp'] = $this->addGpAndJggToTPlayer($lineId, $currentGp, $s);
  2584. return $res;
  2585. }
  2586. public function addGpAndJggToTPlayer($lineId, $gp, $jgg)
  2587. {
  2588. // 計算增加後的吉點
  2589. $info = $this->tplayerManagementDb->select(['gp'])->where('lineId', $lineId)->first()->toArray();
  2590. $gp += $info['gp'];
  2591. $d = ['gp' => $gp];
  2592. $d = array_merge($d, $jgg);
  2593. // 更新
  2594. $this->tplayerManagementDb->where('lineId', $lineId)->update($d);
  2595. return $d;
  2596. }
  2597. public function tredeeminfo($lineId)
  2598. {
  2599. $tplayerinfo = $this->tplayerManagementDb
  2600. ->select(['gp'])
  2601. ->where('lineId', $lineId)
  2602. ->first()
  2603. ->toArray();
  2604. return ($tplayerinfo['gp'] >= $this->settingManagementSv->getSetting()['TMZ_GP_DRAW']);
  2605. }
  2606. public function redeemTGood($lineId, $name)
  2607. {
  2608. $redeeminfo = [];
  2609. try {
  2610. // 回合資訊 (注意用於兌獎資訊的日期區間判定與玩遊戲不同)
  2611. $troundinfo = $this->troundManagementDb
  2612. ->select(['id'])
  2613. ->where(\DB::raw("DATE_ADD(dateFinal, INTERVAL +redeemExtra DAY)"), '>=', date("Y-m-d"))
  2614. ->where('dateBegin', '<=', date("Y-m-d"))
  2615. ->where('active', '=', GeneralConst::ACTIVE_YES)
  2616. ->get()
  2617. ->toArray();
  2618. // 來到這邊代表一定有資料
  2619. // 準備轉蛋資訊
  2620. $tgoodinfo = $this->tgoodManagementDb
  2621. ->select([
  2622. 'id', // 回寫用
  2623. 'ratio', // 抽獎製造冗餘資料用
  2624. 'active', //
  2625. 'lp', //
  2626. 'totalQty', //
  2627. 'issuedQty', //
  2628. ])
  2629. ->get()
  2630. ->toArray();
  2631. $tgoodArr = [];
  2632. foreach ($tgoodinfo as $g) for ($i = 0; $i < $g['ratio']; $i++) $tgoodArr[] = ['id' => $g['id'], 'lp' => $g['lp'], 'totalQty' => $g['totalQty'], 'issuedQty' => $g['issuedQty'], 'active' => $g['active']];
  2633. shuffle($tgoodArr);
  2634. $tgoodinfo = $tgoodArr[0];
  2635. // good 準備資料
  2636. $fromIssuedQty = $tgoodinfo['issuedQty'];
  2637. $toIssuedQty = $fromIssuedQty + 1;
  2638. // player 準備資料
  2639. $tplayerinfo = $this->tplayerManagementDb->select(['gp'])->where('lineId', $lineId)->first()->toArray();
  2640. $fromGp = $tplayerinfo['gp'];
  2641. $toGp = $tplayerinfo['gp'] - $this->settingManagementSv->getSetting()['TMZ_GP_DRAW'];
  2642. // 所有銘謝惠顧的可能(獎項雖然有點數但已被兌換完畢 || 獎項被設定為未啟用)
  2643. if (
  2644. $tgoodinfo['totalQty'] - $tgoodinfo['issuedQty'] <= $this->settingManagementSv->getSetting()['TMZ_ISSUE_BUFFER']
  2645. || $tgoodinfo['active'] == GeneralConst::ACTIVE_NO
  2646. ) {
  2647. return false;
  2648. }
  2649. // 銘謝惠顧獎項還是要扣除集點,所以不能返回錯誤,只是需要先控制不要進入直通
  2650. // 直通打點與確認 (先打點,依照返回正確與否再決定要不要後續操作本地資料)
  2651. if ($this->settingManagementSv->getSetting()['TMZ_ESI_LINEPOINT'] == 'true' && $tgoodinfo['lp'] > 0) {
  2652. // 共用變數
  2653. $uid = $lineId;
  2654. $rqidA = $this->getRqId();
  2655. $rqidB = $this->getRqId();
  2656. $seedKey = $this->getSeedKey();
  2657. $divDataA = $this->getDivData();
  2658. $divDataB = $this->getDivData();
  2659. //
  2660. //
  2661. // 1/2 打點
  2662. //
  2663. //
  2664. // 產生本次調用的 sessionKey
  2665. $sessionKey = $this->getSessionKey($seedKey, $divDataA);
  2666. // 整理押碼用的資料欄內容
  2667. $msgRaw = $this->getMsgRaw([
  2668. 'RqClient' => env('RQ_CLIENT'),
  2669. 'RqId' => $rqidA,
  2670. 'UID' => $uid,
  2671. 'Amount' => $tgoodinfo['lp'],
  2672. ]);
  2673. $msgHash = $this->getMsgHash($msgRaw);
  2674. // 建立ICV
  2675. $icv = $this->getICV();
  2676. // 建立押碼
  2677. $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
  2678. $mac = $this->getMAC($macRaw);
  2679. // 參數打包
  2680. $param = [
  2681. 'RqClient' => env('RQ_CLIENT'),
  2682. 'RqId' => $rqidA,
  2683. 'ICV' => $icv,
  2684. 'DivData' => $divDataA,
  2685. 'MAC' => $mac,
  2686. 'UID' => $uid,
  2687. 'Amount' => $tgoodinfo['lp'],
  2688. ];
  2689. // 相關中繼資料打包(LOG 用)
  2690. $logdata = [
  2691. 'sessionKey' => $sessionKey,
  2692. 'msgRaw' => $msgRaw,
  2693. 'msgHash' => $msgHash,
  2694. 'macRaw' => $macRaw,
  2695. ];
  2696. // 憑證參數整理
  2697. $curlparam = [
  2698. CURLOPT_SSLCERTTYPE => 'PEM',
  2699. CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
  2700. CURLOPT_SSLKEYTYPE => 'PEM',
  2701. CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
  2702. CURLOPT_SSL_VERIFYHOST => 0,
  2703. CURLOPT_SSL_VERIFYPEER => 0,
  2704. ];
  2705. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
  2706. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
  2707. // 直通紀錄
  2708. $data = [
  2709. 'type' => GeneralConst::LOG_ESI,
  2710. 'func' => 'LinepointIssue',
  2711. 'k' => $uid,
  2712. 'kk' => 'tmz',
  2713. 'cdate' => date("Y-m-d H:i:s"),
  2714. ];
  2715. // 調用
  2716. $req = [
  2717. 'curl' => $curlparam,
  2718. 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
  2719. 'headers' => [
  2720. 'Content-Type' => 'application/json',
  2721. 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
  2722. 'Accept' => '*/*',
  2723. ]
  2724. ];
  2725. if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.16.196', '172.31.1.169', '172.31.9.46'])) $req['proxy'] = env('CCH_PROXY');
  2726. $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/linepoint/issue', $req);
  2727. $resA = json_decode($response->getBody());
  2728. $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
  2729. $data['memoOut'] = json_encode($resA, JSON_UNESCAPED_UNICODE);
  2730. $this->syslog($data);
  2731. //
  2732. //
  2733. // 2/2 打點確認
  2734. //
  2735. //
  2736. // 產生本次調用的 sessionKey
  2737. $sessionKey = $this->getSessionKey($seedKey, $divDataB);
  2738. // 整理押碼用的資料欄內容
  2739. $msgRaw = $this->getMsgRaw([
  2740. 'RqClient' => env('RQ_CLIENT'),
  2741. 'RqId' => $rqidB,
  2742. 'QueryRqid' => $rqidA, // 剛才的RQID
  2743. 'UID' => $uid,
  2744. ]);
  2745. $msgHash = $this->getMsgHash($msgRaw);
  2746. // 建立ICV
  2747. $icv = $this->getICV();
  2748. // 建立押碼
  2749. $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
  2750. $mac = $this->getMAC($macRaw);
  2751. // 參數打包
  2752. $param = [
  2753. 'RqClient' => env('RQ_CLIENT'),
  2754. 'RqId' => $rqidB,
  2755. 'ICV' => $icv,
  2756. 'DivData' => $divDataB,
  2757. 'MAC' => $mac,
  2758. 'QueryRqid' => $rqidA, // 剛才的RQID
  2759. 'UID' => $uid,
  2760. ];
  2761. // 相關中繼資料打包(LOG 用)
  2762. $logdata = [
  2763. 'sessionKey' => $sessionKey,
  2764. 'msgRaw' => $msgRaw,
  2765. 'msgHash' => $msgHash,
  2766. 'macRaw' => $macRaw,
  2767. ];
  2768. // 憑證參數整理
  2769. $curlparam = [
  2770. CURLOPT_SSLCERTTYPE => 'PEM',
  2771. CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
  2772. CURLOPT_SSLKEYTYPE => 'PEM',
  2773. CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
  2774. CURLOPT_SSL_VERIFYHOST => 0,
  2775. CURLOPT_SSL_VERIFYPEER => 0,
  2776. ];
  2777. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
  2778. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
  2779. // 直通紀錄
  2780. $data = [
  2781. 'type' => GeneralConst::LOG_ESI,
  2782. 'func' => 'LinepointQuery',
  2783. 'k' => $uid,
  2784. 'kk' => 'tmz',
  2785. 'cdate' => date("Y-m-d H:i:s"),
  2786. ];
  2787. // 調用
  2788. $req = [
  2789. 'curl' => $curlparam,
  2790. 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
  2791. 'headers' => [
  2792. 'Content-Type' => 'application/json',
  2793. 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
  2794. 'Accept' => '*/*',
  2795. ]
  2796. ];
  2797. if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.16.196', '172.31.1.169', '172.31.9.46'])) $req['proxy'] = env('CCH_PROXY');
  2798. $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/linepoint/query', $req);
  2799. $resB = json_decode($response->getBody());
  2800. $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
  2801. $data['memoOut'] = json_encode($resB, JSON_UNESCAPED_UNICODE);
  2802. $this->syslog($data);
  2803. // 打點返回成功,需要確認
  2804. // 確認成功,對用戶返回已打點的資訊,也計入資料庫
  2805. // 確認失敗,對用戶返回已打點的資訊,也計入資料庫(差別只在於 LOG 註記的內容不同)
  2806. // 打點返回失敗,需要確認
  2807. // 確認成功,對用戶返回已打點的資訊,也計入資料庫(差別也只在於 LOG 註記的內容不同)
  2808. // 確認失敗,報錯
  2809. if ($resA->RCode != '0000' && $resB->RCode != '0000') {
  2810. return false;
  2811. }
  2812. }
  2813. // session 插入
  2814. $res = [
  2815. 'pid' => $this->getTPidBylineId($lineId),
  2816. 'lineId' => $lineId,
  2817. 'userName' => $name,
  2818. 'rid' => $troundinfo[0]['id'],
  2819. 'gid' => 0,
  2820. 'cid' => $tgoodinfo['id'], // 獎項的 ID
  2821. 'eventDate' => date("Y-m-d"),
  2822. 'currentGp' => -$this->settingManagementSv->getSetting()['TMZ_GP_DRAW'],
  2823. 'currentGameGp' => 0,
  2824. 'currentCheckinGp' => 0,
  2825. 'cdate' => date('Y-m-d H:i:s'),
  2826. ];
  2827. $this->tsessionManagementDb->insert($res);
  2828. // good 更新資料
  2829. $this->tgoodManagementDb->where('id', $tgoodinfo['id'])->update(['issuedQty' => $toIssuedQty]);
  2830. // tplayer 更新資料
  2831. $this->tplayerManagementDb->where('lineId', $lineId)->update(['gp' => $toGp]);
  2832. // 整理本地紀錄資料
  2833. $redeeminfo = [
  2834. 'playerinfo' => [
  2835. 'lineId' => $lineId,
  2836. 'fromGp' => $fromGp,
  2837. 'toGp' => $toGp,
  2838. ],
  2839. 'redeeminfo' => [
  2840. 'id' => $tgoodinfo['id'],
  2841. 'lp' => $tgoodinfo['lp'],
  2842. 'fromIssuedQty' => $fromIssuedQty,
  2843. 'toIssuedQty' => $toIssuedQty,
  2844. ],
  2845. 'session' => $res,
  2846. ];
  2847. } catch (Exception $ex) {
  2848. // 錯誤可能發生在 memoIn 或是 out
  2849. $data['memoIn'] = json_encode(['error' => 'exception'], JSON_UNESCAPED_UNICODE);
  2850. $data['memoOut'] = json_encode(['code' => $ex->getCode(), 'msg' => $ex->getMessage()], JSON_UNESCAPED_UNICODE);
  2851. $this->syslog($data);
  2852. return false;
  2853. }
  2854. return $redeeminfo;
  2855. }
  2856. }