common.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 流年 <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // 应用公共文件
  12. /*app用
  13. * 20210927
  14. * wj
  15. */
  16. function backjson3($code, $msg = "", $data = [], $type = 320)
  17. {
  18. $jsonData = [
  19. 'code' => $code,
  20. 'msg' => $msg,
  21. 'data' => $data,
  22. ];
  23. return json($jsonData);
  24. }
  25. /*
  26. /*app用
  27. * 20210927
  28. * wj
  29. */
  30. function backjson2($code, $msg = "", $data = [], $type = 320)
  31. {
  32. $jsonData = [
  33. 'code' => $code,
  34. 'msg' => $msg,
  35. 'data' => $data,
  36. ];
  37. return json_encode($jsonData, $type);
  38. }
  39. /* 内部使用
  40. * 20210927
  41. * wj
  42. */
  43. function backarr($status, $msg = "", $data = [])
  44. {
  45. $backData = [
  46. 'status' => $status,
  47. 'msg' => $msg,
  48. 'data' => $data,
  49. ];
  50. return $backData;
  51. }
  52. /**
  53. * 获取星期几
  54. *
  55. * @param string $day
  56. * @param integer $type
  57. * @return void
  58. * @author wj
  59. * @date 2022-12-02
  60. */
  61. function getweek($day = '', $type = 1)
  62. {
  63. $weekarr = [7, 1, 2, 3, 4, 5, 6];
  64. if (empty($day)) {
  65. $day = time();
  66. } else {
  67. switch ($type) {
  68. case 1:
  69. $day = strtotime($day);
  70. break;
  71. }
  72. }
  73. $date = date('w', $day);
  74. return $weekarr[$date];
  75. }
  76. /**
  77. * 数组转xml
  78. *
  79. * @param [type] $arr
  80. * @param [type] $xml
  81. * @param string $root
  82. * @return void
  83. * @author wj
  84. * @date 2022-12-02
  85. */
  86. function arrtoxml($arr, $xml = null, $root = '')
  87. {
  88. if (empty($xml)) {
  89. header("Content-type:text/xml;Charset=UTF-8");
  90. $xml = new XMLWriter();
  91. $xml->openMemory();
  92. //$xml->startDocument('1.0', 'UTF-8');
  93. if (!empty($root)) {
  94. $xml->startElement($root);
  95. } else {
  96. $xml->setIndent(true);
  97. }
  98. arrtoxml($arr, $xml);
  99. if (!empty($root)) {
  100. $xml->endElement();
  101. }
  102. $xml->endDocument();
  103. return $xml->outputMemory(true);
  104. } else {
  105. if (is_array($arr)) {
  106. foreach ($arr as $key => $value) {
  107. if (is_array($value)) {
  108. $xml->startElement($key);
  109. arrtoxml($value, $xml);
  110. $xml->endElement();
  111. } else {
  112. if (is_numeric($value)) {
  113. $xml->writeElement($key, $value);
  114. } else {
  115. $xml->startElement($key);
  116. $xml->writeCdata($value);
  117. $xml->endElement();
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124. /**
  125. * xml转数组
  126. *
  127. * @return void
  128. * @author wj
  129. * @date 2022-12-02
  130. */
  131. function xmltoarr($xml)
  132. {
  133. $arr = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  134. $arr = json_decode(json_encode($arr), true);
  135. return $arr;
  136. }
  137. //为cp后台请求验证token
  138. function checkToken()
  139. {
  140. $l_w = new app\admin\logic\webmanger();
  141. $token = request()->server('HTTP_TOKEN');
  142. if (empty($token)) {
  143. $token = request()->param('token');
  144. }
  145. $param = ['token' => $token];
  146. $result = $l_w->queryinfobytoken($param);
  147. if (1 != $result['status']) {
  148. $str = backjson2(0, '登录失效', $result['data']);
  149. exit($str);
  150. }
  151. return $result['data'];
  152. }
  153. //判断是否是手机号
  154. function isMoblid($tel)
  155. {
  156. if (preg_match("/^1[3-9]{1}\d{9}$/", $tel)) {
  157. return true;
  158. } else {
  159. return false;
  160. }
  161. }
  162. //curl 请求
  163. function curl_request($url, $data = null, $header = [])
  164. {
  165. //$headerArray = array("Content-type:application/json;charset='utf-8'", "Accept:application/json");
  166. $curl = curl_init();
  167. curl_setopt($curl, CURLOPT_URL, $url);
  168. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  169. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  170. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  171. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  172. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  173. if (!empty($data)) {
  174. curl_setopt($curl, CURLOPT_POST, true);
  175. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  176. }
  177. $output = curl_exec($curl);
  178. curl_close($curl);
  179. $res = json_decode($output, true);
  180. return $res;
  181. }