User.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. 'userCompanyBasicList' => 'userCompanyBasicList',//【列表】
  27. 'userCompanyBasicAdd' => 'userCompanyBasicAdd',//【添加】
  28. 'userCompanyBasicDelete' => 'userCompanyBasicDelete',//【删除】
  29. 'userCompanyBasicUpdate' => 'userCompanyBasicUpdate',//【修改】
  30. 'userCompanyBasicStatus' => 'userCompanyBasicStatus',//【启禁用】
  31. 'userCompanyBasicInfo' => 'userCompanyBasicInfo',//【详情】
  32. //【公司账号查询】
  33. 'userCompanyList' => 'userCompanyList',//【列表】
  34. 'userCompanyStatus' => 'userCompanyStatus',//【启禁用】
  35. 'userCompanyInfo' => 'userCompanyInfo',//【详情】
  36. ];
  37. public function __construct()
  38. {
  39. $this->Sign = new Sign($this->appid, $this->appkey);
  40. $this->host = env("user.hosturl", '');
  41. }
  42. /**
  43. * 生成请求header参数
  44. */
  45. private function makeHeader()
  46. {
  47. $headerArr = ["appid" => $this->appid, "noce" => randomkeys(16), "sign" => '', "timestamp" => time()];
  48. $value = array_merge($this->param, $headerArr);
  49. $headerArr['sign'] = $this->Sign->makeSign($value);
  50. foreach ($headerArr as $key => $value) {
  51. $this->header[] = $key . ":" . $value;
  52. }
  53. }
  54. /**模拟post 请求数据
  55. * @param $url
  56. * @param array $param
  57. */
  58. private function post($url, $param = [])
  59. {
  60. $this->param = $param;
  61. $this->makeHeader();
  62. if (is_array($param)) $post = http_build_query($param);
  63. $curl = curl_init();
  64. curl_setopt($curl, CURLOPT_URL, $url);
  65. curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
  66. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  67. curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  68. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  69. if ($post) {
  70. curl_setopt($curl, CURLOPT_POST, 1);
  71. curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
  72. }
  73. curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  74. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  75. curl_setopt($curl, CURLOPT_HTTPHEADER, $this->header);
  76. $data = curl_exec($curl);
  77. if (curl_errno($curl)) {
  78. return curl_error($curl);
  79. }
  80. curl_close($curl);
  81. return $data;
  82. }
  83. /** 登录接口
  84. * @param $param
  85. * @return bool|string
  86. */
  87. public function Login($param)
  88. {
  89. return $this->post($this->host . $this->api["login"], $param);
  90. }
  91. public function VerifyTokens($param)
  92. {
  93. return $this->post($this->host . $this->api["verifyToken"], $param);
  94. }
  95. public function GetUserInfo($param)
  96. {
  97. return $this->post($this->host . $this->api["userinfo"], $param);
  98. }
  99. public function GetUserlist($param)
  100. {
  101. return $this->post($this->host . $this->api["userlist"], $param);
  102. }
  103. public function GetList($param)
  104. {
  105. return $this->post($this->host . $this->api["userlistbycompany"], $param);
  106. }
  107. public function GetAccountall($param)
  108. {
  109. return $this->post($this->host . $this->api["userlist"], $param);
  110. }
  111. public function resetPasswd($param)
  112. {
  113. return $this->post($this->host . $this->api["setpasswd"], $param);
  114. }
  115. public function resetInfo($param)
  116. {
  117. return $this->post($this->host . $this->api["usersave"], $param);
  118. }
  119. public function resetState($param)
  120. {
  121. return $this->post($this->host . $this->api["userstatus"], $param);
  122. }
  123. //删除
  124. public function userDelete(array $param = [])
  125. {
  126. return $this->post($this->host . $this->api['userDelete'], $param);
  127. }
  128. //修改
  129. public function userSave(array $param = [])
  130. {
  131. return $this->post($this->host . $this->api['usersave'], $param);
  132. }
  133. //通用
  134. public function handle(string $key = '', array $param = [])
  135. {
  136. return $this->post($this->host . $this->api[$key], $param);
  137. }
  138. }