common.php 9.7 KB

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