common.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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){
  200. $config = Config::get("sign");
  201. $appid=$config['appid'];
  202. $appkey=$config['appkey'];
  203. $headerArr=["appid"=>'123',"noce"=>randomkeys(16),"sign"=>'',"timestamp"=>time()];
  204. $value =array_merge($post,$headerArr);
  205. $Sign= new \Sign($appid,$appkey);
  206. $headerArr['sign'] = $Sign->makeSign($value);
  207. $list=[];
  208. foreach ($headerArr as $key=>$value){
  209. $list[]=$key.":".$value;
  210. }
  211. return $list;
  212. }
  213. }
  214. //参数1:访问的URL,参数2:post数据(不填则为GET),参数3:提交的$cookies,参数4:是否返回$cookies
  215. function curl_request($url,$post='',$header=["Content-Type: multipart/json;charset=utf-8"]){
  216. $header = empty($header) ? '' : $header;
  217. $singHead = headerSign($post);
  218. if(is_array($post)) $post=json_encode($post,JSON_UNESCAPED_UNICODE);
  219. $curl = curl_init();
  220. curl_setopt($curl, CURLOPT_URL, $url);
  221. curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
  222. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  223. curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  224. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  225. if($post) {
  226. curl_setopt($curl, CURLOPT_POST, 1);
  227. curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
  228. }
  229. curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  230. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  231. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  232. $data = curl_exec($curl);
  233. if (curl_errno($curl)) {
  234. return curl_error($curl);
  235. }
  236. curl_close($curl);
  237. return $data;
  238. }
  239. /**
  240. * @param $roleid
  241. * @param $menu
  242. * @return bool
  243. * @throws \think\db\exception\DataNotFoundException
  244. * @throws \think\db\exception\DbException
  245. * @throws \think\db\exception\ModelNotFoundException
  246. */
  247. function checkRole($roleid,$menu){
  248. $roleinfo = \think\facade\Db::name("role_action")->where([['role_id',"=",$roleid],["status","=",1]])->find();
  249. if($roleinfo['private_data']!=""){
  250. $private = explode(",",$roleinfo['private_data']);
  251. if(in_array($menu,$private)){
  252. return true;
  253. }
  254. }
  255. return false;
  256. }
  257. /**
  258. * @param $row
  259. * @param $list
  260. */
  261. function makeMenu($row,&$list){
  262. $list[$row['id']]=$row;
  263. if($row['pid']==0){
  264. return $list;
  265. }
  266. $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();
  267. if($parent==false) return $list;
  268. if($parent['menu_type']==2)$value['action']=[];
  269. if($parent['menu_type']==1)$value['child']=[];
  270. makeMenu($parent,$list);
  271. }
  272. /**
  273. * @param $menuArr
  274. */
  275. function MenuTree(&$menuArr,$pid=0){
  276. $meun=[];
  277. foreach ($menuArr as $value){
  278. if($value['pid']==$pid){
  279. if($value['menu_type']==1)$value['child']=MenuTree($menuArr,$value['id']);
  280. $meun[]=$value ;
  281. }
  282. }
  283. return $meun;
  284. };
  285. function upload($files,$extend="xls")
  286. {
  287. // 获取表单上传文件
  288. try {
  289. validate([
  290. 'file' => [
  291. 'fileExt' => 'xlsx,xls'
  292. ]
  293. ],
  294. [
  295. 'file.fileExt' => '不支持的文件',
  296. ]
  297. )->check(['file' => $files]);
  298. if ($extend == 'xlsx') {
  299. $objReader = PHPExcel_IOFactory::createReader('Excel2007');
  300. } else {
  301. $objReader = PHPExcel_IOFactory::createReader('Excel5');
  302. }
  303. $savename = Filesystem::disk('public')->putFile('topic/excel', $files);
  304. $import_path = root_path() . 'public/storage/' . $savename;
  305. $spreadsheet = $objReader->load($import_path);
  306. $sheet = $spreadsheet->getActiveSheet();
  307. $sheetData = $sheet->toArray();
  308. if (empty($sheetData) || !is_array($sheetData)) {
  309. return ['code' => 1003, "msg" => '数据不能为空'];
  310. }
  311. return ['code' => 0, "msg" => '数据解析成功', 'data' => $sheetData];
  312. } catch (think\exception\ValidateException $e) {
  313. // echo $e->getMessage();
  314. return ['code' => 1003, "msg" => $e->getMessage()];
  315. }
  316. }
  317. /**
  318. * @param $files
  319. * @return array
  320. */
  321. function UploadImg($files)
  322. {
  323. $savename = [];
  324. $files = !is_array($files) ? [$files] : $files;
  325. try {
  326. //验证
  327. validate(['imgFile' => ['fileSize' => 10240000, 'fileExt' => 'jpg,jpeg,png,bmp,gif', 'fileMime' => 'image/jpeg,image/png,image/gif']])->check(['imgFile' => $files]);
  328. foreach ($files as $file) {
  329. $url = Filesystem::disk('public')->putFile('img/' . date("Ymd"), $file, function () use ($file) {
  330. return str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName() . "_" . date('YmdHis'));
  331. });
  332. $name = str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName());
  333. $temp = ["url" =>"storage/".$url, "name" => $name];
  334. $savename[] = $temp;
  335. }
  336. return $savename;
  337. } catch (\think\exception\ValidateException $e) {
  338. return $e->getMessage();
  339. }
  340. }
  341. if(!function_exists("invoiceType")){
  342. //发票类型 1 专用2普通3电子普通4 电子专用
  343. function invoiceType($key=0){
  344. $panda= ['',"004",'007','026','028'];
  345. return $panda[$key]??'';
  346. }
  347. }