CommonFunc.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Commonfunc.php UTF-8
  4. * 公共函数
  5. *
  6. * @date : 2016年11月9日下午11:29:44
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 7.0
  11. * @modified: 2016年11月9日下午11:29:44
  12. */
  13. namespace huo\controller\common;
  14. use huo\controller\agent\AgentCache;
  15. use huo\controller\member\MemCache;
  16. use huolib\constant\GameConst;
  17. use wxapp\aes\ErrorCode;
  18. use wxapp\aes\WXBizDataCrypt;
  19. class CommonFunc {
  20. /**
  21. * 生成Order
  22. *
  23. * @param int $mem_id
  24. * @param string $agent_game
  25. * @param int $agent_id
  26. * @param string $device_id
  27. *
  28. * @return int|string
  29. */
  30. public static function getAgentId($mem_id = 0, $agent_game = '', $agent_id = 0, $device_id = '') {
  31. if (!empty($mem_id)) {
  32. $_mem_data = MemCache::ins()->getInfoById($mem_id);
  33. return $_mem_data['agent_id'];
  34. }
  35. if (empty($agent_id)) {
  36. $agent_id = (new HuoSession($mem_id))->getAgentId();
  37. }
  38. if (!empty($agent_game)) {
  39. /* 根据agentgame获得agent_id */
  40. return AgentCache::ins()->getAgentIdByAg($agent_game);
  41. }
  42. /* 通过设备判定渠道归属 */
  43. if (!empty($device_id)) {
  44. // TODO: wuyonghong 2018/4/26 根据设备判定渠道归属
  45. return $agent_id;
  46. }
  47. return $agent_id;
  48. }
  49. /**
  50. * @return float 默认为1
  51. */
  52. public static function getPtbRmbRate() {
  53. $_rate = config('ptb_rmb_rate');
  54. if (empty($_rate)) {
  55. $_rate = 1;
  56. }
  57. return $_rate;
  58. }
  59. /**
  60. * @return float 默认为1
  61. */
  62. public static function getGmRmbRate() {
  63. $_rate = config('gm_rmb_rate');
  64. if (empty($_rate)) {
  65. $_rate = 1;
  66. }
  67. return $_rate;
  68. }
  69. /**
  70. * 获取金币与人民币比例
  71. *
  72. * @return float 默认为100
  73. */
  74. public static function getGoldRmbRate() {
  75. $_rate = config('gold_rmb_rate');
  76. if (empty($_rate)) {
  77. $_rate = 100;
  78. }
  79. return $_rate;
  80. }
  81. /**
  82. * 获取心跳时长
  83. */
  84. public static function getHeartbeatTime() {
  85. $_g_heartbeat_time = config('G_HEARTBEAT_TIME');
  86. if (empty($_g_heartbeat_time)) {
  87. /* 默认时长300秒 */
  88. return 300;
  89. }
  90. return $_g_heartbeat_time;
  91. }
  92. /**
  93. * @param $classify
  94. *
  95. * @return array|int
  96. */
  97. public static function getAppAppId($classify = 0) {
  98. $_and_app_id = config('app_app_id');
  99. $_ios_app_id = config('ios_app_id');
  100. switch ($classify) {
  101. case GameConst::GAME_IOS:
  102. return $_ios_app_id;
  103. case GameConst::GAME_ANDROID:
  104. return $_and_app_id;
  105. default:
  106. return [$_and_app_id, $_ios_app_id];
  107. }
  108. }
  109. /**
  110. * 从加密数据中获取unionid
  111. *
  112. * @param $app_key
  113. * @param $session_key
  114. *
  115. * @return mixed|string
  116. */
  117. public static function getUnionIdByEncryptedData($app_key, $session_key) {
  118. $_iv = request()->param('iv/s', '');
  119. $_encrypted_data = request()->param('encrypted_data/s', '');
  120. $_union_id = '';
  121. if (!empty($_iv) && !empty($_encrypted_data)) {
  122. $_wx_biz_data_crypt = new WXBizDataCrypt($app_key, $session_key);
  123. $_err_code = $_wx_biz_data_crypt->decryptData($_encrypted_data, $_iv, $_wx_data);
  124. if (ErrorCode::$OK == $_err_code) {
  125. $_wx_data = json_decode($_wx_data, true);
  126. $_union_id = get_val($_wx_data, 'unionId', '');
  127. }
  128. }
  129. return $_union_id;
  130. }
  131. public static function safeDivMinValue($a, $b) {
  132. $_div = $a - $b;
  133. return ($_div < 0) ? 0 : $_div;
  134. }
  135. }