Api2021Service.php 121KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999
  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. // 替用互打一次性標籤 testonly 暫時關閉
  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. } catch (Exception $ex) {
  949. // 錯誤可能發生在 memoIn 或是 out
  950. $data['memoIn'] = json_encode(['error' => 'exception'], JSON_UNESCAPED_UNICODE);
  951. $data['memoOut'] = json_encode(['code' => $ex->getCode(), 'msg' => $ex->getMessage()], JSON_UNESCAPED_UNICODE);
  952. // $this->syslog($data);
  953. $this->debuglog('playerTag', [
  954. 'rqid' => $rqid ?? 0
  955. ], [
  956. 'errCode' => $ex->getCode(),
  957. 'errMsg' => $ex->getMessage(),
  958. ]);
  959. return false;
  960. }
  961. return true;
  962. }
  963. public function syslog($sysLog)
  964. {
  965. // 此功能暫時只有小遊戲+類發票有,所以偵測 k 值存在才紀錄
  966. if (isset($sysLog['k'])) {
  967. if (!isset($sysLog['kk'])) {
  968. // 偵測到不存在特殊 kk 鍵的請求就進入小遊戲
  969. $this->syslogManagementDb->insert($sysLog);
  970. } else {
  971. // 偵測到存在特殊 kk 鍵的請求就
  972. if ($sysLog['kk'] == 'activity') {
  973. //進入類發票(需要先移除特殊鍵)
  974. unset($sysLog['kk']);
  975. $this->syslogactManagementDb->insert($sysLog);
  976. }
  977. if ($sysLog['kk'] == 'tmz') {
  978. //進入串門子(需要先移除特殊鍵)
  979. unset($sysLog['kk']);
  980. $this->syslogtManagementDb->insert($sysLog);
  981. }
  982. }
  983. }
  984. return true;
  985. }
  986. public function debuglog($func, $dataIn, $dataOut)
  987. {
  988. $this->syslogManagementDb->insert([
  989. 'type' => 'debug',
  990. 'func' => $func,
  991. 'k' => $_SERVER['SERVER_ADDR'],
  992. 'memoIn' => json_encode($dataIn),
  993. 'memoOut' => substr(json_encode($dataOut),0,150),
  994. 'cdate' => date("Y-m-d H:i:s"),
  995. ]);
  996. }
  997. // 直通 API 串聯流程
  998. public function esiFlow()
  999. {
  1000. // 本次調用的參數整理
  1001. $tid = '87B3984D-15EC-480C-8E23-25275F2D2DF2';
  1002. $uid = 'Ud2534d6e1eed9cee2c919a993443352b';
  1003. $rqid = $this->getRqId();
  1004. // 取得 seedKey
  1005. $seedKey = $this->getSeedKey();
  1006. // 隨機產生多樣性資料
  1007. $divData = $this->getDivData();
  1008. // 產生本次調用的 sessionKey
  1009. $sessionKey = $this->getSessionKey($seedKey, $divData);
  1010. // 整理押碼用的資料欄內容
  1011. $msgRaw = $this->getMsgRaw([
  1012. 'RqClient' => env('RQ_CLIENT'),
  1013. 'RqId' => $rqid,
  1014. 'TagInfos' => [
  1015. [
  1016. 'Tagid' => $tid,
  1017. 'UIDs' => [$uid]
  1018. ],
  1019. ],
  1020. ]);
  1021. $msgHash = $this->getMsgHash($msgRaw);
  1022. // 建立ICV
  1023. $icv = $this->getICV();
  1024. // 建立押碼
  1025. $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
  1026. $mac = $this->getMAC($macRaw);
  1027. // 參數打包
  1028. $param = [
  1029. 'RqClient' => env('RQ_CLIENT'),
  1030. 'RqId' => $rqid,
  1031. 'ICV' => $icv,
  1032. 'DivData' => $divData,
  1033. 'MAC' => $mac,
  1034. 'TagInfos' => [
  1035. [
  1036. 'Tagid' => $tid,
  1037. 'UIDs' => [$uid]
  1038. ],
  1039. ],
  1040. ];
  1041. // 相關中繼資料打包(LOG 用)
  1042. $logdata = [
  1043. 'sessionKey' => $sessionKey,
  1044. 'msgRaw' => $msgRaw,
  1045. 'msgHash' => $msgHash,
  1046. 'macRaw' => $macRaw,
  1047. ];
  1048. // 憑證參數整理
  1049. $curlparam = [
  1050. CURLOPT_SSLCERTTYPE => 'PEM',
  1051. CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
  1052. CURLOPT_SSLKEYTYPE => 'PEM',
  1053. CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
  1054. CURLOPT_SSL_VERIFYHOST => 0,
  1055. CURLOPT_SSL_VERIFYPEER => 0,
  1056. ];
  1057. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
  1058. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
  1059. // 調用
  1060. try {
  1061. $req = [
  1062. 'curl' => $curlparam,
  1063. 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
  1064. 'headers' => [
  1065. 'Content-Type' => 'application/json',
  1066. 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
  1067. 'Accept' => '*/*',
  1068. ]
  1069. ];
  1070. if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.16.196', '172.31.1.169', '172.31.9.46'])) $req['proxy'] = env('CCH_PROXY');
  1071. $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/tag/Tagging', $req);
  1072. $res = $response->getBody();
  1073. } catch (Exception $ex) {
  1074. }
  1075. // 直通紀錄
  1076. $data = [
  1077. 'type' => GeneralConst::LOG_ESI,
  1078. 'func' => 'TagTagging',
  1079. 'k' => '',
  1080. 'cdate' => date("Y-m-d H:i:s"),
  1081. ];
  1082. // 經由計算取得的完整 request JSON
  1083. $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
  1084. // 調用 API 取得返回
  1085. $data['memoOut'] = $res;
  1086. // 紀錄
  1087. $this->syslog($data);
  1088. return $res;
  1089. }
  1090. public function esiFlowTagUpdate()
  1091. {
  1092. // 本次調用的參數整理
  1093. $tid = 'f7d27d19-b05e-4ec8-bb49-2c5b0525e095';
  1094. $uid = 'Ud2534d6e1eed9cee2c919a993443352b';
  1095. $rqid = $this->getRqId();
  1096. // 取得 seedKey
  1097. $seedKey = $this->getSeedKey();
  1098. // 隨機產生多樣性資料
  1099. $divData = $this->getDivData();
  1100. // 產生本次調用的 sessionKey
  1101. $sessionKey = $this->getSessionKey($seedKey, $divData);
  1102. // 整理押碼用的資料欄內容
  1103. $msgRaw = $this->getMsgRaw([
  1104. 'RqClient' => env('RQ_CLIENT'),
  1105. 'RqId' => $rqid,
  1106. 'Tags' => [
  1107. [
  1108. 'Tagid' => $tid,
  1109. 'Name' => '奧美測試用標籤' . date("YmdHis"),
  1110. ]
  1111. ],
  1112. ]);
  1113. //$msgRaw = preg_replace('/[\-]/', '', $msgRaw);
  1114. $msgHash = $this->getMsgHash($msgRaw);
  1115. // 建立ICV
  1116. $icv = $this->getICV();
  1117. // 建立押碼
  1118. $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
  1119. $mac = $this->getMAC($macRaw);
  1120. // 參數打包
  1121. $param = [
  1122. 'RqClient' => env('RQ_CLIENT'),
  1123. 'RqId' => $rqid,
  1124. 'ICV' => $icv,
  1125. 'DivData' => $divData,
  1126. 'MAC' => $mac,
  1127. 'Tags' => [
  1128. [
  1129. 'Tagid' => $tid,
  1130. 'Name' => '奧美測試用標籤' . date("YmdHis"),
  1131. ]
  1132. ],
  1133. ];
  1134. // 相關中繼資料打包(LOG 用)
  1135. $logdata = [
  1136. 'sessionKey' => $sessionKey,
  1137. 'msgRaw' => $msgRaw,
  1138. 'msgHash' => $msgHash,
  1139. 'macRaw' => $macRaw,
  1140. ];
  1141. // 憑證參數整理
  1142. $curlparam = [
  1143. CURLOPT_SSLCERTTYPE => 'PEM',
  1144. CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
  1145. CURLOPT_SSLKEYTYPE => 'PEM',
  1146. CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
  1147. CURLOPT_SSL_VERIFYHOST => 0,
  1148. CURLOPT_SSL_VERIFYPEER => 0,
  1149. ];
  1150. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
  1151. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
  1152. // 調用
  1153. $req = [
  1154. 'curl' => $curlparam,
  1155. 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
  1156. 'headers' => [
  1157. 'Content-Type' => 'application/json',
  1158. 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
  1159. 'Accept' => '*/*',
  1160. ]
  1161. ];
  1162. if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.16.196', '172.31.1.169', '172.31.9.46'])) $req['proxy'] = env('CCH_PROXY');
  1163. $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/tag/Update', $req);
  1164. $res = $response->getBody();
  1165. // 直通紀錄
  1166. $data = [
  1167. 'type' => GeneralConst::LOG_ESI,
  1168. 'func' => 'TagUpdate',
  1169. 'k' => '',
  1170. 'cdate' => date("Y-m-d H:i:s"),
  1171. ];
  1172. // 經由計算取得的完整 request JSON
  1173. $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
  1174. // 調用 API 取得返回
  1175. $data['memoOut'] = $res;
  1176. // 紀錄
  1177. $this->syslog($data);
  1178. return [
  1179. 'in' => json_decode($data['memoIn']),
  1180. 'out' => json_decode($data['memoOut']),
  1181. ];
  1182. }
  1183. public function esiFlowTagTagging()
  1184. {
  1185. // 本次調用的參數整理
  1186. $tid = 'f7d27d19-b05e-4ec8-bb49-2c5b0525e095';
  1187. $uid = 'Ud2534d6e1eed9cee2c919a993443352b';
  1188. $rqid = $this->getRqId();
  1189. // 取得 seedKey
  1190. $seedKey = $this->getSeedKey();
  1191. // 隨機產生多樣性資料
  1192. $divData = $this->getDivData();
  1193. // 產生本次調用的 sessionKey
  1194. $sessionKey = $this->getSessionKey($seedKey, $divData);
  1195. // 整理押碼用的資料欄內容
  1196. $msgRaw = $this->getMsgRaw([
  1197. 'RqClient' => env('RQ_CLIENT'),
  1198. 'RqId' => $rqid,
  1199. 'TagInfos' => [
  1200. [
  1201. 'Tagid' => $tid,
  1202. 'UIDs' => [$uid]
  1203. ],
  1204. ],
  1205. ]);
  1206. $msgHash = $this->getMsgHash($msgRaw);
  1207. // 建立ICV
  1208. $icv = $this->getICV();
  1209. // 建立押碼
  1210. $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
  1211. $mac = $this->getMAC($macRaw);
  1212. // 參數打包
  1213. $param = [
  1214. 'RqClient' => env('RQ_CLIENT'),
  1215. 'RqId' => $rqid,
  1216. 'ICV' => $icv,
  1217. 'DivData' => $divData,
  1218. 'MAC' => $mac,
  1219. 'TagInfos' => [
  1220. [
  1221. 'Tagid' => $tid,
  1222. 'UIDs' => [$uid]
  1223. ],
  1224. ],
  1225. ];
  1226. // 相關中繼資料打包(LOG 用)
  1227. $logdata = [
  1228. 'sessionKey' => $sessionKey,
  1229. 'msgRaw' => $msgRaw,
  1230. 'msgHash' => $msgHash,
  1231. 'macRaw' => $macRaw,
  1232. ];
  1233. // 憑證參數整理
  1234. $curlparam = [
  1235. CURLOPT_SSLCERTTYPE => 'PEM',
  1236. CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
  1237. CURLOPT_SSLKEYTYPE => 'PEM',
  1238. CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
  1239. CURLOPT_SSL_VERIFYHOST => 0,
  1240. CURLOPT_SSL_VERIFYPEER => 0,
  1241. ];
  1242. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
  1243. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
  1244. // 調用
  1245. $req = [
  1246. 'curl' => $curlparam,
  1247. 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
  1248. 'headers' => [
  1249. 'Content-Type' => 'application/json',
  1250. 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
  1251. 'Accept' => '*/*',
  1252. ]
  1253. ];
  1254. if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.16.196', '172.31.1.169', '172.31.9.46'])) $req['proxy'] = env('CCH_PROXY');
  1255. $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/tag/Tagging', $req);
  1256. $res = $response->getBody();
  1257. // 直通紀錄
  1258. $data = [
  1259. 'type' => GeneralConst::LOG_ESI,
  1260. 'func' => 'TagTagging',
  1261. 'k' => '',
  1262. 'cdate' => date("Y-m-d H:i:s"),
  1263. ];
  1264. // 經由計算取得的完整 request JSON
  1265. $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
  1266. // 調用 API 取得返回
  1267. $data['memoOut'] = $res;
  1268. // 紀錄
  1269. $this->syslog($data);
  1270. return [
  1271. 'in' => json_decode($data['memoIn']),
  1272. 'out' => json_decode($data['memoOut']),
  1273. ];
  1274. }
  1275. public function esiFlowTagInsert()
  1276. {
  1277. // 本次調用的參數整理
  1278. $tag = '串門子-所有';
  1279. $rqid = $this->getRqId();
  1280. // 取得 seedKey
  1281. $seedKey = $this->getSeedKey();
  1282. // 隨機產生多樣性資料
  1283. $divData = $this->getDivData();
  1284. // 產生本次調用的 sessionKey
  1285. $sessionKey = $this->getSessionKey($seedKey, $divData);
  1286. // 整理押碼用的資料欄內容
  1287. $msgRaw = $this->getMsgRaw([
  1288. 'RqClient' => env('RQ_CLIENT'),
  1289. 'RqId' => $rqid,
  1290. 'Tags' => [['Name' => $tag]],
  1291. ]);
  1292. //$msgRaw = preg_replace('/[\-]/', '', $msgRaw);
  1293. $msgHash = $this->getMsgHash($msgRaw);
  1294. // 建立ICV
  1295. $icv = $this->getICV();
  1296. // 建立押碼
  1297. $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
  1298. $mac = $this->getMAC($macRaw);
  1299. // 參數打包
  1300. $param = [
  1301. 'RqClient' => env('RQ_CLIENT'),
  1302. 'RqId' => $rqid,
  1303. 'ICV' => $icv,
  1304. 'DivData' => $divData,
  1305. 'MAC' => $mac,
  1306. 'Tags' => [['Name' => $tag]],
  1307. ];
  1308. // 相關中繼資料打包(LOG 用)
  1309. $logdata = [
  1310. 'sessionKey' => $sessionKey,
  1311. 'msgRaw' => $msgRaw,
  1312. 'msgHash' => $msgHash,
  1313. 'macRaw' => $macRaw,
  1314. ];
  1315. // 憑證參數整理
  1316. $curlparam = [
  1317. CURLOPT_SSLCERTTYPE => 'PEM',
  1318. CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
  1319. CURLOPT_SSLKEYTYPE => 'PEM',
  1320. CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
  1321. CURLOPT_SSL_VERIFYHOST => 0,
  1322. CURLOPT_SSL_VERIFYPEER => 0,
  1323. ];
  1324. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
  1325. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
  1326. // 調用
  1327. $req = [
  1328. 'curl' => $curlparam,
  1329. 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
  1330. 'headers' => [
  1331. 'Content-Type' => 'application/json',
  1332. 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
  1333. 'Accept' => '*/*',
  1334. ]
  1335. ];
  1336. if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.16.196', '172.31.1.169', '172.31.9.46'])) $req['proxy'] = env('CCH_PROXY');
  1337. $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/tag/Insert', $req);
  1338. $res = $response->getBody();
  1339. // 直通紀錄
  1340. $data = [
  1341. 'type' => GeneralConst::LOG_ESI,
  1342. 'func' => 'TagInsert',
  1343. 'k' => '',
  1344. 'cdate' => date("Y-m-d H:i:s"),
  1345. ];
  1346. // 經由計算取得的完整 request JSON
  1347. $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
  1348. // 調用 API 取得返回
  1349. $data['memoOut'] = $res;
  1350. // 紀錄
  1351. $this->syslog($data);
  1352. return [
  1353. 'in' => json_decode($data['memoIn']),
  1354. 'out' => json_decode($data['memoOut']),
  1355. ];
  1356. }
  1357. public function esiFlowLP()
  1358. {
  1359. // 共用變數
  1360. $uid = "Ub02cb4e598a7d6b3802cc6fb073ce717";
  1361. $rqidA = $this->getRqId();
  1362. $seedKey = $this->getSeedKey();
  1363. $divDataA = $this->getDivData();
  1364. // 產生本次調用的 sessionKey
  1365. $sessionKey = $this->getSessionKey($seedKey, $divDataA);
  1366. // 整理押碼用的資料欄內容
  1367. $msgRaw = $this->getMsgRaw([
  1368. 'RqClient' => env('RQ_CLIENT'),
  1369. 'RqId' => $rqidA,
  1370. 'UID' => $uid,
  1371. 'Amount' => 1,
  1372. ]);
  1373. $msgHash = $this->getMsgHash($msgRaw);
  1374. // 建立ICV
  1375. $icv = $this->getICV();
  1376. // 建立押碼
  1377. $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
  1378. $mac = $this->getMAC($macRaw);
  1379. // 參數打包
  1380. $param = [
  1381. 'RqClient' => env('RQ_CLIENT'),
  1382. 'RqId' => $rqidA,
  1383. 'ICV' => $icv,
  1384. 'DivData' => $divDataA,
  1385. 'MAC' => $mac,
  1386. 'UID' => $uid,
  1387. 'Amount' => 1,
  1388. ];
  1389. // 相關中繼資料打包(LOG 用)
  1390. $logdata = [
  1391. 'sessionKey' => $sessionKey,
  1392. 'msgRaw' => $msgRaw,
  1393. 'msgHash' => $msgHash,
  1394. 'macRaw' => $macRaw,
  1395. ];
  1396. // 憑證參數整理
  1397. $curlparam = [
  1398. CURLOPT_SSLCERTTYPE => 'PEM',
  1399. CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
  1400. CURLOPT_SSLKEYTYPE => 'PEM',
  1401. CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
  1402. CURLOPT_SSL_VERIFYHOST => 0,
  1403. CURLOPT_SSL_VERIFYPEER => 0,
  1404. ];
  1405. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
  1406. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
  1407. // 直通紀錄
  1408. $data = [
  1409. 'type' => GeneralConst::LOG_ESI,
  1410. 'func' => 'LinepointIssue',
  1411. 'k' => $uid,
  1412. 'cdate' => date("Y-m-d H:i:s"),
  1413. ];
  1414. // 調用
  1415. $req = [
  1416. 'curl' => $curlparam,
  1417. 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
  1418. 'headers' => [
  1419. 'Content-Type' => 'application/json',
  1420. 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
  1421. 'Accept' => '*/*',
  1422. ]
  1423. ];
  1424. if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.16.196', '172.31.1.169', '172.31.9.46'])) $req['proxy'] = env('CCH_PROXY');
  1425. $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/linepoint/issue', $req);
  1426. $resA = json_decode($response->getBody());
  1427. $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
  1428. $data['memoOut'] = json_encode($resA, JSON_UNESCAPED_UNICODE);
  1429. $this->syslog($data);
  1430. return [
  1431. 'in' => json_decode($data['memoIn']),
  1432. 'out' => json_decode($data['memoOut']),
  1433. ];
  1434. }
  1435. public function esiFlowBak()
  1436. {
  1437. // 本次調用的參數整理
  1438. $uid = 'Ud2534d6e1eed9cee2c919a993443352b';
  1439. $rqid = $this->getRqId();
  1440. // 取得 seedKey
  1441. $seedKey = $this->getSeedKey();
  1442. // 隨機產生多樣性資料
  1443. $divData = $this->getDivData();
  1444. // 產生本次調用的 sessionKey
  1445. $sessionKey = $this->getSessionKey($seedKey, $divData);
  1446. // 整理押碼用的資料欄內容
  1447. $msgRaw = $this->getMsgRaw([
  1448. 'RqClient' => env('RQ_CLIENT'),
  1449. 'RqId' => $rqid,
  1450. 'UID' => $uid,
  1451. ]);
  1452. $msgHash = $this->getMsgHash($msgRaw);
  1453. // 建立ICV
  1454. $icv = $this->getICV();
  1455. // 建立押碼
  1456. $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
  1457. $mac = $this->getMAC($macRaw);
  1458. // 參數打包
  1459. $param = [
  1460. 'RqClient' => env('RQ_CLIENT'),
  1461. 'RqId' => $rqid,
  1462. 'ICV' => $icv,
  1463. 'DivData' => $divData,
  1464. 'MAC' => $mac,
  1465. 'UID' => $uid,
  1466. ];
  1467. // 相關中繼資料打包(LOG 用)
  1468. $logdata = [
  1469. 'sessionKey' => $sessionKey,
  1470. 'msgRaw' => $msgRaw,
  1471. 'msgHash' => $msgHash,
  1472. 'macRaw' => $macRaw,
  1473. ];
  1474. // 憑證參數整理
  1475. $curlparam = [
  1476. CURLOPT_SSLCERTTYPE => 'PEM',
  1477. CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
  1478. CURLOPT_SSLKEYTYPE => 'PEM',
  1479. CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
  1480. CURLOPT_SSL_VERIFYHOST => 0,
  1481. CURLOPT_SSL_VERIFYPEER => 0,
  1482. ];
  1483. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
  1484. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
  1485. // 調用
  1486. $req = [
  1487. 'curl' => $curlparam,
  1488. 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
  1489. 'headers' => [
  1490. 'Content-Type' => 'application/json',
  1491. 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
  1492. 'Accept' => '*/*',
  1493. ]
  1494. ];
  1495. if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.16.196', '172.31.1.169', '172.31.9.46'])) $req['proxy'] = env('CCH_PROXY');
  1496. $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/contact/QueryQualification', $req);
  1497. $res = $response->getBody();
  1498. // 直通紀錄
  1499. $data = [
  1500. 'type' => GeneralConst::LOG_ESI,
  1501. 'func' => 'QueryQualification',
  1502. 'k' => $uid,
  1503. 'cdate' => date("Y-m-d H:i:s"),
  1504. ];
  1505. // 經由計算取得的完整 request JSON
  1506. $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
  1507. // 調用 API 取得返回
  1508. $data['memoOut'] = $res;
  1509. // 紀錄
  1510. $this->syslog($data);
  1511. // 返回值判斷
  1512. $res = $res . json_encode($req);
  1513. return $res;
  1514. $rcode = $res->RCode ?? '';
  1515. if ($rcode != '0000') return false;
  1516. $isfollow = $res->Result->IsFollow ?? false;
  1517. $ismember = $res->Result->IsTWPMember ?? false;
  1518. if (!$isfollow || !$ismember) return false;
  1519. return true;
  1520. }
  1521. public function getSeedKey()
  1522. {
  1523. // 參照官方文件教學
  1524. // HEX 的 XOR 運算
  1525. $all = strtoupper(bin2hex(pack('H*', env('SEED_KEY_A')) ^ pack('H*', env('SEED_KEY_B'))));
  1526. // 分為前後的 A B 兩段,而不是原先文章定義的 A B 兩個 KEY
  1527. $a = substr($all, 0, strlen($all) / 2);
  1528. $b = substr($all, strlen($all) / 2, strlen($all) / 2);
  1529. // 最後結合的是 A-B-A 段
  1530. return $a . $b . $a;
  1531. }
  1532. public function getSessionKey($seedKey, $divData)
  1533. {
  1534. // 先將 HEX 還原成 BYTE 運算
  1535. $sessionKey = bin2hex(openssl_encrypt(hex2bin($divData), 'DES-EDE3-ECB', hex2bin($seedKey), OPENSSL_RAW_DATA));
  1536. // 按照文件擷取前 24 BYTE (也可以不用)
  1537. $sessionKey = substr($sessionKey, 0, 48);
  1538. // 按照文件轉成大寫 (也可以不用)
  1539. $sessionKey = strtoupper($sessionKey);
  1540. return $sessionKey;
  1541. }
  1542. public function getRqId()
  1543. {
  1544. $data = $data ?? random_bytes(16);
  1545. assert(strlen($data) == 16);
  1546. $data[6] = chr(ord($data[6]) & 0x0f | 0x40);
  1547. $data[8] = chr(ord($data[8]) & 0x3f | 0x80);
  1548. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  1549. }
  1550. public function getDivData()
  1551. {
  1552. return bin2hex(random_bytes(24));
  1553. }
  1554. public function getMsgRaw($msg)
  1555. {
  1556. $ret = '';
  1557. foreach ($msg as $m) {
  1558. $ret .= (is_array($m)) ? json_encode($m, JSON_UNESCAPED_UNICODE) : preg_replace('/[^A-Za-z0-9\-]/', '', $m);
  1559. }
  1560. return $ret;
  1561. }
  1562. public function getMsgHash($msg)
  1563. {
  1564. // 返回可以大寫也可以不用
  1565. return strtoupper(hash('sha256', $msg));
  1566. }
  1567. public function getICV()
  1568. {
  1569. return bin2hex(random_bytes(8));
  1570. }
  1571. public function getMACRaw($msgHash, $sessionKey, $icv)
  1572. {
  1573. // 先將 HEX 還原成 BYTE 運算 (其中 ICV 可以大寫也可以不用)
  1574. $macRaw = bin2hex(openssl_encrypt(hex2bin($msgHash), 'DES-EDE3-CBC', hex2bin($sessionKey), OPENSSL_RAW_DATA, hex2bin($icv)));
  1575. // 返回可以大寫也可以不用
  1576. return strtoupper($macRaw);
  1577. }
  1578. public function getMAC($macRaw)
  1579. {
  1580. // 按照文件擷取前 32 BYTE (必要)
  1581. $macRaw = substr($macRaw, 0, 64);
  1582. // 可以大寫也可以不用
  1583. $macRaw = strtoupper($macRaw);
  1584. return substr($macRaw, strlen($macRaw) - 16, 8);
  1585. }
  1586. // 類發票
  1587. public function setdoc($data)
  1588. {
  1589. // 調用
  1590. $response = $this->imgC->annotateImage(
  1591. $data,
  1592. [Type::TEXT_DETECTION]
  1593. );
  1594. $texts = $response->getTextAnnotations();
  1595. // 整理字串
  1596. $ret = [];
  1597. foreach ($texts as $text) $ret[] = $text->getDescription();
  1598. // 銀行 APP 判別
  1599. $account_bank = '';
  1600. $detect = explode("\n", $ret[0]);
  1601. foreach ($detect as $d) {
  1602. if ($d == '出示退款碼') $account_bank = '007';
  1603. if ($d == '登入/安控') $account_bank = '009';
  1604. if ($d == '繼續台灣 Pay交易') $account_bank = '016';
  1605. if ($d == '我要退款') $account_bank = '017';
  1606. if ($d == '繼續行動支付') $account_bank = '050';
  1607. if ($d == '完成') $account_bank = '806';
  1608. }
  1609. // 整理觸發字串
  1610. $order_no = '';
  1611. $account_no = '';
  1612. $order_date = '';
  1613. switch ($account_bank) {
  1614. case '007':
  1615. for ($i = 0; $i < count($ret); $i++) {
  1616. if ($ret[ $i ] == '訂單') $order_no = $ret[ $i + 2 ] ?? '';
  1617. if ($ret[ $i ] == '帳號') $account_no = $ret[ $i + 1 ] ?? '';
  1618. if (preg_match("/^[0-9]{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
  1619. $order_date = $ret[ $i ] ?? '';
  1620. }
  1621. }
  1622. break;
  1623. case '009':
  1624. for ($i = 0; $i < count($ret); $i++) {
  1625. if ($ret[ $i ] == '訂單') $order_no = $ret[ $i + 2 ] ?? '';
  1626. if ($ret[ $i ] == '帳號') $account_no = $ret[ $i + 1 ] ?? '';
  1627. if (preg_match("/^[0-9]{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
  1628. $order_date = $ret[ $i ] ?? '';
  1629. }
  1630. }
  1631. break;
  1632. case '016':
  1633. for ($i = 0; $i < count($ret); $i++) {
  1634. if ($ret[ $i ] == '訂單') $order_no = $ret[ $i + 2 ] ?? '';
  1635. if ($ret[ $i ] == '帳號') $account_no = $ret[ $i + 1 ] ?? '';
  1636. if (preg_match("/^[0-9]{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
  1637. $order_date = $ret[ $i ] ?? '';
  1638. }
  1639. }
  1640. break;
  1641. case '017':
  1642. for ($i = 0; $i < count($ret); $i++) {
  1643. if ($ret[ $i ] == '訂單') $order_no = $ret[ $i + 2 ] ?? '';
  1644. if ($ret[ $i ] == '方式') $account_no = $ret[ $i + 1 ] ?? '';
  1645. if (preg_match("/^[0-9]{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
  1646. $order_date = $ret[ $i ] ?? '';
  1647. }
  1648. }
  1649. break;
  1650. case '050':
  1651. for ($i = 0; $i < count($ret); $i++) {
  1652. if ($ret[ $i ] == '訂單') $order_no = $ret[ $i + 2 ] ?? '';
  1653. if ($ret[ $i ] == '帳號') $account_no = $ret[ $i + 1 ] ?? '';
  1654. // 沒有時間資訊可取得,這個是虛設
  1655. if (preg_match("/^[0-9]{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
  1656. $order_date = $ret[ $i ] ?? '';
  1657. }
  1658. }
  1659. break;
  1660. case '806':
  1661. for ($i = 0; $i < count($ret); $i++) {
  1662. if ($ret[ $i ] == '編號') $order_no = $ret[ $i + 1 ] ?? '';
  1663. if ($ret[ $i ] == '商戶') $account_no = preg_replace('/-/', "", $ret[ $i - 1 ]) ?? '';
  1664. if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
  1665. $order_date = preg_replace('/-/', "/", $ret[ $i ]) ?? '';
  1666. }
  1667. }
  1668. break;
  1669. case '':
  1670. for ($i = 0; $i < count($ret); $i++) {
  1671. if ($ret[ $i ] == '訂單') $order_no = $ret[ $i + 2 ] ?? '';
  1672. if ($ret[ $i ] == '付款' && $ret[ $i + 1 ] == '帳號') $account_bank = $ret[ $i + 2 ] ?? '';
  1673. if ($ret[ $i ] == '付款' && $ret[ $i + 1 ] == '帳號') $account_no = $ret[ $i + 4 ] ?? '';
  1674. //if ($ret[ $i ] == '時間') $order_date = $ret[ $i + 2 ] ?? '';
  1675. if (preg_match("/^[0-9]{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
  1676. $order_date = $ret[ $i ] ?? '';
  1677. }
  1678. }
  1679. break;
  1680. }
  1681. return [
  1682. 'order_no' => $order_no,
  1683. 'account_bank' => $account_bank,
  1684. 'account_no' => $account_no,
  1685. 'order_date' => $order_date,
  1686. //'debug' => $ret,
  1687. ];
  1688. }
  1689. public function activityinfo()
  1690. {
  1691. // 活動資訊
  1692. $activityinfo = $this->activityManagementDb
  1693. ->select(['id', 'reqTimeBegin', 'reqTimeFinal'])
  1694. ->where('reqTimeFinal', '>=', date("Y-m-d"))
  1695. ->where('reqTimeBegin', '<=', date("Y-m-d"))
  1696. ->where('isDraw', '')
  1697. ->get();
  1698. if (count($activityinfo) == 0) {
  1699. return false;
  1700. } else {
  1701. $activityinfo = $activityinfo->toArray();
  1702. }
  1703. return $activityinfo;
  1704. }
  1705. public function activities()
  1706. {
  1707. // 活動資訊
  1708. $activities = $this->activityManagementDb
  1709. ->select([
  1710. 'id',
  1711. 'activityName',
  1712. 'reqTimeBegin',
  1713. 'reqTimeFinal',
  1714. 'drawTime',
  1715. 'drawNumbers',
  1716. 'isDraw',
  1717. 'redeemTimeBegin',
  1718. 'redeemTimeFinal',
  1719. 'isCheckBegin',
  1720. 'checkTimeBegin',
  1721. 'checkTimeFinal',
  1722. 'getTimeBegin',
  1723. 'getTimeFinal',
  1724. 'isGetFinal',
  1725. ])
  1726. ->get();
  1727. if (count($activities) == 0) {
  1728. return false;
  1729. } else {
  1730. $activities = $activities->toArray();
  1731. }
  1732. return $activities;
  1733. }
  1734. public function reqreceipt($info)
  1735. {
  1736. // 重複登錄資料邏輯判斷
  1737. $r = $this->receiptManagementDb
  1738. ->select(['id'])
  1739. ->where('lineId', $info['lineId'])
  1740. ->where('lineName', $info['name'])
  1741. ->where('tranDate', $info['order_date'])
  1742. ->where('tranBank', \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $info['account_bank'] . "'), \"" . env('KK') . "\"))"))
  1743. ->where('tranAccount', \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $info['account_no'] . "'), \"" . env('KK') . "\"))"))
  1744. ->where('tranOrderNo', \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $info['order_no'] . "'), \"" . env('KK') . "\"))"))
  1745. ->get();
  1746. if (count($r) == 0) return 0;
  1747. // 實際寫入
  1748. $this->receiptManagementDb
  1749. ->insert([
  1750. 'aid' => $info['aid'],
  1751. 'tranDate' => $info['order_date'],
  1752. 'tranBank' => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $info['account_bank'] . "'), \"" . env('KK') . "\"))"),
  1753. 'tranAccount' => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $info['account_no'] . "'), \"" . env('KK') . "\"))"),
  1754. 'tranOrderNo' => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $info['order_no'] . "'), \"" . env('KK') . "\"))"),
  1755. 'lineId' => $info['lineId'],
  1756. 'lineName' => $info['name'],
  1757. 'cdate' => date('Y-m-d H:i:s'),
  1758. 'mdate' => date('Y-m-d H:i:s'),
  1759. 'oid' => 0, // 外部用戶使用0
  1760. ]);
  1761. $id = \DB::getPdo()->lastInsertId();
  1762. return $id;
  1763. }
  1764. public function lstreceipt($lineId)
  1765. {
  1766. // 依照(有限的)不同活動逐次取得該活動的所有紀錄
  1767. $activity = $this->activityManagementDb->select(['id', 'activityName'])->orderby('drawTime', 'desc')->get();
  1768. if (count($activity) == 0) {
  1769. return [];
  1770. } else {
  1771. $activity = $activity->toArray();
  1772. }
  1773. $ret = [];
  1774. // 選欄位
  1775. $rStatusMsg = '';
  1776. $rStatusBtn = '';
  1777. foreach (GeneralConst::$rStatusMap as $k => $v) {
  1778. $rStatusMsg .= ' when \'' . $k . '\' then \'' . $v['front'] . '\'';
  1779. $rStatusBtn .= ' when \'' . $k . '\' then \'' . $v['btn'] . '\'';
  1780. }
  1781. // 逐個活動取得紀錄
  1782. foreach ($activity as $a) {
  1783. $receipt = $this->receiptManagementDb
  1784. ->select([
  1785. 'id',
  1786. \DB::raw("tranDate as order_date"),
  1787. \DB::raw("FROM_BASE64(AES_DECRYPT(UNHEX(tranBank), \"" . env('KK') . "\")) as account_bank"),
  1788. \DB::raw("FROM_BASE64(AES_DECRYPT(UNHEX(tranAccount), \"" . env('KK') . "\")) as account_no"),
  1789. \DB::raw("FROM_BASE64(AES_DECRYPT(UNHEX(tranOrderNo), \"" . env('KK') . "\")) as order_no"),
  1790. 'rStatus',
  1791. \DB::raw("(case rStatus $rStatusMsg end) as rStatusMsg"),
  1792. \DB::raw("(case rStatus $rStatusBtn end) as rStatusBtn"),
  1793. ])
  1794. ->where('lineId', $lineId) // 自己的訂單
  1795. ->where('aid', $a['id']) // 某一期的訂單
  1796. ->orderby('cdate', 'desc')
  1797. ->get();
  1798. if (count($receipt) != 0) {
  1799. $receipt = $receipt->toArray();
  1800. // 依照順序切分(356412978)
  1801. $tmp = [];
  1802. foreach ($receipt as $r) $tmp[ $r['rStatus'] ][] = $r;
  1803. // 整合
  1804. $ret[] = [
  1805. 'activityName' => $a['activityName'],
  1806. 'receipts' => array_merge(
  1807. $tmp[ GeneralConst::RSTATUS_DRAW_DONE ] ?? [],
  1808. $tmp[ GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE ] ?? [],
  1809. $tmp[ GeneralConst::RSTATUS_DRAW_DONE_REDEEM_FAIL ] ?? [],
  1810. $tmp[ GeneralConst::RSTATUS_DRAW_DONE_REDEEM ] ?? [],
  1811. $tmp[ GeneralConst::RSTATUS_INIT ] ?? [],
  1812. $tmp[ GeneralConst::RSTATUS_DRAW_FAIL ] ?? [],
  1813. $tmp[ GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE_GET ] ?? [],
  1814. $tmp[ GeneralConst::RSTATUS_DRAW_DONE_EXPIRED ] ?? [],
  1815. $tmp[ GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE_EXPIRED ] ?? []
  1816. ),
  1817. ];
  1818. }
  1819. }
  1820. return $ret;
  1821. }
  1822. public function redeemCheck($lineId, $rid)
  1823. {
  1824. // 收據資訊
  1825. $receiptinfo = $this->receiptManagementDb
  1826. ->select(['aid'])
  1827. ->where('lineId', $lineId) // 找自己的
  1828. ->where('id', $rid) // 收據
  1829. ->where('rStatus', GeneralConst::RSTATUS_DRAW_DONE) // 可驗證的狀態
  1830. ->get();
  1831. if (count($receiptinfo) == 0) {
  1832. return false;
  1833. } else {
  1834. $receiptinfo = $receiptinfo->toArray();
  1835. }
  1836. // 活動資訊
  1837. $activityinfo = $this->activityManagementDb
  1838. ->select(['id', 'reqTimeBegin', 'reqTimeFinal'])
  1839. ->where('id', $receiptinfo[0]['aid']) // 找該收據活動
  1840. ->where('isDraw', 'Y') // 已經開獎
  1841. ->where('redeemTimeFinal', '>=', date("Y-m-d")) // 且還在可登記的時間範圍內
  1842. ->where('redeemTimeBegin', '<=', date("Y-m-d"))
  1843. ->get();
  1844. if (count($activityinfo) == 0) {
  1845. return false;
  1846. } else {
  1847. return true;
  1848. }
  1849. }
  1850. public function redeemExecute($lineId, $rid)
  1851. {
  1852. $this->receiptManagementDb
  1853. ->where('lineId', $lineId)
  1854. ->where('id', $rid)
  1855. ->where('rStatus', GeneralConst::RSTATUS_DRAW_DONE)
  1856. ->update([
  1857. 'rStatus' => GeneralConst::RSTATUS_DRAW_DONE_REDEEM,
  1858. 'mdate' => date('Y-m-d H:i:s'),
  1859. 'oid' => 0, // 外部用戶使用0
  1860. ]);
  1861. return true;
  1862. }
  1863. public function getCheck($lineId, $rid)
  1864. {
  1865. // 收據資訊
  1866. $receiptinfo = $this->receiptManagementDb
  1867. ->select(['aid'])
  1868. ->where('lineId', $lineId) // 找自己的
  1869. ->where('id', $rid) // 收據
  1870. ->where('rStatus', GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE) // 可領獎的狀態
  1871. ->where('canGet', 'Y') // 可領獎的狀態(輔助狀態)
  1872. ->get();
  1873. if (count($receiptinfo) == 0) {
  1874. return false;
  1875. } else {
  1876. $receiptinfo = $receiptinfo->toArray();
  1877. }
  1878. // 活動資訊
  1879. $activityinfo = $this->activityManagementDb
  1880. ->select(['id', 'reqTimeBegin', 'reqTimeFinal'])
  1881. ->where('id', $receiptinfo[0]['aid']) // 找該收據活動
  1882. ->where('isDraw', 'Y') // 已經開獎
  1883. ->where('getTimeFinal', '>=', date("Y-m-d")) // 且還在可領獎的時間範圍內
  1884. ->where('getTimeBegin', '<=', date("Y-m-d"))
  1885. ->get();
  1886. if (count($activityinfo) == 0) {
  1887. return false;
  1888. } else {
  1889. return true;
  1890. }
  1891. }
  1892. public function getExecute($lineId, $rid)
  1893. {
  1894. try {
  1895. if ($this->settingManagementSv->getSetting()['RECEIPT_ESI_LINEPOINT'] == 'true') {
  1896. // 共用變數
  1897. $uid = $lineId;
  1898. $rqidA = $this->getRqId();
  1899. $rqidB = $this->getRqId();
  1900. $seedKey = $this->getSeedKey();
  1901. $divDataA = $this->getDivData();
  1902. $divDataB = $this->getDivData();
  1903. //
  1904. //
  1905. // 1/2 打點
  1906. //
  1907. //
  1908. // 產生本次調用的 sessionKey
  1909. $sessionKey = $this->getSessionKey($seedKey, $divDataA);
  1910. // 整理押碼用的資料欄內容
  1911. $msgRaw = $this->getMsgRaw([
  1912. 'RqClient' => env('RQ_CLIENT'),
  1913. 'RqId' => $rqidA,
  1914. 'UID' => $uid,
  1915. 'Amount' => 1,
  1916. ]);
  1917. $msgHash = $this->getMsgHash($msgRaw);
  1918. // 建立ICV
  1919. $icv = $this->getICV();
  1920. // 建立押碼
  1921. $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
  1922. $mac = $this->getMAC($macRaw);
  1923. // 參數打包
  1924. $param = [
  1925. 'RqClient' => env('RQ_CLIENT'),
  1926. 'RqId' => $rqidA,
  1927. 'ICV' => $icv,
  1928. 'DivData' => $divDataA,
  1929. 'MAC' => $mac,
  1930. 'UID' => $uid,
  1931. 'Amount' => 1,
  1932. ];
  1933. // 相關中繼資料打包(LOG 用)
  1934. $logdata = [
  1935. 'sessionKey' => $sessionKey,
  1936. 'msgRaw' => $msgRaw,
  1937. 'msgHash' => $msgHash,
  1938. 'macRaw' => $macRaw,
  1939. ];
  1940. // 憑證參數整理
  1941. $curlparam = [
  1942. CURLOPT_SSLCERTTYPE => 'PEM',
  1943. CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
  1944. CURLOPT_SSLKEYTYPE => 'PEM',
  1945. CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
  1946. CURLOPT_SSL_VERIFYHOST => 0,
  1947. CURLOPT_SSL_VERIFYPEER => 0,
  1948. ];
  1949. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
  1950. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
  1951. // 直通紀錄
  1952. $data = [
  1953. 'type' => GeneralConst::LOG_ESI,
  1954. 'func' => 'LinepointIssue',
  1955. 'k' => $uid,
  1956. 'kk' => 'activity',
  1957. 'cdate' => date("Y-m-d H:i:s"),
  1958. ];
  1959. // 調用
  1960. $req = [
  1961. 'curl' => $curlparam,
  1962. 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
  1963. 'headers' => [
  1964. 'Content-Type' => 'application/json',
  1965. 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
  1966. 'Accept' => '*/*',
  1967. ]
  1968. ];
  1969. if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.16.196', '172.31.1.169', '172.31.9.46'])) $req['proxy'] = env('CCH_PROXY');
  1970. $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/linepoint/issue', $req);
  1971. $resA = json_decode($response->getBody());
  1972. $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
  1973. $data['memoOut'] = json_encode($resA, JSON_UNESCAPED_UNICODE);
  1974. $this->syslog($data);
  1975. //
  1976. //
  1977. // 2/2 打點確認
  1978. //
  1979. //
  1980. // 產生本次調用的 sessionKey
  1981. $sessionKey = $this->getSessionKey($seedKey, $divDataB);
  1982. // 整理押碼用的資料欄內容
  1983. $msgRaw = $this->getMsgRaw([
  1984. 'RqClient' => env('RQ_CLIENT'),
  1985. 'RqId' => $rqidB,
  1986. 'QueryRqid' => $rqidA, // 剛才的RQID
  1987. 'UID' => $uid,
  1988. ]);
  1989. $msgHash = $this->getMsgHash($msgRaw);
  1990. // 建立ICV
  1991. $icv = $this->getICV();
  1992. // 建立押碼
  1993. $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
  1994. $mac = $this->getMAC($macRaw);
  1995. // 參數打包
  1996. $param = [
  1997. 'RqClient' => env('RQ_CLIENT'),
  1998. 'RqId' => $rqidB,
  1999. 'ICV' => $icv,
  2000. 'DivData' => $divDataB,
  2001. 'MAC' => $mac,
  2002. 'QueryRqid' => $rqidA, // 剛才的RQID
  2003. 'UID' => $uid,
  2004. ];
  2005. // 相關中繼資料打包(LOG 用)
  2006. $logdata = [
  2007. 'sessionKey' => $sessionKey,
  2008. 'msgRaw' => $msgRaw,
  2009. 'msgHash' => $msgHash,
  2010. 'macRaw' => $macRaw,
  2011. ];
  2012. // 憑證參數整理
  2013. $curlparam = [
  2014. CURLOPT_SSLCERTTYPE => 'PEM',
  2015. CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
  2016. CURLOPT_SSLKEYTYPE => 'PEM',
  2017. CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
  2018. CURLOPT_SSL_VERIFYHOST => 0,
  2019. CURLOPT_SSL_VERIFYPEER => 0,
  2020. ];
  2021. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
  2022. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
  2023. // 直通紀錄
  2024. $data = [
  2025. 'type' => GeneralConst::LOG_ESI,
  2026. 'func' => 'LinepointQuery',
  2027. 'k' => $uid,
  2028. 'kk' => 'activity',
  2029. 'cdate' => date("Y-m-d H:i:s"),
  2030. ];
  2031. // 調用
  2032. $req = [
  2033. 'curl' => $curlparam,
  2034. 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
  2035. 'headers' => [
  2036. 'Content-Type' => 'application/json',
  2037. 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
  2038. 'Accept' => '*/*',
  2039. ]
  2040. ];
  2041. if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.16.196', '172.31.1.169', '172.31.9.46'])) $req['proxy'] = env('CCH_PROXY');
  2042. $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/linepoint/query', $req);
  2043. $resB = json_decode($response->getBody());
  2044. $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
  2045. $data['memoOut'] = json_encode($resB, JSON_UNESCAPED_UNICODE);
  2046. $this->syslog($data);
  2047. // 打點返回成功,需要確認
  2048. // 確認成功,對用戶返回已打點的資訊,也計入資料庫
  2049. // 確認失敗,對用戶返回已打點的資訊,也計入資料庫(差別只在於 LOG 註記的內容不同)
  2050. // 打點返回失敗,需要確認
  2051. // 確認成功,對用戶返回已打點的資訊,也計入資料庫(差別也只在於 LOG 註記的內容不同)
  2052. // 確認失敗,報錯
  2053. if ($resA->RCode != '0000' && $resB->RCode != '0000') {
  2054. return false;
  2055. }
  2056. }
  2057. // 修改資料庫狀態
  2058. $this->receiptManagementDb
  2059. ->where('lineId', $lineId)
  2060. ->where('id', $rid)
  2061. ->where('rStatus', GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE)
  2062. ->update([
  2063. 'rStatus' => GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE_GET,
  2064. 'mdate' => date('Y-m-d H:i:s'),
  2065. 'oid' => 0, // 外部用戶使用0
  2066. ]);
  2067. } catch (Exception $ex) {
  2068. // 錯誤可能發生在 memoIn 或是 out
  2069. $data['memoIn'] = json_encode(['error' => 'exception'], JSON_UNESCAPED_UNICODE);
  2070. $data['memoOut'] = json_encode(['code' => $ex->getCode(), 'msg' => $ex->getMessage()], JSON_UNESCAPED_UNICODE);
  2071. $this->syslog($data);
  2072. return false;
  2073. }
  2074. return true;
  2075. }
  2076. // 串門子
  2077. public function tplayerinfo($lineId)
  2078. {
  2079. // 取得用戶在本地的資訊
  2080. $tplayerinfo = $this->tplayerManagementDb
  2081. ->select(['id'])
  2082. ->where('lineId', $lineId)
  2083. ->get()
  2084. ->toArray();
  2085. // 取得用戶在 LINE 上的 profile
  2086. // $profile = $this->lineProfile($lineId);
  2087. if (count($tplayerinfo) == 0) {
  2088. // 用戶不在本地資料庫,新增
  2089. // 寫入資料表
  2090. $d = [
  2091. 'lineId' => $lineId,
  2092. 'userName' => '',
  2093. 'userPhoto' => '',
  2094. 'c1' => 0,
  2095. 'c2' => 0,
  2096. 'c3' => 0,
  2097. 'c4' => 0,
  2098. 'c5' => 0,
  2099. 'c6' => 0,
  2100. 'c7' => 0,
  2101. 'c8' => 0,
  2102. 'c9' => 0,
  2103. 'gp' => 0,
  2104. 'cdate' => date("Y-m-d H:i:s"),
  2105. 'mdate' => date("Y-m-d H:i:s"),
  2106. ];
  2107. $this->tplayerManagementDb
  2108. ->insert($d);
  2109. $id = \DB::getPdo()->lastInsertId();
  2110. // 替用互打一次性標籤
  2111. $this->tplayerTag($lineId);
  2112. } else {
  2113. // 用戶的 LINE 資訊可能會更新,所以這個 API 還身兼更新資訊的功能
  2114. // $d = [
  2115. // 'userName' => $profile->displayName,
  2116. // 'userPhoto' => $profile->pictureUrl,
  2117. // ];
  2118. // $this->tplayerManagementDb
  2119. // ->where('lineId', $lineId)
  2120. // ->update($d);
  2121. }
  2122. // 再重新查一遍
  2123. $tplayerinfo = $this->tplayerManagementDb
  2124. ->select([
  2125. // 'id',
  2126. 'lineId',
  2127. // 'userName',
  2128. // 'userPhoto',
  2129. 'gp',
  2130. 'c1',
  2131. 'c2',
  2132. 'c3',
  2133. 'c4',
  2134. 'c5',
  2135. 'c6',
  2136. 'c7',
  2137. 'c8',
  2138. 'c9',
  2139. \DB::raw("FLOOR(gp / " . $this->settingManagementSv->getSetting()['TMZ_GP_DRAW'] . ") as draw"),
  2140. // 'cdate',
  2141. // 'mdate',
  2142. ])
  2143. ->where('lineId', $lineId)
  2144. ->first()
  2145. ->toArray();
  2146. return $tplayerinfo;
  2147. }
  2148. public function tplayerTag($lineId)
  2149. {
  2150. try {
  2151. // 本次調用的參數整理
  2152. $tid = $this->settingManagementSv->getSetting()['TMZ_CURRENT_TAG'];
  2153. $uid = $lineId;
  2154. $rqid = $this->getRqId();
  2155. // 取得 seedKey
  2156. $seedKey = $this->getSeedKey();
  2157. // 隨機產生多樣性資料
  2158. $divData = $this->getDivData();
  2159. // 產生本次調用的 sessionKey
  2160. $sessionKey = $this->getSessionKey($seedKey, $divData);
  2161. // 整理押碼用的資料欄內容
  2162. $msgRaw = $this->getMsgRaw([
  2163. 'RqClient' => env('RQ_CLIENT'),
  2164. 'RqId' => $rqid,
  2165. 'TagInfos' => [
  2166. [
  2167. 'Tagid' => $tid,
  2168. 'UIDs' => [$uid]
  2169. ],
  2170. ],
  2171. ]);
  2172. $msgHash = $this->getMsgHash($msgRaw);
  2173. // 建立ICV
  2174. $icv = $this->getICV();
  2175. // 建立押碼
  2176. $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
  2177. $mac = $this->getMAC($macRaw);
  2178. // 參數打包
  2179. $param = [
  2180. 'RqClient' => env('RQ_CLIENT'),
  2181. 'RqId' => $rqid,
  2182. 'ICV' => $icv,
  2183. 'DivData' => $divData,
  2184. 'MAC' => $mac,
  2185. 'TagInfos' => [
  2186. [
  2187. 'Tagid' => $tid,
  2188. 'UIDs' => [$uid]
  2189. ],
  2190. ],
  2191. ];
  2192. // 相關中繼資料打包(LOG 用)
  2193. $logdata = [
  2194. 'sessionKey' => $sessionKey,
  2195. 'msgRaw' => $msgRaw,
  2196. 'msgHash' => $msgHash,
  2197. 'macRaw' => $macRaw,
  2198. ];
  2199. // 憑證參數整理
  2200. $curlparam = [
  2201. CURLOPT_SSLCERTTYPE => 'PEM',
  2202. CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
  2203. CURLOPT_SSLKEYTYPE => 'PEM',
  2204. CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
  2205. CURLOPT_SSL_VERIFYHOST => 0,
  2206. CURLOPT_SSL_VERIFYPEER => 0,
  2207. ];
  2208. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
  2209. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
  2210. // 直通紀錄
  2211. $data = [
  2212. 'type' => GeneralConst::LOG_ESI,
  2213. 'func' => 'TagTagging',
  2214. 'k' => $uid,
  2215. 'kk' => 'tmz',
  2216. 'cdate' => date("Y-m-d H:i:s"),
  2217. ];
  2218. // 調用
  2219. $req = [
  2220. 'curl' => $curlparam,
  2221. 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
  2222. 'headers' => [
  2223. 'Content-Type' => 'application/json',
  2224. 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
  2225. 'Accept' => '*/*',
  2226. ]
  2227. ];
  2228. if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.16.196', '172.31.1.169', '172.31.9.46'])) $req['proxy'] = env('CCH_PROXY');
  2229. $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/tag/Tagging', $req);
  2230. $res = $response->getBody();
  2231. // 經由計算取得的完整 request JSON
  2232. $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
  2233. // 調用 API 取得返回
  2234. $data['memoOut'] = $res;
  2235. // 紀錄
  2236. $this->syslog($data);
  2237. } catch (Exception $ex) {
  2238. // 錯誤可能發生在 memoIn 或是 out
  2239. $data['memoIn'] = json_encode(['error' => 'exception'], JSON_UNESCAPED_UNICODE);
  2240. $data['memoOut'] = json_encode(['code' => $ex->getCode(), 'msg' => $ex->getMessage()], JSON_UNESCAPED_UNICODE);
  2241. $this->syslog($data);
  2242. return false;
  2243. }
  2244. return true;
  2245. }
  2246. public function tcheckin($lineId, $name)
  2247. {
  2248. // 回合資訊
  2249. $troundinfo = $this->troundManagementDb
  2250. ->select(['id'])
  2251. ->where('dateFinal', '>=', date("Y-m-d"))
  2252. ->where('dateBegin', '<=', date("Y-m-d"))
  2253. ->where('active', '=', GeneralConst::ACTIVE_YES)
  2254. ->get()
  2255. ->toArray();
  2256. // 一定有回合所以不防呆(參考本函數註解)
  2257. // 依照簽到狀態決定需不需要簽到 & 決定簽到吉點
  2258. $playinfo = $this->tsessionManagementDb
  2259. ->select(['currentCheckinGp', 'eventDate'])
  2260. ->where('lineId', $lineId)
  2261. ->where('rid', $troundinfo[0]['id'])
  2262. ->where('gid', '=', -1) // 僅篩選簽到
  2263. ->get()
  2264. ->toArray();
  2265. $cancheckin = true;
  2266. $checkins = 0; // 本回合不含這一次,共簽到過幾次
  2267. foreach ($playinfo as $p) {
  2268. // 檢驗當天是否有簽到
  2269. if ($p['eventDate'] == date("Y-m-d")) {
  2270. if ($p['currentCheckinGp'] >= 0) $cancheckin = false;
  2271. }
  2272. // 統計本回合已簽到過幾次
  2273. if ($p['currentCheckinGp'] >= 0) {
  2274. $checkins++;
  2275. }
  2276. }
  2277. //if ($lineId == $this->settingManagementSv->getSetting()['GAME_TESTING_LINE_ID']) $checkins = 0;
  2278. $currentCheckinGp = ($cancheckin) ? $this->getGpByTCheckins($checkins + 1) : 0;
  2279. // 拼湊 tsession 紀錄
  2280. $currentGp = $currentCheckinGp;
  2281. $res = [
  2282. 'pid' => $this->getTPidBylineId($lineId),
  2283. 'lineId' => $lineId,
  2284. 'userName' => $name,
  2285. 'rid' => $troundinfo[0]['id'],
  2286. 'gid' => -1, // 簽到專用
  2287. 'cid' => 0,
  2288. 'eventDate' => date("Y-m-d"),
  2289. 'currentGp' => $currentGp,
  2290. 'currentGameGp' => 0,
  2291. 'currentCheckinGp' => $currentCheckinGp,
  2292. 'cdate' => date('Y-m-d H:i:s'),
  2293. ];
  2294. // 重複簽到不會有點數
  2295. if ($currentGp > 0) {
  2296. $this->tsessionManagementDb->insert($res);
  2297. $id = \DB::getPdo()->lastInsertId();
  2298. $res['id'] = $id;
  2299. // 上檔玩家吉點計算
  2300. $res['gp'] = $this->addGpToTPlayer($lineId, $currentGp);
  2301. }
  2302. return $res;
  2303. }
  2304. public function getGpByTCheckins($checkins)
  2305. {
  2306. $info = $this->tcheckinManagementDb->select(['gp'])->where('day', $checkins)->first()->toArray();
  2307. return $info['gp'];
  2308. }
  2309. public function getTPidBylineId($lineId)
  2310. {
  2311. $info = $this->tplayerManagementDb->select(['id'])->where('lineId', $lineId)->first()->toArray();
  2312. return $info['id'];
  2313. }
  2314. public function troundinfo($lineId)
  2315. {
  2316. // 回合資訊
  2317. $troundinfo = $this->troundManagementDb
  2318. ->select(['id', 'dateBegin', 'dateFinal'])
  2319. ->where('dateFinal', '>=', date("Y-m-d"))
  2320. ->where('dateBegin', '<=', date("Y-m-d"))
  2321. ->where('active', '=', GeneralConst::ACTIVE_YES)
  2322. ->get();
  2323. if (count($troundinfo) == 0) {
  2324. return false;
  2325. } else {
  2326. $troundinfo = $troundinfo->toArray();
  2327. }
  2328. // 簽到資訊
  2329. $checkininfo = $this->tsessionManagementDb
  2330. ->select(['eventDate', 'currentGameGp'])
  2331. ->where('lineId', $lineId)
  2332. ->where('rid', $troundinfo[0]['id'])
  2333. ->where('gid', '=', -1) // 僅篩選簽到
  2334. ->orderby('id', 'asc') // 確保按照時間排
  2335. ->get()
  2336. ->toArray();
  2337. // 整理
  2338. $res = [
  2339. 'dateBegin' => $troundinfo[0]['dateBegin'],
  2340. 'dateFinal' => $troundinfo[0]['dateFinal'],
  2341. 'today' => date("Y-m-d"),
  2342. 'checkins' => [],
  2343. ];
  2344. foreach ($checkininfo as $c) {
  2345. // 相同天可能會有重新再玩,因此會有超過1筆資料的可能,不想使用 SQL group by 耗能
  2346. $res['checkins'][ $c['eventDate'] ] = [
  2347. 'date' => $c['eventDate'],
  2348. ];
  2349. }
  2350. $res['checkins'] = array_values($res['checkins']);
  2351. return $res;
  2352. }
  2353. public function addGpToTPlayer($lineId, $gp)
  2354. {
  2355. $info = $this->tplayerManagementDb->select(['gp'])->where('lineId', $lineId)->first()->toArray();
  2356. $gp += $info['gp'];
  2357. $this->tplayerManagementDb->where('lineId', $lineId)->update(['gp' => $gp]);
  2358. return $gp;
  2359. }
  2360. public function isTPlayer($lineId)
  2361. {
  2362. $tplayerinfo = $this->tplayerManagementDb->select(['id'])->where('lineId', $lineId)->get()->toArray();
  2363. return (count($tplayerinfo) > 0);
  2364. }
  2365. public function playTGame($lineId, $name, $c)
  2366. {
  2367. // 回合資訊
  2368. $troundinfo = $this->troundManagementDb
  2369. ->select(['id'])
  2370. ->where('dateFinal', '>=', date("Y-m-d"))
  2371. ->where('dateBegin', '<=', date("Y-m-d"))
  2372. ->where('active', '=', GeneralConst::ACTIVE_YES)
  2373. ->get()
  2374. ->toArray();
  2375. // 一定有回合所以不防呆(參考本函數註解)
  2376. // 檢查本回合玩過沒(同時看 tsession & tplayer,順便取得上檔九宮格分布)
  2377. $playinfo = $this->tsessionManagementDb
  2378. ->select(['currentCheckinGp', 'eventDate'])
  2379. ->where('lineId', $lineId)
  2380. ->where('rid', $troundinfo[0]['id'])
  2381. ->where('gid', '=', $c) // 僅篩選九宮格的這一格
  2382. ->get();
  2383. if (count($playinfo) > 0) return false; // 本周期玩過了
  2384. $playinfo = $this->tplayerManagementDb
  2385. ->select(['c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9'])
  2386. ->where('lineId', $lineId)
  2387. ->first()
  2388. ->toArray();
  2389. $s = [];
  2390. foreach ($playinfo as $k => $v) {
  2391. if ('c' . $c == $k && $v != 0) return false; // 本周期玩過了
  2392. $s[ $k ] = $v;
  2393. }
  2394. // 依照九宮格分布計算本次應得吉點,以及紀錄九宮格狀態變遷
  2395. $gp = 1; // 答對本來就有1點
  2396. $s[ 'c' . $c ] = 2; // 答對初始狀態
  2397. switch ($c) {
  2398. case 1:
  2399. if ($playinfo['c2'] != 0 && $playinfo['c3'] != 0) {
  2400. $gp += 3;
  2401. $s[ 'c' . $c ] = 3;
  2402. $s['c2'] = 3;
  2403. $s['c3'] = 3;
  2404. }
  2405. if ($playinfo['c4'] != 0 && $playinfo['c7'] != 0) {
  2406. $gp += 3;
  2407. $s[ 'c' . $c ] = 3;
  2408. $s['c4'] = 3;
  2409. $s['c7'] = 3;
  2410. }
  2411. if ($playinfo['c5'] != 0 && $playinfo['c9'] != 0) {
  2412. $gp += 3;
  2413. $s[ 'c' . $c ] = 3;
  2414. $s['c5'] = 3;
  2415. $s['c9'] = 3;
  2416. }
  2417. break;
  2418. case 2:
  2419. if ($playinfo['c1'] != 0 && $playinfo['c3'] != 0) {
  2420. $gp += 3;
  2421. $s[ 'c' . $c ] = 3;
  2422. $s['c1'] = 3;
  2423. $s['c3'] = 3;
  2424. }
  2425. if ($playinfo['c5'] != 0 && $playinfo['c8'] != 0) {
  2426. $gp += 3;
  2427. $s[ 'c' . $c ] = 3;
  2428. $s['c5'] = 3;
  2429. $s['c8'] = 3;
  2430. }
  2431. break;
  2432. case 3:
  2433. if ($playinfo['c1'] != 0 && $playinfo['c2'] != 0) {
  2434. $gp += 3;
  2435. $s[ 'c' . $c ] = 3;
  2436. $s['c1'] = 3;
  2437. $s['c2'] = 3;
  2438. }
  2439. if ($playinfo['c6'] != 0 && $playinfo['c9'] != 0) {
  2440. $gp += 3;
  2441. $s[ 'c' . $c ] = 3;
  2442. $s['c6'] = 3;
  2443. $s['c9'] = 3;
  2444. }
  2445. if ($playinfo['c5'] != 0 && $playinfo['c7'] != 0) {
  2446. $gp += 3;
  2447. $s[ 'c' . $c ] = 3;
  2448. $s['c5'] = 3;
  2449. $s['c7'] = 3;
  2450. }
  2451. break;
  2452. case 4:
  2453. if ($playinfo['c1'] != 0 && $playinfo['c7'] != 0) {
  2454. $gp += 3;
  2455. $s[ 'c' . $c ] = 3;
  2456. $s['c1'] = 3;
  2457. $s['c7'] = 3;
  2458. }
  2459. if ($playinfo['c5'] != 0 && $playinfo['c6'] != 0) {
  2460. $gp += 3;
  2461. $s[ 'c' . $c ] = 3;
  2462. $s['c5'] = 3;
  2463. $s['c6'] = 3;
  2464. }
  2465. break;
  2466. case 5:
  2467. if ($playinfo['c4'] != 0 && $playinfo['c6'] != 0) {
  2468. $gp += 3;
  2469. $s[ 'c' . $c ] = 3;
  2470. $s['c4'] = 3;
  2471. $s['c6'] = 3;
  2472. }
  2473. if ($playinfo['c2'] != 0 && $playinfo['c8'] != 0) {
  2474. $gp += 3;
  2475. $s[ 'c' . $c ] = 3;
  2476. $s['c2'] = 3;
  2477. $s['c8'] = 3;
  2478. }
  2479. if ($playinfo['c1'] != 0 && $playinfo['c9'] != 0) {
  2480. $gp += 3;
  2481. $s[ 'c' . $c ] = 3;
  2482. $s['c1'] = 3;
  2483. $s['c9'] = 3;
  2484. }
  2485. if ($playinfo['c3'] != 0 && $playinfo['c7'] != 0) {
  2486. $gp += 3;
  2487. $s[ 'c' . $c ] = 3;
  2488. $s['c3'] = 3;
  2489. $s['c7'] = 3;
  2490. }
  2491. break;
  2492. case 6:
  2493. if ($playinfo['c3'] != 0 && $playinfo['c9'] != 0) {
  2494. $gp += 3;
  2495. $s[ 'c' . $c ] = 3;
  2496. $s['c3'] = 3;
  2497. $s['c9'] = 3;
  2498. }
  2499. if ($playinfo['c4'] != 0 && $playinfo['c5'] != 0) {
  2500. $gp += 3;
  2501. $s[ 'c' . $c ] = 3;
  2502. $s['c4'] = 3;
  2503. $s['c5'] = 3;
  2504. }
  2505. break;
  2506. case 7:
  2507. if ($playinfo['c8'] != 0 && $playinfo['c9'] != 0) {
  2508. $gp += 3;
  2509. $s[ 'c' . $c ] = 3;
  2510. $s['c8'] = 3;
  2511. $s['c9'] = 3;
  2512. }
  2513. if ($playinfo['c1'] != 0 && $playinfo['c4'] != 0) {
  2514. $gp += 3;
  2515. $s[ 'c' . $c ] = 3;
  2516. $s['c1'] = 3;
  2517. $s['c4'] = 3;
  2518. }
  2519. if ($playinfo['c3'] != 0 && $playinfo['c5'] != 0) {
  2520. $gp += 3;
  2521. $s[ 'c' . $c ] = 3;
  2522. $s['c3'] = 3;
  2523. $s['c5'] = 3;
  2524. }
  2525. break;
  2526. case 8:
  2527. if ($playinfo['c2'] != 0 && $playinfo['c5'] != 0) {
  2528. $gp += 3;
  2529. $s[ 'c' . $c ] = 3;
  2530. $s['c2'] = 3;
  2531. $s['c5'] = 3;
  2532. }
  2533. if ($playinfo['c7'] != 0 && $playinfo['c9'] != 0) {
  2534. $gp += 3;
  2535. $s[ 'c' . $c ] = 3;
  2536. $s['c7'] = 3;
  2537. $s['c9'] = 3;
  2538. }
  2539. break;
  2540. case 9:
  2541. if ($playinfo['c7'] != 0 && $playinfo['c8'] != 0) {
  2542. $gp += 3;
  2543. $s[ 'c' . $c ] = 3;
  2544. $s['c7'] = 3;
  2545. $s['c8'] = 3;
  2546. }
  2547. if ($playinfo['c3'] != 0 && $playinfo['c6'] != 0) {
  2548. $gp += 3;
  2549. $s[ 'c' . $c ] = 3;
  2550. $s['c3'] = 3;
  2551. $s['c6'] = 3;
  2552. }
  2553. if ($playinfo['c1'] != 0 && $playinfo['c5'] != 0) {
  2554. $gp += 3;
  2555. $s[ 'c' . $c ] = 3;
  2556. $s['c1'] = 3;
  2557. $s['c5'] = 3;
  2558. }
  2559. break;
  2560. }
  2561. // 拼湊 tsession 紀錄
  2562. $currentGp = $gp;
  2563. $res = [
  2564. 'pid' => $this->getTPidBylineId($lineId),
  2565. 'lineId' => $lineId,
  2566. 'userName' => $name,
  2567. 'rid' => $troundinfo[0]['id'],
  2568. 'gid' => $c,
  2569. 'cid' => 0,
  2570. 'eventDate' => date("Y-m-d"),
  2571. 'currentGp' => $currentGp,
  2572. 'currentGameGp' => $currentGp,
  2573. 'currentCheckinGp' => 0,
  2574. 'cdate' => date('Y-m-d H:i:s'),
  2575. ];
  2576. $this->tsessionManagementDb->insert($res);
  2577. $id = \DB::getPdo()->lastInsertId();
  2578. $res['id'] = $id;
  2579. // 上檔玩家吉點計算+九宮格布局修改
  2580. $res['gp'] = $this->addGpAndJggToTPlayer($lineId, $currentGp, $s);
  2581. return $res;
  2582. }
  2583. public function addGpAndJggToTPlayer($lineId, $gp, $jgg)
  2584. {
  2585. // 計算增加後的吉點
  2586. $info = $this->tplayerManagementDb->select(['gp'])->where('lineId', $lineId)->first()->toArray();
  2587. $gp += $info['gp'];
  2588. $d = ['gp' => $gp];
  2589. $d = array_merge($d, $jgg);
  2590. // 更新
  2591. $this->tplayerManagementDb->where('lineId', $lineId)->update($d);
  2592. return $d;
  2593. }
  2594. public function tredeeminfo($lineId)
  2595. {
  2596. $tplayerinfo = $this->tplayerManagementDb
  2597. ->select(['gp'])
  2598. ->where('lineId', $lineId)
  2599. ->first()
  2600. ->toArray();
  2601. return ($tplayerinfo['gp'] >= $this->settingManagementSv->getSetting()['TMZ_GP_DRAW']);
  2602. }
  2603. public function redeemTGood($lineId, $name)
  2604. {
  2605. $redeeminfo = [];
  2606. try {
  2607. // 回合資訊 (注意用於兌獎資訊的日期區間判定與玩遊戲不同)
  2608. $troundinfo = $this->troundManagementDb
  2609. ->select(['id'])
  2610. ->where(\DB::raw("DATE_ADD(dateFinal, INTERVAL +redeemExtra DAY)"), '>=', date("Y-m-d"))
  2611. ->where('dateBegin', '<=', date("Y-m-d"))
  2612. ->where('active', '=', GeneralConst::ACTIVE_YES)
  2613. ->get()
  2614. ->toArray();
  2615. // 來到這邊代表一定有資料
  2616. // 準備轉蛋資訊
  2617. $tgoodinfo = $this->tgoodManagementDb
  2618. ->select([
  2619. 'id', // 回寫用
  2620. 'ratio', // 抽獎製造冗餘資料用
  2621. 'active', //
  2622. 'lp', //
  2623. 'totalQty', //
  2624. 'issuedQty', //
  2625. ])
  2626. ->get()
  2627. ->toArray();
  2628. $tgoodArr = [];
  2629. 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']];
  2630. shuffle($tgoodArr);
  2631. $tgoodinfo = $tgoodArr[0];
  2632. // good 準備資料
  2633. $fromIssuedQty = $tgoodinfo['issuedQty'];
  2634. $toIssuedQty = $fromIssuedQty + 1;
  2635. // player 準備資料
  2636. $tplayerinfo = $this->tplayerManagementDb->select(['gp'])->where('lineId', $lineId)->first()->toArray();
  2637. $fromGp = $tplayerinfo['gp'];
  2638. $toGp = $tplayerinfo['gp'] - $this->settingManagementSv->getSetting()['TMZ_GP_DRAW'];
  2639. // 所有銘謝惠顧的可能(獎項雖然有點數但已被兌換完畢 || 獎項被設定為未啟用)
  2640. if (
  2641. $tgoodinfo['totalQty'] - $tgoodinfo['issuedQty'] <= $this->settingManagementSv->getSetting()['TMZ_ISSUE_BUFFER']
  2642. || $tgoodinfo['active'] == GeneralConst::ACTIVE_NO
  2643. ) {
  2644. return false;
  2645. }
  2646. // 銘謝惠顧獎項還是要扣除集點,所以不能返回錯誤,只是需要先控制不要進入直通
  2647. // 直通打點與確認 (先打點,依照返回正確與否再決定要不要後續操作本地資料)
  2648. if ($this->settingManagementSv->getSetting()['TMZ_ESI_LINEPOINT'] == 'true' && $tgoodinfo['lp'] > 0) {
  2649. // 共用變數
  2650. $uid = $lineId;
  2651. $rqidA = $this->getRqId();
  2652. $rqidB = $this->getRqId();
  2653. $seedKey = $this->getSeedKey();
  2654. $divDataA = $this->getDivData();
  2655. $divDataB = $this->getDivData();
  2656. //
  2657. //
  2658. // 1/2 打點
  2659. //
  2660. //
  2661. // 產生本次調用的 sessionKey
  2662. $sessionKey = $this->getSessionKey($seedKey, $divDataA);
  2663. // 整理押碼用的資料欄內容
  2664. $msgRaw = $this->getMsgRaw([
  2665. 'RqClient' => env('RQ_CLIENT'),
  2666. 'RqId' => $rqidA,
  2667. 'UID' => $uid,
  2668. 'Amount' => $tgoodinfo['lp'],
  2669. ]);
  2670. $msgHash = $this->getMsgHash($msgRaw);
  2671. // 建立ICV
  2672. $icv = $this->getICV();
  2673. // 建立押碼
  2674. $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
  2675. $mac = $this->getMAC($macRaw);
  2676. // 參數打包
  2677. $param = [
  2678. 'RqClient' => env('RQ_CLIENT'),
  2679. 'RqId' => $rqidA,
  2680. 'ICV' => $icv,
  2681. 'DivData' => $divDataA,
  2682. 'MAC' => $mac,
  2683. 'UID' => $uid,
  2684. 'Amount' => $tgoodinfo['lp'],
  2685. ];
  2686. // 相關中繼資料打包(LOG 用)
  2687. $logdata = [
  2688. 'sessionKey' => $sessionKey,
  2689. 'msgRaw' => $msgRaw,
  2690. 'msgHash' => $msgHash,
  2691. 'macRaw' => $macRaw,
  2692. ];
  2693. // 憑證參數整理
  2694. $curlparam = [
  2695. CURLOPT_SSLCERTTYPE => 'PEM',
  2696. CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
  2697. CURLOPT_SSLKEYTYPE => 'PEM',
  2698. CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
  2699. CURLOPT_SSL_VERIFYHOST => 0,
  2700. CURLOPT_SSL_VERIFYPEER => 0,
  2701. ];
  2702. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
  2703. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
  2704. // 直通紀錄
  2705. $data = [
  2706. 'type' => GeneralConst::LOG_ESI,
  2707. 'func' => 'LinepointIssue',
  2708. 'k' => $uid,
  2709. 'kk' => 'tmz',
  2710. 'cdate' => date("Y-m-d H:i:s"),
  2711. ];
  2712. // 調用
  2713. $req = [
  2714. 'curl' => $curlparam,
  2715. 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
  2716. 'headers' => [
  2717. 'Content-Type' => 'application/json',
  2718. 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
  2719. 'Accept' => '*/*',
  2720. ]
  2721. ];
  2722. if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.16.196', '172.31.1.169', '172.31.9.46'])) $req['proxy'] = env('CCH_PROXY');
  2723. $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/linepoint/issue', $req);
  2724. $resA = json_decode($response->getBody());
  2725. $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
  2726. $data['memoOut'] = json_encode($resA, JSON_UNESCAPED_UNICODE);
  2727. $this->syslog($data);
  2728. //
  2729. //
  2730. // 2/2 打點確認
  2731. //
  2732. //
  2733. // 產生本次調用的 sessionKey
  2734. $sessionKey = $this->getSessionKey($seedKey, $divDataB);
  2735. // 整理押碼用的資料欄內容
  2736. $msgRaw = $this->getMsgRaw([
  2737. 'RqClient' => env('RQ_CLIENT'),
  2738. 'RqId' => $rqidB,
  2739. 'QueryRqid' => $rqidA, // 剛才的RQID
  2740. 'UID' => $uid,
  2741. ]);
  2742. $msgHash = $this->getMsgHash($msgRaw);
  2743. // 建立ICV
  2744. $icv = $this->getICV();
  2745. // 建立押碼
  2746. $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
  2747. $mac = $this->getMAC($macRaw);
  2748. // 參數打包
  2749. $param = [
  2750. 'RqClient' => env('RQ_CLIENT'),
  2751. 'RqId' => $rqidB,
  2752. 'ICV' => $icv,
  2753. 'DivData' => $divDataB,
  2754. 'MAC' => $mac,
  2755. 'QueryRqid' => $rqidA, // 剛才的RQID
  2756. 'UID' => $uid,
  2757. ];
  2758. // 相關中繼資料打包(LOG 用)
  2759. $logdata = [
  2760. 'sessionKey' => $sessionKey,
  2761. 'msgRaw' => $msgRaw,
  2762. 'msgHash' => $msgHash,
  2763. 'macRaw' => $macRaw,
  2764. ];
  2765. // 憑證參數整理
  2766. $curlparam = [
  2767. CURLOPT_SSLCERTTYPE => 'PEM',
  2768. CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
  2769. CURLOPT_SSLKEYTYPE => 'PEM',
  2770. CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
  2771. CURLOPT_SSL_VERIFYHOST => 0,
  2772. CURLOPT_SSL_VERIFYPEER => 0,
  2773. ];
  2774. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
  2775. if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
  2776. // 直通紀錄
  2777. $data = [
  2778. 'type' => GeneralConst::LOG_ESI,
  2779. 'func' => 'LinepointQuery',
  2780. 'k' => $uid,
  2781. 'kk' => 'tmz',
  2782. 'cdate' => date("Y-m-d H:i:s"),
  2783. ];
  2784. // 調用
  2785. $req = [
  2786. 'curl' => $curlparam,
  2787. 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
  2788. 'headers' => [
  2789. 'Content-Type' => 'application/json',
  2790. 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
  2791. 'Accept' => '*/*',
  2792. ]
  2793. ];
  2794. if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.16.196', '172.31.1.169', '172.31.9.46'])) $req['proxy'] = env('CCH_PROXY');
  2795. $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/linepoint/query', $req);
  2796. $resB = json_decode($response->getBody());
  2797. $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
  2798. $data['memoOut'] = json_encode($resB, JSON_UNESCAPED_UNICODE);
  2799. $this->syslog($data);
  2800. // 打點返回成功,需要確認
  2801. // 確認成功,對用戶返回已打點的資訊,也計入資料庫
  2802. // 確認失敗,對用戶返回已打點的資訊,也計入資料庫(差別只在於 LOG 註記的內容不同)
  2803. // 打點返回失敗,需要確認
  2804. // 確認成功,對用戶返回已打點的資訊,也計入資料庫(差別也只在於 LOG 註記的內容不同)
  2805. // 確認失敗,報錯
  2806. if ($resA->RCode != '0000' && $resB->RCode != '0000') {
  2807. return false;
  2808. }
  2809. }
  2810. // session 插入
  2811. $res = [
  2812. 'pid' => $this->getTPidBylineId($lineId),
  2813. 'lineId' => $lineId,
  2814. 'userName' => $name,
  2815. 'rid' => $troundinfo[0]['id'],
  2816. 'gid' => 0,
  2817. 'cid' => $tgoodinfo['id'], // 獎項的 ID
  2818. 'eventDate' => date("Y-m-d"),
  2819. 'currentGp' => -$this->settingManagementSv->getSetting()['TMZ_GP_DRAW'],
  2820. 'currentGameGp' => 0,
  2821. 'currentCheckinGp' => 0,
  2822. 'cdate' => date('Y-m-d H:i:s'),
  2823. ];
  2824. $this->tsessionManagementDb->insert($res);
  2825. // good 更新資料
  2826. $this->tgoodManagementDb->where('id', $tgoodinfo['id'])->update(['issuedQty' => $toIssuedQty]);
  2827. // tplayer 更新資料
  2828. $this->tplayerManagementDb->where('lineId', $lineId)->update(['gp' => $toGp]);
  2829. // 整理本地紀錄資料
  2830. $redeeminfo = [
  2831. 'playerinfo' => [
  2832. 'lineId' => $lineId,
  2833. 'fromGp' => $fromGp,
  2834. 'toGp' => $toGp,
  2835. ],
  2836. 'redeeminfo' => [
  2837. 'id' => $tgoodinfo['id'],
  2838. 'lp' => $tgoodinfo['lp'],
  2839. 'fromIssuedQty' => $fromIssuedQty,
  2840. 'toIssuedQty' => $toIssuedQty,
  2841. ],
  2842. 'session' => $res,
  2843. ];
  2844. } catch (Exception $ex) {
  2845. // 錯誤可能發生在 memoIn 或是 out
  2846. $data['memoIn'] = json_encode(['error' => 'exception'], JSON_UNESCAPED_UNICODE);
  2847. $data['memoOut'] = json_encode(['code' => $ex->getCode(), 'msg' => $ex->getMessage()], JSON_UNESCAPED_UNICODE);
  2848. $this->syslog($data);
  2849. return false;
  2850. }
  2851. return $redeeminfo;
  2852. }
  2853. }