Api2021Service.php 121KB

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