WithdrawService.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * @Created by PhpStorm
  4. * @author: Kirin
  5. * @day: 2023/12/19
  6. * @time: 16:09
  7. */
  8. namespace crmeb\services;
  9. use crmeb\exceptions\ApiException;
  10. class WithdrawService
  11. {
  12. static $url = 'https://testshuichou.zhuoyankeji.com';
  13. // static $url = 'https://api.yeeshui.com';
  14. static $token;
  15. static $user_name = '星领测试';
  16. static $password = '123456';
  17. static $secret = '2a879ac637d65ced5ed5892e1bf82e4c';
  18. static $aeskey = '37d65ced5ed5892e';
  19. public static function init()
  20. {
  21. self::$token = CacheService::get('withdraw_token', '');
  22. if (!self::$token) {
  23. self::$token = self::login();
  24. CacheService::set('withdraw_token', self::$token, 500);
  25. }
  26. }
  27. public static function login()
  28. {
  29. $data = [
  30. 'user_name' => self::$user_name,
  31. 'password' => self::$password,
  32. 'timestamp' => time(),
  33. ];
  34. ksort($data);
  35. $signString = http_build_query($data) . '&secret=' . self::$secret;
  36. $sign = md5($signString);
  37. $data['sign'] = $sign;
  38. $res = json_decode(self::do_request(self::$url . '/sdk/v1/login', $data, ['content-type:application/json'], true, true), true);
  39. if ($res['code'] == 200) {
  40. return $res['token'];
  41. } else {
  42. throw new ApiException($res['msg'] ?? '企业登陆失败');
  43. }
  44. }
  45. public static function request($url, $data)
  46. {
  47. $url = self::$url . $url;
  48. $data = ['token' => self::$token, 'data' => $data];
  49. return json_decode(self::do_request(self::$url . $url, $data, ['content-type:application/json'], true, true), true);
  50. }
  51. protected static function do_request($url, $data, $header = null, $post = true, $json = false, $format = 0, $form = false)
  52. {
  53. $curl = curl_init();
  54. curl_setopt($curl, CURLOPT_URL, $url);
  55. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  56. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  57. if ($post) {
  58. curl_setopt($curl, CURLOPT_POST, 1);
  59. if (!$json && !$form) {
  60. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
  61. } else if ($json && !$form) {
  62. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data, $format));
  63. } else {
  64. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  65. }
  66. }
  67. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  68. if ($header) {
  69. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  70. curl_setopt($curl, CURLOPT_HEADER, 0);
  71. }
  72. $result = curl_exec($curl);
  73. if (curl_errno($curl)) {
  74. return json_encode(['status' => curl_errno($curl), 'msg' => '请求失败']);
  75. }
  76. curl_close($curl);
  77. return $result;
  78. }
  79. }