Sport.php 9.7 KB

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