common.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. use think\facade\Config;
  3. use think\facade\Db;
  4. use think\facade\Filesystem;
  5. // 应用公共文件
  6. function error_show($code=0,$message=""){
  7. $result = ['code'=>$code,"message"=>$message];
  8. echo json_encode($result,JSON_UNESCAPED_UNICODE);
  9. die();
  10. }
  11. // 应用公共文件
  12. function app_show($code=0,$message="",$data=[]){
  13. $result = ['code'=>$code,"message"=>$message,"data"=>$data];
  14. echo json_encode($result,JSON_UNESCAPED_UNICODE);
  15. die();
  16. }
  17. function makeNo($str){
  18. $date=date("mdHis");
  19. $year = date("Y")-2000;
  20. $msec=randomkeys(4);
  21. return $str.$msec.$year.$date;
  22. }
  23. function randomkeys($length) {
  24. $returnStr='';
  25. $pattern = '1234567890abcdefghijklmnopqrstuvwxyz';//ABCDEFGHIJKLOMNOPQRSTUVWXYZ
  26. for($i = 0; $i < $length; $i ++) {
  27. $returnStr .= $pattern[mt_rand ( 0, strlen($pattern)-1 )]; //生成php随机数
  28. }
  29. return $returnStr;
  30. }
  31. //参数1:访问的URL,参数2:post数据(不填则为GET),参数3:提交的$cookies,参数4:是否返回$cookies
  32. function curl_request($url,$post=''){
  33. $header = headerSign($post);
  34. if(is_array($post)) $post=http_build_query($post);
  35. $curl = curl_init();
  36. curl_setopt($curl, CURLOPT_URL, $url);
  37. curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
  38. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  39. curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  40. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  41. if($post) {
  42. curl_setopt($curl, CURLOPT_POST, 1);
  43. curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
  44. }
  45. curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  46. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  47. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  48. $data = curl_exec($curl);
  49. if (curl_errno($curl)) {
  50. return curl_error($curl);
  51. }
  52. curl_close($curl);
  53. return $data;
  54. }
  55. if(!function_exists("headerSign")){
  56. function headerSign($post){
  57. $config = Config::get("sign");
  58. $appid=$config['appid'];
  59. $appkey=$config['appkey'];
  60. $headerArr=["appid"=>'123',"noce"=>randomkeys(16),"sign"=>'',"timestamp"=>time()];
  61. $value =array_merge($post,$headerArr);
  62. $Sign= new \Sign($appid,$appkey);
  63. $headerArr['sign'] = $Sign->makeSign($value);
  64. $list=["Content-Type"=>"multipart/json;charset=utf-8"];
  65. foreach ($headerArr as $key=>$value){
  66. $list[]=$key.":".$value;
  67. }
  68. return $list;
  69. }
  70. }
  71. /**
  72. * @param $token
  73. * @return array
  74. * @throws \think\db\exception\DataNotFoundException
  75. * @throws \think\db\exception\DbException
  76. * @throws \think\db\exception\ModelNotFoundException
  77. * @throws \think\exception\DbException
  78. */
  79. function VerifyTokens($token){
  80. $url =env("user.hosturl")."verifyToken";
  81. $data=[
  82. "token"=>$token
  83. ];
  84. $response=curl_request($url,$data);
  85. return json_decode($response,true);
  86. }
  87. /**
  88. * @param $token
  89. * @param $condition
  90. * @return mixed
  91. */
  92. function GetUserlist($token,$condition){
  93. $url = env("user.hosturl")."userlist";
  94. $condition['token']=$token;
  95. $response=curl_request($url,$condition);
  96. return json_decode($response,true);
  97. }
  98. /**
  99. * @param $token
  100. * @param $condition
  101. * @return mixed
  102. */
  103. function GetAccountall($token, $condition){
  104. $url = env("user.hosturl")."userall";
  105. $condition['token']=$token;
  106. $response=curl_request($url,$condition);
  107. return json_decode($response,true);
  108. }
  109. function GetList($token,$condition){
  110. $url = env("user.hosturl")."userlist";
  111. $condition['token']=$token;
  112. $response=curl_request($url,$condition);
  113. return json_decode($response,true);
  114. }
  115. function GetUserInfo($condition){
  116. $url = env("user.hosturl")."userinfo";
  117. $response=curl_request($url,$condition);
  118. return json_decode($response,true);
  119. }
  120. /**手机号验证
  121. * @param $mobile
  122. * @return bool
  123. */
  124. function checkMobile($mobile){
  125. if (!is_numeric($mobile)) {
  126. return false;
  127. }
  128. return preg_match('#^1[3,4,5,6,7,8,9]{1}[\d]{9}$#', $mobile) ? true : false;
  129. }
  130. /**
  131. * @param $token
  132. * @param $condition
  133. * @return mixed
  134. */
  135. function resetinfo($token,$condition){
  136. $url = env("user.hosturl")."usersave";
  137. $condition['token']=$token;
  138. $response=curl_request($url,$condition);
  139. return json_decode($response,true);
  140. }
  141. /**
  142. * @param $token
  143. * @param $condition
  144. * @return mixed
  145. */
  146. function resetstatus($token,$condition){
  147. $host = Config::get("app");
  148. $url = env("user.hosturl")."userstatus";
  149. $condition['token']=$token;
  150. $response=curl_request($url,$condition);
  151. return json_decode($response,true);
  152. }
  153. function crea($data,$vio=0)
  154. {
  155. $db = Db::name("company_item")->where(['pid'=>$data['id'],'is_del'=>0])->select()->toArray();
  156. if($vio==1){
  157. $d = Db::name("depart_user")->where(['itemid'=>$data['id'],'is_del'=>0])->select()->toArray();
  158. if(empty($d)){
  159. $data['item']=[];
  160. }else{
  161. $data['item']=$d;
  162. }
  163. }
  164. if(empty($db)){
  165. $data['child']=[];
  166. return $data;
  167. }
  168. //var_dump($db);
  169. foreach ($db as $p){
  170. $data['child'][]=crea($p,$vio);
  171. }
  172. return $data;
  173. }
  174. /**
  175. * @param $files
  176. * @return array
  177. */
  178. function UploadImg($files)
  179. {
  180. $savename = [];
  181. $files = !is_array($files) ? [$files] : $files;
  182. try {
  183. //验证
  184. validate(['imgFile' => ['fileSize' => 10240000, 'fileExt' => 'jpg,jpeg,png,bmp,gif', 'fileMime' => 'image/jpeg,image/png,image/gif']])->check(['imgFile' => $files]);
  185. foreach ($files as $file) {
  186. $url = Filesystem::disk('public')->putFile('topic/' . date("Ymd"), $file, function () use ($file) {
  187. return str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName() . "_" . date('YmdHis'));
  188. });
  189. $name = str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName());
  190. $temp = ["url" => $url, "name" => $name];
  191. $savename[] = $temp;
  192. }
  193. return $savename;
  194. } catch (\think\exception\ValidateException $e) {
  195. return $e->getMessage();
  196. }
  197. }
  198. function made($var,$data=[]){
  199. $str = Db::name('model')->where(['id'=>$var])->find();
  200. $vmn =[];
  201. $vmn['id'] =$str['id'];
  202. $vmn['rename'] =$str['name'];
  203. array_unshift($data,$vmn);
  204. if($str['pid']==0){
  205. return $data;
  206. }else{
  207. return made($str['pid'],$data);
  208. }
  209. }
  210. /**
  211. * @param $token
  212. * @param $condition ['id'=>1]
  213. * @return mixed
  214. */
  215. function GetInfoById($token,$condition){
  216. $url = env("user.hosturl")."userinfo";
  217. $condition['token']=$token;
  218. $response=curl_request($url,$condition);
  219. return json_decode($response,true);
  220. }
  221. function make($var,$data=[]){
  222. $str = Db::name('company_item')->where(['id'=>$var])->find();
  223. $vmn =[];
  224. $vmn['id'] =$str['id'];
  225. $vmn['name'] =$str['name'];
  226. array_unshift($data,$vmn);
  227. // $var['id']=made();
  228. if($str['pid']==0){
  229. // krsort($data);
  230. return $data;
  231. }else{
  232. return make($str['pid'],$data);
  233. }
  234. }