common.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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"]."/Api/userinfo";
  19. $data=[
  20. "token"=>$token
  21. ];
  22. $response=curl_request($url,$data);
  23. return json_decode($response,true);
  24. }
  25. /**手机号验证
  26. * @param $mobile
  27. * @return bool
  28. */
  29. function checkMobile($mobile){
  30. if (!is_numeric($mobile)) {
  31. return false;
  32. }
  33. return preg_match('#^1[3,4,5,6,7,8,9]{1}[\d]{9}$#', $mobile) ? true : false;
  34. }
  35. function checkTel($tel){
  36. if (!$tel) {
  37. return false;
  38. }
  39. return preg_match('/^(0[0-9]{2,3}\-)([0-9]{7,8})+(\-[0-9]{1,4})?$/', $tel) ? true : false;
  40. }
  41. /**邮箱验证
  42. * @param $email
  43. * @return bool
  44. */
  45. function checkEmail($email){
  46. if (!$email) {
  47. return false;
  48. }
  49. return preg_match('#[a-z0-9&\-_.]+@[\w\-_]+([\w\-.]+)?\.[\w\-]+#is', $email) ? true : false;
  50. }
  51. /**
  52. * @param
  53. * @return int
  54. */
  55. function makeSalt(){
  56. $salt = rand(10000000,99999999);
  57. return $salt;
  58. }
  59. /**
  60. * @param $token
  61. * @return array
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\DbException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. * @throws \think\exception\DbException
  66. */
  67. function VerifyTokens($token){
  68. $host = Config::get("app");
  69. $url = $host["api_host"]."/Api/verify_token";
  70. $data=[
  71. "token"=>$token
  72. ];
  73. $response=curl_request($url,$data);
  74. return json_decode($response,true);
  75. }
  76. /**
  77. * @param $token
  78. * @param $condition
  79. * @return mixed
  80. */
  81. function GetUserlist($token,$condition){
  82. $host = Config::get("app");
  83. $url = $host["api_host"]."/Api/getuserlist";
  84. $condition['token']=$token;
  85. $response=curl_request($url,$condition);
  86. return json_decode($response,true);
  87. }
  88. /**
  89. * @param $token
  90. * @param $condition
  91. * @return mixed
  92. */
  93. function GetAccountall($token, $condition){
  94. $host = Config::get("app");
  95. $url = $host["api_host"]."/Api/userall";
  96. $condition['token']=$token;
  97. $response=curl_request($url,$condition);
  98. return json_decode($response,true);
  99. }
  100. function GetList($token,$condition){
  101. $host = Config::get("app");
  102. $url = $host["api_host"]."/Api/userlist";
  103. $condition['token']=$token;
  104. $response=curl_request($url,$condition);
  105. return json_decode($response,true);
  106. }
  107. /**
  108. * @param $token
  109. * @param $condition ['id'=>1]
  110. * @return mixed
  111. */
  112. function GetInfoById($token,$condition){
  113. $host = Config::get("app");
  114. $url = $host["api_host"]."/Api/userinfobyid";
  115. $condition['token']=$token;
  116. $response=curl_request($url,$condition);
  117. return json_decode($response,true);
  118. }
  119. /**
  120. * @param $str
  121. * @return string
  122. */
  123. function makeNo($str){
  124. $date=date("mdHis");
  125. $year = date("Y")-2000;
  126. $msec=rand(1000,9999);
  127. return $str.$year.$date.$msec;
  128. }
  129. function makeStr($str){
  130. $date=date("mdHis");
  131. $year = date("Y")-2000;
  132. $msec=randomkeys(4);
  133. return $str.$msec.$year.$date;
  134. }
  135. function randomkeys($length) {
  136. $returnStr='';
  137. $pattern = '1234567890abcdefghijklmnopqrstuvwxyz';//ABCDEFGHIJKLOMNOPQRSTUVWXYZ
  138. for($i = 0; $i < $length; $i ++) {
  139. $returnStr .= $pattern[mt_rand ( 0, strlen($pattern)-1 )]; //生成php随机数
  140. }
  141. return $returnStr;
  142. }
  143. /**
  144. * @param $token
  145. * @param $condition
  146. * @return mixed
  147. */
  148. function resetpwd($token,$condition){
  149. $host = Config::get("app");
  150. $url = $host["api_host"]."/Api/passset";
  151. $condition['token']=$token;
  152. $response=curl_request($url,$condition);
  153. return json_decode($response,true);
  154. }
  155. function resetpasswd($token,$condition){
  156. $host = Config::get("app");
  157. $url = $host["api_host"]."/Api/passsave";
  158. $condition['token']=$token;
  159. $response=curl_request($url,$condition);
  160. return json_decode($response,true);
  161. }
  162. /**
  163. * @param $token
  164. * @param $condition
  165. * @return mixed
  166. */
  167. function resetinfo($token,$condition){
  168. $host = Config::get("app");
  169. $url = $host["api_host"]."/Api/usersave";
  170. $condition['token']=$token;
  171. $response=curl_request($url,$condition);
  172. return json_decode($response,true);
  173. }
  174. //参数1:访问的URL,参数2:post数据(不填则为GET),参数3:提交的$cookies,参数4:是否返回$cookies
  175. function curl_request($url,$post=''){
  176. $curl = curl_init();
  177. curl_setopt($curl, CURLOPT_URL, $url);
  178. curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
  179. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  180. curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  181. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  182. if($post) {
  183. curl_setopt($curl, CURLOPT_POST, 1);
  184. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
  185. }
  186. curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  187. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  188. $data = curl_exec($curl);
  189. if (curl_errno($curl)) {
  190. return curl_error($curl);
  191. }
  192. curl_close($curl);
  193. return $data;
  194. }
  195. /**
  196. * @param $roleid
  197. * @param $menu
  198. * @return bool
  199. * @throws \think\db\exception\DataNotFoundException
  200. * @throws \think\db\exception\DbException
  201. * @throws \think\db\exception\ModelNotFoundException
  202. */
  203. function checkRole($roleid,$menu){
  204. $roleinfo = \think\facade\Db::name("role_action")->where([['role_id',"=",$roleid],["status","=",1]])->find();
  205. if($roleinfo['private_data']!=""){
  206. $private = explode(",",$roleinfo['private_data']);
  207. if(in_array($menu,$private)){
  208. return true;
  209. }
  210. }
  211. return false;
  212. }
  213. /**
  214. * @param $row
  215. * @param $list
  216. */
  217. function makeMenu($row,&$list){
  218. $list[$row['id']]=$row;
  219. if($row['pid']==0){
  220. return $list;
  221. }
  222. $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();
  223. if($parent==false) return $list;
  224. if($parent['menu_type']==2)$value['action']=[];
  225. if($parent['menu_type']==1)$value['child']=[];
  226. makeMenu($parent,$list);
  227. }
  228. /**
  229. * @param $menuArr
  230. */
  231. function MenuTree(&$menuArr,$pid=0){
  232. $meun=[];
  233. foreach ($menuArr as $value){
  234. if($value['pid']==$pid){
  235. if($value['menu_type']==1)$value['child']=MenuTree($menuArr,$value['id']);
  236. $meun[]=$value ;
  237. }
  238. }
  239. return $meun;
  240. };
  241. function upload($files,$extend="xls")
  242. {
  243. // 获取表单上传文件
  244. try {
  245. validate([
  246. 'file' => [
  247. // 限制文件大小(单位b),这里限制为4M
  248. //fileSize' => 4 * 1024 * 1024,
  249. 'fileExt' => 'xlsx,xls'
  250. ]
  251. ],
  252. [
  253. //'file.fileSize' => '文件太大',
  254. 'file.fileExt' => '不支持的文件',
  255. ]
  256. )->check(['file' => $files]);
  257. if ($extend == 'xlsx') {
  258. $objReader = PHPExcel_IOFactory::createReader('Excel2007');
  259. } else {
  260. $objReader = PHPExcel_IOFactory::createReader('Excel5');
  261. }
  262. $savename = Filesystem::disk('public')->putFile('topic/excel', $files);
  263. $import_path = root_path() . 'public/storage/' . $savename;
  264. $spreadsheet = $objReader->load($import_path);
  265. $sheet = $spreadsheet->getActiveSheet();
  266. $sheetData = $sheet->toArray();
  267. if (empty($sheetData) || !is_array($sheetData)) {
  268. return ['code' => 1003, "msg" => '数据不能为空'];
  269. }
  270. return ['code' => 0, "msg" => '数据解析成功', 'data' => $sheetData];
  271. } catch (think\exception\ValidateException $e) {
  272. // echo $e->getMessage();
  273. return ['code' => 1003, "msg" => $e->getMessage()];
  274. }
  275. }
  276. /**
  277. * @param $files
  278. * @return array
  279. */
  280. function UploadImg($files)
  281. {
  282. $savename = [];
  283. $files = !is_array($files) ? [$files] : $files;
  284. try {
  285. //验证
  286. validate(['imgFile' => ['fileSize' => 10240000, 'fileExt' => 'jpg,jpeg,png,bmp,gif', 'fileMime' => 'image/jpeg,image/png,image/gif']])->check(['imgFile' => $files]);
  287. foreach ($files as $file) {
  288. $url = Filesystem::disk('public')->putFile('img/' . date("Ymd"), $file, function () use ($file) {
  289. return str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName() . "_" . date('YmdHis'));
  290. });
  291. $name = str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName());
  292. $temp = ["url" =>"storage/".$url, "name" => $name];
  293. $savename[] = $temp;
  294. }
  295. return $savename;
  296. } catch (\think\exception\ValidateException $e) {
  297. return $e->getMessage();
  298. }
  299. }
  300. if(!function_exists("invoiceType")){
  301. //发票类型 1 专用2普通3电子普通4 电子专用
  302. function invoiceType($key=0){
  303. $panda= ['',"004",'007','026','028'];
  304. return $panda[$key]??'';
  305. }
  306. }