WagentProxy.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/8/15
  6. * Time: 16:56
  7. */
  8. namespace App\Lib\Biz\Sport;
  9. class WagentProxy
  10. {
  11. private $token = '';
  12. private $cacheFile = '';
  13. private $domain = 'http://sports.bocai108.com';
  14. private $configs = [];
  15. public function setCacheFile($file = '')
  16. {
  17. if (empty($file)) {
  18. $file = __DIR__ . DIRECTORY_SEPARATOR . 'token.cache';
  19. }
  20. $this->cacheFile = $file;
  21. }
  22. public function getCacheFile()
  23. {
  24. if (empty($this->cacheFile)) {
  25. $this->cacheFile = __DIR__ . DIRECTORY_SEPARATOR . 'token.cache';
  26. }
  27. return $this->cacheFile;
  28. }
  29. //写入本地缓存 注意目录和文件要有与入权根
  30. public function writeTokenCache($array)
  31. {
  32. $ret = file_put_contents($this->cacheFile, json_encode($array, 256));
  33. if (!$ret) {
  34. throw new \Exception("缓存文件写入失败,请检查文件写入权限", 1005);
  35. }
  36. return $ret;
  37. }
  38. //读取本地缓存
  39. public function readTokenCache()
  40. {
  41. if (!file_exists($this->cacheFile)) {
  42. return false;
  43. }
  44. $ret = file_get_contents($this->cacheFile);
  45. if ($ret) {
  46. return $ret;
  47. } else {
  48. return false;
  49. }
  50. }
  51. //请求 url地址get时可以带参数 ,param为post提交的参数数组 isPost是否是post方式提交,默认为get
  52. function request_post($url = '', $param = '', $isPost = 0)
  53. {
  54. if (empty($url)) {
  55. return false;
  56. }
  57. if ($isPost && empty($param)) {
  58. return false;
  59. }
  60. $postUrl = $url;
  61. $curlPost = $param;
  62. $ch = curl_init();//初始化curl
  63. if (!$isPost && $param) {
  64. $postUrl = $postUrl . '?' . $param;
  65. }
  66. curl_setopt($ch, CURLOPT_URL, $postUrl);//抓取指定网页
  67. curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
  68. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
  69. if ($isPost) {
  70. curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
  71. curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
  72. }
  73. $data = curl_exec($ch);//运行curl
  74. curl_close($ch);
  75. return $data;
  76. }
  77. //获取token
  78. protected function getToken()
  79. {
  80. $cacheData = json_decode($this->readTokenCache(), true);
  81. if ($cacheData && isset($cacheData['ctime']) && isset($cacheData['token']) && isset($cacheData['domain'])) {
  82. $ctime = intval($cacheData['ctime']);
  83. $token = $cacheData['token'];
  84. $domain = $this->domain = $cacheData['domain'];
  85. if ((time() - $ctime) >= 24 * 60 * 60 - 300) {
  86. $token = '';
  87. }
  88. if ($token) {
  89. $this->domain = $domain;
  90. $this->token = $token;
  91. return $token;
  92. }
  93. }
  94. return false;
  95. }
  96. //刷新token值
  97. public function refreshTokenData($config)
  98. {
  99. $name = isset($config['name']) ? $config['name'] : '';
  100. $key = isset($config['key']) ? $config['key'] : '';
  101. $secret = isset($config['secret']) ? $config['secret'] : '';
  102. $domain = isset($config['domain']) ? $config['domain'] : '';
  103. if (empty($name) || empty($key) || empty($secret) || empty($domain)) {
  104. throw new \Exception('require params error!', 1000);
  105. }
  106. $url = $domain . '/InApi-index/getToken';
  107. $tokenTmp = $this->request_post($url, ['wagent_name' => $name, 'wagent_key' => $key, 'wagent_secret' => $secret], 1);
  108. if ($tokenTmp) {
  109. $tokenData = json_decode($tokenTmp, true);
  110. if (isset($tokenData['status']) && $tokenData['status'] == 1 && isset($tokenData['data']['token']) && ($tokenData['data']['token'] != '')) {
  111. $this->writeTokenCache(['ctime' => time(), 'token' => $tokenData['data']['token'], 'domain' => $domain]);
  112. $this->token = $tokenData['data']['token'];
  113. return $tokenData['data']['token'];
  114. } else {
  115. return $tokenTmp;
  116. }
  117. }
  118. throw new \Exception('get Token False', 1001);
  119. }
  120. //配置
  121. public function setConfigs(Array $config)
  122. {
  123. $name = isset($config['name']) ? $config['name'] : '';
  124. $key = isset($config['key']) ? $config['key'] : '';
  125. $secret = isset($config['secret']) ? $config['secret'] : '';
  126. $domain = isset($config['domain']) ? $config['domain'] : '';
  127. $cachefile = isset($config['cachefile']) ? $config['cachefile'] : '';
  128. if (empty($name) || empty($key) || empty($secret)) {
  129. throw new \Exception('初始化参数错误,name,key,secret不能为空', 1002);
  130. }
  131. if (empty($domain)) {
  132. $domain = $this->domain;
  133. }
  134. $this->configs = ['name' => $name, 'key' => $key, 'secret' => $secret, 'domain' => $domain];
  135. $this->setCacheFile($cachefile);
  136. return true;
  137. }
  138. //生成带验证的请求url
  139. public function makeUrl(String $route, Array $paraArray, $arrayRet = 0)
  140. {
  141. $str = '';
  142. foreach ($paraArray as $nkey => $val) {
  143. $str .= "&" . $nkey . '=' . $val;
  144. }
  145. $param = base64_encode(substr($str, 1));
  146. if (empty($this->token)) {
  147. throw new \Exception("token 不能为空!", 1007);
  148. }
  149. $Key = md5($param . $this->token);
  150. if ($arrayRet) {
  151. return ['url' => $this->domain . $route, 'param' => $param, 'key' => $Key];
  152. }
  153. $url = $this->domain . $route . '?params=' . $param . '&Key=' . $Key;
  154. return $url;
  155. }
  156. //初始化操作
  157. public function Init($config)
  158. {
  159. $this->setConfigs($config);
  160. if ($this->getToken()) {
  161. return true;
  162. }
  163. $this->refreshTokenData($this->configs);
  164. return true;
  165. }
  166. //抽象公共请求方法[一定得带上method参数]
  167. public function commfun(Array $datas, $isPost = 0)
  168. {
  169. if (!isset($datas['method'])) {
  170. throw new \Exception("modthod参数缺失", 1006);
  171. }
  172. $arrcomm = ['agent' => $this->configs['name'], 'method' => $datas['method']];
  173. unset ($datas['method']);
  174. $urlData = $this->makeUrl('/InApi-index/dobusiness', array_merge($datas, $arrcomm), $isPost);
  175. if ($isPost) {
  176. $ret = $this->request_post($urlData['url'], ['params' => $urlData['param'], 'Key' => $urlData['key']], 1);
  177. } else {
  178. $ret = $this->request_post($urlData);
  179. }
  180. return $ret;
  181. }
  182. /////////////////////////////////////以下为具体业务逻辑
  183. //请在调用以下方法前,调用 Init方法作初始化操作
  184. //2.1检测账户是否存在
  185. public function caie($name)
  186. {
  187. return $this->commfun(['method' => 'caie', 'username' => $name]);
  188. }
  189. //2.2 检测并创建账号
  190. public function caca($name, $pass)
  191. {
  192. return $this->commfun(['method' => 'caca', 'username' => $name, 'password' => $pass]);
  193. }
  194. ///2.3用户查询余额
  195. public function gb($name, $pass)
  196. {
  197. return $this->commfun(['method' => 'gb', 'username' => $name, 'password' => $pass]);
  198. }
  199. ///2.4用户密码更改 (用post方法提交的例子,注意参数格式)
  200. public function ua($name, $pass)
  201. {
  202. return $this->commfun(['method' => 'ua', 'username' => $name, 'password' => $pass], 1);
  203. }
  204. ///2.5 代理转给用户或用户转回代理
  205. public function ptc($name, $pass, $billno, $credit, $type)
  206. {
  207. return $this->commfun(['method' => 'ptc', 'username' => $name, 'password' => $pass, 'billno' => $billno, 'credit' => $credit, 'type' => $type], 1);
  208. }
  209. ///2.6 代理转给用户或用户转回代理的转账查询
  210. public function ctc($name, $pass, $billno, $type)
  211. {
  212. return $this->commfun(['method' => 'ctc', 'username' => $name, 'password' => $pass, 'billno' => $billno, 'type' => $type]);
  213. }
  214. //2.7 按订单查询转账信息
  215. public function gct($billno)
  216. {
  217. return $this->commfun(['method' => 'gct', 'billno' => $billno]);
  218. }
  219. //2.8 用户进入游戏
  220. public function tg($name, $pass, $gametype = 2, $domain = '', $gamekind = 0, $iframe = '0', $platformname = '', $lang = 'zh')
  221. {
  222. return $this->commfun(['method' => 'tg', 'username' => $name, 'password' => $pass, 'gametype' => $gametype, 'domain' => $domain, 'gamekind' => $gamekind, 'iframe' => $iframe, 'platformname' => $platformname, 'lang' => $lang], 1);
  223. }
  224. //2.9 用户进入游戏
  225. public function gr($name, $pass, $datestart, $dateend)
  226. {
  227. return $this->commfun(['method' => 'tg', 'username' => $name, 'password' => $pass, 'datestart' => $datestart, 'dateend' => $dateend]);
  228. }
  229. // 2.10 获取用户的体育投注数据
  230. public function gsbrbv($billno, $isjs)
  231. {
  232. return $this->commfun(['method' => 'gsbrbv', 'vendorid' => $billno, 'isjs' => $isjs]);
  233. }
  234. // 3.1 获取用户当天赚钱排行榜 (30分钟缓存)
  235. public function todaycr()
  236. {
  237. return $this->commfun(['method' => 'todaycr']);
  238. }
  239. //...............
  240. }
  241. /**
  242. * 使用例子
  243. * // $config = ['name' => 'agentname', 'key' => 'agentkey', 'secret' => 'agentsecret', 'domain' => 'http://sports.bocai108.com', 'cachefile' => '/tmp/t234asfasf212128233333.cache'];
  244. * $config = ['name' => 'agentname', 'key' => 'agentkey', 'secret' => 'agentsecret'];
  245. * $proxy = new WagentProxy();
  246. * $proxy->Init($config);
  247. * $ret1 = $proxy->ua('onmygod', '123456789');
  248. *
  249. * print_r($ret1);
  250. */