common.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. // 应用公共文件
  3. use think\facade\Config;
  4. // 应用公共文件
  5. function app_show($code=0,$message="",$data=[]){
  6. $result = ['code'=>$code,"message"=>$message,"data"=>$data];
  7. echo json_encode($result,JSON_UNESCAPED_UNICODE);
  8. }
  9. // 应用公共文件
  10. function error_show($code=0,$message=""){
  11. $result = ['code'=>$code,"message"=>$message];
  12. echo json_encode($result,JSON_UNESCAPED_UNICODE);
  13. }
  14. function GetUserInfo($token){
  15. $host = Config::get("app");
  16. $url = $host["api_host"]."/Api/userinfo";
  17. $data=[
  18. "token"=>$token
  19. ];
  20. $response=curl_request($url,$data);
  21. return json_decode($response,true);
  22. }
  23. //参数1:访问的URL,参数2:post数据(不填则为GET),参数3:提交的$cookies,参数4:是否返回$cookies
  24. function curl_request($url,$post=''){
  25. $curl = curl_init();
  26. curl_setopt($curl, CURLOPT_URL, $url);
  27. curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
  28. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  29. curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  30. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  31. if($post) {
  32. curl_setopt($curl, CURLOPT_POST, 1);
  33. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
  34. }
  35. curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  36. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  37. $data = curl_exec($curl);
  38. if (curl_errno($curl)) {
  39. return curl_error($curl);
  40. }
  41. curl_close($curl);
  42. return $data;
  43. }
  44. /**手机号验证
  45. * @param $mobile
  46. * @return bool
  47. */
  48. function checkMobile($mobile){
  49. if (!is_numeric($mobile)) {
  50. return false;
  51. }
  52. return preg_match('#^1[3,4,5,6,7,8,9]{1}[\d]{9}$#', $mobile) ? true : false;
  53. }
  54. function checkTel($tel){
  55. if (!$tel) {
  56. return false;
  57. }
  58. return preg_match('/^(0[0-9]{2,3}\-)([0-9]{7,8})+(\-[0-9]{1,4})?$/', $tel) ? true : false;
  59. }
  60. /**邮箱验证
  61. * @param $email
  62. * @return bool
  63. */
  64. function checkEmail($email){
  65. if (!$email) {
  66. return false;
  67. }
  68. return preg_match('#[a-z0-9&\-_.]+@[\w\-_]+([\w\-.]+)?\.[\w\-]+#is', $email) ? true : false;
  69. }
  70. /**
  71. * @param
  72. * @return int
  73. */
  74. function makeSalt(){
  75. $salt = rand(10000000,99999999);
  76. return $salt;
  77. }
  78. /**
  79. * @param $token
  80. * @return array
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\DbException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. * @throws \think\exception\DbException
  85. */
  86. function VerifyTokens($token){
  87. $host = Config::get("app");
  88. $url = $host["api_host"]."/Api/verify_token";
  89. $data=[
  90. "token"=>$token
  91. ];
  92. $response=curl_request($url,$data);
  93. return json_decode($response,true);
  94. }
  95. /**
  96. * @param $token
  97. * @param $condition
  98. * @return mixed
  99. */
  100. function GetUserlist($token,$condition){
  101. $host = Config::get("app");
  102. $url = $host["api_host"]."/Api/getuserlist";
  103. $condition['token']=$token;
  104. $response=curl_request($url,$condition);
  105. return json_decode($response,true);
  106. }
  107. /**
  108. * @param $token
  109. * @param $condition
  110. * @return mixed
  111. */
  112. function GetAccountall($token){
  113. $host = Config::get("app");
  114. $url = $host["api_host"]."/Api/userall";
  115. $condition['token']=$token;
  116. $response=curl_request($url,$condition);
  117. return json_decode($response,true);
  118. }
  119. function GetList($token,$condition){
  120. $host = Config::get("app");
  121. $url = $host["api_host"]."/Api/userlist";
  122. $condition['token']=$token;
  123. $response=curl_request($url,$condition);
  124. return json_decode($response,true);
  125. }
  126. function GetInfoById($token,$condition){
  127. $host = Config::get("app");
  128. $url = $host["api_host"]."/Api/userinfobyid";
  129. $condition['token']=$token;
  130. $response=curl_request($url,$condition);
  131. return json_decode($response,true);
  132. }
  133. function makeNo($str){
  134. $date=date("mdHis");
  135. $year = date("Y")-2000;
  136. $msec=randomkeys(4);
  137. return $str.$msec.$year.$date;
  138. }
  139. function randomkeys($length) {
  140. $returnStr='';
  141. $pattern = '1234567890abcdefghijklmnopqrstuvwxyz';//ABCDEFGHIJKLOMNOPQRSTUVWXYZ
  142. for($i = 0; $i < $length; $i ++) {
  143. $returnStr .= $pattern[mt_rand ( 0, strlen($pattern)-1 )]; //生成php随机数
  144. }
  145. return $returnStr;
  146. }
  147. /**
  148. * @param $files
  149. * @return array
  150. */
  151. function UploadImg($files){
  152. $savename = [];
  153. $files= !is_array($files) ? [$files] : $files;
  154. try{
  155. //验证
  156. validate(['imgFile'=>['fileSize'=>10240000,'fileExt'=>'jpg,jpeg,png,bmp,gif', 'fileMime'=>'image/jpeg,image/png,image/gif']])->check(['imgFile'=>$files]);
  157. foreach($files as $file){
  158. $url= Filesystem::disk('public')->putFile( 'topic/'.date("Ymd"), $file,function ()use($file){
  159. return str_replace('.'.$file->getOriginalExtension(),'',$file->getOriginalName()."_".date('YmdHis'));
  160. });
  161. $name = str_replace('.'.$file->getOriginalExtension(),'',$file->getOriginalName());
  162. $temp = ["url"=>$url,"name"=>$name];
  163. $savename[]=$temp;
  164. }
  165. return $savename;
  166. }catch (\think\exception\ValidateException $e) {
  167. return $e->getMessage();
  168. }
  169. }