WithdrawService.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 function __construct($token)
  20. {
  21. self::$token = $token;
  22. }
  23. public static function init()
  24. {
  25. $token = CacheService::get('withdraw_token', '');
  26. if (!$token) {
  27. $token = self::login();
  28. CacheService::set('withdraw_token', $token, 500);
  29. }
  30. return new self($token);
  31. }
  32. public static function getToken()
  33. {
  34. return self::$token;
  35. }
  36. public static function login()
  37. {
  38. $data = [
  39. 'user_name' => self::$user_name,
  40. 'password' => self::$password,
  41. 'timestamp' => time(),
  42. ];
  43. ksort($data);
  44. $signString = http_build_query($data) . '&secret=' . self::$secret;
  45. $sign = md5($signString);
  46. $data['sign'] = $sign;
  47. $res = json_decode(self::do_request(self::$url . '/sdk/v1/login', $data, ['content-type:application/json'], true, true), true);
  48. if ($res['code'] == 200) {
  49. return $res['token'];
  50. } else {
  51. throw new ApiException($res['msg'] ?? '企业登陆失败');
  52. }
  53. }
  54. public static function request($url, $data)
  55. {
  56. $url = self::$url . $url;
  57. $data = ['token' => self::$token, 'data' => $data];
  58. return json_decode(self::do_request(self::$url . $url, $data, ['content-type:application/json'], true, true), true);
  59. }
  60. protected static function do_request($url, $data, $header = null, $post = true, $json = false, $format = 0, $form = false)
  61. {
  62. $curl = curl_init();
  63. curl_setopt($curl, CURLOPT_URL, $url);
  64. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  65. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  66. if ($post) {
  67. curl_setopt($curl, CURLOPT_POST, 1);
  68. if (!$json && !$form) {
  69. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
  70. } else if ($json && !$form) {
  71. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data, $format));
  72. } else {
  73. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  74. }
  75. }
  76. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  77. if ($header) {
  78. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  79. curl_setopt($curl, CURLOPT_HEADER, 0);
  80. }
  81. $result = curl_exec($curl);
  82. if (curl_errno($curl)) {
  83. return json_encode(['status' => curl_errno($curl), 'msg' => '请求失败']);
  84. }
  85. curl_close($curl);
  86. return $result;
  87. }
  88. }