common.php 8.0 KB

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