123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <?php
- // 应用公共文件
- define('OBJ','obj');
- /*
- * @param $url
- * @return mixed|string
- * 自定义 curlGet请求
- */
- function curlGet($url)
- {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- $output = curl_exec($curl);
- if (curl_errno($curl)) {
- return 'ERROR ' . curl_error($curl);
- }
- curl_close($curl);
- return $output;
- }
- /*
- * @param $url
- * @return mixed|string
- * 自定义 curlPost请求
- */
- function curlPost($url, $data = null)
- {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- if (!empty($data)) {
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- }
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- $output = curl_exec($curl);
- curl_close($curl);
- return $output;
- }
- /**
- * @param null|string $id
- * @param array $parameters
- * @param string|null $domain
- * @param string|null $locale
- * @return string
- */
- function trans(string $id, array $parameters = [], string $domain = null, string $locale = null)
- {
- return \support\bootstrap\Translation::trans($id, $parameters, $domain, $locale);
- }
- /**
- * 标准返回
- * @param $msg
- * @param $code
- * @param string $data
- */
- function reMsg($msg, $code, $data = null, $type = '')
- {
- $options = JSON_UNESCAPED_UNICODE;
- if ($data instanceof think\model\Collection or $data instanceof \think\Model) $data = $data->toArray();
- if (empty($data) and $type == OBJ) $options = $options | JSON_FORCE_OBJECT;
- return json(['code' => $code, 'msg' => $msg, 'data' => $data], $options);
- }
- /**
- * 标准成功返回 列表
- * @param string $msg
- * @param string $data
- */
- function success($msg = '', $data = [], $type = '')
- {
- if (empty($msg)) $msg = trans('Success');
- return reMsg($msg, \app\common\ReturnCode::SUCCESS, $data, $type);
- }
- /**
- * @param string $msg
- * @param int $code
- * @param array $data
- * @param string $return_type
- * @return array|\think\response\Json
- */
- function apiResponseSuccess( $msg = 'Success',$code = 200,$data = [] ,$return_type = 'json')
- {
- $result = [
- 'msg' => $msg,
- 'code' => $code,
- 'data' => empty($data) ? [] : $data,
- ];
- return $return_type == 'json' ? json($result) : $result;
- }
- /**
- * 标准错误返回
- * @param string $msg
- * @param string $data
- */
- function error($msg = '', $data = null, $type = OBJ)
- {
- if (empty($msg)) $msg = trans('Error');
- return reMsg($msg, \app\common\ReturnCode::ERROR, $data, $type);
- }
- /**
- * @param array $fields
- * @param string $type
- * @return array|mixed
- * 验证请求必须字段,并返回所有请求数据
- */
- function checkField(array $fields = [], $type = 'post')
- {
- $request = request();
- $data = [];
- if ($type == 'post') {
- $data = $request->post();
- } else if ($type == 'get') {
- $data = $request->get();
- } else if ($type == 'request') {
- $data = $request->request();
- }
- if ($fields) {
- foreach ($fields as $field) {
- if (!isset($data[$field])) {
- return error( '参数异常');
- } else if ($field == 'phone') {
- if (!preg_match('/^1[23456789]\d{9}$/', $data['phone'])) {
- return error( '请输入正确的手机号码');
- }
- }
- }
- }
- return $data;
- }
- /**
- * 将对象转换成数组
- * @param $array
- * @return array
- */
- function objectToArray(array $array)
- {
- if (is_object($array)) {
- $array = (array)$array;
- }
- if (is_array($array)) {
- foreach ($array as $key => $value) {
- $array[$key] = objectToArray($value);
- }
- }
- return $array;
- }
- /**
- * 无限极分类
- * @param $list
- * @param string $pk
- * @param string $pid
- * @param string $child
- * @param int $root
- * @return array
- */
- function makeTree($list, $pk = 'id', $pid = 'pid', $child = 'son', $root = 0)
- {
- $tree = [];
- $packData = [];
- foreach ($list as $data) {
- $packData[$data[$pk]] = $data;
- }
- foreach ($packData as $key => $val) {
- if ($val[$pid] == $root) {
- $tree[] =& $packData[$key];
- } else {
- $packData[$val[$pid]][$child][] =& $packData[$key];
- }
- }
- return $tree;
- }
- /**
- * 根据开始时间和结束时间获取中间时间
- * @param $beginTime
- * @param $endTime
- * @return array
- */
- function periodTime($beginTime, $endTime):array
- {
- $i = 0;
- $arr = [];
- while ($beginTime <= $endTime) {
- $arr[$i] = date('Y-m-d', $beginTime);
- //$arr[$i] = $beginTime;
- $beginTime = strtotime('+1 day', $beginTime);
- $i++;
- }
- return $arr;
- }
|