Api2021Service.php 123KB

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