common.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. <?php
  2. // 应用公共文件
  3. use think\facade\Config;
  4. use think\facade\Db;
  5. use think\facade\Filesystem;
  6. // 应用公共文件
  7. function app_show($code=0,$message="",$data=[]){
  8. $result = ['code'=>$code,"message"=>$message,"data"=>$data];
  9. echo json_encode($result,JSON_UNESCAPED_UNICODE);
  10. die();
  11. }
  12. // 应用公共文件
  13. function error_show($code=0,$message=""){
  14. $result = ['code'=>$code,"message"=>$message];
  15. echo json_encode($result,JSON_UNESCAPED_UNICODE);
  16. die();
  17. }
  18. function GetUserInfo($token){
  19. $host = Config::get("app");
  20. $url = $host["api_host"]."/Api/userinfo";
  21. $data=[
  22. "token"=>$token
  23. ];
  24. $response=curl_request($url,$data);
  25. return json_decode($response,true);
  26. }
  27. //参数1:访问的URL,参数2:post数据(不填则为GET),参数3:提交的$cookies,参数4:是否返回$cookies
  28. function curl_request($url,$post=''){
  29. $curl = curl_init();
  30. curl_setopt($curl, CURLOPT_URL, $url);
  31. curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
  32. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  33. curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  34. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  35. if($post) {
  36. curl_setopt($curl, CURLOPT_POST, 1);
  37. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
  38. }
  39. curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  40. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  41. $data = curl_exec($curl);
  42. if (curl_errno($curl)) {
  43. return curl_error($curl);
  44. }
  45. curl_close($curl);
  46. return $data;
  47. }
  48. /**手机号验证
  49. * @param $mobile
  50. * @return bool
  51. */
  52. function checkMobile($mobile){
  53. if (!is_numeric($mobile)) {
  54. return false;
  55. }
  56. return preg_match('#^1[3,4,5,6,7,8,9]{1}[\d]{9}$#', $mobile) ? true : false;
  57. }
  58. function checkTel($tel){
  59. if (!$tel) {
  60. return false;
  61. }
  62. return preg_match('/^(0[0-9]{2,3}\-)([0-9]{7,8})+(\-[0-9]{1,4})?$/', $tel) ? true : false;
  63. }
  64. /**邮箱验证
  65. * @param $email
  66. * @return bool
  67. */
  68. function checkEmail($email){
  69. if (!$email) {
  70. return false;
  71. }
  72. return preg_match('#[a-z0-9&\-_.]+@[\w\-_]+([\w\-.]+)?\.[\w\-]+#is', $email) ? true : false;
  73. }
  74. /**
  75. * @param
  76. * @return int
  77. */
  78. function makeSalt(){
  79. $salt = rand(10000000,99999999);
  80. return $salt;
  81. }
  82. /**
  83. * @param $token
  84. * @return array
  85. * @throws \think\db\exception\DataNotFoundException
  86. * @throws \think\db\exception\DbException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. * @throws \think\exception\DbException
  89. */
  90. function VerifyTokens($token){
  91. $host = Config::get("app");
  92. $url = $host["api_host"]."/Api/verify_token";
  93. $data=[
  94. "token"=>$token
  95. ];
  96. $response=curl_request($url,$data);
  97. return json_decode($response,true);
  98. }
  99. /**
  100. * @param $token
  101. * @param $condition
  102. * @return mixed
  103. */
  104. function GetUserlist($token,$condition){
  105. $host = Config::get("app");
  106. $url = $host["api_host"]."/Api/getuserlist";
  107. $condition['token']=$token;
  108. $response=curl_request($url,$condition);
  109. return json_decode($response,true);
  110. }
  111. /**
  112. * @param $token
  113. * @param $condition
  114. * @return mixed
  115. */
  116. function GetAccountall($token, $condition){
  117. $host = Config::get("app");
  118. $url = $host["api_host"]."/Api/userall";
  119. $condition['token']=$token;
  120. $response=curl_request($url,$condition);
  121. return json_decode($response,true);
  122. }
  123. function GetList($token,$condition){
  124. $host = Config::get("app");
  125. $url = $host["api_host"]."/Api/userlist";
  126. $condition['token']=$token;
  127. $response=curl_request($url,$condition);
  128. return json_decode($response,true);
  129. }
  130. /**
  131. * @param $token
  132. * @param $condition ['id'=>1]
  133. * @return mixed
  134. */
  135. function GetInfoById($token,$condition){
  136. $host = Config::get("app");
  137. $url = $host["api_host"]."/Api/userinfobyid";
  138. $condition['token']=$token;
  139. $response=curl_request($url,$condition);
  140. return json_decode($response,true);
  141. }
  142. function makeNo($str){
  143. $date=date("mdHis");
  144. $year = date("Y")-2000;
  145. $msec=randomkeys(4);
  146. return $str.$msec.$year.$date;
  147. }
  148. function randomkeys($length) {
  149. $returnStr='';
  150. $pattern = '1234567890abcdefghijklmnopqrstuvwxyz';//ABCDEFGHIJKLOMNOPQRSTUVWXYZ
  151. for($i = 0; $i < $length; $i ++) {
  152. $returnStr .= $pattern[mt_rand ( 0, strlen($pattern)-1 )]; //生成php随机数
  153. }
  154. return $returnStr;
  155. }
  156. function tree(){
  157. }
  158. /**
  159. * @param $files
  160. * @return array
  161. */
  162. function UploadImg($files)
  163. {
  164. $savename = [];
  165. $files = !is_array($files) ? [$files] : $files;
  166. try {
  167. //验证
  168. validate(['imgFile' => ['fileSize' => 10240000, 'fileExt' => 'jpg,jpeg,png,bmp,gif', 'fileMime' => 'image/jpeg,image/png,image/gif']])->check(['imgFile' => $files]);
  169. foreach ($files as $file) {
  170. $url = Filesystem::disk('public')->putFile('topic/' . date("Ymd"), $file, function () use ($file) {
  171. return str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName() . "_" . date('YmdHis'));
  172. });
  173. $name = str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName());
  174. $temp = ["url" => $url, "name" => $name];
  175. $savename[] = $temp;
  176. }
  177. return $savename;
  178. } catch (\think\exception\ValidateException $e) {
  179. return $e->getMessage();
  180. }
  181. }
  182. /**
  183. * @param $files
  184. * @return array
  185. */
  186. function UploadFile($files)
  187. {
  188. $savename = [];
  189. $files = !is_array($files) ? [$files] : $files;
  190. try {
  191. //验证
  192. validate(['imgFile' => ['fileSize' => 10240000,'fileExt' => 'xlsx,xls,pdf']])->check(['imgFile' => $files]);
  193. foreach ($files as $file) {
  194. $url = Filesystem::disk('public')->putFile('files/' . date("Ymd"), $file, function () use ($file) {
  195. return str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName() . "_" . date('YmdHis'));
  196. });
  197. $name = str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName());
  198. $temp = ["url" => $url, "name" => $name];
  199. $savename[] = $temp;
  200. }
  201. return $savename;
  202. } catch (\think\exception\ValidateException $e) {
  203. return $e->getMessage();
  204. }
  205. }
  206. /**
  207. * @param $token
  208. * @param $condition
  209. * @return mixed
  210. */
  211. function resetpwd($token,$condition){
  212. $host = Config::get("app");
  213. $url = $host["api_host"]."/Api/passset";
  214. $condition['token']=$token;
  215. $response=curl_request($url,$condition);
  216. return json_decode($response,true);
  217. }
  218. function resetpasswd($token,$condition){
  219. $host = Config::get("app");
  220. $url = $host["api_host"]."/Api/passsave";
  221. $condition['token']=$token;
  222. $response=curl_request($url,$condition);
  223. return json_decode($response,true);
  224. }
  225. /**
  226. * @param $token
  227. * @param $condition
  228. * @return mixed
  229. */
  230. function resetinfo($token,$condition){
  231. $host = Config::get("app");
  232. $url = $host["api_host"]."/Api/usersave";
  233. $condition['token']=$token;
  234. $response=curl_request($url,$condition);
  235. return json_decode($response,true);
  236. }
  237. /**
  238. * @param $token
  239. * @param $condition
  240. * @return mixed
  241. */
  242. function resetstatus($token,$condition){
  243. $host = Config::get("app");
  244. $url = $host["api_host"]."/Api/userstatus";
  245. $condition['token']=$token;
  246. $response=curl_request($url,$condition);
  247. return json_decode($response,true);
  248. }
  249. /**
  250. * @param $data
  251. * @throws \think\db\exception\DataNotFoundException
  252. * @throws \think\db\exception\DbException
  253. * @throws \think\db\exception\ModelNotFoundException
  254. */
  255. function crea($data,$vio=0)
  256. {
  257. $db = Db::name("company_item")->where(['pid'=>$data['id'],'is_del'=>0])->select()->toArray();
  258. if($vio==1){
  259. $d = Db::name("depart_user")->where(['itemid'=>$data['id'],'is_del'=>0])->select()->toArray();
  260. if(empty($d)){
  261. $data['item']=[];
  262. }else{
  263. $data['item']=$d;
  264. }
  265. }
  266. if(empty($db)){
  267. $data['child']=[];
  268. return $data;
  269. }
  270. //var_dump($db);
  271. foreach ($db as $p){
  272. $data['child'][]=crea($p,$vio);
  273. }
  274. return $data;
  275. }
  276. function stro($data){
  277. $db=Db::name('cat')->where(['pid'=>$data['id']])->select()->toArray();
  278. if(empty($db)){
  279. $data['child']=[];
  280. return $data;
  281. }
  282. foreach ($db as $item) {
  283. $data['child'][]=stro($item);
  284. }
  285. return $data;
  286. }
  287. function made($var,$data=[]){
  288. $str = Db::name('cat')->where(['id'=>$var])->find();
  289. $vmn =[];
  290. $vmn['id'] =$str['id'];
  291. $vmn['name'] =$str['cat_name'];
  292. array_unshift($data,$vmn);
  293. // $var['id']=made();
  294. if($str['pid']==0){
  295. // krsort($data);
  296. return $data;
  297. }else{
  298. return made($str['pid'],$data);
  299. }
  300. }
  301. function manger($list=[],$level=1){
  302. $var = Db::name('cat')->where(['pid'=>$list,'level'=>$level+1])->column("id");
  303. if(empty($var)){
  304. return $list;
  305. }
  306. $a=array_merge($list,$var);
  307. return manger($a,$level+1);
  308. }
  309. /**
  310. * @param $files
  311. * @param string $extend
  312. * @return array
  313. * @throws PHPExcel_Exception
  314. * @throws PHPExcel_Reader_Exception
  315. */
  316. function upload_excel($files,$extend="xls")
  317. {
  318. // 获取表单上传文件
  319. try {
  320. validate([
  321. 'file' => [
  322. // 限制文件大小(单位b),这里限制为4M
  323. //fileSize' => 4 * 1024 * 1024,
  324. 'fileExt' => 'xlsx,xls'
  325. ]
  326. ],
  327. [
  328. //'file.fileSize' => '文件太大',
  329. 'file.fileExt' => '不支持的文件',
  330. ]
  331. )->check(['file' => $files]);
  332. // $name = $files->getOriginalExtension();
  333. if ($extend == 'xlsx') {
  334. $objReader = PHPExcel_IOFactory::createReader('Excel2007');
  335. } else {
  336. $objReader = PHPExcel_IOFactory::createReader('Excel5');
  337. }
  338. $savename = Filesystem::disk('public')->putFile('topic/excel', $files);
  339. $import_path = root_path() . 'public/storage/' . $savename;
  340. $spreadsheet = $objReader->load($import_path);
  341. $sheet = $spreadsheet->getActiveSheet();
  342. $sheetData = $sheet->toArray();
  343. if (empty($sheetData) || !is_array($sheetData)) {
  344. return ['code' => 1003, "msg" => '数据不能为空'];
  345. }
  346. $list = [];
  347. foreach ($sheetData as $key => $value) {
  348. $list[] = $value;
  349. }
  350. return ['code' => 0, "msg" => '数据解析成功', 'data' => $list];
  351. } catch (think\exception\ValidateException $e) {
  352. // echo $e->getMessage();
  353. return ['code' => 1003, "msg" => $e->getMessage()];
  354. }
  355. }
  356. /**
  357. * @param string $fileName
  358. * @param array $headArr
  359. * @param array $data
  360. */
  361. function excelSave($fileName = '', $headArr = [], $data = [])
  362. {
  363. $objPHPExcel = new PHPExcel();
  364. $objPHPExcel->getProperties();
  365. $keyA = 0; // 设置表头
  366. foreach ($headArr as $v) {
  367. $colum = PHPExcel_Cell::stringFromColumnIndex($keyA);
  368. $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
  369. $keyA += 1;
  370. }
  371. $column = 2;
  372. $objActSheet = $objPHPExcel->getActiveSheet();
  373. foreach ($data as $key => $rows) { // 行写入
  374. $span = 0;
  375. foreach ($rows as $keyName => $value) { // 列写入
  376. //判断数据是否有数组,如果有数组,转换成字符串
  377. if(is_array($value)){
  378. $value = implode("、", $value);
  379. }
  380. $objActSheet->setCellValue(PHPExcel_Cell::stringFromColumnIndex($span) . $column, $value);
  381. $span++;
  382. }
  383. $column++;
  384. }
  385. // var_dump($objActSheet->getActiveCell());
  386. $file = $fileName. ".xls";
  387. //$fileName .= "_" . date("Y_m_d", Request()->instance()->time()) . ".xls";
  388. //$fileName = iconv("utf-8", "gb2312", $fileName); // 重命名表
  389. $dir =root_path() . 'public/storage/report/'.date("YmdHis")."/";
  390. if(!is_dir($dir)){
  391. mkdir($dir,0777,true);
  392. }
  393. $objPHPExcel->setActiveSheetIndex(0); // 设置活动单指数到第一个表,所以Excel打开这是第一个表
  394. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  395. $objWriter->save($dir . $file); // 文件通过浏览器下载
  396. $url = $dir . $file;
  397. if(!file_exists($url)){
  398. echo "文件生成失败";
  399. }
  400. $saveDir = root_path()."public/storage/zip/";
  401. if(!is_dir( $saveDir)){
  402. mkdir($saveDir,0777,true);
  403. }
  404. $datetime = date("Y-m-d H:i:s");
  405. $file_dir = $saveDir.$datetime.".zip";
  406. # 5.1 文件打包,提示:使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释
  407. $zip = new \ZipArchive ();
  408. # 5.2 文件不存在则生成一个新的文件 用CREATE打开文件会追加内容至zip
  409. if ($zip->open($file_dir, \ZipArchive::OVERWRITE) !== true && $zip->open($file_dir, \ZipArchive::CREATE) !==
  410. true) echo '无法打开文件或者文件创建失败';
  411. # 5.3 批量写入压缩包
  412. $zip->addEmptyDir($fileName);
  413. // @$zip->addFile($v['file_path'], 'resume'.DIRECTORY_SEPARATOR.basename($headername));
  414. @$zip->addFile($url,$fileName.DIRECTORY_SEPARATOR.basename($url));
  415. # 5.4 关闭压缩包写入
  416. $zip->close();
  417. @deldir($dir);
  418. # 6. 检查文件是否存在,并输出文件
  419. if (! file_exists ( $file_dir )) echo '简历文件不存在';
  420. ob_clean();
  421. flush();
  422. header("Cache-Control: max-age=0");
  423. header("Content-Description: File Transfer");
  424. header('Content-disposition: attachment; filename=' . basename($file_dir)); # 处理文件名
  425. header("Content-Type: application/octet-stream"); # 流文件输出
  426. header("Content-Transfer-Encoding: binary"); # 告诉浏览器,这是二进制文件
  427. header('Content-Length: ' . filesize($file_dir)); # 告诉浏览器,文件大小
  428. readfile($file_dir); # 输出文件
  429. @ unlink($file_dir);
  430. exit();
  431. }
  432. function deldir($path){
  433. //如果是目录则继续
  434. if(is_dir($path)){
  435. //扫描一个文件夹内的所有文件夹和文件并返回数组
  436. $p = scandir($path);
  437. //如果 $p 中有两个以上的元素则说明当前 $path 不为空
  438. if(count($p)>2){
  439. foreach($p as $val){
  440. //排除目录中的.和..
  441. if($val !="." && $val !=".."){
  442. //如果是目录则递归子目录,继续操作
  443. if(is_dir($path.$val)){
  444. //子目录中操作删除文件夹和文件
  445. deldir($path.$val.'/');
  446. }else{
  447. //如果是文件直接删除
  448. unlink($path.$val);
  449. }
  450. }
  451. }
  452. }
  453. }
  454. //删除目录
  455. return rmdir($path);
  456. }
  457. /**
  458. * @param string $AddrJson
  459. */
  460. function GetAddr($AddrJson=""){
  461. if($AddrJson==""){
  462. return '';
  463. }
  464. $adr = json_decode($AddrJson,true);
  465. if(!is_array($adr)){
  466. return '';
  467. }
  468. $addr='';
  469. if(isset($adr['provice_code'])&&$adr['provice_code']!=''){
  470. $provice = Db::name("province")->where(["province_code"=>$adr['provice_code']])->find();
  471. $addr.=isset($provice['name']) ? $provice['name']:"";
  472. }
  473. if(isset($adr['city_code'])&&$adr['city_code']!=''){
  474. $city = Db::name("city")->where(["city_code"=>$adr['city_code']])->find();
  475. $addr.=isset($city['name']) ? $city['name']:"";
  476. }
  477. if(isset($adr['area_code'])&&$adr['area_code']!=''){
  478. $area = Db::name("area")->where(["area_code"=>$adr['area_code']])->find();
  479. $addr.=isset($area['name']) ? $area['name']:"";
  480. }
  481. return $addr;
  482. }