common.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. /**
  3. * 常用自定义函数库
  4. */
  5. use think\Response;
  6. use Ramsey\Uuid\Uuid as CreateUuid;
  7. use think\facade\Request;
  8. if (!function_exists('enjson')) {
  9. /**
  10. * 接口参数返回
  11. * @param mixed $var 返回的数据
  12. * @param string $msg 返回提示语,如果是数组就自动代替未返回的参数
  13. * @param integer $code 状态码 200
  14. * @return \think\response\Json
  15. */
  16. function enjson($code = 200,$msg = '',$var = [],$type = 'json'){
  17. switch ($code) {
  18. case 200:
  19. $error = '成功';
  20. break;
  21. case 401:
  22. $error = '未授权';
  23. break;
  24. case 403:
  25. $error = '没有权限';
  26. break;
  27. default:
  28. $error = '失败';
  29. break;
  30. }
  31. if(is_array($msg) || is_object($msg)){
  32. $var = $msg;
  33. $data['msg'] = $error;
  34. }else{
  35. $data['msg'] = empty($msg) ? $error : $msg;
  36. }
  37. if(isset($var['url'])){
  38. $data['url'] = $var['url'];
  39. unset($var['url']);
  40. }
  41. $data['code'] = $code;
  42. $data['data'] = $var;
  43. if($type == 'array'){
  44. return $data;
  45. }
  46. return Response::create($data,$type);
  47. }
  48. }
  49. if (!function_exists('code')) {
  50. /**
  51. * 友好的调试打印方法
  52. * @param $var
  53. */
  54. function code($var, $exit = true)
  55. {
  56. $output = print_r($var, true);
  57. $output = "<pre>" . htmlspecialchars($output, ENT_QUOTES)."</pre>";
  58. echo $output;
  59. if ($exit) {
  60. exit();
  61. }
  62. }
  63. }
  64. if (!function_exists('dehtml')) {
  65. /**
  66. * 把HTML实体转换为HTML可视标签
  67. */
  68. function dehtml($str)
  69. {
  70. return htmlspecialchars_decode($str);
  71. }
  72. }
  73. if (!function_exists('is_wechat')) {
  74. /**
  75. * 判断是否在微信中打开
  76. * @return boolean
  77. */
  78. function is_wechat()
  79. {
  80. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
  81. return true;
  82. }
  83. return false;
  84. }
  85. }
  86. if (!function_exists('en_phone')) {
  87. /**
  88. * 隐藏手机号中间四位
  89. * @param [type] $phone
  90. * @return 从第三个字符隐藏4个字符
  91. */
  92. function en_phone($phone,$s = 3,$d = 4)
  93. {
  94. return substr_replace($phone, '****',$s,$d);
  95. }
  96. }
  97. if (!function_exists('money')) {
  98. /**
  99. * 格式化钱保留小数点
  100. * @param float $amount
  101. */
  102. function money($amount)
  103. {
  104. $amount = round($amount,2);
  105. return sprintf("%01.2f", $amount);
  106. }
  107. }
  108. if (!function_exists('uuid')) {
  109. /**
  110. * 获取唯一ID
  111. *
  112. * @param intege $type 类型
  113. * @param intege $data 要计算的KEY数据
  114. * @param Boolean $hex 返回的字符是否带-风格符
  115. * @return void
  116. */
  117. function uuid(int $type = 0, $hex = true, $data = null)
  118. {
  119. switch ($type) {
  120. case 1: //基于散列的MD5
  121. $uuid = CreateUuid::uuid4();
  122. break;
  123. case 2: //基于散列的MD5
  124. $uuid = CreateUuid::uuid3(CreateUuid::NAMESPACE_DNS, $data);
  125. break;
  126. case 3: //基于SHA1
  127. $uuid = CreateUuid::uuid5(CreateUuid::NAMESPACE_DNS, $data);
  128. break;
  129. default: //基于时间
  130. $uuid = CreateUuid::uuid1();
  131. break;
  132. }
  133. return $hex ? $uuid->getHex() : $uuid->toString();
  134. }
  135. }
  136. if (!function_exists('order_no')) {
  137. /**
  138. * 订单号
  139. */
  140. function order_no($pre = '')
  141. {
  142. return $pre.\uuid\Intuuid::generateParticle();
  143. }
  144. }
  145. if (!function_exists('getcode')) {
  146. /**
  147. * 生成随机数
  148. * @param int $limit 要生成的随机数长度
  149. **/
  150. function getcode($limit=6)
  151. {
  152. $rand_array = range(0, 9);
  153. shuffle($rand_array); //调用现成的数组随机排列函数
  154. $str = array_slice($rand_array, 0, $limit);//截取前$limit个
  155. return implode(null, $str);
  156. }
  157. }
  158. if (!function_exists('create_code')) {
  159. /**
  160. * 生成邀请码
  161. * @param int $id 要加密换算的ID
  162. *
  163. */
  164. function create_code($id)
  165. {
  166. static $source_string = 'E5FCDG3HQA4B1NOPIJ2RSTUV67MWX89KLYZ';
  167. $num = $id;
  168. $code = '';
  169. while ($num > 0) {
  170. $mod = $num % 35;
  171. $num = ($num - $mod) / 35;
  172. $code = $source_string[$mod].$code;
  173. }
  174. if (empty($code[3])) {
  175. $code = str_pad($code, 4, '0', STR_PAD_LEFT);
  176. }
  177. return $code;
  178. }
  179. }
  180. if (!function_exists('de_code')) {
  181. /**
  182. * 解密邀请码
  183. * @param string $code 要解密经过create_code函数加密ID
  184. */
  185. function de_code($code)
  186. {
  187. static $source_string = 'E5FCDG3HQA4B1NOPIJ2RSTUV67MWX89KLYZ';
  188. if (strrpos($code, '0') !== false) {
  189. $code = substr($code, strrpos($code, '0')+1);
  190. }
  191. $len = strlen($code);
  192. $code = strrev($code);
  193. $num = 0;
  194. for ($i=0; $i < $len; $i++) {
  195. $num += strpos($source_string, $code[$i]) * pow(35, $i);
  196. }
  197. return $num;
  198. }
  199. }
  200. if (!function_exists('ids')) {
  201. /**
  202. * Ids 参数强制转换为整形
  203. * @param string|array $array 要强制转换的参数是字符串1,2,3还是数组[1,2,3]
  204. * @param bool $is_ary 返回是字符串1,2,3还是数组[1,2,3]
  205. * @return string
  206. */
  207. function ids($ids,$is_ary = false)
  208. {
  209. if(empty($ids)){
  210. return $is_ary ? [] : '';
  211. }
  212. if (is_array($ids)) {
  213. $ids_ary = $ids;
  214. } else {
  215. $ids_ary = explode(',', trim($ids, ','));
  216. }
  217. $id_array = [];
  218. foreach ($ids_ary as $key => $value) {
  219. $id_array[$key] = abs(intval($value));
  220. }
  221. return $is_ary ? $id_array : implode(',', $id_array);
  222. }
  223. }
  224. if (!function_exists('array_values_unset')) {
  225. /**
  226. * 删除数组中指定键值
  227. * @param array $arr 要删除的数组
  228. * @return array
  229. */
  230. function array_values_unset(string $values, array $ary)
  231. {
  232. $ary_key = array_search($values, $ary);
  233. if ($ary_key !== false) {
  234. unset($ary[$ary_key]);
  235. }
  236. return $ary;
  237. }
  238. }
  239. if (!function_exists('sbc2Dbc')) {
  240. /**
  241. * 全角转半角
  242. * @param string $str
  243. * @return string
  244. **/
  245. function sbc2Dbc($str)
  246. {
  247. $arr = array(
  248. '0'=>'0', '1'=>'1', '2'=>'2', '3'=>'3', '4'=>'4','5'=>'5', '6'=>'6', '7'=>'7', '8'=>'8', '9'=>'9',
  249. 'A'=>'A', 'B'=>'B', 'C'=>'C', 'D'=>'D', 'E'=>'E','F'=>'F', 'G'=>'G', 'H'=>'H', 'I'=>'I', 'J'=>'J',
  250. 'K'=>'K', 'L'=>'L', 'M'=>'M', 'N'=>'N', 'O'=>'O','P'=>'P', 'Q'=>'Q', 'R'=>'R', 'S'=>'S', 'T'=>'T',
  251. 'U'=>'U', 'V'=>'V', 'W'=>'W', 'X'=>'X', 'Y'=>'Y','Z'=>'Z', 'a'=>'a', 'b'=>'b', 'c'=>'c', 'd'=>'d',
  252. 'e'=>'e', 'f'=>'f', 'g'=>'g', 'h'=>'h', 'i'=>'i','j'=>'j', 'k'=>'k', 'l'=>'l', 'm'=>'m', 'n'=>'n',
  253. 'o'=>'o', 'p'=>'p', 'q'=>'q', 'r'=>'r', 's'=>'s', 't'=>'t', 'u'=>'u', 'v'=>'v', 'w'=>'w', 'x'=>'x',
  254. 'y'=>'y', 'z'=>'z',
  255. '('=>'(', ')'=>')', '〔'=>'(', '〕'=>')', '【'=>'[','】'=>']', '〖'=>'[', '〗'=>']', '“'=>'"', '”'=>'"',
  256. '‘'=>'\'', '’'=>'\'', '{'=>'{', '}'=>'}', '《'=>'<','》'=>'>','%'=>'%', '+'=>'+', '—'=>'-', '-'=>'-',
  257. '~'=>'~',':'=>':', '。'=>'.', '、'=>',', ','=>',', '、'=>',', ';'=>';', '?'=>'?', '!'=>'!', '…'=>'-',
  258. '‖'=>'|', '”'=>'"', '’'=>'`', '‘'=>'`', '|'=>'|', '〃'=>'"',' '=>' ', '×'=>'*', ' ̄'=>'~', '.'=>'.', '*'=>'*',
  259. '&'=>'&','<'=>'<', '>'=>'>', '$'=>'$', '@'=>'@', '^'=>'^', '_'=>'_', '"'=>'"', '¥'=>'$', '='=>'=',
  260. '\'=>'\\', '/'=>'/'
  261. );
  262. return strtr($str, $arr);
  263. }
  264. }
  265. if (!function_exists('api')) {
  266. /**
  267. * Url生成
  268. * @param integer $version API版本
  269. * @param string $url 访问网址
  270. * @param integer $appid 当前APPID
  271. * @param integer $id 传入参数
  272. * @param array $vars 变量
  273. * @return void
  274. */
  275. function api(int $version,$url,int $appid = 0,array $vars = []){
  276. $id = isset($vars['id']) ? '/'.$vars['id'] : '';
  277. if(isset($vars['id'])){
  278. unset($vars['id']);
  279. }
  280. $vars = empty($vars) ? '' : '?'.http_build_query($vars);
  281. $appid = $appid ? '/'.$appid : '';
  282. $url = strtolower($url);
  283. $urls = explode('/',$url);
  284. if('system' == $urls[0]){
  285. unset($urls[0]);
  286. if('miniprogram' == strtolower($urls[1])){
  287. unset($urls[1]);
  288. }
  289. $urls = implode('/',$urls);
  290. return Request::root(true)."/openapi{$appid}/v{$version}/{$urls}";
  291. }else{
  292. return Request::root(true)."/api{$appid}/v{$version}/{$url}{$id}.html".$vars;
  293. }
  294. }
  295. }
  296. if (!function_exists('urls')) {
  297. /**
  298. * Url生成
  299. * @param integer $version API版本
  300. * @param string $url 访问网址
  301. * @param integer $appid 当前APPID
  302. * @param integer $id 传入参数
  303. * @param array $vars 变量
  304. * @return void
  305. */
  306. function urls($url,int $appid = 0,array $vars = []){
  307. $id = isset($vars['id']) ? '/'.$vars['id'] : '';
  308. if(isset($vars['id'])){
  309. unset($vars['id']);
  310. }
  311. $vars = empty($vars) ? '' : '?'.http_build_query($vars);
  312. $appid = $appid ? '/'.$appid : '';
  313. return Request::root(true).'/app'.$appid.'/'.strtolower($url).$id.$vars;
  314. }
  315. }