WagentProxy.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. $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. //...............
  232. }
  233. /**
  234. * 使用例子
  235. * // $config = ['name' => 'agentname', 'key' => 'agentkey', 'secret' => 'agentsecret', 'domain' => 'http://sports.bocai108.com', 'cachefile' => '/tmp/t234asfasf212128233333.cache'];
  236. * $config = ['name' => 'agentname', 'key' => 'agentkey', 'secret' => 'agentsecret'];
  237. * $proxy = new WagentProxy();
  238. * $proxy->Init($config);
  239. * $ret1 = $proxy->ua('onmygod', '123456789');
  240. *
  241. * print_r($ret1);
  242. */