common.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. // 应用公共文件
  3. define('OBJ','obj');
  4. /*
  5. * @param $url
  6. * @return mixed|string
  7. * 自定义 curlGet请求
  8. */
  9. function curlGet($url)
  10. {
  11. $curl = curl_init();
  12. curl_setopt($curl, CURLOPT_URL, $url);
  13. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  14. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  15. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  16. $output = curl_exec($curl);
  17. if (curl_errno($curl)) {
  18. return 'ERROR ' . curl_error($curl);
  19. }
  20. curl_close($curl);
  21. return $output;
  22. }
  23. /*
  24. * @param $url
  25. * @return mixed|string
  26. * 自定义 curlPost请求
  27. */
  28. function curlPost($url, $data = null)
  29. {
  30. $curl = curl_init();
  31. curl_setopt($curl, CURLOPT_URL, $url);
  32. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  33. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  34. if (!empty($data)) {
  35. curl_setopt($curl, CURLOPT_POST, 1);
  36. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  37. }
  38. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  39. $output = curl_exec($curl);
  40. curl_close($curl);
  41. return $output;
  42. }
  43. /**
  44. * @param null|string $id
  45. * @param array $parameters
  46. * @param string|null $domain
  47. * @param string|null $locale
  48. * @return string
  49. */
  50. function trans(string $id, array $parameters = [], string $domain = null, string $locale = null)
  51. {
  52. return \support\bootstrap\Translation::trans($id, $parameters, $domain, $locale);
  53. }
  54. /**
  55. * 标准返回
  56. * @param $msg
  57. * @param $code
  58. * @param string $data
  59. */
  60. function reMsg($msg, $code, $data = null, $type = '')
  61. {
  62. $options = JSON_UNESCAPED_UNICODE;
  63. if ($data instanceof think\model\Collection or $data instanceof \think\Model) $data = $data->toArray();
  64. if (empty($data) and $type == OBJ) $options = $options | JSON_FORCE_OBJECT;
  65. return json(['code' => $code, 'msg' => $msg, 'data' => $data], $options);
  66. }
  67. /**
  68. * 标准成功返回 列表
  69. * @param string $msg
  70. * @param string $data
  71. */
  72. function success($msg = '', $data = [], $type = '')
  73. {
  74. if (empty($msg)) $msg = trans('Success');
  75. return reMsg($msg, \app\common\ReturnCode::SUCCESS, $data, $type);
  76. }
  77. /**
  78. * @param string $msg
  79. * @param int $code
  80. * @param array $data
  81. * @param string $return_type
  82. * @return array|\think\response\Json
  83. */
  84. function apiResponseSuccess( $msg = 'Success',$code = 200,$data = [] ,$return_type = 'json')
  85. {
  86. $result = [
  87. 'msg' => $msg,
  88. 'code' => $code,
  89. 'data' => empty($data) ? [] : $data,
  90. ];
  91. return $return_type == 'json' ? json($result) : $result;
  92. }
  93. /**
  94. * 标准错误返回
  95. * @param string $msg
  96. * @param string $data
  97. */
  98. function error($msg = '', $data = null, $type = OBJ)
  99. {
  100. if (empty($msg)) $msg = trans('Error');
  101. return reMsg($msg, \app\common\ReturnCode::ERROR, $data, $type);
  102. }
  103. /**
  104. * @param array $fields
  105. * @param string $type
  106. * @return array|mixed
  107. * 验证请求必须字段,并返回所有请求数据
  108. */
  109. function checkField(array $fields = [], $type = 'post')
  110. {
  111. $request = request();
  112. $data = [];
  113. if ($type == 'post') {
  114. $data = $request->post();
  115. } else if ($type == 'get') {
  116. $data = $request->get();
  117. } else if ($type == 'request') {
  118. $data = $request->request();
  119. }
  120. if ($fields) {
  121. foreach ($fields as $field) {
  122. if (!isset($data[$field])) {
  123. return error( '参数异常');
  124. } else if ($field == 'phone') {
  125. if (!preg_match('/^1[23456789]\d{9}$/', $data['phone'])) {
  126. return error( '请输入正确的手机号码');
  127. }
  128. }
  129. }
  130. }
  131. return $data;
  132. }
  133. /**
  134. * 将对象转换成数组
  135. * @param $array
  136. * @return array
  137. */
  138. function objectToArray(array $array)
  139. {
  140. if (is_object($array)) {
  141. $array = (array)$array;
  142. }
  143. if (is_array($array)) {
  144. foreach ($array as $key => $value) {
  145. $array[$key] = objectToArray($value);
  146. }
  147. }
  148. return $array;
  149. }
  150. /**
  151. * 无限极分类
  152. * @param $list
  153. * @param string $pk
  154. * @param string $pid
  155. * @param string $child
  156. * @param int $root
  157. * @return array
  158. */
  159. function makeTree($list, $pk = 'id', $pid = 'pid', $child = 'son', $root = 0)
  160. {
  161. $tree = [];
  162. $packData = [];
  163. foreach ($list as $data) {
  164. $packData[$data[$pk]] = $data;
  165. }
  166. foreach ($packData as $key => $val) {
  167. if ($val[$pid] == $root) {
  168. $tree[] =& $packData[$key];
  169. } else {
  170. $packData[$val[$pid]][$child][] =& $packData[$key];
  171. }
  172. }
  173. return $tree;
  174. }
  175. /**
  176. * 根据开始时间和结束时间获取中间时间
  177. * @param $beginTime
  178. * @param $endTime
  179. * @return array
  180. */
  181. function periodTime($beginTime, $endTime):array
  182. {
  183. $i = 0;
  184. $arr = [];
  185. while ($beginTime <= $endTime) {
  186. $arr[$i] = date('Y-m-d', $beginTime);
  187. //$arr[$i] = $beginTime;
  188. $beginTime = strtotime('+1 day', $beginTime);
  189. $i++;
  190. }
  191. return $arr;
  192. }