Api2021Service.php 123KB

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