Api2021Service.php 121KB

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