common.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. return json($result);
  10. }
  11. // 应用公共文件
  12. function error_show($code=0,$message=""){
  13. $result = ['code'=>$code,"message"=>$message];
  14. return json($result);
  15. }
  16. function GetUserInfo($token){
  17. $host = Config::get("app");
  18. $url = $host["api_host"]."/verifyToken";
  19. $data=[
  20. "token"=>$token
  21. ];
  22. $response=curl_request($url,$data);
  23. return json_decode($response,true);
  24. }
  25. function setUserCompany($condition){
  26. $host = Config::get("app");
  27. $url = $host["api_host"]."/setcompany";
  28. $response=curl_request($url,$condition);
  29. return json_decode($response,true);
  30. }
  31. function setCompanyStatus($condition){
  32. $host = Config::get("app");
  33. $url = $host["api_host"]."/companystatus";
  34. $response=curl_request($url,$condition);
  35. return json_decode($response,true);
  36. }
  37. function setStatus($condition){
  38. $host = Config::get("app");
  39. $url = $host["api_host"]."/userstatus";
  40. $response=curl_request($url,$condition);
  41. return json_decode($response,true);
  42. }
  43. /**手机号验证
  44. * @param $mobile
  45. * @return bool
  46. */
  47. function checkMobile($mobile){
  48. if (!is_numeric($mobile)) {
  49. return false;
  50. }
  51. return preg_match('#^1[3,4,5,6,7,8,9]{1}[\d]{9}$#', $mobile) ? true : false;
  52. }
  53. function checkTel($tel){
  54. if (!$tel) {
  55. return false;
  56. }
  57. return preg_match('/^(0[0-9]{2,3}\-)([0-9]{7,8})+(\-[0-9]{1,4})?$/', $tel) ? true : false;
  58. }
  59. /**邮箱验证
  60. * @param $email
  61. * @return bool
  62. */
  63. function checkEmail($email){
  64. if (!$email) {
  65. return false;
  66. }
  67. return preg_match('#[a-z0-9&\-_.]+@[\w\-_]+([\w\-.]+)?\.[\w\-]+#is', $email) ? true : false;
  68. }
  69. /**
  70. * @param
  71. * @return int
  72. */
  73. function makeSalt(){
  74. $salt = rand(10000000,99999999);
  75. return $salt;
  76. }
  77. /**
  78. * @param $token
  79. * @return array
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\DbException
  82. * @throws \think\db\exception\ModelNotFoundException
  83. * @throws \think\exception\DbException
  84. */
  85. function VerifyTokens($token){
  86. $host = Config::get("app");
  87. $url = $host["api_host"]."/verifyToken";
  88. $data=[
  89. "token"=>$token
  90. ];
  91. $response=curl_request($url,$data);
  92. return json_decode($response,true);
  93. }
  94. /**
  95. * @param $token
  96. * @param $condition
  97. * @return mixed
  98. */
  99. function GetUserlist($condition){
  100. $host = Config::get("app");
  101. $url = $host["api_host"]."/userlistbycompany";
  102. $response=curl_request($url,$condition);
  103. return json_decode($response,true);
  104. }
  105. ///**
  106. // * @param $token
  107. // * @param $condition
  108. // * @return mixed
  109. // */
  110. //function GetAccountall($token, $condition){
  111. // $host = Config::get("app");
  112. //
  113. // $url = $host["api_host"]."/Api/userall";
  114. // $condition['token']=$token;
  115. // $response=curl_request($url,$condition);
  116. //
  117. // return json_decode($response,true);
  118. //}
  119. function GetList($condition){
  120. $host = Config::get("app");
  121. $url = $host["api_host"]."/userlist";
  122. $response=curl_request($url,$condition);
  123. return json_decode($response,true);
  124. }
  125. /**
  126. * @param $token
  127. * @param $condition ['id'=>1]
  128. * @return mixed
  129. */
  130. function GetInfoById($token,$condition){
  131. $host = Config::get("app");
  132. $url = $host["api_host"]."/userinfo";
  133. $condition['token']=$token;
  134. $response=curl_request($url,$condition);
  135. return json_decode($response,true);
  136. }
  137. /**
  138. * @param $str
  139. * @return string
  140. */
  141. function makeNo($str){
  142. $date=date("mdHis");
  143. $year = date("Y")-2000;
  144. $msec=rand(1000,9999);
  145. return $str.$year.$date.$msec;
  146. }
  147. function makeStr($str){
  148. $date=date("mdHis");
  149. $year = date("Y")-2000;
  150. $msec=randomkeys(4);
  151. return $str.$msec.$year.$date;
  152. }
  153. function randomkeys($length) {
  154. $returnStr='';
  155. $pattern = '1234567890abcdefghijklmnopqrstuvwxyz';//ABCDEFGHIJKLOMNOPQRSTUVWXYZ
  156. for($i = 0; $i < $length; $i ++) {
  157. $returnStr .= $pattern[mt_rand ( 0, strlen($pattern)-1 )]; //生成php随机数
  158. }
  159. return $returnStr;
  160. }
  161. /**
  162. * @param $token
  163. * @param $condition
  164. * @return mixed
  165. */
  166. function resetpwd($condition){
  167. $host = Config::get("app");
  168. $url = $host["api_host"]."/setpasswd";
  169. $response=curl_request($url,$condition);
  170. return json_decode($response,true);
  171. }
  172. function resetpasswd($token,$condition){
  173. $host = Config::get("app");
  174. $url = $host["api_host"]."/Api/passsave";
  175. $condition['token']=$token;
  176. $response=curl_request($url,$condition);
  177. return json_decode($response,true);
  178. }
  179. /**
  180. * @param $condition
  181. * @return array|bool|float|int|mixed|\stdClass|string|null
  182. */
  183. function checkLogin($condition){
  184. $host = Config::get("app");
  185. $url = $host["api_host"]."/login";
  186. $response=curl_request($url,$condition);
  187. return json_decode($response,true);
  188. }
  189. /**
  190. * @param $token
  191. * @param $condition
  192. * @return mixed
  193. */
  194. function resetinfo($condition){
  195. $host = Config::get("app");
  196. $url = $host["api_host"]."/usersave";
  197. $response=curl_request($url,$condition);
  198. return json_decode($response,true);
  199. }
  200. /**
  201. * @param $condition
  202. * @return array|bool|float|int|mixed|\stdClass|string|null
  203. */
  204. function addacount($condition){
  205. $host = Config::get("app");
  206. $url = $host["api_host"]."/useradd";
  207. $response=curl_request($url,$condition);
  208. return json_decode($response,true);
  209. }
  210. if(!function_exists("headerSign")){
  211. function headerSign($post){
  212. $config = Config::get("sign");
  213. $appid=$config['appid'];
  214. $appkey=$config['appkey'];
  215. $headerArr=["appid"=>'123',"noce"=>randomkeys(16),"sign"=>'',"timestamp"=>time()];
  216. $value =array_merge($post,$headerArr);
  217. $Sign= new \Sign($appid,$appkey);
  218. $headerArr['sign'] = $Sign->makeSign($value);
  219. $list=["Content-Type"=>"multipart/json;charset=utf-8"];
  220. foreach ($headerArr as $key=>$value){
  221. $list[]=$key.":".$value;
  222. }
  223. return $list;
  224. }
  225. }
  226. //参数1:访问的URL,参数2:post数据(不填则为GET),参数3:提交的$cookies,参数4:是否返回$cookies
  227. function curl_request($url,$post='',$header=[]){
  228. // $header = empty($header) ? '' : $header;
  229. $header = headerSign($post);
  230. if(is_array($post)) $post=http_build_query($post);
  231. $curl = curl_init();
  232. curl_setopt($curl, CURLOPT_URL, $url);
  233. curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
  234. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  235. curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  236. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  237. if($post) {
  238. curl_setopt($curl, CURLOPT_POST, 1);
  239. curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
  240. }
  241. curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  242. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  243. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  244. $data = curl_exec($curl);
  245. if (curl_errno($curl)) {
  246. return curl_error($curl);
  247. }
  248. curl_close($curl);
  249. return $data;
  250. }
  251. /**
  252. * @param $roleid
  253. * @param $menu
  254. * @return bool
  255. * @throws \think\db\exception\DataNotFoundException
  256. * @throws \think\db\exception\DbException
  257. * @throws \think\db\exception\ModelNotFoundException
  258. */
  259. function checkRole($roleid,$menu){
  260. $roleinfo = \think\facade\Db::name("role_action")->where([['role_id',"=",$roleid],["status","=",1]])->find();
  261. if($roleinfo['private_data']!=""){
  262. $private = explode(",",$roleinfo['private_data']);
  263. if(in_array($menu,$private)){
  264. return true;
  265. }
  266. }
  267. return false;
  268. }
  269. /**
  270. * @param $row
  271. * @param $list
  272. */
  273. function makeMenu($row,&$list){
  274. $list[$row['id']]=$row;
  275. if($row['pid']==0){
  276. return $list;
  277. }
  278. $parent =Db::name("admin_menu")->where(["id"=>$row['pid'],"status"=>1,"is_del"=>0])->field("id,menu_name,menu_img,menu_url,menu_route,pid,is_show,is_private,menu_type,weight")->find();
  279. if($parent==false) return $list;
  280. makeMenu($parent,$list);
  281. }
  282. /**
  283. * 遍历集合处理
  284. * @param $menuArr
  285. */
  286. function MenuTree(&$menuArr,$pid=0){
  287. $meun=[];
  288. foreach ($menuArr as $value){
  289. if($value['pid']==$pid){
  290. if($value['menu_type']==1)$value['child']=MenuTree($menuArr,$value['id']);
  291. $meun[]=$value ;
  292. }
  293. }
  294. return $meun;
  295. };
  296. function upload($files,$extend="xls")
  297. {
  298. // 获取表单上传文件
  299. try {
  300. validate([
  301. 'file' => [
  302. 'fileExt' => 'xlsx,xls'
  303. ]
  304. ],
  305. [
  306. 'file.fileExt' => '不支持的文件',
  307. ]
  308. )->check(['file' => $files]);
  309. if ($extend == 'xlsx') {
  310. $objReader = PHPExcel_IOFactory::createReader('Excel2007');
  311. } else {
  312. $objReader = PHPExcel_IOFactory::createReader('Excel5');
  313. }
  314. $savename = Filesystem::disk('public')->putFile('topic/excel', $files);
  315. $import_path = root_path() . 'public/storage/' . $savename;
  316. $spreadsheet = $objReader->load($import_path);
  317. $sheet = $spreadsheet->getActiveSheet();
  318. $sheetData = $sheet->toArray();
  319. if (empty($sheetData) || !is_array($sheetData)) {
  320. return ['code' => 1003, "msg" => '数据不能为空'];
  321. }
  322. return ['code' => 0, "msg" => '数据解析成功', 'data' => $sheetData];
  323. } catch (think\exception\ValidateException $e) {
  324. // echo $e->getMessage();
  325. return ['code' => 1003, "msg" => $e->getMessage()];
  326. }
  327. }
  328. /**
  329. * @param $files
  330. * @return array
  331. */
  332. function UploadImg($files)
  333. {
  334. $savename = [];
  335. $files = !is_array($files) ? [$files] : $files;
  336. try {
  337. //验证
  338. validate(['imgFile' => ['fileSize' => 10240000, 'fileExt' => 'jpg,jpeg,png,bmp,gif', 'fileMime' => 'image/jpeg,image/png,image/gif']])->check(['imgFile' => $files]);
  339. foreach ($files as $file) {
  340. $url = Filesystem::disk('public')->putFile('img/' . date("Ymd"), $file, function () use ($file) {
  341. return str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName() . "_" . date('YmdHis'));
  342. });
  343. $name = str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName());
  344. $temp = ["url" =>"storage/".$url, "name" => $name];
  345. $savename[] = $temp;
  346. }
  347. return $savename;
  348. } catch (\think\exception\ValidateException $e) {
  349. return $e->getMessage();
  350. }
  351. }
  352. if(!function_exists("invoiceType")){
  353. //发票类型 1 专用2普通3电子普通4 电子专用 5 q全电子发票
  354. function invoiceType($key=0){
  355. $panda= ['',"004",'007','026','028','000'];
  356. return $panda[$key]??'';
  357. }
  358. }
  359. if(!function_exists('excelExport')){
  360. /**
  361. * @param string $fileName
  362. * @param array $headArr
  363. * @param array $data
  364. * @throws \PHPExcel_Exception
  365. * @throws \PHPExcel_Reader_Exception
  366. * @throws \PHPExcel_Writer_Exception
  367. */
  368. function excelExport($fileName = '', $headArr = [], $data = [])
  369. {
  370. $objPHPExcel = new PHPExcel();
  371. $objPHPExcel->getProperties();
  372. $keyA = 0; // 设置表头
  373. foreach ($headArr as $v) {
  374. $colum = PHPExcel_Cell::stringFromColumnIndex($keyA);
  375. $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
  376. $keyA += 1;
  377. }
  378. $column = 2;
  379. $objActSheet = $objPHPExcel->getActiveSheet();
  380. foreach ($data as $key => $rows) { // 行写入
  381. $span = 0;
  382. foreach ($rows as $keyName => $value) { // 列写入
  383. //判断数据是否有数组,如果有数组,转换成字符串
  384. if(is_array($value)){
  385. $value = implode("、", $value);
  386. }
  387. $objActSheet->setCellValue(PHPExcel_Cell::stringFromColumnIndex($span) . $column, $value);
  388. $span++;
  389. }
  390. $column++;
  391. }
  392. // var_dump($objActSheet->getActiveCell());
  393. $fileName .= "_" . date("Y_m_d", time()) . ".xls";
  394. //$fileName .= "_" . date("Y_m_d", Request()->instance()->time()) . ".xls";
  395. //$fileName = iconv("utf-8", "gb2312", $fileName); // 重命名表
  396. $objPHPExcel->setActiveSheetIndex(0); // 设置活动单指数到第一个表,所以Excel打开这是第一个表
  397. // Redirect output to a client’s web browser (Excel2007)
  398. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  399. header('Content-Disposition: attachment;filename="'.$fileName.'"');
  400. header('Cache-Control: max-age=0');
  401. // If you're serving to IE 9, then the following may be needed
  402. header('Cache-Control: max-age=1');
  403. // If you're serving to IE over SSL, then the following may be needed
  404. header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  405. header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
  406. header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  407. header ('Pragma: public'); // HTTP/1.0
  408. // header("Content-Type: application/octet-stream"); # 流文件输出
  409. // header("Content-Transfer-Encoding: binary"); # 告诉浏览器,这是二进制文件
  410. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  411. $objWriter->save('php://output'); // 文件通过浏览器下载
  412. exit();
  413. }
  414. }
  415. if(!function_exists('menuAction')){
  416. function menuAction($row,&$list=[]){
  417. $temp=[];
  418. foreach ($row as $key=>$value){
  419. if($value['pid']==0){
  420. $list[]=$value;
  421. }else{
  422. $menu=Db::name("admin_menu")->where(["id"=>$value['pid'],"is_del"=>0,"status"=>1])->field("id,menu_name,menu_img,menu_route,menu_url,pid,level,is_show,is_private,menu_type,status")->findOrEmpty();
  423. if(empty($menu)) continue;
  424. if(!isset($temp[$value['pid']]))$temp[$value['pid']]=$menu;
  425. $temp[$value['pid']]['child'][]=$value;
  426. }
  427. }
  428. // $list=$temp;
  429. if (!empty($temp)){
  430. menuAction($temp,$list);
  431. }
  432. }
  433. }