TapDBUtil.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace loyalsoft;
  3. require_once __DIR__ . '/TapDB_config.php';
  4. require_once __DIR__ . '/Events/TapDBEvent.php';
  5. require_once __DIR__ . '/Events/TapDBPayEvent.php';
  6. /**
  7. * Description of TapDBUtil
  8. *
  9. * @author gwang
  10. */
  11. class TapDBUtil {
  12. /**
  13. * 主动推送充值记录
  14. * @param \loyalsoft\pay\OrderNotice $order
  15. * @param string $payMent 支付渠道: wx/zfb/yyb/hw
  16. */
  17. public static function PushPayEvent($order, $payMent) {
  18. $eventData = new TapDBPayEvent($order, $payMent);
  19. $json = json_encode($eventData);
  20. CLog::pay("TapDB上报 $json");
  21. return self::PushEventData($json);
  22. }
  23. /**
  24. * 上报用户登录
  25. * @return type
  26. */
  27. public static function SOnUserLogin() {
  28. $data = new TapDBPreEvent(req()->uid, 'user_login', array());
  29. return self::PushEventData(JsonUtil::encode($data));
  30. }
  31. /**
  32. * 接收客户端上报数据并转发给tapdb
  33. */
  34. public static function OnClientReport() {
  35. $params = query_paras();
  36. ['UID' => $uid, 'eventName' => $eventName] = $params;
  37. unset($params['eventName']);
  38. $props = self::dealPropsName($params);
  39. return self::PushJsonEvent($uid, $eventName, $props);
  40. }
  41. public static function TestPay() {
  42. $args = [];
  43. $args['openid'] = 'oEW_p0gt1shMJMY4S2KiUSaLxNp0_yyb'; // openid # 玩家uid(不带_yyb)
  44. $args['amt'] = 600; # 支付金额, 游戏后台统一单位是分
  45. $args['appmeta'] = 'oEW_p0gt1shMJMY4S2KiUSaLxNp0_yyb,1,1022304201408000815*qbqd*wechat';
  46. $order = pay\OrderNotice::Parse_ysdkOrder($args);
  47. self::PushPayEvent($order, 'yyb');
  48. }
  49. public static function Test() {
  50. $params = ['UID' => "Wanggang", 'eventName' => "#EventCount", 'zoneid' => 1];
  51. ['UID' => $uid, 'eventName' => $eventName] = $params;
  52. unset($params['eventName']);
  53. $props = self::dealPropsName($params);
  54. return self::PushJsonEvent($uid, $eventName, $props);
  55. }
  56. /**
  57. * 处理下事件属性, 加上'#'前缀
  58. * @param dic $param
  59. * @return asocArray
  60. */
  61. private static function dealPropsName($param) {
  62. $arr = [];
  63. foreach ($param as $k => $v) {
  64. $k = urldecode($k);
  65. $nk = str_starts_with($k, '#') ? $k : "#$k";
  66. $arr[$nk] = urldecode($v);
  67. }
  68. return $arr;
  69. }
  70. /**
  71. * 向tapdb后台主动推送事件
  72. * @param string $uid 玩家唯一id
  73. * @param string $eventName
  74. * @param dic $params
  75. */
  76. public static function PushJsonEvent($uid, $eventName, $params) {
  77. $data = new TapDBEvent($uid, $eventName, $params);
  78. return self::PushEventData(JsonUtil::encode($data));
  79. }
  80. /**
  81. * 向TapDB后台推送事件数据
  82. * @param string $json
  83. * @return boolean 推送成功 true, 失败: false
  84. */
  85. private static function PushEventData($json) {
  86. $cfg = TapDB_config::Ins();
  87. ['result' => $ok, 'msg' => $resp] = HttpUtil::makeRequest($cfg->serverUrl, $json, [], [$cfg->ContentType], $cfg->method);
  88. if (!$ok) { # 如果失败, 打下日志
  89. CLog::err("TapDB上报失败!( $resp ) " . $json);
  90. }
  91. // $arr = HttpUtil::makeRequest($cfg->serverUrl, $json, [], [$cfg->ContentType], $cfg->method);
  92. // $ok = $arr['result'];
  93. // $resp = $arr['msg'];
  94. // if (!$ok) { # 如果失败, 打下日志
  95. // CLog::err("TapDB上报失败!( $resp ) " . $json);
  96. // }
  97. return $ok;
  98. }
  99. }