Sport.php 9.5 KB

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