User.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\admin\common;
  3. class User
  4. {
  5. private $appid = 'cx';
  6. private $appkey = 'cx123123';
  7. private $Sign;
  8. private $param = [];
  9. private $header = ["Content-Type" => "multipart/json;charset=utf-8"];
  10. private $host;
  11. private $api = [
  12. "login" => 'login',
  13. "verifyToken" => 'verifyToken',
  14. "register" => 'register',
  15. "userlist" => 'userlist',
  16. "usersave" => 'usersave',
  17. "userstatus" => 'userstatus',
  18. "userinfo" => 'userinfo',
  19. "setpasswd" => 'setpasswd',
  20. "setcompany" => 'setcompany',
  21. "companystatus" => 'companystatus',
  22. "userlistbycompany" => 'userlistbycompany',
  23. "useradd" => 'useradd',
  24. "userDelete" => 'userDelete',//删除
  25. ];
  26. public function __construct()
  27. {
  28. $this->Sign = new Sign($this->appid, $this->appkey);
  29. $this->host = env("user.hosturl", '');
  30. }
  31. /**
  32. * 生成请求header参数
  33. */
  34. private function makeHeader()
  35. {
  36. $headerArr = ["appid" => $this->appid, "noce" => randomkeys(16), "sign" => '', "timestamp" => time()];
  37. $value = array_merge($this->param, $headerArr);
  38. $headerArr['sign'] = $this->Sign->makeSign($value);
  39. foreach ($headerArr as $key => $value) {
  40. $this->header[] = $key . ":" . $value;
  41. }
  42. }
  43. /**模拟post 请求数据
  44. * @param $url
  45. * @param array $param
  46. */
  47. private function post($url, $param = [])
  48. {
  49. $this->param = $param;
  50. $this->makeHeader();
  51. if (is_array($param)) $post = http_build_query($param);
  52. $curl = curl_init();
  53. curl_setopt($curl, CURLOPT_URL, $url);
  54. curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
  55. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  56. curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  57. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  58. if ($post) {
  59. curl_setopt($curl, CURLOPT_POST, 1);
  60. curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
  61. }
  62. curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  63. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  64. curl_setopt($curl, CURLOPT_HTTPHEADER, $this->header);
  65. $data = curl_exec($curl);
  66. if (curl_errno($curl)) {
  67. return curl_error($curl);
  68. }
  69. curl_close($curl);
  70. return $data;
  71. }
  72. /** 登录接口
  73. * @param $param
  74. * @return bool|string
  75. */
  76. public function Login($param)
  77. {
  78. return $this->post($this->host . $this->api["login"], $param);
  79. }
  80. public function VerifyTokens($param)
  81. {
  82. return $this->post($this->host . $this->api["verifyToken"], $param);
  83. }
  84. public function GetUserInfo($param)
  85. {
  86. return $this->post($this->host . $this->api["userinfo"], $param);
  87. }
  88. public function GetUserlist($param)
  89. {
  90. return $this->post($this->host . $this->api["userlist"], $param);
  91. }
  92. public function GetList($param)
  93. {
  94. return $this->post($this->host . $this->api["userlistbycompany"], $param);
  95. }
  96. public function GetAccountall($param)
  97. {
  98. return $this->post($this->host . $this->api["userlist"], $param);
  99. }
  100. public function resetPasswd($param)
  101. {
  102. return $this->post($this->host . $this->api["setpasswd"], $param);
  103. }
  104. public function resetInfo($param)
  105. {
  106. return $this->post($this->host . $this->api["usersave"], $param);
  107. }
  108. public function resetState($param)
  109. {
  110. return $this->post($this->host . $this->api["userstatus"], $param);
  111. }
  112. //删除
  113. public function userDelete(array $param = [])
  114. {
  115. return $this->post($this->host . $this->api['userDelete'], $param);
  116. }
  117. }