Kuaishou.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. namespace app\api\controller;
  3. class Kuaishou extends Base
  4. {
  5. public $appKey = 'ks698620896473026758';
  6. public $appSecret = 'cvbOgiVC6rSvPs0sBASfkg';
  7. public $signSecret = '71443462ca217e1c29425e7aabc257cc';
  8. public $msgSecret = '4fxDQv7tJKzIc1D7CNNe6A==';
  9. public $serverHost = "https://openapi.kwaixiaodian.com/";
  10. public $access_token = 'ChFvYXV0aC5hY2Nlc3NUb2tlbhJwJsfgEcw8cYbOARym_nQvgdOKc9-8eKn_0pLGJ5VMMOdq7ql_yEETn74H-BFNDeSPq1ujcKxf55SaRrJ4O0WfT86MY-oqzU79eysFt2AcgOOXc49AcscyjKm3I8RTjzC1CIBTmaNdrtYxDTyc_kjjNxoSIEH75Kg-RaqlrUNstFhsUg4wIiDQ_BkNN4qQCet_XmgzWrAJmuoISlLGlgM6WJh5djxTpigFMAE';
  11. public $debug = false;
  12. public $code = "ae727ca7a93aa4434335c4d3e9a1773d169a36f6c65b965a726a1d3c8b85699e62cfd671";
  13. public function notify()
  14. {
  15. $data = $this->request->param();
  16. $url = $this->serverHost . "oauth2/access_token?app_id={$this->appKey}&app_secret={$this->appSecret}&code={$data['code']}&grant_type=authorization_code";
  17. $s = $this->curl_https_get($url);
  18. $s = json_decode($s, true);
  19. var_dump($data,$s);
  20. // return isset($s['access_token']) ? $s['access_token'] : '';
  21. }
  22. public function getAccessToken()
  23. {
  24. // $url = $this->serverHost . "oauth2/access_token?app_id={$this->appKey}&app_secret={$this->appSecret}&code={$this->code}&grant_type=authorization_code";
  25. $url = $this->serverHost . "oauth2/access_token?app_id={$this->appKey}&app_secret={$this->appSecret}&grant_type=client_credentials";
  26. $s = $this->curl_https_get($url);
  27. $s = json_decode($s, true);
  28. return isset($s['access_token']) ? $s['access_token'] : '';
  29. }
  30. public function getSelTime($sel = 30,$days =3)
  31. {
  32. $start = $sel;
  33. $end = $sel - $days;
  34. var_dump([strtotime("- $start days") * 1000, strtotime("-$end days") * 1000]);
  35. // return [strtotime("- $start days") * 1000, strtotime("-$end days") * 1000];
  36. }
  37. /**
  38. * @title 获取订单详情【商家自己的】
  39. * @desc 获取商家视角的订单详情
  40. * @author qc
  41. * @url /api/Kuaishou/getOrderDetail
  42. * @method GET
  43. * @header name:Authorization require:1 desc:Token
  44. * @param name:oid type:int request:1 default:0 desc:id
  45. */
  46. public function getOrderDetail()
  47. {
  48. $apiType = "open.order.detail";
  49. $method = 'get';
  50. $param = ['oid'=>input('oid')];
  51. $arr['appkey'] = $this->appKey;
  52. $arr['version'] = 1;
  53. $arr['access_token'] = $this->access_token;
  54. $arr['timestamp'] = $this->getMillisecond();
  55. $arr['method'] = trim($apiType);
  56. $arr['param'] = $param ? json_encode($param) : '{}';
  57. $arr['signMethod'] = 'MD5';
  58. ksort($arr); // 排序
  59. $arr['sign'] = $this->getSign($arr, $this->signSecret);
  60. $apiInfo = str_replace('.', '/', $arr['method']);
  61. $url = $this->serverHost . $apiInfo;
  62. $s = static::apiSend($url,$arr,$method);
  63. $this->success('ok',[
  64. 'res'=>$s,
  65. 'token'=>$arr['access_token'],
  66. 'param'=>$param,
  67. 'api'=>$apiType ]);
  68. }
  69. /**
  70. * @title 获取订单费用详情【商家自己的】
  71. * @desc 获取订单费用详情
  72. * @author qc
  73. * @url /api/Kuaishou/getOrderFee
  74. * @method GET
  75. * @header name:Authorization require:1 desc:Token
  76. * @param name:oid type:int request:1 default:0 desc:id
  77. */
  78. public function getOrderFee()
  79. {
  80. $apiType = "open.seller.order.fee.detail";
  81. $method = 'get';
  82. $param = ['orderId'=>input('oid')];
  83. $arr['appkey'] = $this->appKey;
  84. $arr['version'] = 1;
  85. $arr['access_token'] = $this->access_token;
  86. $arr['timestamp'] = $this->getMillisecond();
  87. $arr['method'] = trim($apiType);
  88. $arr['param'] = $param ? json_encode($param) : '{}';
  89. $arr['signMethod'] = 'MD5';
  90. ksort($arr); // 排序
  91. $arr['sign'] = $this->getSign($arr, $this->signSecret);
  92. $apiInfo = str_replace('.', '/', $arr['method']);
  93. $url = $this->serverHost . $apiInfo;
  94. $s = static::apiSend($url,$arr,$method);
  95. $this->success('ok',[
  96. 'res'=>$s,
  97. 'token'=>$arr['access_token'],
  98. 'param'=>$param,
  99. 'api'=>$apiType ]);
  100. }
  101. /**
  102. * @title 获取分销订单详情
  103. * @desc 获取分销订单详情
  104. * @author qc
  105. * @url /api/Kuaishou/getFxInfo
  106. * @method GET
  107. * @header name:Authorization require:1 desc:Token
  108. * @param name:oid type:int request:1 default:0 desc:id
  109. */
  110. public function getFxInfo()
  111. {
  112. $apiType = "open.seller.order.cps.detail";
  113. $method = 'get';
  114. $param = ['oid'=>[input('oid')]];
  115. $arr['appkey'] = $this->appKey;
  116. $arr['version'] = 1;
  117. $arr['access_token'] = $this->access_token;
  118. $arr['timestamp'] = $this->getMillisecond();
  119. $arr['method'] = trim($apiType);
  120. $arr['param'] = $param ? json_encode($param) : '{}';
  121. $arr['signMethod'] = 'MD5';
  122. ksort($arr); // 排序
  123. $arr['sign'] = $this->getSign($arr, $this->signSecret);
  124. $apiInfo = str_replace('.', '/', $arr['method']);
  125. $url = $this->serverHost . $apiInfo;
  126. $s = static::apiSend($url,$arr,$method);
  127. $this->success('ok',[
  128. 'res'=>$s,
  129. 'token'=>$arr['access_token'],
  130. 'param'=>$param,
  131. 'api'=>$apiType ]);
  132. }
  133. /**
  134. * @title 获取分销订单列表
  135. * @desc 获取分销订单列表
  136. * @author qc
  137. * @url /api/Kuaishou/getFxList
  138. * @method GET
  139. * @header name:Authorization require:1 desc:Token
  140. * @param name:oid type:int request:1 default:0 desc:id
  141. */
  142. public function getFxList()
  143. {
  144. }
  145. /**
  146. * @title 获取卖家对应买家的订单列表
  147. * @desc 获取分销订单详情
  148. * @author qc
  149. * @url /api/Kuaishou/getOrderList
  150. * @method GET
  151. * @header name:Authorization require:1 desc:Token
  152. * @param name:buyerOpenId type:string request:1 default:0 desc:buyerOpenId
  153. * @param name:cursor type:string request:1 default:0 desc:游标
  154. * @param name:orderStatus type:int request:1 default:0 desc:[0,10,30,40,50,70,80]
  155. */
  156. public function getOrderList()
  157. {
  158. $apiType = "open.order.buyer.order.list";
  159. $method = 'get';
  160. $param = [
  161. 'buyerOpenId'=>input('buyerOpenId'),
  162. 'cursor'=>input('cursor','0'),
  163. 'limit'=> 20,
  164. 'orderStatus'=> [],
  165. 'orderSourceType'=> [],
  166. ];
  167. $arr['appkey'] = $this->appKey;
  168. $arr['version'] = 1;
  169. $arr['access_token'] = $this->access_token;
  170. $arr['timestamp'] = $this->getMillisecond();
  171. $arr['method'] = trim($apiType);
  172. $arr['param'] = $param ? json_encode($param) : '{}';
  173. $arr['signMethod'] = 'MD5';
  174. ksort($arr); // 排序
  175. $arr['sign'] = $this->getSign($arr, $this->signSecret);
  176. $apiInfo = str_replace('.', '/', $arr['method']);
  177. $url = $this->serverHost . $apiInfo;
  178. $s = static::apiSend($url,$arr,$method);
  179. $this->success('ok',['res'=>$s]);
  180. }
  181. private function apiSend($url,$arr,$method)
  182. {
  183. if ($method == 'get') {
  184. $s = $this->curl_https_get($url. '?' . http_build_query($arr, '', '&'), array());
  185. } else {
  186. $s = $this->curl_https_post($url, $arr);
  187. }
  188. return json_decode($s, true);
  189. }
  190. public function getSign($params, $key) {
  191. $unSignParaString = $this->formatQueryParaMap($params, false);
  192. $signStr = (md5($unSignParaString . "&signSecret=" . $this->signSecret));
  193. return $signStr;
  194. }
  195. public function formatQueryParaMap(array $paraMap, $urlEncode = false) {
  196. $buff = "";
  197. ksort($paraMap);
  198. foreach ($paraMap as $k => $v) {
  199. if (null != $v && "null" != $v) {
  200. if ($urlEncode) {
  201. $v = urlencode($v);
  202. }
  203. $buff.= $k . "=" . $v . "&";
  204. }
  205. }
  206. $reqPar = '';
  207. if (strlen($buff) > 0) {
  208. $reqPar = substr($buff, 0, strlen($buff) - 1);
  209. }
  210. return $reqPar;
  211. }
  212. /*获取13位时间戳*/
  213. private static function getMillisecond() {
  214. list($t1, $t2) = explode(' ', microtime());
  215. return sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
  216. }
  217. private function curl_get_contents($url, $data = array(), $https = false) {
  218. $results['error'] = '';
  219. $results['status'] = 0;
  220. $results['data'] = array();
  221. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  222. $curl = curl_init(); // 启动一个CURL会话
  223. if (!empty($data) && is_array($data)) {
  224. curl_setopt($curl, CURLOPT_POST, TRUE);
  225. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
  226. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
  227. }
  228. if ($https) {
  229. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
  230. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
  231. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
  232. }
  233. curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
  234. curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
  235. curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
  236. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
  237. curl_setopt($curl, CURLOPT_USERAGENT, $user_agent); // 模拟用户使用的浏览器
  238. curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
  239. $results['data'] = curl_exec($curl); // 执行操作
  240. if (curl_errno($curl)) {
  241. $results['error'] = curl_error($curl); //捕抓异常
  242. }
  243. curl_close($curl); // 关闭CURL会话
  244. return $results['data']; // 返回数据
  245. }
  246. private function curl_https_post($url, $data) {
  247. return $this->curl_get_contents($url, $data, true);
  248. }
  249. private function curl_https_get($url) {
  250. return $this->curl_get_contents($url, array(), true);
  251. }
  252. }