12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068 |
- <?php
-
- namespace App\Http\Services\Web;
-
- use Exception;
- use App\Models\Web\Event;
- use App\Models\Web\Item;
- use App\Models\Web\Submit;
- use Google\Cloud\Vision\V1\Feature\Type;
- use Google\Cloud\Vision\V1\ImageAnnotatorClient;
- use GuzzleHttp\Client;
- use App\Http\Services\ConstDef\GeneralConst;
- use Illuminate\Support\Facades\DB;
- use Dompdf\Dompdf;
- use Dompdf\Options;
- use App\Models\Web\Syslog;
- use App\Models\Web\Syslogact;
- use App\Models\Web\Syslogt;
- use App\Models\Web\Player;
- use App\Models\Web\Round;
- use App\Models\Web\Good;
- use App\Models\Web\Session;
- use App\Models\Web\CheckinGpAlloc;
- use App\Models\Web\GameGpRatio;
- use App\Models\Web\Activity;
- use App\Models\Web\Receipt;
- use App\Models\Web\TPlayer;
- use App\Models\Web\TRound;
- use App\Models\Web\TGood;
- use App\Models\Web\TSession;
- use App\Models\Web\TCheckinGpAlloc;
- use App\Models\Web\AuthLineUsers;
-
- use App\Http\Services\Backend\DataManagement\SettingManagementService;
-
- class Api2021Service
- {
-
- private $c;
- private $eventManagementDb;
- private $itemManagementDb;
- private $submitManagementDb;
- private $syslogManagementDb;
- private $syslogactManagementDb;
- private $syslogtManagementDb;
- private $playerManagementDb;
- private $roundManagementDb;
- private $goodManagementDb;
- private $sessionManagementDb;
- private $checkinManagementDb;
- private $gameManagementDb;
- private $activityManagementDb;
- private $receiptManagementDb;
- private $tplayerManagementDb;
- private $troundManagementDb;
- private $tgoodManagementDb;
- private $tsessionManagementDb;
- private $tcheckinManagementDb;
- private $authLineUsersDb;
- private $settingManagementSv;
- private $imgC;
- private $basedir;
-
- public function __construct()
- {
- date_default_timezone_set("Asia/Taipei");
- $this->c = new Client();
- // 建構 model 調用器
- $this->eventManagementDb = new Event();
- $this->itemManagementDb = new Item();
- $this->submitManagementDb = new Submit();
- $this->syslogManagementDb = new Syslog();
- $this->syslogactManagementDb = new Syslogact();
- $this->syslogtManagementDb = new Syslogt();
- $this->playerManagementDb = new Player();
- $this->roundManagementDb = new Round();
- $this->goodManagementDb = new Good();
- $this->sessionManagementDb = new Session();
- $this->checkinManagementDb = new CheckinGpAlloc();
- $this->gameManagementDb = new GameGpRatio();
- $this->activityManagementDb = new Activity();
- $this->receiptManagementDb = new Receipt();
- $this->tplayerManagementDb = new TPlayer();
- $this->troundManagementDb = new TRound();
- $this->tsessionManagementDb = new TSession();
- $this->tgoodManagementDb = new TGood();
- $this->tcheckinManagementDb = new TCheckinGpAlloc();
- $this->authLineUsersDb = new AuthLineUsers();
- $this->settingManagementSv = new SettingManagementService();
- $this->settingManagementSv = new SettingManagementService();
- $this->basedir = preg_replace('/\/app\/.*/', '/public/', __DIR__);
- $this->imgC = new ImageAnnotatorClient([
- 'credentials' => $this->basedir . 'private/google.json',
- ]);
- }
-
- // 申報系統(已廢棄)
-
- public function hashParse($hash)
- {
- $hash = \DB::select("SELECT FROM_BASE64(AES_DECRYPT(UNHEX(\"" . $hash . "\"), \"" . env('KK') . "\")) as str;");
-
- return explode(',', $hash[0]->str);
- }
-
- public function hashCheck($hash)
- {
- // 格式判斷
- if (count($hash) != 2) return false;
- // 業務邏輯判斷
- $items = $this->eventManagementDb
- ->leftJoin('items', 'events.id', '=', 'items.eid')
- ->select([
- 'events.archive',
- ])
- ->where('events.id', $hash[0])
- ->where('items.id', $hash[1])
- ->where('events.date_begin', '<=', date("Y-m-d"))
- ->where('events.date_final', '>=', date("Y-m-d"))
- ->get()
- ->toArray();
- if (count($items) == 0) return false;
-
- return ($items[0]['archive'] == GeneralConst::ARCHIVE_NO) ? true : false;
- }
-
- public function getinfo($hash)
- {
- // 業務邏輯判斷
- $info = $this->eventManagementDb
- ->leftJoin('items', 'events.id', '=', 'items.eid')
- ->select([
- 'events.name',
- 'events.kv',
- 'events.email',
- 'events.tel',
- \DB::raw("items.name as item_name"),
- 'items.is_ide',
- 'items.id_acc',
- ])
- ->where('events.id', $hash[0])
- ->where('items.id', $hash[1])
- ->where('events.date_begin', '<=', date("Y-m-d"))
- ->where('events.date_final', '>=', date("Y-m-d"))
- ->first()
- ->toArray();
-
- return $info;
- }
-
- public function makePDF($info, $name, $identity, $add_host, $add_contact, $tel, $ida, $idb, $acc)
- {
- // 初期設定
- $options = new Options();
- $options->set('isRemoteEnabled', true);
- $options->set('chroot', '/');
- $dompdf = new Dompdf();
- $dompdf->setOptions($options);
- // 內容拼湊
- $html =
- '<!doctype html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><style> .font-zh { font-family: "msjh" } </style></head><body>' .
- '<span class="font-zh">申報資料</span><hr>' .
- '<span class="font-zh">姓名: ' . $name . '</span><br>' .
- '<span class="font-zh">身分證字號: ' . $identity . '</span><br>' .
- '<span class="font-zh">戶籍地址: ' . $add_host . '</span><br>' .
- '<span class="font-zh">通訊地址: ' . $add_contact . '</span><br>' .
- '<span class="font-zh">電話: ' . $tel . '</span><br>' .
- '<span class="font-zh"><br>' .
- '<span class="font-zh">檢附文件:</span><hr>' .
- '[is_ide]' .
- '[id_acc]' .
- '</body></html>';
- if ($info['is_ide'] == GeneralConst::PHOTO_YES) {
- $html = str_replace('[is_ide]',
- '<div class="page_break"><span class="font-zh">身分證正面</span><br>' .
- '<img src="' . env('LOCAL_PATH') . 'public/' . $ida . '" /></div>' .
- '<div class="page_break"><span class="font-zh">身分證背面</span><br>' .
- '<img src="' . env('LOCAL_PATH') . 'public/' . $idb . '" /></div>'
- , $html);
- } else {
- $html = str_replace('[is_ide]', '', $html);
- }
- if ($info['id_acc'] == GeneralConst::PHOTO_YES) {
- $html = str_replace('[id_acc]',
- '<div class="page_break"><span class="font-zh">存摺</span><br>' .
- '<img src="' . env('LOCAL_PATH') . 'public/' . $acc . '" /></div>'
- , $html);
- } else {
- $html = str_replace('[id_acc]', '', $html);
- }
- $dompdf->setPaper('A4');
- $dompdf->loadHtml($html, 'UTF-8');
- $dompdf->render();
- $output = $dompdf->output();
- // 路徑命名
- $dirname = 'material';
- $date = date("Ymd");
- if (!file_exists($dirname)) mkdir($dirname, 0777, true);
- if (!file_exists($dirname . '/' . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['web_path'])) mkdir($dirname . '/' . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['web_path'], 0777, true);
- 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);
- $basepath = $dirname . '/' . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['web_path'] . '/' . $date . '/';
- $time = md5(bcrypt(microtime()));
- file_put_contents($basepath . $time . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['ext'], $output);
-
- return $basepath . $time . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['ext'];
- }
-
- public function savedata($iid, $name, $identity, $add_host, $add_contact, $tel, $ida, $idb, $acc, $pdf)
- {
- // 取得參數
- // 調用資料庫(或者其他業務邏輯)
- $this->submitManagementDb
- ->insert([
- 'iid' => $iid,
- 'name' => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $name . "'), \"" . env('KK') . "\"))"),
- 'identity' => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $identity . "'), \"" . env('KK') . "\"))"),
- 'add_host' => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $add_host . "'), \"" . env('KK') . "\"))"),
- 'add_contact' => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $add_contact . "'), \"" . env('KK') . "\"))"),
- 'tel' => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $tel . "'), \"" . env('KK') . "\"))"),
- GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_FRONT ]['sql'] => $ida,
- GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_BACK ]['sql'] => $idb,
- GeneralConst::$appendixMap[ GeneralConst::APPENDIX_PASSBOOK ]['sql'] => $acc,
- GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['sql'] => $pdf,
- 'cdate' => date('Y-m-d H:i:s'),
- ]);
- $id = \DB::getPdo()->lastInsertId();
-
- // 整理返回值並返回
- return $id;
- }
-
- // 小遊戲
-
- // LINE 好友判斷 (實作介接直通部分)
- public function lineFriendCheck($lineId, $from = null)
- {
- try {
- // 本次調用的參數整理
- $uid = $lineId;
- $rqid = $this->getRqId();
- // 取得 seedKey
- $seedKey = $this->getSeedKey();
- // 隨機產生多樣性資料
- $divData = $this->getDivData();
- // 產生本次調用的 sessionKey
- $sessionKey = $this->getSessionKey($seedKey, $divData);
- // 整理押碼用的資料欄內容
- $msgRaw = $this->getMsgRaw([
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqid,
- 'UID' => $uid,
- ]);
- $msgHash = $this->getMsgHash($msgRaw);
- // 建立ICV
- $icv = $this->getICV();
- // 建立押碼
- $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
- $mac = $this->getMAC($macRaw);
- // 參數打包
- $param = [
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqid,
- 'ICV' => $icv,
- 'DivData' => $divData,
- 'MAC' => $mac,
- 'UID' => $uid,
- ];
- // 相關中繼資料打包(LOG 用)
- $logdata = [
- 'sessionKey' => $sessionKey,
- 'msgRaw' => $msgRaw,
- 'msgHash' => $msgHash,
- 'macRaw' => $macRaw,
- ];
- // 憑證參數整理
- $curlparam = [
- CURLOPT_SSLCERTTYPE => 'PEM',
- CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
- CURLOPT_SSLKEYTYPE => 'PEM',
- CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
- CURLOPT_SSL_VERIFYHOST => 0,
- CURLOPT_SSL_VERIFYPEER => 0,
- ];
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
- // 調用
- $req = [
- 'curl' => $curlparam,
- 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
- 'Accept' => '*/*',
- ],
- 'timeout' => 15, // Response timeout
- 'connect_timeout' => 10, // Connection timeout
- // 'on_stats' => function (\GuzzleHttp\TransferStats $stats) use ($rqid) {
- // if ($stats->hasResponse()) {
- // $this->debuglog('lineFriendCheck-on_stats_ok', [
- // 'rqid' => $rqid ?? 0
- // ], [
- // 'transferTime' => $stats->getTransferTime(),
- // 'handlerStats' => $stats->getHandlerStats(),
- // ]);
- // } else {
- // $this->debuglog('lineFriendCheck-on_stats_ng', [
- // 'rqid' => $rqid ?? 0
- // ], [
- // 'handlerErrorData' => $stats->getHandlerErrorData(),
- // ]);
- // }
- // }
- ];
-
- if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
-
- $requestTime = date("Y-m-d H:i:s");
- $logdata['requestTime'] = $requestTime;
-
- $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/contact/QueryQualification', $req);
- $res = $response->getBody();
-
- // 直通紀錄
- $data = [
- 'type' => GeneralConst::LOG_ESI,
- 'func' => 'QueryQualification',
- 'k' => $uid,
- 'cdate' => date("Y-m-d H:i:s"),
- ];
- if ($from) $data['kk'] = $from; // 區分來源(紀錄的 log 表不同)
-
- $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
- $data['memoOut'] = $res;
- $this->syslog($data);
-
- // 返回值判斷
- $res = json_decode($res);
- $rcode = $res->RCode ?? '';
- if ($rcode != '0000') return false;
- $isfollow = $res->Result->IsFollow ?? false;
- $ismember = $res->Result->IsTWPMember ?? false;
- if (!$isfollow || !$ismember) return false;
- } catch (Exception $ex) {
- // 錯誤可能發生在 memoIn 或是 out
- // $data['memoIn'] = json_encode(['error' => 'exception', 'param' => array_merge($param, $logdata)], JSON_UNESCAPED_UNICODE);
- // $data['memoOut'] = json_encode(['code' => $ex->getCode(), 'msg' => $ex->getMessage()], JSON_UNESCAPED_UNICODE);
- // $this->syslog($data);
-
- $this->debuglog('lineFriendCheck', [
- 'rqid' => $rqid ?? 0
- ], [
- 'errCode' => $ex->getCode(),
- 'requestTime' => $requestTime ?? '',
- 'logTime' => date("Y-m-d H:i:s"),
- 'errMsg' => $ex->getMessage(),
- ]);
-
- return false;
- }
-
- return true;
- }
-
- public function isAuthUserExists($lineUserId)
- {
- return $this->authLineUsersDb->where('lineUserId', $lineUserId)->exists();
- }
-
- public function addAuthUser($lineUserId)
- {
- $this->authLineUsersDb->insert([
- 'lineUserId' => $lineUserId
- ]);
- }
-
- // LINE 取得個資(建立在好友判斷後才可以執行)
- public function lineProfile($lineId)
- {
- $response = $this->c->request('GET', env('LINE_API_BASE') . 'bot/profile/' . $lineId, [
- 'headers' => [
- 'Content-Type' => 'application/json; charset=utf-8',
- 'Accept' => '*/*',
- 'Authorization' => 'Bearer ' . env('LINE_CHANNEL_TOKEN'),
- 'Accept-Encoding' => 'gzip, deflate, br',
- ]
- ]);
- $profile = json_decode($response->getBody());
-
- return $profile;
- }
-
- // 新增或取得用戶資訊
- public function playerinfo($lineId)
- {
- // 取得用戶在本地的資訊
- $playerinfo = $this->playerManagementDb
- ->select(['id'])
- ->where('lineId', $lineId)
- ->get()
- ->toArray();
-
- // 取得用戶在 LINE 上的 profile
- // $profile = $this->lineProfile($lineId);
-
- if (count($playerinfo) == 0) {
- // 用戶不在本地資料庫,新增
- // 寫入資料表
- $d = [
- 'lineId' => $lineId,
- 'userName' => '',
- 'userPhoto' => '',
- 'gp' => 0,
- 'cdate' => date("Y-m-d H:i:s"),
- 'mdate' => date("Y-m-d H:i:s"),
- ];
- $this->playerManagementDb
- ->insert($d);
- $id = \DB::getPdo()->lastInsertId();
-
- // 替用互打一次性標籤
- $this->playerTag($lineId);
- } else {
- // 用戶的 LINE 資訊可能會更新,所以這個 API 還身兼更新資訊的功能
- // $d = [
- // 'userName' => $profile->displayName,
- // 'userPhoto' => $profile->pictureUrl,
- // ];
- // $this->playerManagementDb
- // ->where('lineId', $lineId)
- // ->update($d);
- }
-
- // 再重新查一遍
- $playerinfo = $this->playerManagementDb
- ->select([
- // 'id',
- 'lineId',
- // 'userName',
- // 'userPhoto',
- 'gp',
- // 'cdate',
- // 'mdate',
- ])
- ->where('lineId', $lineId)
- ->first()
- ->toArray();
-
- return $playerinfo;
- }
-
- // 檢查是否在本地有資料
- public function isPlayer($lineId)
- {
- $playerinfo = $this->playerManagementDb->select(['id'])->where('lineId', $lineId)->get()->toArray();
-
- return (count($playerinfo) > 0);
- }
-
- // 取得回合資訊
- public function roundinfo($lineId)
- {
- // 回合資訊
- $roundinfo = $this->roundManagementDb
- ->select(['id', 'dateBegin', 'dateFinal'])
- ->where('dateFinal', '>=', date("Y-m-d"))
- ->where('dateBegin', '<=', date("Y-m-d"))
- ->where('active', '=', GeneralConst::ACTIVE_YES)
- ->get();
- if (count($roundinfo) == 0) {
- return false;
- } else {
- $roundinfo = $roundinfo->toArray();
- }
- // 簽到資訊
- $checkininfo = $this->sessionManagementDb
- ->select(['eventDate', 'currentGameGp'])
- ->where('lineId', $lineId)
- ->where('rid', $roundinfo[0]['id'])
- ->where('gid', '<>', 0) // 僅篩選簽到或遊戲
- ->orderby('id', 'asc') // 確保按照時間排
- ->get()
- ->toArray();
- // 整理
- $res = [
- 'dateBegin' => $roundinfo[0]['dateBegin'],
- 'dateFinal' => $roundinfo[0]['dateFinal'],
- 'today' => date("Y-m-d"),
- 'checkins' => [],
- ];
- foreach ($checkininfo as $c) {
- // 相同天可能會有重新再玩,因此會有超過1筆資料的可能,不想使用 SQL group by 耗能
- $res['checkins'][ $c['eventDate'] ] = [
- 'date' => $c['eventDate'],
- 'canplay' => ($c['eventDate'] != date("Y-m-d")) ? false : ($c['currentGameGp'] < 0) ? true : false,
- ];
- }
- $res['checkins'] = array_values($res['checkins']);
-
- return $res;
- }
-
- // 取得獎項兌換資訊
- public function goodinfo($lineId)
- {
- // 獎項資訊
- $goodinfo = $this->goodManagementDb
- ->select(['id', 'lp', 'gp', 'active', 'totalQty', 'issuedQty'])
- ->get()
- ->toArray();
- // 一定有獎項所以不防呆
-
- // 回合資訊 (注意用於兌獎資訊的日期區間判定與玩遊戲不同)
- $roundinfo = $this->roundManagementDb
- ->select(['id'])
- ->where(\DB::raw("DATE_ADD(dateFinal, INTERVAL +redeemExtra DAY)"), '>=', date("Y-m-d"))
- ->where('dateBegin', '<=', date("Y-m-d"))
- ->where('active', '=', GeneralConst::ACTIVE_YES)
- ->get();
- // 若由 INFO 調用,則會先調用回合資訊確保來到這邊時一定拿到數值
- // 若由 REDEEM 調用,由於回合的日期限制比兌換的日期限制來的窄,不能優先於函數執行
- // 因此這邊有可能拿不到值,需要防呆
- if (count($roundinfo) == 0) {
- return false;
- } else {
- $roundinfo = $roundinfo->toArray();
- }
-
- // player 準備資料
- $playerinfo = $this->playerManagementDb->select(['gp'])->where('lineId', $lineId)->get();
- if (count($playerinfo) == 0) {
- return false;
- } else {
- $playerinfo = $playerinfo->toArray();
- }
- $mygp = $playerinfo[0]['gp'];
-
- // 兌獎資訊
- $redeeminfo = $this->sessionManagementDb
- ->select(['cid']) // 獎項 ID
- ->where('lineId', $lineId)
- ->where('rid', $roundinfo[0]['id'])
- ->where('cid', '<>', 0) // 僅篩選兌獎
- ->get()
- ->toArray();
- $redeemIds = [];
- foreach ($redeeminfo as $r) $redeemIds[] = $r['cid'];
-
- // 整理
- $res = [];
- foreach ($goodinfo as $g) {
- // 稽核用,如果已核發數量 >= 可發數量,就不顯示
- if ($g['issuedQty'] >= $g['totalQty'] && $g['totalQty'] > 0) {
- continue;
- }
- // 可用性限制
- $issoldout = false;
- $isredeemed = false;
- $isnomoney = false;
- $canuse = false;
- if ($g['totalQty'] - $g['issuedQty'] <= $this->settingManagementSv->getSetting()['GAME_ISSUE_BUFFER']) $issoldout = true;
- if (in_array($g['id'], $redeemIds)) $isredeemed = true;
- if ($mygp < $g['gp']) $isnomoney = true;
- if (
- ($g['totalQty'] - $g['issuedQty'] > $this->settingManagementSv->getSetting()['GAME_ISSUE_BUFFER']) // 核發數量限制還夠
- && (!in_array($g['id'], $redeemIds)) // 本回合沒有兌換過
- && ($mygp >= $g['gp']) // 自己的吉點足夠
- ) {
- $canuse = true;
- }
- // 最後一道防線就是新添加的 active 欄位(一般來說如果其他三個偵測有任何一個是 true 的話,canuse 就是 false ;但是當其他三個偵測都是 false 的情況如果 canuse 也是 false 就是該獎項被關閉)
- if ($g['active'] == GeneralConst::ACTIVE_NO) $canuse = false;
-
- $res[] = [
- 'id' => $g['id'],
- 'lp' => $g['lp'], // 獎項名
- 'gp' => $g['gp'], // 所需吉點
- 'issoldout' => $issoldout, // 是否全數兌換完畢
- 'isredeemed' => $isredeemed, // 是否已兌換過
- 'isnomoney' => $isnomoney, // 是否吉點還夠
- 'canuse' => $canuse, // 綜合顯示
- ];
- }
-
- return $res;
- }
-
- // 是否今日可玩遊戲
- public function canPlay($lineId)
- {
- // 回合資訊
- $roundinfo = $this->roundManagementDb
- ->select(['id'])
- ->where('dateFinal', '>=', date("Y-m-d"))
- ->where('dateBegin', '<=', date("Y-m-d"))
- ->where('active', '=', GeneralConst::ACTIVE_YES)
- ->get();
- if (count($roundinfo) == 0) {
- return false;
- } else {
- $roundinfo = $roundinfo->toArray();
- }
- $playinfo = $this->sessionManagementDb
- ->select(['currentGameGp'])
- ->where('lineId', $lineId)
- ->where('rid', $roundinfo[0]['id'])
- ->where('gid', '<>', 0) // 僅篩選簽到或遊戲
- ->where('eventDate', date("Y-m-d"))
- ->get()
- ->toArray();
- // 可用性限制
- $canplay = true;
- foreach ($playinfo as $p) {
- // 目前只有在玩一次的狀態是 -1
- if ($p['currentGameGp'] >= 0) $canplay = false;
- }
-
- return $canplay;
- }
-
- // 玩遊戲簽到(建立在可玩的狀態下才會進來)
- public function playGameAndCheckin($lineId, $name)
- {
- // 回合資訊
- $roundinfo = $this->roundManagementDb
- ->select(['id'])
- ->where('dateFinal', '>=', date("Y-m-d"))
- ->where('dateBegin', '<=', date("Y-m-d"))
- ->where('active', '=', GeneralConst::ACTIVE_YES)
- ->get()
- ->toArray();
- // 一定有回合所以不防呆(參考本函數註解)
-
- // 依照比例決定返回的轉盤結果
- $gameinfo = $this->gameManagementDb
- ->select(['id', 'ratio', 'gp'])
- ->get()
- ->toArray();
- $gameArr = [];
- foreach ($gameinfo as $g) for ($i = 0; $i < $g['ratio']; $i++) $gameArr[] = ['id' => $g['id'], 'gp' => $g['gp']];
- shuffle($gameArr);
-
- // 依照簽到狀態決定需不需要簽到 & 決定簽到吉點
- $playinfo = $this->sessionManagementDb
- ->select(['currentCheckinGp', 'eventDate'])
- ->where('lineId', $lineId)
- ->where('rid', $roundinfo[0]['id'])
- ->where('gid', '<>', 0) // 僅篩選簽到或遊戲
- ->get()
- ->toArray();
-
- $cancheckin = true;
- $checkins = 0; // 本回合不含這一次,共簽到過幾次
- foreach ($playinfo as $p) {
- // 檢驗當天是否有簽到
- if ($p['eventDate'] == date("Y-m-d")) {
- // 目前只有在玩一次的狀態是 -1
- if ($p['currentCheckinGp'] >= 0) $cancheckin = false;
- }
- // 統計本回合已簽到過幾次
- if ($p['currentCheckinGp'] > 0) {
- $checkins++;
- }
- }
- if ($lineId == $this->settingManagementSv->getSetting()['GAME_TESTING_LINE_ID']) $checkins = 0;
- $currentCheckinGp = ($cancheckin) ? $this->getGpByCheckins($checkins + 1) : 0;
- // 拼湊 session 紀錄
- $currentGp = ((($gameArr[0]['gp'] < 0) ? 0 : $gameArr[0]['gp']) + ($currentCheckinGp)); // 將遊戲吉點小於 0 的部分歸零
- $res = [
- 'pid' => $this->getPidBylineId($lineId),
- 'lineId' => $lineId,
- 'userName' => $name,
- 'rid' => $roundinfo[0]['id'],
- 'gid' => $gameArr[0]['id'],
- 'cid' => 0,
- 'eventDate' => date("Y-m-d"),
- 'currentGp' => $currentGp,
- 'currentGameGp' => $gameArr[0]['gp'],
- 'currentCheckinGp' => $currentCheckinGp,
- 'cdate' => date('Y-m-d H:i:s'),
- ];
- $this->sessionManagementDb->insert($res);
- $id = \DB::getPdo()->lastInsertId();
- $res['id'] = $id;
- // 上檔玩家吉點計算
- $res['gp'] = $this->addGpToPlayer($lineId, $currentGp);
-
- return $res;
- }
-
- public function getPidBylineId($lineId)
- {
- $info = $this->playerManagementDb->select(['id'])->where('lineId', $lineId)->first()->toArray();
-
- return $info['id'];
- }
-
- public function getGpByCheckins($checkins)
- {
- $info = $this->checkinManagementDb->select(['gp'])->where('day', $checkins)->first()->toArray();
-
- return $info['gp'];
- }
-
- public function addGpToPlayer($lineId, $gp)
- {
- $info = $this->playerManagementDb->select(['gp'])->where('lineId', $lineId)->first()->toArray();
- $gp += $info['gp'];
- $this->playerManagementDb->where('lineId', $lineId)->update(['gp' => $gp]);
-
- return $gp;
- }
-
- // 兌點(建立在 goodinfo 執行過確認兌獎資格的情況)
- // 採取策略是我們資料庫先改動 > 調用直通 > 如果直通有問題我們就會加註 MEMO > 但本地的資料就不更改了,避免重複讓用戶調用
- public function redeemGood($lineId, $gid, $name)
- {
- $redeeminfo = [];
- try {
- // 回合資訊 (注意用於兌獎資訊的日期區間判定與玩遊戲不同)
- $roundinfo = $this->roundManagementDb
- ->select(['id'])
- ->where(\DB::raw("DATE_ADD(dateFinal, INTERVAL +redeemExtra DAY)"), '>=', date("Y-m-d"))
- ->where('dateBegin', '<=', date("Y-m-d"))
- ->where('active', '=', GeneralConst::ACTIVE_YES)
- ->get()
- ->toArray();
- // 來到這邊代表一定有資料
- // good 準備資料
- $goodinfo = $this->goodManagementDb->select(['issuedQty', 'gp', 'lp'])->where('id', $gid)->first()->toArray();
- $fromIssuedQty = $goodinfo['issuedQty'];
- $toIssuedQty = $fromIssuedQty + 1;
- // player 準備資料
- $playerinfo = $this->playerManagementDb->select(['gp'])->where('lineId', $lineId)->first()->toArray();
- $fromGp = $playerinfo['gp'];
- $toGp = $playerinfo['gp'] - $goodinfo['gp'];
- // 直通打點與確認 (先打點,依照返回正確與否再決定要不要後續操作本地資料)
- if ($this->settingManagementSv->getSetting()['GAME_ESI_LINEPOINT'] == 'true') {
- // 共用變數
- $uid = $lineId;
- $rqidA = $this->getRqId();
- $rqidB = $this->getRqId();
- $seedKey = $this->getSeedKey();
- $divDataA = $this->getDivData();
- $divDataB = $this->getDivData();
- //
- //
- // 1/2 打點
- //
- //
- // 產生本次調用的 sessionKey
- $sessionKey = $this->getSessionKey($seedKey, $divDataA);
- // 整理押碼用的資料欄內容
- $msgRaw = $this->getMsgRaw([
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqidA,
- 'UID' => $uid,
- 'Amount' => $goodinfo['lp'],
- ]);
- $msgHash = $this->getMsgHash($msgRaw);
- // 建立ICV
- $icv = $this->getICV();
- // 建立押碼
- $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
- $mac = $this->getMAC($macRaw);
- // 參數打包
- $param = [
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqidA,
- 'ICV' => $icv,
- 'DivData' => $divDataA,
- 'MAC' => $mac,
- 'UID' => $uid,
- 'Amount' => $goodinfo['lp'],
- ];
- // 相關中繼資料打包(LOG 用)
- $logdata = [
- 'sessionKey' => $sessionKey,
- 'msgRaw' => $msgRaw,
- 'msgHash' => $msgHash,
- 'macRaw' => $macRaw,
- ];
- // 憑證參數整理
- $curlparam = [
- CURLOPT_SSLCERTTYPE => 'PEM',
- CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
- CURLOPT_SSLKEYTYPE => 'PEM',
- CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
- CURLOPT_SSL_VERIFYHOST => 0,
- CURLOPT_SSL_VERIFYPEER => 0,
- ];
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
- // 直通紀錄
- $data = [
- 'type' => GeneralConst::LOG_ESI,
- 'func' => 'LinepointIssue',
- 'k' => $uid,
- 'cdate' => date("Y-m-d H:i:s"),
- ];
- // 調用
- $req = [
- 'curl' => $curlparam,
- 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
- 'Accept' => '*/*',
- ]
- ];
- if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
- $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/linepoint/issue', $req);
- $resA = json_decode($response->getBody());
- $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
- $data['memoOut'] = json_encode($resA, JSON_UNESCAPED_UNICODE);
- $this->syslog($data);
- //
- //
- // 2/2 打點確認
- //
- //
- // 產生本次調用的 sessionKey
- $sessionKey = $this->getSessionKey($seedKey, $divDataB);
- // 整理押碼用的資料欄內容
- $msgRaw = $this->getMsgRaw([
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqidB,
- 'QueryRqid' => $rqidA, // 剛才的RQID
- 'UID' => $uid,
- ]);
- $msgHash = $this->getMsgHash($msgRaw);
- // 建立ICV
- $icv = $this->getICV();
- // 建立押碼
- $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
- $mac = $this->getMAC($macRaw);
- // 參數打包
- $param = [
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqidB,
- 'ICV' => $icv,
- 'DivData' => $divDataB,
- 'MAC' => $mac,
- 'QueryRqid' => $rqidA, // 剛才的RQID
- 'UID' => $uid,
- ];
- // 相關中繼資料打包(LOG 用)
- $logdata = [
- 'sessionKey' => $sessionKey,
- 'msgRaw' => $msgRaw,
- 'msgHash' => $msgHash,
- 'macRaw' => $macRaw,
- ];
- // 憑證參數整理
- $curlparam = [
- CURLOPT_SSLCERTTYPE => 'PEM',
- CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
- CURLOPT_SSLKEYTYPE => 'PEM',
- CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
- CURLOPT_SSL_VERIFYHOST => 0,
- CURLOPT_SSL_VERIFYPEER => 0,
- ];
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
- // 直通紀錄
- $data = [
- 'type' => GeneralConst::LOG_ESI,
- 'func' => 'LinepointQuery',
- 'k' => $uid,
- 'cdate' => date("Y-m-d H:i:s"),
- ];
- // 調用
- $req = [
- 'curl' => $curlparam,
- 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
- 'Accept' => '*/*',
- ]
- ];
- if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
- $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/linepoint/query', $req);
- $resB = json_decode($response->getBody());
- $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
- $data['memoOut'] = json_encode($resB, JSON_UNESCAPED_UNICODE);
- $this->syslog($data);
- // 打點返回成功,需要確認
- // 確認成功,對用戶返回已打點的資訊,也計入資料庫
- // 確認失敗,對用戶返回已打點的資訊,也計入資料庫(差別只在於 LOG 註記的內容不同)
- // 打點返回失敗,需要確認
- // 確認成功,對用戶返回已打點的資訊,也計入資料庫(差別也只在於 LOG 註記的內容不同)
- // 確認失敗,報錯
- if ($resA->RCode != '0000' && $resB->RCode != '0000') {
- return false;
- }
- }
- // session 插入
- $res = [
- 'pid' => $this->getPidBylineId($lineId),
- 'lineId' => $lineId,
- 'userName' => $name,
- 'rid' => $roundinfo[0]['id'],
- 'gid' => 0, // 注意這是轉盤的獎項 ID 不是兌獎的 ID
- 'cid' => $gid,
- 'eventDate' => date("Y-m-d"),
- 'currentGp' => -$goodinfo['gp'],
- 'currentGameGp' => 0,
- 'currentCheckinGp' => 0,
- 'cdate' => date('Y-m-d H:i:s'),
- ];
- $this->sessionManagementDb->insert($res);
- // good 更新資料
- $this->goodManagementDb->where('id', $gid)->update(['issuedQty' => $toIssuedQty]);
- // player 更新資料
- $this->playerManagementDb->where('lineId', $lineId)->update(['gp' => $toGp]);
- // 整理本地紀錄資料
- $redeeminfo = [
- 'player' => [
- 'lineId' => $lineId,
- 'fromGp' => $fromGp,
- 'toGp' => $toGp,
- ],
- 'good' => [
- 'id' => $gid,
- 'fromIssuedQty' => $fromIssuedQty,
- 'toIssuedQty' => $toIssuedQty,
- ],
- 'session' => $res,
- ];
- } catch (Exception $ex) {
- // 錯誤可能發生在 memoIn 或是 out
- $data['memoIn'] = json_encode(['error' => 'exception'], JSON_UNESCAPED_UNICODE);
- $data['memoOut'] = json_encode(['code' => $ex->getCode(), 'msg' => $ex->getMessage()], JSON_UNESCAPED_UNICODE);
- $this->syslog($data);
-
- return false;
- }
-
- return $redeeminfo;
- }
-
- public function playerTag($lineId)
- {
- try {
- // 本次調用的參數整理
- $tid = $this->settingManagementSv->getSetting()['GAME_CURRENT_TAG'];
- $uid = $lineId;
- $rqid = $this->getRqId();
- // 取得 seedKey
- $seedKey = $this->getSeedKey();
- // 隨機產生多樣性資料
- $divData = $this->getDivData();
- // 產生本次調用的 sessionKey
- $sessionKey = $this->getSessionKey($seedKey, $divData);
- // 整理押碼用的資料欄內容
- $msgRaw = $this->getMsgRaw([
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqid,
- 'TagInfos' => [
- [
- 'Tagid' => $tid,
- 'UIDs' => [$uid]
- ],
- ],
- ]);
- $msgHash = $this->getMsgHash($msgRaw);
- // 建立ICV
- $icv = $this->getICV();
- // 建立押碼
- $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
- $mac = $this->getMAC($macRaw);
- // 參數打包
- $param = [
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqid,
- 'ICV' => $icv,
- 'DivData' => $divData,
- 'MAC' => $mac,
- 'TagInfos' => [
- [
- 'Tagid' => $tid,
- 'UIDs' => [$uid]
- ],
- ],
- ];
- // 相關中繼資料打包(LOG 用)
- $logdata = [
- 'sessionKey' => $sessionKey,
- 'msgRaw' => $msgRaw,
- 'msgHash' => $msgHash,
- 'macRaw' => $macRaw,
- ];
- // 憑證參數整理
- $curlparam = [
- CURLOPT_SSLCERTTYPE => 'PEM',
- CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
- CURLOPT_SSLKEYTYPE => 'PEM',
- CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
- CURLOPT_SSL_VERIFYHOST => 0,
- CURLOPT_SSL_VERIFYPEER => 0,
- ];
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
- // 直通紀錄
- $data = [
- 'type' => GeneralConst::LOG_ESI,
- 'func' => 'TagTagging',
- 'k' => $uid,
- 'cdate' => date("Y-m-d H:i:s"),
- ];
- // 調用
- $req = [
- 'curl' => $curlparam,
- 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
- 'Accept' => '*/*',
- ],
- 'timeout' => 15, // Response timeout
- 'connect_timeout' => 10, // Connection timeout
- 'on_stats' => function (\GuzzleHttp\TransferStats $stats) use ($rqid) {
- if ($stats->hasResponse()) {
- $this->debuglog('playerTag-on_stats_ok', [
- 'rqid' => $rqid ?? 0
- ], [
- 'transferTime' => $stats->getTransferTime(),
- 'handlerStats' => $stats->getHandlerStats(),
- ]);
- } else {
- $this->debuglog('playerTag-on_stats_ng', [
- 'rqid' => $rqid ?? 0
- ], [
- 'handlerErrorData' => $stats->getHandlerErrorData(),
- ]);
- }
- }
- ];
- if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
-
- $requestTime = date("Y-m-d H:i:s");
- $logdata['requestTime'] = $requestTime;
-
- $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/tag/Tagging', $req);
- $res = $response->getBody();
-
- // 經由計算取得的完整 request JSON
- $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
- // 調用 API 取得返回
- $data['memoOut'] = $res;
-
- // 紀錄
- $this->syslog($data);
-
- } catch (Exception $ex) {
- // 錯誤可能發生在 memoIn 或是 out
- // $data['memoIn'] = json_encode(['error' => 'exception'], JSON_UNESCAPED_UNICODE);
- // $data['memoOut'] = json_encode(['code' => $ex->getCode(), 'msg' => $ex->getMessage()], JSON_UNESCAPED_UNICODE);
- // $this->syslog($data);
-
- $this->debuglog('playerTag', [
- 'rqid' => $rqid ?? 0
- ], [
- 'errCode' => $ex->getCode(),
- 'requestTime' => $requestTime ?? '',
- 'logTime' => date("Y-m-d H:i:s"),
- 'errMsg' => $ex->getMessage(),
- ]);
-
- return false;
- }
-
- return true;
- }
-
- public function syslog($sysLog)
- {
- // 此功能暫時只有小遊戲+類發票有,所以偵測 k 值存在才紀錄
- if (isset($sysLog['k'])) {
- if (!isset($sysLog['kk'])) {
- // 偵測到不存在特殊 kk 鍵的請求就進入小遊戲
- $this->syslogManagementDb->insert($sysLog);
- } else {
- // 偵測到存在特殊 kk 鍵的請求就
- if ($sysLog['kk'] == 'activity') {
- //進入類發票(需要先移除特殊鍵)
- unset($sysLog['kk']);
- $this->syslogactManagementDb->insert($sysLog);
- }
- if ($sysLog['kk'] == 'tmz') {
- //進入串門子(需要先移除特殊鍵)
- unset($sysLog['kk']);
- $this->syslogtManagementDb->insert($sysLog);
- }
- }
- }
-
- return true;
- }
-
- public function debuglog($func, $dataIn, $dataOut = '')
- {
- $this->syslogManagementDb->insert([
- 'type' => 'debug',
- 'func' => $func,
- 'k' => $_SERVER['SERVER_ADDR'],
- 'memoIn' => json_encode($dataIn),
- 'memoOut' => substr(json_encode($dataOut),0,1024),
- 'cdate' => date("Y-m-d H:i:s"),
- ]);
- }
-
- // 直通 API 串聯流程
- public function esiFlow()
- {
- // 本次調用的參數整理
- $tid = '87B3984D-15EC-480C-8E23-25275F2D2DF2';
- $uid = 'Ud2534d6e1eed9cee2c919a993443352b';
- $rqid = $this->getRqId();
- // 取得 seedKey
- $seedKey = $this->getSeedKey();
- // 隨機產生多樣性資料
- $divData = $this->getDivData();
- // 產生本次調用的 sessionKey
- $sessionKey = $this->getSessionKey($seedKey, $divData);
- // 整理押碼用的資料欄內容
- $msgRaw = $this->getMsgRaw([
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqid,
- 'TagInfos' => [
- [
- 'Tagid' => $tid,
- 'UIDs' => [$uid]
- ],
- ],
- ]);
- $msgHash = $this->getMsgHash($msgRaw);
- // 建立ICV
- $icv = $this->getICV();
- // 建立押碼
- $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
- $mac = $this->getMAC($macRaw);
- // 參數打包
- $param = [
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqid,
- 'ICV' => $icv,
- 'DivData' => $divData,
- 'MAC' => $mac,
- 'TagInfos' => [
- [
- 'Tagid' => $tid,
- 'UIDs' => [$uid]
- ],
- ],
- ];
- // 相關中繼資料打包(LOG 用)
- $logdata = [
- 'sessionKey' => $sessionKey,
- 'msgRaw' => $msgRaw,
- 'msgHash' => $msgHash,
- 'macRaw' => $macRaw,
- ];
- // 憑證參數整理
- $curlparam = [
- CURLOPT_SSLCERTTYPE => 'PEM',
- CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
- CURLOPT_SSLKEYTYPE => 'PEM',
- CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
- CURLOPT_SSL_VERIFYHOST => 0,
- CURLOPT_SSL_VERIFYPEER => 0,
- ];
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
- // 調用
- try {
- $req = [
- 'curl' => $curlparam,
- 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
- 'Accept' => '*/*',
- ]
- ];
- if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
- $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/tag/Tagging', $req);
- $res = $response->getBody();
- } catch (Exception $ex) {
-
- }
- // 直通紀錄
- $data = [
- 'type' => GeneralConst::LOG_ESI,
- 'func' => 'TagTagging',
- 'k' => '',
- 'cdate' => date("Y-m-d H:i:s"),
- ];
- // 經由計算取得的完整 request JSON
- $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
- // 調用 API 取得返回
- $data['memoOut'] = $res;
- // 紀錄
- $this->syslog($data);
-
- return $res;
- }
-
- public function esiFlowTagUpdate()
- {
- // 本次調用的參數整理
- $tid = 'f7d27d19-b05e-4ec8-bb49-2c5b0525e095';
- $uid = 'Ud2534d6e1eed9cee2c919a993443352b';
- $rqid = $this->getRqId();
- // 取得 seedKey
- $seedKey = $this->getSeedKey();
- // 隨機產生多樣性資料
- $divData = $this->getDivData();
- // 產生本次調用的 sessionKey
- $sessionKey = $this->getSessionKey($seedKey, $divData);
- // 整理押碼用的資料欄內容
- $msgRaw = $this->getMsgRaw([
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqid,
- 'Tags' => [
- [
- 'Tagid' => $tid,
- 'Name' => '奧美測試用標籤' . date("YmdHis"),
- ]
- ],
- ]);
- //$msgRaw = preg_replace('/[\-]/', '', $msgRaw);
- $msgHash = $this->getMsgHash($msgRaw);
- // 建立ICV
- $icv = $this->getICV();
- // 建立押碼
- $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
- $mac = $this->getMAC($macRaw);
- // 參數打包
- $param = [
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqid,
- 'ICV' => $icv,
- 'DivData' => $divData,
- 'MAC' => $mac,
- 'Tags' => [
- [
- 'Tagid' => $tid,
- 'Name' => '奧美測試用標籤' . date("YmdHis"),
- ]
- ],
- ];
- // 相關中繼資料打包(LOG 用)
- $logdata = [
- 'sessionKey' => $sessionKey,
- 'msgRaw' => $msgRaw,
- 'msgHash' => $msgHash,
- 'macRaw' => $macRaw,
- ];
- // 憑證參數整理
- $curlparam = [
- CURLOPT_SSLCERTTYPE => 'PEM',
- CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
- CURLOPT_SSLKEYTYPE => 'PEM',
- CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
- CURLOPT_SSL_VERIFYHOST => 0,
- CURLOPT_SSL_VERIFYPEER => 0,
- ];
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
- // 調用
- $req = [
- 'curl' => $curlparam,
- 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
- 'Accept' => '*/*',
- ]
- ];
- if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
- $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/tag/Update', $req);
- $res = $response->getBody();
- // 直通紀錄
- $data = [
- 'type' => GeneralConst::LOG_ESI,
- 'func' => 'TagUpdate',
- 'k' => '',
- 'cdate' => date("Y-m-d H:i:s"),
- ];
- // 經由計算取得的完整 request JSON
- $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
- // 調用 API 取得返回
- $data['memoOut'] = $res;
- // 紀錄
- $this->syslog($data);
-
- return [
- 'in' => json_decode($data['memoIn']),
- 'out' => json_decode($data['memoOut']),
- ];
- }
-
- public function esiFlowTagTagging()
- {
- // 本次調用的參數整理
- $tid = 'f7d27d19-b05e-4ec8-bb49-2c5b0525e095';
- $uid = 'Ud2534d6e1eed9cee2c919a993443352b';
- $rqid = $this->getRqId();
- // 取得 seedKey
- $seedKey = $this->getSeedKey();
- // 隨機產生多樣性資料
- $divData = $this->getDivData();
- // 產生本次調用的 sessionKey
- $sessionKey = $this->getSessionKey($seedKey, $divData);
- // 整理押碼用的資料欄內容
- $msgRaw = $this->getMsgRaw([
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqid,
- 'TagInfos' => [
- [
- 'Tagid' => $tid,
- 'UIDs' => [$uid]
- ],
- ],
- ]);
- $msgHash = $this->getMsgHash($msgRaw);
- // 建立ICV
- $icv = $this->getICV();
- // 建立押碼
- $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
- $mac = $this->getMAC($macRaw);
- // 參數打包
- $param = [
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqid,
- 'ICV' => $icv,
- 'DivData' => $divData,
- 'MAC' => $mac,
- 'TagInfos' => [
- [
- 'Tagid' => $tid,
- 'UIDs' => [$uid]
- ],
- ],
- ];
- // 相關中繼資料打包(LOG 用)
- $logdata = [
- 'sessionKey' => $sessionKey,
- 'msgRaw' => $msgRaw,
- 'msgHash' => $msgHash,
- 'macRaw' => $macRaw,
- ];
- // 憑證參數整理
- $curlparam = [
- CURLOPT_SSLCERTTYPE => 'PEM',
- CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
- CURLOPT_SSLKEYTYPE => 'PEM',
- CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
- CURLOPT_SSL_VERIFYHOST => 0,
- CURLOPT_SSL_VERIFYPEER => 0,
- ];
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
- // 調用
- $req = [
- 'curl' => $curlparam,
- 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
- 'Accept' => '*/*',
- ]
- ];
- if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
- $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/tag/Tagging', $req);
- $res = $response->getBody();
- // 直通紀錄
- $data = [
- 'type' => GeneralConst::LOG_ESI,
- 'func' => 'TagTagging',
- 'k' => '',
- 'cdate' => date("Y-m-d H:i:s"),
- ];
- // 經由計算取得的完整 request JSON
- $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
- // 調用 API 取得返回
- $data['memoOut'] = $res;
- // 紀錄
- $this->syslog($data);
-
- return [
- 'in' => json_decode($data['memoIn']),
- 'out' => json_decode($data['memoOut']),
- ];
- }
-
- public function esiFlowTagInsert()
- {
- // 本次調用的參數整理
- $tag = '串門子-所有';
- $rqid = $this->getRqId();
- // 取得 seedKey
- $seedKey = $this->getSeedKey();
- // 隨機產生多樣性資料
- $divData = $this->getDivData();
- // 產生本次調用的 sessionKey
- $sessionKey = $this->getSessionKey($seedKey, $divData);
- // 整理押碼用的資料欄內容
- $msgRaw = $this->getMsgRaw([
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqid,
- 'Tags' => [['Name' => $tag]],
- ]);
- //$msgRaw = preg_replace('/[\-]/', '', $msgRaw);
- $msgHash = $this->getMsgHash($msgRaw);
- // 建立ICV
- $icv = $this->getICV();
- // 建立押碼
- $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
- $mac = $this->getMAC($macRaw);
- // 參數打包
- $param = [
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqid,
- 'ICV' => $icv,
- 'DivData' => $divData,
- 'MAC' => $mac,
- 'Tags' => [['Name' => $tag]],
- ];
- // 相關中繼資料打包(LOG 用)
- $logdata = [
- 'sessionKey' => $sessionKey,
- 'msgRaw' => $msgRaw,
- 'msgHash' => $msgHash,
- 'macRaw' => $macRaw,
- ];
- // 憑證參數整理
- $curlparam = [
- CURLOPT_SSLCERTTYPE => 'PEM',
- CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
- CURLOPT_SSLKEYTYPE => 'PEM',
- CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
- CURLOPT_SSL_VERIFYHOST => 0,
- CURLOPT_SSL_VERIFYPEER => 0,
- ];
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
- // 調用
- $req = [
- 'curl' => $curlparam,
- 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
- 'Accept' => '*/*',
- ]
- ];
- if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
- $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/tag/Insert', $req);
- $res = $response->getBody();
- // 直通紀錄
- $data = [
- 'type' => GeneralConst::LOG_ESI,
- 'func' => 'TagInsert',
- 'k' => '',
- 'cdate' => date("Y-m-d H:i:s"),
- ];
- // 經由計算取得的完整 request JSON
- $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
- // 調用 API 取得返回
- $data['memoOut'] = $res;
- // 紀錄
- $this->syslog($data);
-
- return [
- 'in' => json_decode($data['memoIn']),
- 'out' => json_decode($data['memoOut']),
- ];
- }
-
- public function esiFlowLP()
- {
- // 共用變數
- $uid = "Ub02cb4e598a7d6b3802cc6fb073ce717";
- $rqidA = $this->getRqId();
- $seedKey = $this->getSeedKey();
- $divDataA = $this->getDivData();
- // 產生本次調用的 sessionKey
- $sessionKey = $this->getSessionKey($seedKey, $divDataA);
- // 整理押碼用的資料欄內容
- $msgRaw = $this->getMsgRaw([
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqidA,
- 'UID' => $uid,
- 'Amount' => 1,
- ]);
- $msgHash = $this->getMsgHash($msgRaw);
- // 建立ICV
- $icv = $this->getICV();
- // 建立押碼
- $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
- $mac = $this->getMAC($macRaw);
- // 參數打包
- $param = [
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqidA,
- 'ICV' => $icv,
- 'DivData' => $divDataA,
- 'MAC' => $mac,
- 'UID' => $uid,
- 'Amount' => 1,
- ];
- // 相關中繼資料打包(LOG 用)
- $logdata = [
- 'sessionKey' => $sessionKey,
- 'msgRaw' => $msgRaw,
- 'msgHash' => $msgHash,
- 'macRaw' => $macRaw,
- ];
- // 憑證參數整理
- $curlparam = [
- CURLOPT_SSLCERTTYPE => 'PEM',
- CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
- CURLOPT_SSLKEYTYPE => 'PEM',
- CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
- CURLOPT_SSL_VERIFYHOST => 0,
- CURLOPT_SSL_VERIFYPEER => 0,
- ];
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
- // 直通紀錄
- $data = [
- 'type' => GeneralConst::LOG_ESI,
- 'func' => 'LinepointIssue',
- 'k' => $uid,
- 'cdate' => date("Y-m-d H:i:s"),
- ];
- // 調用
- $req = [
- 'curl' => $curlparam,
- 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
- 'Accept' => '*/*',
- ]
- ];
- if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
- $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/linepoint/issue', $req);
- $resA = json_decode($response->getBody());
- $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
- $data['memoOut'] = json_encode($resA, JSON_UNESCAPED_UNICODE);
- $this->syslog($data);
-
- return [
- 'in' => json_decode($data['memoIn']),
- 'out' => json_decode($data['memoOut']),
- ];
- }
-
- public function esiFlowBak()
- {
- // 本次調用的參數整理
- $uid = 'Ud2534d6e1eed9cee2c919a993443352b';
- $rqid = $this->getRqId();
- // 取得 seedKey
- $seedKey = $this->getSeedKey();
- // 隨機產生多樣性資料
- $divData = $this->getDivData();
- // 產生本次調用的 sessionKey
- $sessionKey = $this->getSessionKey($seedKey, $divData);
- // 整理押碼用的資料欄內容
- $msgRaw = $this->getMsgRaw([
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqid,
- 'UID' => $uid,
- ]);
- $msgHash = $this->getMsgHash($msgRaw);
- // 建立ICV
- $icv = $this->getICV();
- // 建立押碼
- $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
- $mac = $this->getMAC($macRaw);
- // 參數打包
- $param = [
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqid,
- 'ICV' => $icv,
- 'DivData' => $divData,
- 'MAC' => $mac,
- 'UID' => $uid,
- ];
- // 相關中繼資料打包(LOG 用)
- $logdata = [
- 'sessionKey' => $sessionKey,
- 'msgRaw' => $msgRaw,
- 'msgHash' => $msgHash,
- 'macRaw' => $macRaw,
- ];
- // 憑證參數整理
- $curlparam = [
- CURLOPT_SSLCERTTYPE => 'PEM',
- CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
- CURLOPT_SSLKEYTYPE => 'PEM',
- CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
- CURLOPT_SSL_VERIFYHOST => 0,
- CURLOPT_SSL_VERIFYPEER => 0,
- ];
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
- // 調用
- $req = [
- 'curl' => $curlparam,
- 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
- 'Accept' => '*/*',
- ]
- ];
- if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
- $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/contact/QueryQualification', $req);
- $res = $response->getBody();
- // 直通紀錄
- $data = [
- 'type' => GeneralConst::LOG_ESI,
- 'func' => 'QueryQualification',
- 'k' => $uid,
- 'cdate' => date("Y-m-d H:i:s"),
- ];
- // 經由計算取得的完整 request JSON
- $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
- // 調用 API 取得返回
- $data['memoOut'] = $res;
- // 紀錄
- $this->syslog($data);
- // 返回值判斷
- $res = $res . json_encode($req);
-
- return $res;
-
- $rcode = $res->RCode ?? '';
- if ($rcode != '0000') return false;
- $isfollow = $res->Result->IsFollow ?? false;
- $ismember = $res->Result->IsTWPMember ?? false;
- if (!$isfollow || !$ismember) return false;
-
-
- return true;
- }
-
- public function getSeedKey()
- {
- // 參照官方文件教學
- // HEX 的 XOR 運算
- $all = strtoupper(bin2hex(pack('H*', env('SEED_KEY_A')) ^ pack('H*', env('SEED_KEY_B'))));
- // 分為前後的 A B 兩段,而不是原先文章定義的 A B 兩個 KEY
- $a = substr($all, 0, strlen($all) / 2);
- $b = substr($all, strlen($all) / 2, strlen($all) / 2);
-
- // 最後結合的是 A-B-A 段
- return $a . $b . $a;
- }
-
- public function getSessionKey($seedKey, $divData)
- {
- // 先將 HEX 還原成 BYTE 運算
- $sessionKey = bin2hex(openssl_encrypt(hex2bin($divData), 'DES-EDE3-ECB', hex2bin($seedKey), OPENSSL_RAW_DATA));
- // 按照文件擷取前 24 BYTE (也可以不用)
- $sessionKey = substr($sessionKey, 0, 48);
- // 按照文件轉成大寫 (也可以不用)
- $sessionKey = strtoupper($sessionKey);
-
- return $sessionKey;
- }
-
- public function getRqId()
- {
- $data = $data ?? random_bytes(16);
- assert(strlen($data) == 16);
- $data[6] = chr(ord($data[6]) & 0x0f | 0x40);
- $data[8] = chr(ord($data[8]) & 0x3f | 0x80);
-
- return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
- }
-
- public function getDivData()
- {
- return bin2hex(random_bytes(24));
- }
-
- public function getMsgRaw($msg)
- {
- $ret = '';
- foreach ($msg as $m) {
- $ret .= (is_array($m)) ? json_encode($m, JSON_UNESCAPED_UNICODE) : preg_replace('/[^A-Za-z0-9\-]/', '', $m);
- }
-
- return $ret;
- }
-
- public function getMsgHash($msg)
- {
- // 返回可以大寫也可以不用
- return strtoupper(hash('sha256', $msg));
- }
-
- public function getICV()
- {
- return bin2hex(random_bytes(8));
- }
-
- public function getMACRaw($msgHash, $sessionKey, $icv)
- {
- // 先將 HEX 還原成 BYTE 運算 (其中 ICV 可以大寫也可以不用)
- $macRaw = bin2hex(openssl_encrypt(hex2bin($msgHash), 'DES-EDE3-CBC', hex2bin($sessionKey), OPENSSL_RAW_DATA, hex2bin($icv)));
-
- // 返回可以大寫也可以不用
- return strtoupper($macRaw);
- }
-
- public function getMAC($macRaw)
- {
- // 按照文件擷取前 32 BYTE (必要)
- $macRaw = substr($macRaw, 0, 64);
- // 可以大寫也可以不用
- $macRaw = strtoupper($macRaw);
-
- return substr($macRaw, strlen($macRaw) - 16, 8);
- }
-
- // 類發票
-
- public function setdoc($data)
- {
- // 調用
- $response = $this->imgC->annotateImage(
- $data,
- [Type::TEXT_DETECTION]
- );
- $texts = $response->getTextAnnotations();
- // 整理字串
- $ret = [];
- foreach ($texts as $text) $ret[] = $text->getDescription();
- // 銀行 APP 判別
- $account_bank = '';
- $detect = explode("\n", $ret[0]);
- foreach ($detect as $d) {
- if ($d == '出示退款碼') $account_bank = '007';
- if ($d == '登入/安控') $account_bank = '009';
- if ($d == '繼續台灣 Pay交易') $account_bank = '016';
- if ($d == '我要退款') $account_bank = '017';
- if ($d == '繼續行動支付') $account_bank = '050';
- if ($d == '完成') $account_bank = '806';
- }
- // 整理觸發字串
- $order_no = '';
- $account_no = '';
- $order_date = '';
- switch ($account_bank) {
- case '007':
- for ($i = 0; $i < count($ret); $i++) {
- if ($ret[ $i ] == '訂單') $order_no = $ret[ $i + 2 ] ?? '';
- if ($ret[ $i ] == '帳號') $account_no = $ret[ $i + 1 ] ?? '';
- if (preg_match("/^[0-9]{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
- $order_date = $ret[ $i ] ?? '';
- }
- }
- break;
- case '009':
- for ($i = 0; $i < count($ret); $i++) {
- if ($ret[ $i ] == '訂單') $order_no = $ret[ $i + 2 ] ?? '';
- if ($ret[ $i ] == '帳號') $account_no = $ret[ $i + 1 ] ?? '';
- if (preg_match("/^[0-9]{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
- $order_date = $ret[ $i ] ?? '';
- }
- }
- break;
- case '016':
- for ($i = 0; $i < count($ret); $i++) {
- if ($ret[ $i ] == '訂單') $order_no = $ret[ $i + 2 ] ?? '';
- if ($ret[ $i ] == '帳號') $account_no = $ret[ $i + 1 ] ?? '';
- if (preg_match("/^[0-9]{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
- $order_date = $ret[ $i ] ?? '';
- }
- }
- break;
- case '017':
- for ($i = 0; $i < count($ret); $i++) {
- if ($ret[ $i ] == '訂單') $order_no = $ret[ $i + 2 ] ?? '';
- if ($ret[ $i ] == '方式') $account_no = $ret[ $i + 1 ] ?? '';
- if (preg_match("/^[0-9]{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
- $order_date = $ret[ $i ] ?? '';
- }
- }
- break;
- case '050':
- for ($i = 0; $i < count($ret); $i++) {
- if ($ret[ $i ] == '訂單') $order_no = $ret[ $i + 2 ] ?? '';
- if ($ret[ $i ] == '帳號') $account_no = $ret[ $i + 1 ] ?? '';
- // 沒有時間資訊可取得,這個是虛設
- if (preg_match("/^[0-9]{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
- $order_date = $ret[ $i ] ?? '';
- }
- }
- break;
- case '806':
- for ($i = 0; $i < count($ret); $i++) {
- if ($ret[ $i ] == '編號') $order_no = $ret[ $i + 1 ] ?? '';
- if ($ret[ $i ] == '商戶') $account_no = preg_replace('/-/', "", $ret[ $i - 1 ]) ?? '';
- if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
- $order_date = preg_replace('/-/', "/", $ret[ $i ]) ?? '';
- }
- }
- break;
- case '':
- for ($i = 0; $i < count($ret); $i++) {
- if ($ret[ $i ] == '訂單') $order_no = $ret[ $i + 2 ] ?? '';
- if ($ret[ $i ] == '付款' && $ret[ $i + 1 ] == '帳號') $account_bank = $ret[ $i + 2 ] ?? '';
- if ($ret[ $i ] == '付款' && $ret[ $i + 1 ] == '帳號') $account_no = $ret[ $i + 4 ] ?? '';
- //if ($ret[ $i ] == '時間') $order_date = $ret[ $i + 2 ] ?? '';
- if (preg_match("/^[0-9]{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])$/", $ret[ $i ])) {
- $order_date = $ret[ $i ] ?? '';
- }
- }
- break;
- }
-
- return [
- 'order_no' => $order_no,
- 'account_bank' => $account_bank,
- 'account_no' => $account_no,
- 'order_date' => $order_date,
- //'debug' => $ret,
- ];
- }
-
- public function activityinfo()
- {
- // 活動資訊
- $activityinfo = $this->activityManagementDb
- ->select(['id', 'reqTimeBegin', 'reqTimeFinal'])
- ->where('reqTimeFinal', '>=', date("Y-m-d"))
- ->where('reqTimeBegin', '<=', date("Y-m-d"))
- ->where('isDraw', '')
- ->get();
- if (count($activityinfo) == 0) {
- return false;
- } else {
- $activityinfo = $activityinfo->toArray();
- }
-
- return $activityinfo;
- }
-
- public function activities()
- {
- // 活動資訊
- $activities = $this->activityManagementDb
- ->select([
- 'id',
- 'activityName',
- 'reqTimeBegin',
- 'reqTimeFinal',
- 'drawTime',
- 'drawNumbers',
- 'isDraw',
- 'redeemTimeBegin',
- 'redeemTimeFinal',
- 'isCheckBegin',
- 'checkTimeBegin',
- 'checkTimeFinal',
- 'getTimeBegin',
- 'getTimeFinal',
- 'isGetFinal',
- ])
- ->get();
- if (count($activities) == 0) {
- return false;
- } else {
- $activities = $activities->toArray();
- }
-
- return $activities;
- }
-
- public function reqreceipt($info)
- {
- // 重複登錄資料邏輯判斷
- $r = $this->receiptManagementDb
- ->select(['id'])
- ->where('lineId', $info['lineId'])
- ->where('lineName', $info['name'])
- ->where('tranDate', $info['order_date'])
- ->where('tranBank', \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $info['account_bank'] . "'), \"" . env('KK') . "\"))"))
- ->where('tranAccount', \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $info['account_no'] . "'), \"" . env('KK') . "\"))"))
- ->where('tranOrderNo', \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $info['order_no'] . "'), \"" . env('KK') . "\"))"))
- ->get();
- if (count($r) == 0) return 0;
- // 實際寫入
- $this->receiptManagementDb
- ->insert([
- 'aid' => $info['aid'],
- 'tranDate' => $info['order_date'],
- 'tranBank' => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $info['account_bank'] . "'), \"" . env('KK') . "\"))"),
- 'tranAccount' => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $info['account_no'] . "'), \"" . env('KK') . "\"))"),
- 'tranOrderNo' => \DB::raw("HEX(AES_ENCRYPT(TO_BASE64('" . $info['order_no'] . "'), \"" . env('KK') . "\"))"),
- 'lineId' => $info['lineId'],
- 'lineName' => $info['name'],
- 'cdate' => date('Y-m-d H:i:s'),
- 'mdate' => date('Y-m-d H:i:s'),
- 'oid' => 0, // 外部用戶使用0
- ]);
- $id = \DB::getPdo()->lastInsertId();
-
- return $id;
- }
-
- public function lstreceipt($lineId)
- {
- // 依照(有限的)不同活動逐次取得該活動的所有紀錄
- $activity = $this->activityManagementDb->select(['id', 'activityName'])->orderby('drawTime', 'desc')->get();
- if (count($activity) == 0) {
- return [];
- } else {
- $activity = $activity->toArray();
- }
- $ret = [];
- // 選欄位
- $rStatusMsg = '';
- $rStatusBtn = '';
- foreach (GeneralConst::$rStatusMap as $k => $v) {
- $rStatusMsg .= ' when \'' . $k . '\' then \'' . $v['front'] . '\'';
- $rStatusBtn .= ' when \'' . $k . '\' then \'' . $v['btn'] . '\'';
- }
- // 逐個活動取得紀錄
- foreach ($activity as $a) {
- $receipt = $this->receiptManagementDb
- ->select([
- 'id',
- \DB::raw("tranDate as order_date"),
- \DB::raw("FROM_BASE64(AES_DECRYPT(UNHEX(tranBank), \"" . env('KK') . "\")) as account_bank"),
- \DB::raw("FROM_BASE64(AES_DECRYPT(UNHEX(tranAccount), \"" . env('KK') . "\")) as account_no"),
- \DB::raw("FROM_BASE64(AES_DECRYPT(UNHEX(tranOrderNo), \"" . env('KK') . "\")) as order_no"),
- 'rStatus',
- \DB::raw("(case rStatus $rStatusMsg end) as rStatusMsg"),
- \DB::raw("(case rStatus $rStatusBtn end) as rStatusBtn"),
- ])
- ->where('lineId', $lineId) // 自己的訂單
- ->where('aid', $a['id']) // 某一期的訂單
- ->orderby('cdate', 'desc')
- ->get();
- if (count($receipt) != 0) {
- $receipt = $receipt->toArray();
- // 依照順序切分(356412978)
- $tmp = [];
- foreach ($receipt as $r) $tmp[ $r['rStatus'] ][] = $r;
- // 整合
- $ret[] = [
- 'activityName' => $a['activityName'],
- 'receipts' => array_merge(
- $tmp[ GeneralConst::RSTATUS_DRAW_DONE ] ?? [],
- $tmp[ GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE ] ?? [],
- $tmp[ GeneralConst::RSTATUS_DRAW_DONE_REDEEM_FAIL ] ?? [],
- $tmp[ GeneralConst::RSTATUS_DRAW_DONE_REDEEM ] ?? [],
- $tmp[ GeneralConst::RSTATUS_INIT ] ?? [],
- $tmp[ GeneralConst::RSTATUS_DRAW_FAIL ] ?? [],
- $tmp[ GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE_GET ] ?? [],
- $tmp[ GeneralConst::RSTATUS_DRAW_DONE_EXPIRED ] ?? [],
- $tmp[ GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE_EXPIRED ] ?? []
- ),
- ];
- }
- }
-
- return $ret;
- }
-
- public function redeemCheck($lineId, $rid)
- {
- // 收據資訊
- $receiptinfo = $this->receiptManagementDb
- ->select(['aid'])
- ->where('lineId', $lineId) // 找自己的
- ->where('id', $rid) // 收據
- ->where('rStatus', GeneralConst::RSTATUS_DRAW_DONE) // 可驗證的狀態
- ->get();
- if (count($receiptinfo) == 0) {
- return false;
- } else {
- $receiptinfo = $receiptinfo->toArray();
- }
- // 活動資訊
- $activityinfo = $this->activityManagementDb
- ->select(['id', 'reqTimeBegin', 'reqTimeFinal'])
- ->where('id', $receiptinfo[0]['aid']) // 找該收據活動
- ->where('isDraw', 'Y') // 已經開獎
- ->where('redeemTimeFinal', '>=', date("Y-m-d")) // 且還在可登記的時間範圍內
- ->where('redeemTimeBegin', '<=', date("Y-m-d"))
- ->get();
- if (count($activityinfo) == 0) {
- return false;
- } else {
- return true;
- }
- }
-
- public function redeemExecute($lineId, $rid)
- {
- $this->receiptManagementDb
- ->where('lineId', $lineId)
- ->where('id', $rid)
- ->where('rStatus', GeneralConst::RSTATUS_DRAW_DONE)
- ->update([
- 'rStatus' => GeneralConst::RSTATUS_DRAW_DONE_REDEEM,
- 'mdate' => date('Y-m-d H:i:s'),
- 'oid' => 0, // 外部用戶使用0
- ]);
-
- return true;
- }
-
- public function getCheck($lineId, $rid)
- {
- // 收據資訊
- $receiptinfo = $this->receiptManagementDb
- ->select(['aid'])
- ->where('lineId', $lineId) // 找自己的
- ->where('id', $rid) // 收據
- ->where('rStatus', GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE) // 可領獎的狀態
- ->where('canGet', 'Y') // 可領獎的狀態(輔助狀態)
- ->get();
- if (count($receiptinfo) == 0) {
- return false;
- } else {
- $receiptinfo = $receiptinfo->toArray();
- }
- // 活動資訊
- $activityinfo = $this->activityManagementDb
- ->select(['id', 'reqTimeBegin', 'reqTimeFinal'])
- ->where('id', $receiptinfo[0]['aid']) // 找該收據活動
- ->where('isDraw', 'Y') // 已經開獎
- ->where('getTimeFinal', '>=', date("Y-m-d")) // 且還在可領獎的時間範圍內
- ->where('getTimeBegin', '<=', date("Y-m-d"))
- ->get();
- if (count($activityinfo) == 0) {
- return false;
- } else {
- return true;
- }
- }
-
- public function getExecute($lineId, $rid)
- {
- try {
- if ($this->settingManagementSv->getSetting()['RECEIPT_ESI_LINEPOINT'] == 'true') {
- // 共用變數
- $uid = $lineId;
- $rqidA = $this->getRqId();
- $rqidB = $this->getRqId();
- $seedKey = $this->getSeedKey();
- $divDataA = $this->getDivData();
- $divDataB = $this->getDivData();
- //
- //
- // 1/2 打點
- //
- //
- // 產生本次調用的 sessionKey
- $sessionKey = $this->getSessionKey($seedKey, $divDataA);
- // 整理押碼用的資料欄內容
- $msgRaw = $this->getMsgRaw([
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqidA,
- 'UID' => $uid,
- 'Amount' => 1,
- ]);
- $msgHash = $this->getMsgHash($msgRaw);
- // 建立ICV
- $icv = $this->getICV();
- // 建立押碼
- $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
- $mac = $this->getMAC($macRaw);
- // 參數打包
- $param = [
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqidA,
- 'ICV' => $icv,
- 'DivData' => $divDataA,
- 'MAC' => $mac,
- 'UID' => $uid,
- 'Amount' => 1,
- ];
- // 相關中繼資料打包(LOG 用)
- $logdata = [
- 'sessionKey' => $sessionKey,
- 'msgRaw' => $msgRaw,
- 'msgHash' => $msgHash,
- 'macRaw' => $macRaw,
- ];
- // 憑證參數整理
- $curlparam = [
- CURLOPT_SSLCERTTYPE => 'PEM',
- CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
- CURLOPT_SSLKEYTYPE => 'PEM',
- CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
- CURLOPT_SSL_VERIFYHOST => 0,
- CURLOPT_SSL_VERIFYPEER => 0,
- ];
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
- // 直通紀錄
- $data = [
- 'type' => GeneralConst::LOG_ESI,
- 'func' => 'LinepointIssue',
- 'k' => $uid,
- 'kk' => 'activity',
- 'cdate' => date("Y-m-d H:i:s"),
- ];
- // 調用
- $req = [
- 'curl' => $curlparam,
- 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
- 'Accept' => '*/*',
- ]
- ];
- if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
- $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/linepoint/issue', $req);
- $resA = json_decode($response->getBody());
- $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
- $data['memoOut'] = json_encode($resA, JSON_UNESCAPED_UNICODE);
- $this->syslog($data);
- //
- //
- // 2/2 打點確認
- //
- //
- // 產生本次調用的 sessionKey
- $sessionKey = $this->getSessionKey($seedKey, $divDataB);
- // 整理押碼用的資料欄內容
- $msgRaw = $this->getMsgRaw([
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqidB,
- 'QueryRqid' => $rqidA, // 剛才的RQID
- 'UID' => $uid,
- ]);
- $msgHash = $this->getMsgHash($msgRaw);
- // 建立ICV
- $icv = $this->getICV();
- // 建立押碼
- $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
- $mac = $this->getMAC($macRaw);
- // 參數打包
- $param = [
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqidB,
- 'ICV' => $icv,
- 'DivData' => $divDataB,
- 'MAC' => $mac,
- 'QueryRqid' => $rqidA, // 剛才的RQID
- 'UID' => $uid,
- ];
- // 相關中繼資料打包(LOG 用)
- $logdata = [
- 'sessionKey' => $sessionKey,
- 'msgRaw' => $msgRaw,
- 'msgHash' => $msgHash,
- 'macRaw' => $macRaw,
- ];
- // 憑證參數整理
- $curlparam = [
- CURLOPT_SSLCERTTYPE => 'PEM',
- CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
- CURLOPT_SSLKEYTYPE => 'PEM',
- CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
- CURLOPT_SSL_VERIFYHOST => 0,
- CURLOPT_SSL_VERIFYPEER => 0,
- ];
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
- // 直通紀錄
- $data = [
- 'type' => GeneralConst::LOG_ESI,
- 'func' => 'LinepointQuery',
- 'k' => $uid,
- 'kk' => 'activity',
- 'cdate' => date("Y-m-d H:i:s"),
- ];
- // 調用
- $req = [
- 'curl' => $curlparam,
- 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
- 'Accept' => '*/*',
- ]
- ];
- if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
- $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/linepoint/query', $req);
- $resB = json_decode($response->getBody());
- $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
- $data['memoOut'] = json_encode($resB, JSON_UNESCAPED_UNICODE);
- $this->syslog($data);
- // 打點返回成功,需要確認
- // 確認成功,對用戶返回已打點的資訊,也計入資料庫
- // 確認失敗,對用戶返回已打點的資訊,也計入資料庫(差別只在於 LOG 註記的內容不同)
- // 打點返回失敗,需要確認
- // 確認成功,對用戶返回已打點的資訊,也計入資料庫(差別也只在於 LOG 註記的內容不同)
- // 確認失敗,報錯
- if ($resA->RCode != '0000' && $resB->RCode != '0000') {
- return false;
- }
- }
- // 修改資料庫狀態
- $this->receiptManagementDb
- ->where('lineId', $lineId)
- ->where('id', $rid)
- ->where('rStatus', GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE)
- ->update([
- 'rStatus' => GeneralConst::RSTATUS_DRAW_DONE_REDEEM_DONE_GET,
- 'mdate' => date('Y-m-d H:i:s'),
- 'oid' => 0, // 外部用戶使用0
- ]);
- } catch (Exception $ex) {
- // 錯誤可能發生在 memoIn 或是 out
- $data['memoIn'] = json_encode(['error' => 'exception'], JSON_UNESCAPED_UNICODE);
- $data['memoOut'] = json_encode(['code' => $ex->getCode(), 'msg' => $ex->getMessage()], JSON_UNESCAPED_UNICODE);
- $this->syslog($data);
-
- return false;
- }
-
- return true;
- }
-
- // 串門子
-
- public function tplayerinfo($lineId)
- {
- // 取得用戶在本地的資訊
- $tplayerinfo = $this->tplayerManagementDb
- ->select(['id'])
- ->where('lineId', $lineId)
- ->get()
- ->toArray();
- // 取得用戶在 LINE 上的 profile
- // $profile = $this->lineProfile($lineId);
- if (count($tplayerinfo) == 0) {
- // 用戶不在本地資料庫,新增
- // 寫入資料表
- $d = [
- 'lineId' => $lineId,
- 'userName' => '',
- 'userPhoto' => '',
- 'c1' => 0,
- 'c2' => 0,
- 'c3' => 0,
- 'c4' => 0,
- 'c5' => 0,
- 'c6' => 0,
- 'c7' => 0,
- 'c8' => 0,
- 'c9' => 0,
- 'gp' => 0,
- 'cdate' => date("Y-m-d H:i:s"),
- 'mdate' => date("Y-m-d H:i:s"),
- ];
- $this->tplayerManagementDb
- ->insert($d);
- $id = \DB::getPdo()->lastInsertId();
- // 替用互打一次性標籤
- $this->tplayerTag($lineId);
- } else {
- // 用戶的 LINE 資訊可能會更新,所以這個 API 還身兼更新資訊的功能
- // $d = [
- // 'userName' => $profile->displayName,
- // 'userPhoto' => $profile->pictureUrl,
- // ];
- // $this->tplayerManagementDb
- // ->where('lineId', $lineId)
- // ->update($d);
- }
- // 再重新查一遍
- $tplayerinfo = $this->tplayerManagementDb
- ->select([
- // 'id',
- 'lineId',
- // 'userName',
- // 'userPhoto',
- 'gp',
- 'c1',
- 'c2',
- 'c3',
- 'c4',
- 'c5',
- 'c6',
- 'c7',
- 'c8',
- 'c9',
- \DB::raw("FLOOR(gp / " . $this->settingManagementSv->getSetting()['TMZ_GP_DRAW'] . ") as draw"),
- // 'cdate',
- // 'mdate',
- ])
- ->where('lineId', $lineId)
- ->first()
- ->toArray();
-
- return $tplayerinfo;
- }
-
- public function tplayerTag($lineId)
- {
- try {
- // 本次調用的參數整理
- $tid = $this->settingManagementSv->getSetting()['TMZ_CURRENT_TAG'];
- $uid = $lineId;
- $rqid = $this->getRqId();
- // 取得 seedKey
- $seedKey = $this->getSeedKey();
- // 隨機產生多樣性資料
- $divData = $this->getDivData();
- // 產生本次調用的 sessionKey
- $sessionKey = $this->getSessionKey($seedKey, $divData);
- // 整理押碼用的資料欄內容
- $msgRaw = $this->getMsgRaw([
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqid,
- 'TagInfos' => [
- [
- 'Tagid' => $tid,
- 'UIDs' => [$uid]
- ],
- ],
- ]);
- $msgHash = $this->getMsgHash($msgRaw);
- // 建立ICV
- $icv = $this->getICV();
- // 建立押碼
- $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
- $mac = $this->getMAC($macRaw);
- // 參數打包
- $param = [
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqid,
- 'ICV' => $icv,
- 'DivData' => $divData,
- 'MAC' => $mac,
- 'TagInfos' => [
- [
- 'Tagid' => $tid,
- 'UIDs' => [$uid]
- ],
- ],
- ];
- // 相關中繼資料打包(LOG 用)
- $logdata = [
- 'sessionKey' => $sessionKey,
- 'msgRaw' => $msgRaw,
- 'msgHash' => $msgHash,
- 'macRaw' => $macRaw,
- ];
- // 憑證參數整理
- $curlparam = [
- CURLOPT_SSLCERTTYPE => 'PEM',
- CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
- CURLOPT_SSLKEYTYPE => 'PEM',
- CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
- CURLOPT_SSL_VERIFYHOST => 0,
- CURLOPT_SSL_VERIFYPEER => 0,
- ];
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
- // 直通紀錄
- $data = [
- 'type' => GeneralConst::LOG_ESI,
- 'func' => 'TagTagging',
- 'k' => $uid,
- 'kk' => 'tmz',
- 'cdate' => date("Y-m-d H:i:s"),
- ];
- // 調用
- $req = [
- 'curl' => $curlparam,
- 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
- 'Accept' => '*/*',
- ]
- ];
- if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
- $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/tag/Tagging', $req);
- $res = $response->getBody();
- // 經由計算取得的完整 request JSON
- $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
- // 調用 API 取得返回
- $data['memoOut'] = $res;
- // 紀錄
- $this->syslog($data);
- } catch (Exception $ex) {
- // 錯誤可能發生在 memoIn 或是 out
- $data['memoIn'] = json_encode(['error' => 'exception'], JSON_UNESCAPED_UNICODE);
- $data['memoOut'] = json_encode(['code' => $ex->getCode(), 'msg' => $ex->getMessage()], JSON_UNESCAPED_UNICODE);
- $this->syslog($data);
-
- return false;
- }
-
- return true;
- }
-
- public function tcheckin($lineId, $name)
- {
- // 回合資訊
- $troundinfo = $this->troundManagementDb
- ->select(['id'])
- ->where('dateFinal', '>=', date("Y-m-d"))
- ->where('dateBegin', '<=', date("Y-m-d"))
- ->where('active', '=', GeneralConst::ACTIVE_YES)
- ->get()
- ->toArray();
- // 一定有回合所以不防呆(參考本函數註解)
- // 依照簽到狀態決定需不需要簽到 & 決定簽到吉點
- $playinfo = $this->tsessionManagementDb
- ->select(['currentCheckinGp', 'eventDate'])
- ->where('lineId', $lineId)
- ->where('rid', $troundinfo[0]['id'])
- ->where('gid', '=', -1) // 僅篩選簽到
- ->get()
- ->toArray();
- $cancheckin = true;
- $checkins = 0; // 本回合不含這一次,共簽到過幾次
- foreach ($playinfo as $p) {
- // 檢驗當天是否有簽到
- if ($p['eventDate'] == date("Y-m-d")) {
- if ($p['currentCheckinGp'] >= 0) $cancheckin = false;
- }
- // 統計本回合已簽到過幾次
- if ($p['currentCheckinGp'] >= 0) {
- $checkins++;
- }
- }
- //if ($lineId == $this->settingManagementSv->getSetting()['GAME_TESTING_LINE_ID']) $checkins = 0;
- $currentCheckinGp = ($cancheckin) ? $this->getGpByTCheckins($checkins + 1) : 0;
- // 拼湊 tsession 紀錄
- $currentGp = $currentCheckinGp;
- $res = [
- 'pid' => $this->getTPidBylineId($lineId),
- 'lineId' => $lineId,
- 'userName' => $name,
- 'rid' => $troundinfo[0]['id'],
- 'gid' => -1, // 簽到專用
- 'cid' => 0,
- 'eventDate' => date("Y-m-d"),
- 'currentGp' => $currentGp,
- 'currentGameGp' => 0,
- 'currentCheckinGp' => $currentCheckinGp,
- 'cdate' => date('Y-m-d H:i:s'),
- ];
- // 重複簽到不會有點數
- if ($currentGp > 0) {
- $this->tsessionManagementDb->insert($res);
- $id = \DB::getPdo()->lastInsertId();
- $res['id'] = $id;
- // 上檔玩家吉點計算
- $res['gp'] = $this->addGpToTPlayer($lineId, $currentGp);
- }
-
- return $res;
- }
-
- public function getGpByTCheckins($checkins)
- {
- $info = $this->tcheckinManagementDb->select(['gp'])->where('day', $checkins)->first()->toArray();
-
- return $info['gp'];
- }
-
- public function getTPidBylineId($lineId)
- {
- $info = $this->tplayerManagementDb->select(['id'])->where('lineId', $lineId)->first()->toArray();
-
- return $info['id'];
- }
-
- public function troundinfo($lineId)
- {
- // 回合資訊
- $troundinfo = $this->troundManagementDb
- ->select(['id', 'dateBegin', 'dateFinal'])
- ->where('dateFinal', '>=', date("Y-m-d"))
- ->where('dateBegin', '<=', date("Y-m-d"))
- ->where('active', '=', GeneralConst::ACTIVE_YES)
- ->get();
- if (count($troundinfo) == 0) {
- return false;
- } else {
- $troundinfo = $troundinfo->toArray();
- }
- // 簽到資訊
- $checkininfo = $this->tsessionManagementDb
- ->select(['eventDate', 'currentGameGp'])
- ->where('lineId', $lineId)
- ->where('rid', $troundinfo[0]['id'])
- ->where('gid', '=', -1) // 僅篩選簽到
- ->orderby('id', 'asc') // 確保按照時間排
- ->get()
- ->toArray();
- // 整理
- $res = [
- 'dateBegin' => $troundinfo[0]['dateBegin'],
- 'dateFinal' => $troundinfo[0]['dateFinal'],
- 'today' => date("Y-m-d"),
- 'checkins' => [],
- ];
- foreach ($checkininfo as $c) {
- // 相同天可能會有重新再玩,因此會有超過1筆資料的可能,不想使用 SQL group by 耗能
- $res['checkins'][ $c['eventDate'] ] = [
- 'date' => $c['eventDate'],
- ];
- }
- $res['checkins'] = array_values($res['checkins']);
-
- return $res;
- }
-
- public function addGpToTPlayer($lineId, $gp)
- {
- $info = $this->tplayerManagementDb->select(['gp'])->where('lineId', $lineId)->first()->toArray();
- $gp += $info['gp'];
- $this->tplayerManagementDb->where('lineId', $lineId)->update(['gp' => $gp]);
-
- return $gp;
- }
-
- public function isTPlayer($lineId)
- {
- $tplayerinfo = $this->tplayerManagementDb->select(['id'])->where('lineId', $lineId)->get()->toArray();
-
- return (count($tplayerinfo) > 0);
- }
-
- public function playTGame($lineId, $name, $c)
- {
- // 回合資訊
- $troundinfo = $this->troundManagementDb
- ->select(['id'])
- ->where('dateFinal', '>=', date("Y-m-d"))
- ->where('dateBegin', '<=', date("Y-m-d"))
- ->where('active', '=', GeneralConst::ACTIVE_YES)
- ->get()
- ->toArray();
- // 一定有回合所以不防呆(參考本函數註解)
- // 檢查本回合玩過沒(同時看 tsession & tplayer,順便取得上檔九宮格分布)
- $playinfo = $this->tsessionManagementDb
- ->select(['currentCheckinGp', 'eventDate'])
- ->where('lineId', $lineId)
- ->where('rid', $troundinfo[0]['id'])
- ->where('gid', '=', $c) // 僅篩選九宮格的這一格
- ->get();
- if (count($playinfo) > 0) return false; // 本周期玩過了
- $playinfo = $this->tplayerManagementDb
- ->select(['c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9'])
- ->where('lineId', $lineId)
- ->first()
- ->toArray();
- $s = [];
- foreach ($playinfo as $k => $v) {
- if ('c' . $c == $k && $v != 0) return false; // 本周期玩過了
- $s[ $k ] = $v;
- }
- // 依照九宮格分布計算本次應得吉點,以及紀錄九宮格狀態變遷
- $gp = 1; // 答對本來就有1點
- $s[ 'c' . $c ] = 2; // 答對初始狀態
- switch ($c) {
- case 1:
- if ($playinfo['c2'] != 0 && $playinfo['c3'] != 0) {
- $gp += 3;
- $s[ 'c' . $c ] = 3;
- $s['c2'] = 3;
- $s['c3'] = 3;
- }
- if ($playinfo['c4'] != 0 && $playinfo['c7'] != 0) {
- $gp += 3;
- $s[ 'c' . $c ] = 3;
- $s['c4'] = 3;
- $s['c7'] = 3;
- }
- if ($playinfo['c5'] != 0 && $playinfo['c9'] != 0) {
- $gp += 3;
- $s[ 'c' . $c ] = 3;
- $s['c5'] = 3;
- $s['c9'] = 3;
- }
- break;
- case 2:
- if ($playinfo['c1'] != 0 && $playinfo['c3'] != 0) {
- $gp += 3;
- $s[ 'c' . $c ] = 3;
- $s['c1'] = 3;
- $s['c3'] = 3;
- }
- if ($playinfo['c5'] != 0 && $playinfo['c8'] != 0) {
- $gp += 3;
- $s[ 'c' . $c ] = 3;
- $s['c5'] = 3;
- $s['c8'] = 3;
- }
- break;
- case 3:
- if ($playinfo['c1'] != 0 && $playinfo['c2'] != 0) {
- $gp += 3;
- $s[ 'c' . $c ] = 3;
- $s['c1'] = 3;
- $s['c2'] = 3;
- }
- if ($playinfo['c6'] != 0 && $playinfo['c9'] != 0) {
- $gp += 3;
- $s[ 'c' . $c ] = 3;
- $s['c6'] = 3;
- $s['c9'] = 3;
- }
- if ($playinfo['c5'] != 0 && $playinfo['c7'] != 0) {
- $gp += 3;
- $s[ 'c' . $c ] = 3;
- $s['c5'] = 3;
- $s['c7'] = 3;
- }
- break;
- case 4:
- if ($playinfo['c1'] != 0 && $playinfo['c7'] != 0) {
- $gp += 3;
- $s[ 'c' . $c ] = 3;
- $s['c1'] = 3;
- $s['c7'] = 3;
- }
- if ($playinfo['c5'] != 0 && $playinfo['c6'] != 0) {
- $gp += 3;
- $s[ 'c' . $c ] = 3;
- $s['c5'] = 3;
- $s['c6'] = 3;
- }
- break;
- case 5:
- if ($playinfo['c4'] != 0 && $playinfo['c6'] != 0) {
- $gp += 3;
- $s[ 'c' . $c ] = 3;
- $s['c4'] = 3;
- $s['c6'] = 3;
- }
- if ($playinfo['c2'] != 0 && $playinfo['c8'] != 0) {
- $gp += 3;
- $s[ 'c' . $c ] = 3;
- $s['c2'] = 3;
- $s['c8'] = 3;
- }
- if ($playinfo['c1'] != 0 && $playinfo['c9'] != 0) {
- $gp += 3;
- $s[ 'c' . $c ] = 3;
- $s['c1'] = 3;
- $s['c9'] = 3;
- }
- if ($playinfo['c3'] != 0 && $playinfo['c7'] != 0) {
- $gp += 3;
- $s[ 'c' . $c ] = 3;
- $s['c3'] = 3;
- $s['c7'] = 3;
- }
- break;
- case 6:
- if ($playinfo['c3'] != 0 && $playinfo['c9'] != 0) {
- $gp += 3;
- $s[ 'c' . $c ] = 3;
- $s['c3'] = 3;
- $s['c9'] = 3;
- }
- if ($playinfo['c4'] != 0 && $playinfo['c5'] != 0) {
- $gp += 3;
- $s[ 'c' . $c ] = 3;
- $s['c4'] = 3;
- $s['c5'] = 3;
- }
- break;
- case 7:
- if ($playinfo['c8'] != 0 && $playinfo['c9'] != 0) {
- $gp += 3;
- $s[ 'c' . $c ] = 3;
- $s['c8'] = 3;
- $s['c9'] = 3;
- }
- if ($playinfo['c1'] != 0 && $playinfo['c4'] != 0) {
- $gp += 3;
- $s[ 'c' . $c ] = 3;
- $s['c1'] = 3;
- $s['c4'] = 3;
- }
- if ($playinfo['c3'] != 0 && $playinfo['c5'] != 0) {
- $gp += 3;
- $s[ 'c' . $c ] = 3;
- $s['c3'] = 3;
- $s['c5'] = 3;
- }
- break;
- case 8:
- if ($playinfo['c2'] != 0 && $playinfo['c5'] != 0) {
- $gp += 3;
- $s[ 'c' . $c ] = 3;
- $s['c2'] = 3;
- $s['c5'] = 3;
- }
- if ($playinfo['c7'] != 0 && $playinfo['c9'] != 0) {
- $gp += 3;
- $s[ 'c' . $c ] = 3;
- $s['c7'] = 3;
- $s['c9'] = 3;
- }
- break;
- case 9:
- if ($playinfo['c7'] != 0 && $playinfo['c8'] != 0) {
- $gp += 3;
- $s[ 'c' . $c ] = 3;
- $s['c7'] = 3;
- $s['c8'] = 3;
- }
- if ($playinfo['c3'] != 0 && $playinfo['c6'] != 0) {
- $gp += 3;
- $s[ 'c' . $c ] = 3;
- $s['c3'] = 3;
- $s['c6'] = 3;
- }
- if ($playinfo['c1'] != 0 && $playinfo['c5'] != 0) {
- $gp += 3;
- $s[ 'c' . $c ] = 3;
- $s['c1'] = 3;
- $s['c5'] = 3;
- }
- break;
- }
- // 拼湊 tsession 紀錄
- $currentGp = $gp;
- $res = [
- 'pid' => $this->getTPidBylineId($lineId),
- 'lineId' => $lineId,
- 'userName' => $name,
- 'rid' => $troundinfo[0]['id'],
- 'gid' => $c,
- 'cid' => 0,
- 'eventDate' => date("Y-m-d"),
- 'currentGp' => $currentGp,
- 'currentGameGp' => $currentGp,
- 'currentCheckinGp' => 0,
- 'cdate' => date('Y-m-d H:i:s'),
- ];
- $this->tsessionManagementDb->insert($res);
- $id = \DB::getPdo()->lastInsertId();
- $res['id'] = $id;
- // 上檔玩家吉點計算+九宮格布局修改
- $res['gp'] = $this->addGpAndJggToTPlayer($lineId, $currentGp, $s);
-
- return $res;
- }
-
- public function addGpAndJggToTPlayer($lineId, $gp, $jgg)
- {
- // 計算增加後的吉點
- $info = $this->tplayerManagementDb->select(['gp'])->where('lineId', $lineId)->first()->toArray();
- $gp += $info['gp'];
- $d = ['gp' => $gp];
- $d = array_merge($d, $jgg);
- // 更新
- $this->tplayerManagementDb->where('lineId', $lineId)->update($d);
-
- return $d;
- }
-
- public function tredeeminfo($lineId)
- {
- $tplayerinfo = $this->tplayerManagementDb
- ->select(['gp'])
- ->where('lineId', $lineId)
- ->first()
- ->toArray();
-
- return ($tplayerinfo['gp'] >= $this->settingManagementSv->getSetting()['TMZ_GP_DRAW']);
- }
-
- public function redeemTGood($lineId, $name)
- {
- $redeeminfo = [];
- try {
- // 回合資訊 (注意用於兌獎資訊的日期區間判定與玩遊戲不同)
- $troundinfo = $this->troundManagementDb
- ->select(['id'])
- ->where(\DB::raw("DATE_ADD(dateFinal, INTERVAL +redeemExtra DAY)"), '>=', date("Y-m-d"))
- ->where('dateBegin', '<=', date("Y-m-d"))
- ->where('active', '=', GeneralConst::ACTIVE_YES)
- ->get()
- ->toArray();
- // 來到這邊代表一定有資料
- // 準備轉蛋資訊
- $tgoodinfo = $this->tgoodManagementDb
- ->select([
- 'id', // 回寫用
- 'ratio', // 抽獎製造冗餘資料用
- 'active', //
- 'lp', //
- 'totalQty', //
- 'issuedQty', //
- ])
- ->where('active', 'Y')
- ->get()
- ->toArray();
- $tgoodArr = [];
- 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']];
- shuffle($tgoodArr);
- $tgoodinfo = $tgoodArr[0];
- // good 準備資料
- $fromIssuedQty = $tgoodinfo['issuedQty'];
- $toIssuedQty = $fromIssuedQty + 1;
- // player 準備資料
- $tplayerinfo = $this->tplayerManagementDb->select(['gp'])->where('lineId', $lineId)->first()->toArray();
- $fromGp = $tplayerinfo['gp'];
- $toGp = $tplayerinfo['gp'] - $this->settingManagementSv->getSetting()['TMZ_GP_DRAW'];
- // 所有銘謝惠顧的可能(獎項雖然有點數但已被兌換完畢 || 獎項被設定為未啟用)
- if (
- $tgoodinfo['totalQty'] - $tgoodinfo['issuedQty'] <= $this->settingManagementSv->getSetting()['TMZ_ISSUE_BUFFER']
- || $tgoodinfo['active'] == GeneralConst::ACTIVE_NO
- ) {
- $this->debuglog('redeemTGood', [
- 'msg' => '所有銘謝惠顧的可能'
- ]);
- return false;
- }
- // 銘謝惠顧獎項還是要扣除集點,所以不能返回錯誤,只是需要先控制不要進入直通
- // 直通打點與確認 (先打點,依照返回正確與否再決定要不要後續操作本地資料)
- if ($this->settingManagementSv->getSetting()['TMZ_ESI_LINEPOINT'] == 'true' && $tgoodinfo['lp'] > 0) {
- // 共用變數
- $uid = $lineId;
- $rqidA = $this->getRqId();
- $rqidB = $this->getRqId();
- $seedKey = $this->getSeedKey();
- $divDataA = $this->getDivData();
- $divDataB = $this->getDivData();
- //
- //
- // 1/2 打點
- //
- //
- // 產生本次調用的 sessionKey
- $sessionKey = $this->getSessionKey($seedKey, $divDataA);
- // 整理押碼用的資料欄內容
- $msgRaw = $this->getMsgRaw([
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqidA,
- 'UID' => $uid,
- 'Amount' => $tgoodinfo['lp'],
- ]);
- $msgHash = $this->getMsgHash($msgRaw);
- // 建立ICV
- $icv = $this->getICV();
- // 建立押碼
- $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
- $mac = $this->getMAC($macRaw);
- // 參數打包
- $param = [
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqidA,
- 'ICV' => $icv,
- 'DivData' => $divDataA,
- 'MAC' => $mac,
- 'UID' => $uid,
- 'Amount' => $tgoodinfo['lp'],
- ];
- // 相關中繼資料打包(LOG 用)
- $logdata = [
- 'sessionKey' => $sessionKey,
- 'msgRaw' => $msgRaw,
- 'msgHash' => $msgHash,
- 'macRaw' => $macRaw,
- ];
- // 憑證參數整理
- $curlparam = [
- CURLOPT_SSLCERTTYPE => 'PEM',
- CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
- CURLOPT_SSLKEYTYPE => 'PEM',
- CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
- CURLOPT_SSL_VERIFYHOST => 0,
- CURLOPT_SSL_VERIFYPEER => 0,
- ];
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
- // 直通紀錄
- $data = [
- 'type' => GeneralConst::LOG_ESI,
- 'func' => 'LinepointIssue',
- 'k' => $uid,
- 'kk' => 'tmz',
- 'cdate' => date("Y-m-d H:i:s"),
- ];
- // 調用
- $req = [
- 'curl' => $curlparam,
- 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
- 'Accept' => '*/*',
- ]
- ];
- if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
- $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/linepoint/issue', $req);
- $resA = json_decode($response->getBody());
- $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
- $data['memoOut'] = json_encode($resA, JSON_UNESCAPED_UNICODE);
- $this->syslog($data);
-
- //
- //
- // 2/2 打點確認
- //
- //
- // 產生本次調用的 sessionKey
- $sessionKey = $this->getSessionKey($seedKey, $divDataB);
- // 整理押碼用的資料欄內容
- $msgRaw = $this->getMsgRaw([
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqidB,
- 'QueryRqid' => $rqidA, // 剛才的RQID
- 'UID' => $uid,
- ]);
- $msgHash = $this->getMsgHash($msgRaw);
- // 建立ICV
- $icv = $this->getICV();
- // 建立押碼
- $macRaw = $this->getMACRaw($msgHash, $sessionKey, $icv);
- $mac = $this->getMAC($macRaw);
- // 參數打包
- $param = [
- 'RqClient' => env('RQ_CLIENT'),
- 'RqId' => $rqidB,
- 'ICV' => $icv,
- 'DivData' => $divDataB,
- 'MAC' => $mac,
- 'QueryRqid' => $rqidA, // 剛才的RQID
- 'UID' => $uid,
- ];
- // 相關中繼資料打包(LOG 用)
- $logdata = [
- 'sessionKey' => $sessionKey,
- 'msgRaw' => $msgRaw,
- 'msgHash' => $msgHash,
- 'macRaw' => $macRaw,
- ];
- // 憑證參數整理
- $curlparam = [
- CURLOPT_SSLCERTTYPE => 'PEM',
- CURLOPT_SSLCERT => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_pub.pem',
- CURLOPT_SSLKEYTYPE => 'PEM',
- CURLOPT_SSLKEY => env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_key.pem',
- CURLOPT_SSL_VERIFYHOST => 0,
- CURLOPT_SSL_VERIFYPEER => 0,
- ];
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_KEYPASSWD ] = env('CERT_PASS');
- if (env('CERT_PASS') != '') $curlparam[ CURLOPT_CAINFO ] = env('LOCAL_PATH') . 'cert/' . env('APP_ENV') . '_ca.pem';
-
- // 調用
- $req = [
- 'curl' => $curlparam,
- 'body' => json_encode($param, JSON_UNESCAPED_UNICODE),
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'User-Agent' => 'ogilvy-' . env('APP_ENV'),
- 'Accept' => '*/*',
- ]
- ];
-
- if (!in_array($_SERVER['SERVER_ADDR'], ['172.31.30.167', '172.31.9.233', '172.31.42.197', '172.31.34.163'])) $req['proxy'] = env('CCH_PROXY');
-
- $response = $this->c->request('POST', env('API_BASE') . '/line/api/twpapi/linepoint/query', $req);
- $resB = json_decode($response->getBody());
-
- // 直通紀錄
- $data = [
- 'type' => GeneralConst::LOG_ESI,
- 'func' => 'LinepointQuery',
- 'k' => $uid,
- 'kk' => 'tmz',
- 'cdate' => date("Y-m-d H:i:s"),
- ];
- $data['memoIn'] = json_encode(array_merge($param, $logdata), JSON_UNESCAPED_UNICODE);
- $data['memoOut'] = json_encode($resB, JSON_UNESCAPED_UNICODE);
- $this->syslog($data);
-
- // 打點返回成功,需要確認
- // 確認成功,對用戶返回已打點的資訊,也計入資料庫
- // 確認失敗,對用戶返回已打點的資訊,也計入資料庫(差別只在於 LOG 註記的內容不同)
- // 打點返回失敗,需要確認
- // 確認成功,對用戶返回已打點的資訊,也計入資料庫(差別也只在於 LOG 註記的內容不同)
- // 確認失敗,報錯
- if ($resA->RCode != '0000' && $resB->RCode != '0000') {
- $this->debuglog('redeemTGood', [
- 'msg' => '打點返回失敗,需要確認'
- ]);
- return false;
- }
- }
- // session 插入
- $res = [
- 'pid' => $this->getTPidBylineId($lineId),
- 'lineId' => $lineId,
- 'userName' => $name,
- 'rid' => $troundinfo[0]['id'],
- 'gid' => 0,
- 'cid' => $tgoodinfo['id'], // 獎項的 ID
- 'eventDate' => date("Y-m-d"),
- 'currentGp' => -$this->settingManagementSv->getSetting()['TMZ_GP_DRAW'],
- 'currentGameGp' => 0,
- 'currentCheckinGp' => 0,
- 'cdate' => date('Y-m-d H:i:s'),
- ];
- $this->tsessionManagementDb->insert($res);
- // good 更新資料
- $this->tgoodManagementDb->where('id', $tgoodinfo['id'])->update(['issuedQty' => $toIssuedQty]);
- // tplayer 更新資料
- $this->tplayerManagementDb->where('lineId', $lineId)->update(['gp' => $toGp]);
- // 整理本地紀錄資料
- $redeeminfo = [
- 'playerinfo' => [
- 'lineId' => $lineId,
- 'fromGp' => $fromGp,
- 'toGp' => $toGp,
- ],
- 'redeeminfo' => [
- 'id' => $tgoodinfo['id'],
- 'lp' => $tgoodinfo['lp'],
- 'fromIssuedQty' => $fromIssuedQty,
- 'toIssuedQty' => $toIssuedQty,
- ],
- 'session' => $res,
- ];
- } catch (Exception $ex) {
- // 錯誤可能發生在 memoIn 或是 out
- $data['memoIn'] = json_encode(['error' => 'exception'], JSON_UNESCAPED_UNICODE);
- $data['memoOut'] = json_encode(['code' => $ex->getCode(), 'msg' => $ex->getMessage()], JSON_UNESCAPED_UNICODE);
- $this->syslog($data);
-
- $this->debuglog('redeemTGood', [
- 'rqidA' => $rqidA ?? 0,
- 'rqidB' => $rqidB ?? 0
- ], [
- 'errCode' => $ex->getCode(),
- 'requestTime' => $requestTime ?? '',
- 'logTime' => date("Y-m-d H:i:s"),
- 'errMsg' => $ex->getMessage(),
- ]);
-
- return false;
- }
-
- return $redeeminfo;
- }
-
- }
|