Api2021Service.php 123KB

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