common.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. use think\Db;
  3. // 获取模板区域
  4. function get_city_area()
  5. {
  6. $field=['id','pid','name'];
  7. $list=Db::name('china_area')->where('pid',0)->field($field)->select();
  8. foreach ($list as $k=>&$v){
  9. $v['children']= Db::name('china_area')->where('pid',$v['id'])->field($field)->select();
  10. }
  11. return $list;
  12. }
  13. function http_curl($url,$type='get',$res='json',$arr=''){
  14. $headers = array();
  15. //根据API的要求,定义相对应的Content-Type
  16. array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8;application/json");
  17. $curl = curl_init();
  18. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $type);
  19. curl_setopt($curl, CURLOPT_URL, $url);
  20. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  21. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  22. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  23. curl_setopt($curl, CURLOPT_HEADER, false);
  24. $output = curl_exec($curl);
  25. curl_close($curl);
  26. if($res=='json'){
  27. if($output === false){
  28. //请求失败,返回错误信息
  29. return curl_error($curl);
  30. }else{
  31. //请求成功,返回信息
  32. return json_decode($output,true);
  33. }
  34. }
  35. }
  36. function curl_get($url)
  37. {
  38. $ch = curl_init();
  39. curl_setopt($ch, CURLOPT_URL, $url);
  40. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  41. $output = curl_exec($ch);
  42. curl_close($ch);
  43. if($output === false){
  44. return curl_error($ch);
  45. }else{
  46. return json_decode($output,true);
  47. }
  48. }
  49. /**
  50.  * 把返回的数据集转换成Tree
  51.  * @param array $list 要转换的数据集
  52.  * @param string $pk 自增字段(栏目id)
  53.  * @param string $pid parent标记字段
  54.  * @return array
  55.  */
  56. function make_tree($list,$pk='id',$pid='pid',$child='children',$root=0){
  57. if(is_object($list)) $list = $list->toArray();
  58. $tree=array();
  59. $packData=array();
  60. foreach ($list as $data) {
  61. $packData[$data[$pk]] = $data;
  62. }
  63. foreach ($packData as $key =>$val){
  64. if($val[$pid]==$root){//代表跟节点
  65. $tree[]=& $packData[$key];
  66. }else{
  67. $packData[$val[$pid]][$child][]=& $packData[$key]; //找到其父类
  68. }
  69. }
  70. return $tree;
  71. }
  72. //判断字段存在并不为空
  73. function isset_full($arr, $key)
  74. {
  75. if (isset($arr[$key]) && !empty($arr[$key])) {
  76. return true;
  77. } else {
  78. return false;
  79. }
  80. }
  81. //判断字段存在并不为空 并且等于验证值
  82. function isset_full_check($arr, $key,$check_val)
  83. {
  84. if (isset($arr[$key]) && !empty($arr[$key]) && $arr[$key] == $check_val) {
  85. return true;
  86. } else {
  87. return false;
  88. }
  89. }
  90. function all_pay_type()
  91. {
  92. return [ '--','微信[H5]','支付宝[H5]','微信[APP]','支付宝[APP]','微信[公众号]' ];
  93. }
  94. /**
  95. * 生成32位随机数
  96. */
  97. function get32Str($length='32'){
  98. $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  99. $len = strlen($str)-1;
  100. $randstr = '';
  101. for ($i=0;$i<$length;$i++) {
  102. $num=mt_rand(0,$len);
  103. $randstr .= $str[$num];
  104. }
  105. return $randstr;
  106. }