Api2021Service.php 124KB

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