User.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. 'hqList' => 'hqList',
  38. 'hqAdd' => 'hqAdd',
  39. 'hqUpdate' => 'hqUpdate',
  40. 'hqInfo' => 'hqInfo',
  41. 'delete' => 'delete',
  42. 'status' => 'status',
  43. 'sGetList' => 'sGetList',
  44. 'sCreate' => 'sCreate',
  45. 'sInfo' => 'sInfo',
  46. 'sEdit' => 'sEdit',
  47. 'bGetList' => 'bGetList',
  48. 'bCreate' => 'bCreate',
  49. 'bInfo' => 'bInfo',
  50. 'bEdit' => 'bEdit',
  51. 'bTitle' => 'bTitle',
  52. 'cTitle' => 'cTitle',
  53. ];
  54. public function __construct()
  55. {
  56. $this->Sign = new Sign($this->appid, $this->appkey);
  57. $this->host = env("user.hosturl", '');
  58. }
  59. /**
  60. * 生成请求header参数
  61. */
  62. private function makeHeader()
  63. {
  64. $headerArr = ["appid" => $this->appid, "noce" => randomkeys(16), "sign" => '', "timestamp" => time()];
  65. $value = array_merge($this->param, $headerArr);
  66. $headerArr['sign'] = $this->Sign->makeSign($value);
  67. foreach ($headerArr as $key => $value) {
  68. $this->header[] = $key . ":" . $value;
  69. }
  70. }
  71. /**模拟post 请求数据
  72. * @param $url
  73. * @param array $param
  74. */
  75. private function post($url, $param = [])
  76. {
  77. $this->param = $param;
  78. $this->makeHeader();
  79. if (is_array($param)) $post = http_build_query($param);
  80. $curl = curl_init();
  81. curl_setopt($curl, CURLOPT_URL, $url);
  82. curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
  83. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  84. curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  85. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  86. if ($post) {
  87. curl_setopt($curl, CURLOPT_POST, 1);
  88. curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
  89. }
  90. curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  91. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  92. curl_setopt($curl, CURLOPT_HTTPHEADER, $this->header);
  93. $data = curl_exec($curl);
  94. if (curl_errno($curl)) {
  95. return curl_error($curl);
  96. }
  97. curl_close($curl);
  98. return $data;
  99. }
  100. /** 登录接口
  101. * @param $param
  102. * @return bool|string
  103. */
  104. public function Login($param)
  105. {
  106. return $this->post($this->host . $this->api["login"], $param);
  107. }
  108. public function VerifyTokens($param)
  109. {
  110. return $this->post($this->host . $this->api["verifyToken"], $param);
  111. }
  112. public function GetUserInfo($param)
  113. {
  114. return $this->post($this->host . $this->api["userinfo"], $param);
  115. }
  116. public function GetUserlist($param)
  117. {
  118. return $this->post($this->host . $this->api["userlist"], $param);
  119. }
  120. public function GetList($param)
  121. {
  122. return $this->post($this->host . $this->api["userlistbycompany"], $param);
  123. }
  124. public function GetAccountall($param)
  125. {
  126. return $this->post($this->host . $this->api["userlist"], $param);
  127. }
  128. public function resetPasswd($param)
  129. {
  130. return $this->post($this->host . $this->api["setpasswd"], $param);
  131. }
  132. public function resetInfo($param)
  133. {
  134. return $this->post($this->host . $this->api["usersave"], $param);
  135. }
  136. public function resetState($param)
  137. {
  138. return $this->post($this->host . $this->api["userstatus"], $param);
  139. }
  140. //删除
  141. public function userDelete(array $param = [])
  142. {
  143. return $this->post($this->host . $this->api['userDelete'], $param);
  144. }
  145. //修改
  146. public function userSave(array $param = [])
  147. {
  148. return $this->post($this->host . $this->api['usersave'], $param);
  149. }
  150. //通用
  151. public function handle(string $key = '', array $param = [])
  152. {
  153. $rs = $this->post($this->host . $this->api[$key], $param);
  154. return json_decode($rs, true);
  155. }
  156. }