Api2021Service.php 124KB

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