Api2021Service.php 123KB

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