common.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. <?php
  2. use think\Db;
  3. use yzx\Ucpaas;
  4. use aldy\SmsDemo;
  5. /**
  6. * 字符串截取,支持中文和其他编码
  7. * @param $str 截取对象
  8. * @param int $start 开始位置
  9. * @param $length 长度
  10. * @param string $charset 编码
  11. * @param bool $suffix 是否加省略号
  12. * @return string
  13. */
  14. function msubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = false) {
  15. if (function_exists("mb_substr"))
  16. $slice = mb_substr($str, $start, $length, $charset);
  17. elseif (function_exists('iconv_substr')) {
  18. $slice = iconv_substr($str, $start, $length, $charset);
  19. if (false === $slice) {
  20. $slice = '';
  21. }
  22. } else {
  23. $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
  24. $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
  25. $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
  26. $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
  27. preg_match_all($re[$charset], $str, $match);
  28. $slice = join("", array_slice($match[0], $start, $length));
  29. }
  30. return $suffix ? $slice . '...' : $slice;
  31. }
  32. /**
  33. * 调用系统的API接口方法(静态方法)
  34. * api('User/getName','id=5'); 调用公共模块的User接口的getName方法
  35. * api('Admin/User/getName','id=5'); 调用Admin模块的User接口
  36. * @param string $name 格式 [模块名]/接口名/方法名
  37. * @param array|string $vars 参数
  38. */
  39. function api($name,$vars=array()){
  40. $array = explode('/',$name);
  41. $method = array_pop($array);
  42. $classname = array_pop($array);
  43. $module = $array? array_pop($array) : 'common';
  44. $callback = 'app\\'.$module.'\\Api\\'.$classname.'Api::'.$method;
  45. if(is_string($vars)) {
  46. parse_str($vars,$vars);
  47. }
  48. return call_user_func_array($callback,$vars);
  49. }
  50. /**
  51. * 获取配置的分组
  52. * @param string $group 配置分组
  53. * @return string
  54. */
  55. function get_config_group($group=0){
  56. $list = config('config_group_list');
  57. return $group?$list[$group]:'';
  58. }
  59. /**
  60. * 获取配置的类型
  61. * @param string $type 配置类型
  62. * @return string
  63. */
  64. function get_config_type($type=0){
  65. $list = config('config_type_list');
  66. return $list[$type];
  67. }
  68. /*
  69. * alisms 阿里大于验证码
  70. * @param $phoneNum 手机号
  71. * @param $code验证码
  72. * @param $TemplateCode 模板id
  73. * return array Code: OK Message: OK
  74. */
  75. function alisms($phoneNum,$code,$TemplateCode=''){
  76. header('Content-Type: text/plain; charset=utf-8');
  77. $Alidy = new SmsDemo();
  78. if($TemplateCode == ''){
  79. $TemplateCode = config('alidy.TemplateCode');
  80. }
  81. $result = $Alidy->sendSms($phoneNum,config('alidy.SignName'),$TemplateCode,$code);
  82. $result = object_array($result);
  83. return $result;
  84. }
  85. /**
  86. * YzxSms 云之讯短信
  87. * @param $param 验证码
  88. * @param $mobile 手机号
  89. * @param $uid 用户透传id
  90. * @param $templateid 模板id
  91. * @return array code:000000 msg:OK
  92. */
  93. function YzxSms($param,$mobile,$templateid='',$uid=1){
  94. $appid = config('yzx.appid');
  95. if($templateid == ''){
  96. $templateid = config('yzx.templateid');
  97. }
  98. $options = ['accountsid'=>config('yzx.accountsid'),'token'=>config('yzx.token')];
  99. $yzxsms = new Ucpaas($options);
  100. $result = $yzxsms->SendSms($appid,$templateid,$param,$mobile,$uid);
  101. $result = json_decode($result,true);
  102. return $result;
  103. }
  104. /**
  105. * GetRandCode 获取随机6位数字验证码
  106. * @param int $len
  107. * @return string
  108. */
  109. function GetRandCode($len = 6)
  110. {
  111. $chars = array(
  112. "1", "2",
  113. "3", "4", "5", "6", "7", "8", "9"
  114. );
  115. $charsLen = count($chars) - 1;
  116. shuffle($chars);
  117. $output = "";
  118. for ($i=0; $i<$len; $i++)
  119. {
  120. $output .= $chars[mt_rand(0, $charsLen)];
  121. }
  122. return $output;
  123. }
  124. /**
  125. * 删除文件夹及文件夹内文件函数,支持中文路径
  126. * @param string $path 文件夹路径
  127. * @param string $delDir 是否删除该文件夹
  128. * @return boolean
  129. */
  130. function delete_dir_file($path, $del = true){
  131. $result = false;
  132. if(is_dir($path)) {
  133. $path = iconv ("UTF-8" , "GBK" , $path);
  134. $handle = opendir ($path);
  135. if ( $handle ) {
  136. while (false !== ($item = readdir ($handle))) {
  137. if ( ($item != ".") && ($item != "..") ) {
  138. is_dir ("$path/$item") ? delete_dir_file ("$path/$item" , $del) : unlink ("$path/$item");
  139. }
  140. }
  141. closedir ($handle);
  142. if ( $del ) {
  143. rmdir ($path);
  144. }
  145. $result = true;
  146. }
  147. }
  148. return $result;
  149. }
  150. /**
  151. * formatTime 时间格式化1
  152. * @param $time 时间戳
  153. * @return string
  154. */
  155. function formatTime($time) {
  156. $now_time = time();
  157. $t = $now_time - $time;
  158. $mon = (int) ($t / (86400 * 30));
  159. if ($mon >= 1) {
  160. return '一个月前';
  161. }
  162. $day = (int) ($t / 86400);
  163. if ($day >= 1) {
  164. return $day . '天前';
  165. }
  166. $h = (int) ($t / 3600);
  167. if ($h >= 1) {
  168. return $h . '小时前';
  169. }
  170. $min = (int) ($t / 60);
  171. if ($min >= 1) {
  172. return $min . '分钟前';
  173. }
  174. return '刚刚';
  175. }
  176. /**
  177. * pincheTime 时间格式化2
  178. * @param $time 时间戳
  179. * @return string
  180. */
  181. function pincheTime($time) {
  182. $today = strtotime(date('Y-m-d')); //今天零点
  183. $here = (int)(($time - $today)/86400) ;
  184. if($here==1){
  185. return '明天';
  186. }
  187. if($here==2) {
  188. return '后天';
  189. }
  190. if($here>=3 && $here<7){
  191. return $here.'天后';
  192. }
  193. if($here>=7 && $here<30){
  194. return '一周后';
  195. }
  196. if($here>=30 && $here<365){
  197. return '一个月后';
  198. }
  199. if($here>=365){
  200. $r = (int)($here/365).'年后';
  201. return $r;
  202. }
  203. return '今天';
  204. }
  205. /**
  206. * whatWeek 判断日期是星期几
  207. * @param $date 日期
  208. * @return string
  209. */
  210. function whatWeek($date){
  211. $time = strtotime($date);
  212. $week = date('N', $time);
  213. switch ($week){
  214. case '1':
  215. return "星期一";
  216. break;
  217. case '2':
  218. return "星期二";
  219. break;
  220. case '3':
  221. return "星期三";
  222. break;
  223. case '4':
  224. return "星期四";
  225. break;
  226. case '5':
  227. return "星期五";
  228. break;
  229. case '6':
  230. return "星期六";
  231. break;
  232. default:
  233. return "星期天";
  234. }
  235. }
  236. //生成类似uuid的随机字符串
  237. function uuid($prefix = '')
  238. {
  239. $chars = md5(uniqid(mt_rand(), true));
  240. // $uuid = substr($chars,0,8) . '-';
  241. // $uuid .= substr($chars,8,4) . '-';
  242. // $uuid .= substr($chars,12,4) . '-';
  243. // $uuid .= substr($chars,16,4) . '-';
  244. // $uuid .= substr($chars,20,12);
  245. return $prefix . $chars;
  246. }
  247. /**
  248. *
  249. * execl数据导出
  250. * @param string $name 表头信息
  251. * @param string $title 模型名(如Member),用于导出生成文件名的前缀
  252. * @param array $cellName 表头及字段名
  253. * @param array $data 导出的表数据
  254. */
  255. function exportExcel($name,$title,$cellName,$data)
  256. {
  257. //引入核心文件
  258. vendor("PHPExcel.PHPExcel");
  259. $objPHPExcel = new \PHPExcel();
  260. //定义配置
  261. $topNumber = 2;//表头有几行占用
  262. $xlsTitle = iconv('utf-8', 'gb2312', $title);//文件名称
  263. $fileName = $title.date('_YmdHis');//文件名称
  264. $cellKey = array(
  265. 'A','B','C','D','E','F','G','H','I','J','K','L','M',
  266. 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
  267. 'AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM',
  268. 'AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ'
  269. );
  270. //写在处理的前面(了解表格基本知识,已测试)
  271. // $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20);//所有单元格(行)默认高度
  272. // $objPHPExcel->getActiveSheet()->getDefaultColumnDimension()->setWidth(20);//所有单元格(列)默认宽度
  273. // $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);//设置行高度
  274. // $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30);//设置列宽度
  275. // $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18);//设置文字大小
  276. // $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);//设置是否加粗
  277. // $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);// 设置文字颜色
  278. // $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//设置文字居左(HORIZONTAL_LEFT,默认值)中(HORIZONTAL_CENTER)右(HORIZONTAL_RIGHT)
  279. // $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中
  280. // $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);//设置填充颜色
  281. // $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->getStartColor()->setARGB('FF7F24');//设置填充颜色
  282. //处理表头标题
  283. $objPHPExcel->getActiveSheet()->mergeCells('A1:'.$cellKey[count($cellName)-1].'1');//合并单元格(如果要拆分单元格是需要先合并再拆分的,否则程序会报错)
  284. $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1',$name);
  285. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
  286. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18);
  287. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  288. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
  289. //处理表头
  290. foreach ($cellName as $k=>$v)
  291. {
  292. $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellKey[$k].$topNumber, $v[1]);//设置表头数据
  293. // $objPHPExcel->getActiveSheet()->freezePane($cellKey[$k].($topNumber+1));//冻结窗口
  294. $objPHPExcel->getActiveSheet()->getStyle($cellKey[$k].$topNumber)->getFont()->setBold(true);//设置是否加粗
  295. $objPHPExcel->getActiveSheet()->getStyle($cellKey[$k].$topNumber)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中
  296. if($v[2] > 0)//大于0表示需要设置宽度
  297. {
  298. $objPHPExcel->getActiveSheet()->getColumnDimension($cellKey[$k])->setWidth($v[2]);//设置列宽度
  299. }
  300. }
  301. //处理数据
  302. foreach ($data as $k=>$v)
  303. {
  304. foreach ($cellName as $k1=>$v1)
  305. {
  306. $objPHPExcel->getActiveSheet()->setCellValue($cellKey[$k1].($k+1+$topNumber), $v[$v1[0]]);
  307. if($v1[3] != "" && in_array($v1[3], array("LEFT","CENTER","RIGHT")))
  308. {
  309. $v1[3] = eval('return PHPExcel_Style_Alignment::HORIZONTAL_'.$v1[3].';');
  310. //这里也可以直接传常量定义的值,即left,center,right;小写的strtolower
  311. $objPHPExcel->getActiveSheet()->getStyle($cellKey[$k1].($k+1+$topNumber))->getAlignment()->setHorizontal($v1[3]);
  312. }
  313. }
  314. }
  315. //导出execl
  316. header('pragma:public');
  317. header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
  318. header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
  319. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  320. $dir = ROOT_PATH . 'public' . DS . 'excel';
  321. if(!file_exists($dir)){
  322. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  323. mkdir($dir, 0700,true);
  324. }
  325. $filePath = 'excel/'.$fileName.'.xls';
  326. $objWriter->save($filePath);
  327. return ['code'=>200,'msg'=>'/'.$filePath];
  328. }
  329. /**
  330. *
  331. * execl数据导出
  332. * @param string $name 表头信息
  333. * @param string $title 模型名(如Member),用于导出生成文件名的前缀
  334. * @param array $cellName 表头及字段名
  335. * @param array $data 导出的表数据
  336. */
  337. function exportExcels($name,$title,$cellName,$data)
  338. {
  339. //引入核心文件
  340. vendor("PHPExcel.PHPExcel");
  341. $objPHPExcel = new \PHPExcel();
  342. //定义配置
  343. $topNumber = 2;//表头有几行占用
  344. $xlsTitle = iconv('utf-8', 'gb2312', $title);//文件名称
  345. $fileName = $title.date('_YmdHis');//文件名称
  346. $cellKey = array(
  347. 'A','B','C','D','E','F','G','H','I','J','K','L','M',
  348. 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
  349. 'AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM',
  350. 'AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ'
  351. );
  352. //写在处理的前面(了解表格基本知识,已测试)
  353. // $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20);//所有单元格(行)默认高度
  354. // $objPHPExcel->getActiveSheet()->getDefaultColumnDimension()->setWidth(20);//所有单元格(列)默认宽度
  355. // $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);//设置行高度
  356. // $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30);//设置列宽度
  357. // $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18);//设置文字大小
  358. // $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);//设置是否加粗
  359. // $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);// 设置文字颜色
  360. // $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//设置文字居左(HORIZONTAL_LEFT,默认值)中(HORIZONTAL_CENTER)右(HORIZONTAL_RIGHT)
  361. // $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中
  362. // $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);//设置填充颜色
  363. // $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->getStartColor()->setARGB('FF7F24');//设置填充颜色
  364. //处理表头标题
  365. $objPHPExcel->getActiveSheet()->mergeCells('A1:'.$cellKey[count($cellName)-1].'1');//合并单元格(如果要拆分单元格是需要先合并再拆分的,否则程序会报错)
  366. $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1',$name);
  367. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
  368. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18);
  369. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  370. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
  371. //处理表头
  372. foreach ($cellName as $k=>$v)
  373. {
  374. $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellKey[$k].$topNumber, $v[1]);//设置表头数据
  375. // $objPHPExcel->getActiveSheet()->freezePane($cellKey[$k].($topNumber+1));//冻结窗口
  376. $objPHPExcel->getActiveSheet()->getStyle($cellKey[$k].$topNumber)->getFont()->setBold(true);//设置是否加粗
  377. $objPHPExcel->getActiveSheet()->getStyle($cellKey[$k].$topNumber)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中
  378. if($v[2] > 0)//大于0表示需要设置宽度
  379. {
  380. $objPHPExcel->getActiveSheet()->getColumnDimension($cellKey[$k])->setWidth($v[2]);//设置列宽度
  381. }
  382. }
  383. //处理数据
  384. foreach ($data as $k=>$v)
  385. {
  386. foreach ($cellName as $k1=>$v1)
  387. {
  388. $objPHPExcel->getActiveSheet()->setCellValue($cellKey[$k1].($k+1+$topNumber), $v[$v1[0]]);
  389. if($v1[3] != "" && in_array($v1[3], array("LEFT","CENTER","RIGHT")))
  390. {
  391. $v1[3] = eval('return PHPExcel_Style_Alignment::HORIZONTAL_'.$v1[3].';');
  392. //这里也可以直接传常量定义的值,即left,center,right;小写的strtolower
  393. $objPHPExcel->getActiveSheet()->getStyle($cellKey[$k1].($k+1+$topNumber))->getAlignment()->setHorizontal($v1[3]);
  394. }
  395. }
  396. }
  397. //导出execl
  398. header('pragma:public');
  399. header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
  400. header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
  401. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  402. $objWriter->save('php://output');
  403. }
  404. /**
  405. *数字金额转换成中文大写金额的函数
  406. *String Int $num 要转换的小写数字或小写字符串
  407. *return 大写字母
  408. *小数位为两位
  409. **/
  410. function get_amount($num){
  411. $c1 = "零壹贰叁肆伍陆柒捌玖";
  412. $c2 = "分角元拾佰仟万拾佰仟亿";
  413. $num = round($num, 2);
  414. $num = $num * 100;
  415. if (strlen($num) > 10) {
  416. return "数据太长,没有这么大的钱吧,检查下";
  417. }
  418. $i = 0;
  419. $c = "";
  420. while (1) {
  421. if ($i == 0) {
  422. $n = substr($num, strlen($num)-1, 1);
  423. } else {
  424. $n = $num % 10;
  425. }
  426. $p1 = substr($c1, 3 * $n, 3);
  427. $p2 = substr($c2, 3 * $i, 3);
  428. if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {
  429. $c = $p1 . $p2 . $c;
  430. } else {
  431. $c = $p1 . $c;
  432. }
  433. $i = $i + 1;
  434. $num = $num / 10;
  435. $num = (int)$num;
  436. if ($num == 0) {
  437. break;
  438. }
  439. }
  440. $j = 0;
  441. $slen = strlen($c);
  442. while ($j < $slen) {
  443. $m = substr($c, $j, 6);
  444. if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
  445. $left = substr($c, 0, $j);
  446. $right = substr($c, $j + 3);
  447. $c = $left . $right;
  448. $j = $j-3;
  449. $slen = $slen-3;
  450. }
  451. $j = $j + 3;
  452. }
  453. if (substr($c, strlen($c)-3, 3) == '零') {
  454. $c = substr($c, 0, strlen($c)-3);
  455. }
  456. if (empty($c)) {
  457. return "零元整";
  458. }else{
  459. return $c . "整";
  460. }
  461. }
  462. /**
  463. * 生成二维码图片
  464. * @param $content 二维码内容
  465. * @param $logo logo图片
  466. * @return string
  467. */
  468. function Qrcode($content,$logo = ""){
  469. import('org.phpqrcode', EXTEND_PATH);
  470. $date = date('Ymd');
  471. $dir = ROOT_PATH . 'public' . DS . 'uploads/ercode/'.$date;
  472. $url = http_type();
  473. if(!file_exists($dir)){
  474. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  475. mkdir($dir, 0700,true);
  476. }
  477. $name = md5(time().uuid()).".png";
  478. $value = "$content"; //二维码内容
  479. $errorCorrectionLevel = 'M'; //容错级别L(QR_ECLEVEL_L,7%)、M(QR_ECLEVEL_M,15%)、Q(QR_ECLEVEL_Q,25%)、H(QR_ECLEVEL_H,30%)
  480. $matrixPointSize = 6; //生成图片大小
  481. //组装图片路径
  482. $filepath= $dir.DS.$name;
  483. \QRcode::png($value,$filepath , $errorCorrectionLevel, $matrixPointSize, 2);
  484. $QR = $filepath; //已经生成的原始二维码图片文件
  485. if ($logo && file_exists($logo)) {
  486. $QR = imagecreatefromstring(file_get_contents($QR)); //目标图象连接资源。
  487. $logo = imagecreatefromstring(file_get_contents($logo)); //源图象连接资源。
  488. $QR_width = imagesx($QR); //二维码图片宽度
  489. $QR_height = imagesy($QR); //二维码图片高度
  490. $logo_width = imagesx($logo); //logo图片宽度
  491. $logo_height = imagesy($logo); //logo图片高度
  492. $logo_qr_width = $QR_width / 4; //组合之后logo的宽度(占二维码的1/5)
  493. $scale = $logo_width/$logo_qr_width; //logo的宽度缩放比(本身宽度/组合后的宽度)
  494. $logo_qr_height = $logo_height/$scale; //组合之后logo的高度
  495. $from_width = ($QR_width - $logo_qr_width) / 2; //组合之后logo左上角所在坐标点
  496. //重新组合图片并调整大小
  497. /*
  498. * imagecopyresampled() 将一幅图像(源图象)中的一块正方形区域拷贝到另一个图像中
  499. */
  500. imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height);
  501. $newname = 'uploads/ercode/'.$date.DS.md5(time().uuid()).".png";
  502. imagepng($QR, ROOT_PATH . 'public' . DS .$newname);
  503. //输出图片
  504. unlink($filepath);
  505. imagedestroy($QR);
  506. imagedestroy($logo);
  507. return $url.DS.$newname;
  508. }else{
  509. $QR = imagecreatefromstring(file_get_contents($QR));
  510. //输出图片
  511. imagedestroy($QR);
  512. return $url."/uploads/ercode/".$date."/".$name;
  513. }
  514. }
  515. /**
  516. * 对象转数组
  517. * @param $array
  518. * @return array
  519. */
  520. function object_array($array) {
  521. if(is_object($array)) {
  522. $array = (array)$array;
  523. } if(is_array($array)) {
  524. foreach($array as $key=>$value) {
  525. $array[$key] = object_array($value);
  526. }
  527. }
  528. return $array;
  529. }
  530. /**
  531. * curl 请求
  532. * @param $url
  533. * @param string $data
  534. * @param string $method
  535. * @param string $header
  536. * @return mixed
  537. */
  538. function https_request($url, $data = '', $method = 'GET', $header = '')
  539. {
  540. $curl = curl_init(); // 启动一个CURL会话
  541. curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
  542. if ($header) {
  543. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  544. curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
  545. } else {
  546. curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
  547. }
  548. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
  549. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
  550. curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
  551. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
  552. curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
  553. if ($method == 'POST') {
  554. curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
  555. if ($data != '') {
  556. curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
  557. }
  558. }
  559. curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
  560. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
  561. $tmpInfo = curl_exec($curl); // 执行操作
  562. curl_close($curl); // 关闭CURL会话
  563. return $tmpInfo; // 返回数据
  564. }
  565. /**
  566. * 对查询结果集进行排序,支持多维数组
  567. * @param array $list 查询结果
  568. * @param string $field 排序的字段名
  569. * @param array $sortby 排序类型
  570. * asc正向排序 desc逆向排序 nat自然排序
  571. * @return array
  572. */
  573. function list_sort($list,$field, $sortby='asc') {
  574. if(is_array($list)){
  575. $refer = $resultSet = array();
  576. foreach ($list as $i => $data)
  577. $refer[$i] = &$data[$field];
  578. switch ($sortby) {
  579. case 'asc': // 正向排序
  580. asort($refer);
  581. break;
  582. case 'desc':// 逆向排序
  583. arsort($refer);
  584. break;
  585. case 'nat': // 自然排序
  586. natcasesort($refer);
  587. break;
  588. }
  589. foreach ( $refer as $key=> $val)
  590. $resultSet[] = &$list[$key];
  591. return $resultSet;
  592. }
  593. return false;
  594. }
  595. /**
  596. * 邮件发送函数
  597. * @param string $mailTo 收件人邮箱,多个用逗号连接
  598. * @param string $subject 邮件主题
  599. * @param string $body 邮件内容
  600. */
  601. function sendMail($mailTo,$subject,$body,$type = 'HTML',$debug = false) {
  602. import('maile/smtp', EXTEND_PATH);
  603. $smtpserver = config('smtp.server'); //SMTP服务器
  604. $smtpserverport = config('smtp.serverport'); //SMTP服务器端口
  605. $smtpusermail = config('smtp.usermail'); //SMTP服务器的用户邮箱
  606. $smtpemailto = $mailTo;//收件人
  607. $smtpuser = config('smtp.smtpuser'); //SMTP服务器的用户帐号
  608. $smtppass = config('smtp.pass'); //SMTP服务器的用户密码
  609. $mailsubject = $subject; //邮件主题
  610. $mailsubject = "=?UTF-8?B?" . base64_encode($mailsubject) . "?="; //防止乱码
  611. $mailbody = $body; //邮件内容
  612. // $mailbody = "=?UTF-8?B?".base64_encode($mailbody)."?="; //防止乱码
  613. $mailtype = $type; //邮件格式(HTML/TXT),TXT为文本邮件. 139邮箱的短信提醒要设置为HTML才正常
  614. $smtp = new smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass); //这里面的一个true是表示使用身份验证,否则不使用身份验证.
  615. $smtp->debug = $debug; //是否显示发送的调试信息
  616. $sta = $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
  617. return $sta;
  618. }
  619. /**
  620. * @method 多维数组变成一维数组
  621. * @staticvar array $result_array
  622. * @param type $array
  623. * @return type $array
  624. * @author yanhuixian
  625. */
  626. function arrToOne($multi) {
  627. $arr = array();
  628. foreach ($multi as $key => $val) {
  629. if( is_array($val) ) {
  630. $arr = array_merge($arr, arrToOne($val));
  631. } else {
  632. $arr[] = $val;
  633. }
  634. }
  635. return $arr;
  636. }
  637. /*
  638. * 判断当前域名http或https,组装域名
  639. */
  640. function http_type(){
  641. $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
  642. return $http_type . $_SERVER['HTTP_HOST'];
  643. }
  644. /*
  645. * base64格式图片转图片文件
  646. */
  647. function base64_img($base64url,$bool = false)
  648. {
  649. //匹配出图片的格式
  650. $base64url = str_replace (' ' , '+' , $base64url);
  651. if ( preg_match ('/^(data:\s*image\/(\w+);base64,)/' , $base64url , $result) ) {
  652. $type = $result[ 2 ];
  653. $new_file = ROOT_PATH . 'public' . DS . 'uploads/face/' . date ('Ymd' , time ()) . "/";
  654. if ( !file_exists ($new_file) ) {
  655. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  656. mkdir ($new_file , 0700,true);
  657. }
  658. $new = md5 (time ().uuid());
  659. $new_file = $new_file . $new . ".{$type}";
  660. if ( file_put_contents ($new_file , base64_decode (str_replace ($result[ 1 ] , '' , $base64url))) ) {
  661. //压缩图片
  662. if($bool == true){
  663. // $new = md5 (time ().uuid());
  664. image_png_size_add ($new_file , $new_file);
  665. //删除未压缩前图片
  666. // unlink ($new_file);
  667. }
  668. $url = http_type();
  669. $file_name = "/uploads/face/" . date ('Ymd' , time ()) . "/" . $new . ".{$type}";
  670. return ['code'=>200,'msg'=>$file_name];
  671. } else {
  672. return ['code'=>100,'msg'=>'图片不是base64格式!'];
  673. }
  674. }
  675. }
  676. /*
  677. * 关联数组转为索引数组
  678. */
  679. function toIndexArr($arr){
  680. $i=0;
  681. foreach($arr as $key => $value){
  682. $newArr[$i] = $value;
  683. $i++;
  684. }
  685. return $newArr;
  686. }
  687. /**
  688. * 二维数组按某个字段排序
  689. * @param $arr 二维数组
  690. * @param $field 排序字段
  691. * @param string $direction 排序顺序标志 SORT_DESC 降序;SORT_ASC 升序
  692. * @return mixed
  693. */
  694. function arrayToSort($arr,$field,$direction = "SORT_DESC"){
  695. $arrSort = [];
  696. foreach($arr AS $uniqid => $row){
  697. foreach($row AS $key=>$value){
  698. $arrSort[$key][$uniqid] = $value;
  699. }
  700. }
  701. if(!empty($arr)){
  702. array_multisort($arrSort[$field], constant($direction), $arr);
  703. }
  704. return $arr;
  705. }
  706. /*
  707. * 过滤敏感词
  708. */
  709. function filter_words($word){
  710. //获取敏感数据
  711. $sensitive = cache('sensitive_word');
  712. if(!$sensitive){
  713. $sensitive = Db::name('word')->column ('word');
  714. $sensitive = array_combine($sensitive,array_fill(0,count($sensitive),'**'));
  715. cache('sensitive_word',$sensitive);
  716. }
  717. //过滤
  718. $content = strtr($word, $sensitive);
  719. return $content;
  720. }
  721. /*
  722. * 淘宝接口:根据ip获取所在城市名称
  723. */
  724. function get_ip_area($ip){
  725. if($ip == '127.0.0.1'){
  726. return '内网IP';
  727. }
  728. $url = "http://ip.taobao.com/service/getIpInfo.php?ip={$ip}";
  729. $ret = https_request($url);
  730. $arr = json_decode($ret,true);
  731. if($arr['code'] == 1){
  732. return '';
  733. }else{
  734. // return $arr['data']['country'].$arr['data']['region'].$arr['data']['city'].$arr['data']['isp'];
  735. }
  736. }