common.php 67.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535
<?php

// 公共助手函数
function strToUtf8($str)
{

    $encode = mb_detect_encoding($str, array("ASCII", 'UTF-8', "GB2312", "GBK", 'BIG5'));

    if ($encode == 'UTF-8') {

        return $str;

    } else {

        return mb_convert_encoding($str, 'UTF-8', $encode);

    }

}

//处理order表的good信息   后台的
function orderGood_admin($data)
{
    $data = unserialize($data);
    $str = '';
    if (isset($data['order_good'])) {
        foreach ($data['order_good'] as $k => $v) {
            $str .= "{$v['good']['name']}({$v['good']['price']}/{$v['good']['num']});";
        }
    }
    if (isset($data['good'])) {
        $str .= "{$data['good']['name']}({$data['good']['price']}/{$data['good']['gou_num']});";
    }
    return $str;
}


//平台评价数量
function ping_num()
{
    return \app\api\model\Cm0ping::where(wherePing())
        ->where(read_w())
        ->count();
}

//4s店的评价数
function ping_num4()
{
    return \app\api\model\Cm0ping::where(where4s())
        ->where(read_w())
        ->count();
}

//推荐购车的数量
function sug_num()
{
    return \app\api\model\Cm0sug0car::where(where4s())
        ->where(read_w())
        ->count();
}

//4S预约保养用户数
function bao_user_num()
{
    return \app\api\model\Cm0bao0user::where('id', 'in', baoUser())
        ->where(read_w())
        ->count();
}

//4S用户车辆
function che_num()
{
    return \app\api\model\Cm0che::where(where4s())
        ->where(read_w())
        ->count();
}

//4S店配件订单
function pei_num()
{
    return \app\api\model\Cm0s40order::where('id', 'in', wherePei(0))
        ->where(read_w())
        ->count();
}

//4S店的购车订单
function car_num()
{
    return \app\api\model\Cm0s40order::where('id', 'in', whereCar(0))
        ->where(read_w())
        ->count();
}

//4S店保养订单
function bao_num()
{
    return \app\api\model\Cm0s40order::where('id', 'in', whereBao(0))
        ->where(read_w())
        ->count();
}

//4S店退款订单
function tui_num()
{
    return \app\api\model\Cm0s40order::where('id', 'in', whereTui())
        ->where(read_w())
        ->count();
}

//平台底部审核
function under_num()
{
    return \app\api\model\Cm0index0under::where(wherePing2())
        ->where(read_w())
        ->count();
}

//平台消息审核
function info_num()
{
    return \app\api\model\Cm0info::where(wherePing2())
        ->where(read_w())
        ->count();
}

//平台活动审核
function active_num()
{
    return \app\api\model\Cm0active::where(wherePing2())
        ->where(read_w())
        ->count();
}

//平台限时活动审核
function active2_num()
{
    return \app\api\model\Cm0active2::where(wherePing2())
        ->where(read_w())
        ->count();
}

//订单的数量
function order_num()
{
    return \app\api\model\Cm0order::where(whereOrder(0))
        ->where(read_w())
        ->count();
}

//平台的退款订单数
function tui_num2()
{
    return \app\api\model\Cm0order::where(whereTui2())
        ->where(read_w())
        ->count();
}


//读的条件
function read_w()
{
    $where = [];
    $where['read'] = ['eq', 0];
    return $where;
}


function souAdmin($where, $sou_arr)
{
    $where2 = [];
    foreach ($sou_arr as $k => $v) {
        foreach ($where as $kk => $vv) {
            if (isset($where[$k])) {
                $where2[$v] = $where[$k];
                unset($where[$k]);
            }
        }
    }
    return [
        'where' => $where,
        'where2' => $where2,
    ];
}


//车辆违章查询
function checkChe($che_id)
{
    $pai_name = \app\api\model\Cm0che::where('id', $che_id)->value('pai_name');
    $pai_arr = str2arr($pai_name, '·');
    $pai_str = '';
    foreach ($pai_arr as $k => $v) {
        $pai_str .= $v;
    }
    header("Content-type: application/json; charset=utf-8");
    $params = array(
        'carNo' => $pai_str,
        'carType' => '02',
        'appkey' => '210b1f001bbec105ca3ba9f417e07ecb'
    );
    $url = 'https://way.jd.com/weixiaobao/wzQuery';
    $data = wx_http_request($url, $params);
    return json_decode($data, true);
}


//车辆违章查询接口
function wx_http_request($url, $params, $body = "", $isPost = false, $isImage = false)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url . "?" . http_build_query($params));
    if ($isPost) {
        if ($isImage) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                    'Content-Type: multipart/form-data;',
                    "Content-Length: " . strlen($body)
                )
            );
        } else {
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                    'Content-Type: text/plain'
                )
            );
        }
        curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
    }
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}


//此管理员的子代ID
function adminChild($admin_id)
{
    $tree = \fast\Tree::instance();
    $tree->init(collection(\app\admin\model\Admin::field('id,pid')->select())->toArray(), 'pid');
    $data = $tree->getChildrenIds($admin_id);
    return $data;
}

//此管理员的子代ID,包括自己
function adminChild2($admin_id)
{
    $id_arr = adminChild($admin_id);
    array_push($id_arr, $admin_id);
    return $id_arr;
}


//钱转换成积分
function moneyFen($money = 0)
{
    $cm0bi = \app\api\model\Cm0bi::find();
    return round($cm0bi['ping'] * $money * 0.01, 2);
}


function getCateGood2($cate_str_ids)
{
    $arr = str2arr($cate_str_ids);
    $arr2 = [];
    foreach ($arr as $k => $v) {
        $arr2 = array_merge($arr2, getCateGood($v));
    }
    return array_unique($arr2);
}


//获取cate_id下的商品,返回goods_id;
function getCateGood($cate_id)
{
    $find = \app\api\model\Litestorecategory::where('id', $cate_id)->find();
    if (!empty($find['good'])) {
        return array_column($find['good'], 'goods_id');
    }
    if ($find['level'] == 2) {
        $cate_id_arr = \app\api\model\Litestorecategory::where('pid', $find['id'])->column('id');
        return \app\api\model\Litestoregoods::where('category_id', 'in', $cate_id_arr)->column('goods_id');
    }
    if ($find['level'] == 1) {
        $cate_id_arr = \app\api\model\Litestorecategory::where('pid', $find['id'])->column('id');
        $cate_id_arr2 = \app\api\model\Litestorecategory::where('pid', 'in', $cate_id_arr)->column('id');
        return \app\api\model\Litestoregoods::where('category_id', 'in', $cate_id_arr2)->column('goods_id');
    }
    return [];
}


//总平台的退款订单,返回条件数组
function whereTui2()
{
    $where2 = [];
    $where2['xia_time'] = ['neq', 0];//不找垃圾订单
    $where2['state'] = ['in', [2, 3, 4]];//找要退款的
    return $where2;
}


//返回条件数组
function whereOrder($tui = 1)
{
    $where2 = [];
    $where2['xia_time'] = ['neq', 0];//不找垃圾订单
    if ($tui == 1) {
        $where2['state'] = ['in', [1, 2, 3, 4]];//有退款的
    } else {
        $where2['state'] = ['in', [1]];//无退款的
    }
    $where2['cm_cm0order.s4_id'] = ['eq', 0];
    return $where2;
}


//返回平台的where数组
function wherePing()
{
    $where2 = [];
    $where2['s4_id'] = ['eq', 0];
    return $where2;
}

//返回非平台的where数组
function wherePing2()
{
    $where2 = [];
    $where2['s4_id'] = ['neq', 0];
    return $where2;
}


//4s店的退款订单
function whereTui()
{
    $where2 = [];
    $where2['xia_time'] = ['neq', 0];//不找垃圾订单
    $where2['state'] = ['in', [2, 3]];//找要退款的
    $id_arr = \app\api\model\Cm0s40order::where($where2)->where(where4s())->column('id');
    return $id_arr;
}


//4s店的保养订单
function whereBao($tui = 1)
{
    $where2 = [];
    $where2['xia_time'] = ['neq', 0];//不找垃圾订单
    $where2['type'] = ['in', [0, 1, 5, 6]];
    if ($tui == 1) {
        $where2['state'] = ['in', [1, 2, 3]];//默认找退款的
    } else {
        $where2['state'] = ['in', [1]];//不找退款
    }
    $id_arr = \app\api\model\Cm0s40order::where($where2)->where(where4s())->column('id');
    return $id_arr;
}


//后台4s店购车订单的搜索条件
function whereCar($tui = 1)
{
    $where2 = [];
    $where2['xia_time'] = ['neq', 0];//不找垃圾订单
    $where2['type'] = ['in', [2, 4]];//配件
    if ($tui == 1) {
        $where2['state'] = ['in', [1, 2, 3]];//找未支付的
    } else {
        $where2['state'] = ['in', [1]];//未支付的
    }
    $id_arr = \app\api\model\Cm0s40order::where($where2)->where(where4s())->column('id');
    return $id_arr;
}


//后台的4s配件搜索条件
function wherePei($tui = 1)
{
    $where2 = [];
    $where2['xia_time'] = ['neq', 0];//不找垃圾订单
    $where2['type'] = ['eq', 3];//配件
    if ($tui == 1) {
        $where2['state'] = ['in', [1, 2, 3]];//找已退款的
    } else {
        $where2['state'] = ['in', [1]];//找支付的
    }
    $id_arr = \app\api\model\Cm0s40order::where(where4s())
        ->where($where2)
        ->column('id');
    return $id_arr;
}


//后台的4S店的条件
function where4s()
{
    $where = [];
    $where['s4_id'] = ['eq', s4_id02()];
    return $where;
}

//4S预约保养的搜索条件
function baoUser()
{
    //找已经下单支付的+找自家的
    $order_no_arr = \app\api\model\Cm0s40order::where('type', 0)->where('state', 1)->column('order_no');
    $id_arr = \app\api\model\Cm0bao0user::where('s4_id', s4_id02())
        ->where('order_no', 'in', $order_no_arr)
        ->column('id');
    return $id_arr;
}


//绑定4s店的用户id
function userS4($s4_id = '')
{
    if (empty($s4_id)) {
        $s4_id = s4_id02();
    }
    $user_id_arr1 = \app\api\model\User::where('s4_id', $s4_id)->column('id');
    $che_id_arr = \app\api\model\Cm0che::where('s4_id', $s4_id)->column('id');
    $user_id_arr2 = \app\api\model\Cm0che0user::where('che_id', 'in', $che_id_arr)->column('user_id');
    $user_id_arr = array_merge($user_id_arr1, $user_id_arr2);
    return array_unique($user_id_arr);
}


//公共图片
function comImg()
{
    $data = \app\api\model\Cm0images::all();
    $data2 = [];
    foreach ($data as $k => $v) {
        $data2[$v['type2']] = cdnurl($v['image']);
    }
    return $data2;
}


//使用红包,传全部金额
function useRed($red, $all_pay)
{
    if (ck_red($red)) {
        $pay2 = round($all_pay - $red['red']['cheap'], 2);
        if ($pay2 <= 0) {
            $pay2 = 0;
        }
        \app\api\model\Cm0red0user::where('id', $red['id'])->setDec('num');
        delRed($red['id']);
    } else {
        $pay2 = $all_pay;
    }
    return $pay2;
}

//where的group
function whereG()
{
    $where = [];
    // $where['id'] = ['not in', [1, 2, 3, 5]];//这几个是特殊组
    $where['s4_id'] = ['eq', 0];
    return $where;
}


//删除用户自己的红包
function delRed($list_id)
{
    $num = \app\api\model\Cm0red0user::where('id', $list_id)->value('num');
    if ($num <= 0) {
        \app\api\model\Cm0red0user::where('id', $list_id)->delete();
    }
}


//获取后台的
function s4_id02()
{
    $admin = \think\Session::get('admin');
    if (!empty($admin['s4_id'])) {

        return $admin['s4_id'];
    }
    return null;
}


//获取后台的
function admin_id()
{
    $admin = \think\Session::get('admin');
    return $admin['id'];
}


if (!function_exists('__')) {

    /**
     * 获取语言变量值
     * @param string $name 语言变量名
     * @param array $vars 动态变量值
     * @param string $lang 语言
     * @return mixed
     */
    function __($name, $vars = [], $lang = '')
    {
        if (is_numeric($name) || !$name) {
            return $name;
        }
        if (!is_array($vars)) {
            $vars = func_get_args();
            array_shift($vars);
            $lang = '';
        }
        return \think\Lang::get($name, $vars, $lang);
    }
}

if (!function_exists('format_bytes')) {

    /**
     * 将字节转换为可读文本
     * @param int $size 大小
     * @param string $delimiter 分隔符
     * @return string
     */
    function format_bytes($size, $delimiter = '')
    {
        $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
        for ($i = 0; $size >= 1024 && $i < 6; $i++) {
            $size /= 1024;
        }
        return round($size, 2) . $delimiter . $units[$i];
    }
}

if (!function_exists('datetime')) {

    /**
     * 将时间戳转换为日期时间
     * @param int $time 时间戳
     * @param string $format 日期时间格式
     * @return string
     */
    function datetime($time, $format = 'Y-m-d H:i:s')
    {
        $time = is_numeric($time) ? $time : strtotime($time);
        return date($format, $time);
    }
}

if (!function_exists('human_date')) {

    /**
     * 获取语义化时间
     * @param int $time 时间
     * @param int $local 本地时间
     * @return string
     */
    function human_date($time, $local = null)
    {
        return \fast\Date::human($time, $local);
    }
}


//判断添加车辆,绑定4S店的次数为1时,进行反积分
if (!function_exists('addLog')) {
    function addLog($user_id)
    {
        $find = \app\api\model\Cm0add0log::where('user_id', $user_id)->find();
        if ($find['num_che'] == 1 && $find['num_s4'] == 1) {
            $cm0frist0fen = \app\api\model\Cm0frist0fen::find();
            if (add_user_p($user_id, $cm0frist0fen['fen'])) {
                fenLog('首次绑定', $user_id, $cm0frist0fen['fen']);
            }
        }
    }
}


//获取order4s里的data数据
function getOrder4($id)
{
    $data = \app\api\model\Cm0s40order::where('id', $id)->value('data');
    return unserialize($data);
}


//获取order里的data数据
function getOrder($id)
{
    $data = \app\api\model\Cm0order::where('id', $id)->value('data');
    return unserialize($data);
}


if (!function_exists('cdnurl')) {

    /**
     * 获取上传资源的CDN的地址
     * @param string $url 资源相对地址
     * @param boolean $domain 是否显示域名 或者直接传入域名
     * @return string
     */
    function cdnurl($url, $domain = true)
    {
        if ($domain === 'ben') {
            return request()->domain() . $url;
        }

        $regex = "/^((?:[a-z]+:)?\/\/|data:image\/)(.*)/i";
        $url = preg_match($regex, $url) ? $url : \think\Config::get('upload.cdnurl') . $url;
        if ($domain && !preg_match($regex, $url)) {
            $domain = is_bool($domain) ? request()->domain() : $domain;
            $url = $domain . $url;
        }
        return $url;
    }
}


//创建积分记录表
if (!function_exists('fenLog')) {
    function fenLog($why, $user_id, $fen, $s4_id = 0)
    {
        if ($fen == 0) {
            return;
        }
        $param = [];
        $param['why'] = $why;
        $param['user_id'] = $user_id;
        $param['fen'] = $fen;
        $param['s4_id'] = $s4_id;
        $param['time'] = time();
        $param['tou_time'] = tou_yue();
        \app\api\model\Cm0fen0log::create($param, true);
    }
}


//4s店减积分
function del_fen4($s4_id, $fen)
{
    $fen4 = \app\api\model\Cm0s4::where('id', $s4_id)->value('fen');
    if ($fen4 < $fen) {
        return false;
    } else {
        $res = \app\api\model\Cm0s4::where('id', $s4_id)->setDec('fen', $fen);
        //提醒4S店,积分过线
        if ($res) {
            $fen4_2 = \app\api\model\Cm0s4::where('id', $s4_id)->value('fen');
            $phone = \app\api\model\Cm0s4::where('id', $s4_id)->value('phone');
            $find = \app\api\model\Cm0ti0xing::find();
            if ($fen4 >= $find['fen'] && $fen4_2 < $find['fen']) {
                if ($find['switch'] == 1) {
                    sendMsg($phone, $find['content']);
                }
            }
        }
        return $res;
    }


}


//4s店加积分
function add_fen4($s4_id, $fen)
{
    $res = \app\api\model\Cm0s4::where('id', $s4_id)->setInc('fen', $fen);
    if ($res) {
        return true;//添加成功
    } else {
        return false;//添加失败
    }
}


//给用户加4s店积分,可能添加失败
function add_user4($user_id, $fen)
{
    $user = \app\api\model\User::where('id', $user_id)->find();
    $res = \app\api\model\Cm0che::where('id', $user['che_id'])->setInc('s4_fen', $fen);
    if ($res) {
        return true;//添加成功
    } else {
        return false;//添加失败
    }
}


//给用户添加平台积分,可能添加失败
function add_user_p($user_id, $fen)
{
    $user = \app\api\model\User::where('id', $user_id)->find();
    $res = \app\api\model\Cm0che::where('id', $user['che_id'])->setInc('ping_fen', $fen);
    if ($res) {
        return true;//添加成功
    } else {
        return false;//添加失败
    }
}

//普通商品返还积分+4s返还现金
function backFen_pu($order_no)
{
    $find = \app\api\model\Cm0order::where('order_no', $order_no)->find();
    $data = unserialize($find['data']);
    $good = $data['order_good'];
    $user = \app\api\model\User::where('id', $find['user_id'])->find();

    //返还积分
    $back_fen = 0;
    $money_4s = 0;
    $dong = unserialize($find['dong']);
    foreach ($good as $k => $v) {
        $goodsspec = \app\api\model\Litestoregoodsspec::where('goods_id', $v['goods_id'])
            ->where('spec_sku_id', $v['spec_sku_id'])
            ->find();
        $back_fen += $goodsspec['fen'] * $v['num'];
        $money_4s += $goodsspec['money4'] * $v['num'];
    }
    $back_fen = round($back_fen, 2);
    $money_4s = round($money_4s, 2);
    if (add_money4($user['s4_id'], $money_4s)) {
        $why = "{$user['nickname']}购买同放商品";
        money_log4($why, $user['s4_id'], $money_4s, $find['user_id'], $order_no);
    }

    $s4_fen = $dong['s4_fen'] ?? 0;
    $ping_fen = $dong['ping_fen'] ?? 0;

    if ($s4_fen == 0 && $ping_fen == 0 && empty($find['red'])) {//没用积分抵扣,则返积分
        if (add_user_p($find['user_id'], $back_fen)) {//给用户加平台积分
            fenLog('购买同放商品', $find['user_id'], $back_fen);//记录log表
        }
    }
}


//普通商品4S店返积分
function backFen_pu_s4($order_no)
{
    $find = \app\api\model\Cm0order::where('order_no', $order_no)->find();
    $data = unserialize($find['data']);
    $good = $data['order_good'];
    $user = \app\api\model\User::where('id', $find['user_id'])->find();

    //返还积分
    $back_fen = 0;
    foreach ($good as $k => $v) {
        $back_fen += \app\api\model\Litestoregoods::where('goods_id', $v['goods_id'])->value('s4_fen') ?? 0;
        $s4_id = \app\api\model\Litestoregoods::where('goods_id', $v['goods_id'])->value('s4_id');
    }
    $back_fen = round($back_fen, 2);

    $dong = unserialize($find['dong']);
    $s4_fen = $dong['s4_fen'] ?? 0;
    $ping_fen = $dong['ping_fen'] ?? 0;

    if ($s4_fen == 0 && $ping_fen == 0 && empty($find['red'])) {//没用积分抵扣,则返积分
        s4userFen($s4_id, $user['id'], $back_fen, '购买4S店普通商品', '4S店普通商品出售');
    }
}


//点赞商品返还积分+4s店返现金
function backFen_zan($order_no)
{
    $find = \app\api\model\Cm0order::where('order_no', $order_no)->find();
    $user = \app\api\model\User::where('id', $find['user_id'])->find();
    //返还积分
    $dong = unserialize($find['dong']);
    $good = unserialize($find['data']);
    $back_fen = 0;
    $money_4s = 0;
    $zan0good = \app\api\model\Cm0zan0good::where('id', $good['good']['id'])->find();
    $back_fen += round($zan0good['fen'] * $good['good']['gou_num'], 2);
    $money_4s += round($zan0good['money4'] * $good['good']['gou_num'], 2);

    $dong['s4_fen'] = $dong['s4_fen'] ?? 0;
    $dong['ping_fen'] = $dong['ping_fen'] ?? 0;


    if ($good['good']['s4_id'] == 0) {//平台的点赞商品
        //给用户加平台积分
        if ($dong['s4_fen'] == 0 && $dong['ping_fen'] == 0 && empty($find['red'])) {//没用积分抵扣,则返积分
            if (add_user_p($find['user_id'], $back_fen)) {
                fenLog('购买有赞商品', $user['id'], $back_fen);//记录log表
            }
        }
        //给4S店加金钱
        if (add_money4($user['s4_id'], $money_4s)) {
            $why = "{$user['nickname']}购买同放商品";
            money_log4($why, $user['s4_id'], $money_4s, $find['user_id'], $order_no);
        }
    } else {//4S店的点赞商品

        if ($dong['s4_fen'] == 0 && $dong['ping_fen'] == 0 && empty($find['red'])) {//没用积分抵扣,则返积分
            s4userFen($good['good']['s4_id'], $find['user_id'], $back_fen, '购买4S店点赞商品', "点赞商品出售,订单号:{$order_no}");
        }

    }
}


//配件订单返积分
function backFen_pei($order_no)
{
    $find = \app\api\model\Cm0s40order::where('order_no', $order_no)->find();
    $dong = unserialize($find['dong']);
    $data = unserialize($find['data']);
    $back_fen = 0;

    foreach ($data['order_good'] as $k => $v) {
        $pei0good = \app\api\model\Cm0pei0good::where('id', $v['good']['id'])->find();
        $back_fen += $pei0good['fen'];
    }
    $back_fen = round($back_fen, 2);
    $dong['ping_fen'] = $dong['ping_fen'] ?? 0;

    if ($dong['ping_fen'] == 0 && empty($find['red'])) {
        s4userFen($find['s4_id'], $find['user_id'], $back_fen, '购买配件商品', "配件商品出售,订单号:{$order_no}");
    }
}


//解冻,库存加回,若使用积分则积分加回
function jieDong($order_no)
{
    $order1 = \app\api\model\Cm0order::where('order_no', $order_no)->find();
    $order2 = \app\api\model\Cm0s40order::where('order_no', $order_no)->find();

    if (!empty($order1)) {
        if ($order1['type'] == 0) {//普通商品
            $dong = unserialize($order1['dong']);
            $s4_fen = $dong['s4_fen'] ?? 0;
            $ping_fen = $dong['ping_fen'] ?? 0;

            //积分处理
            if ($s4_fen != 0) {
                if (add_user4($order1['user_id'], $s4_fen)) {
                    $user = \app\api\model\User::where('id', $order1['user_id'])->find();
                    fenLog('取消或退款订单', $order1['user_id'], $s4_fen, $user['s4_id']);
                }
            }
            if ($ping_fen != 0) {
                if (add_user_p($order1['user_id'], $ping_fen)) {
                    fenLog('取消或退款订单', $order1['user_id'], $ping_fen);
                }
            }

            //库存处理
            $dong['ku'] = $dong['ku'] ?? [];
            foreach ($dong['ku'] as $k => $v) {
                \app\api\model\Litestoregoodsspec::where('goods_spec_id', $v['spec_id'])
                    ->setInc('stock_num', $v['num']);
            }
        }
        if ($order1['type'] == 1) {//有赞商品
            $s4_fen = $dong['s4_fen'] ?? 0;
            $ping_fen = $dong['ping_fen'] ?? 0;

            //积分处理
            if ($s4_fen != 0) {
                if (add_user4($order1['user_id'], $s4_fen)) {
                    $user = \app\api\model\User::where('id', $order1['user_id'])->find();
                    fenLog('取消或退款有赞商品订单', $order1['user_id'], $s4_fen, $user['s4_id']);
                }
            }
            if ($ping_fen != 0) {
                if (add_user_p($order1['user_id'], $ping_fen)) {
                    fenLog('取消或退款有赞商品订单', $order1['user_id'], $ping_fen);
                }
            }

            //库存处理
            if (!empty($order1['dong'])) {
                $dong = unserialize($order1['dong']);
                $ku = $dong['ku'] ?? [];
                if (!empty($ku)) {
                    \app\api\model\Cm0zan0good::where('id', $ku['zan_id'])->setInc('ku_num', $ku['num']);
                }
            }

        }
    }


    if (!empty($order2)) {
        $dong = unserialize($order2['dong']);
        if ($order2['type'] == 3) {//配件订单
            $ping_fen = $dong['ping_fen'] ?? 0;

            //积分处理
            if ($ping_fen != 0) {
                if (add_user4($order2['user_id'], $ping_fen)) {//给用户加回积分
                    fenLog('取消或退款配件订单', $order2['user_id'], $ping_fen, $order2['s4_id']);
                }
            }
            //库存处理
            foreach ($dong['ku'] as $k => $v) {
                \app\api\model\Cm0pei0good::where('id', $v['id'])->setInc('ku_num', $v['num']);
            }

        }
    }

}


//给4s店加钱
function add_money4($s4_id, $money)
{
    $res = \app\api\model\Cm0s4::where('id', $s4_id)->setInc('money', $money);
    if ($res) {
        return true;
    } else {
        return false;
    }
}

//创建记录4s店[返金钱]
function money_log4($why, $s4_id, $money, $user_id = 0, $order_no)
{
    if ($money == 0) {
        return;
    }
    $param['why'] = $why;
    $param['s4_id'] = $s4_id;
    $param['money'] = $money;
    $param['time'] = time();
    $param['user_id'] = $user_id;
    $param['order_no'] = $order_no;
    \app\api\model\Cm0money0log4::create($param, true);
}


//创建4s店的积分记录
function fen_log4($why, $user_id, $s4_id, $fen)
{
    if ($fen == 0) {
        return;
    }
    $param['why'] = $why;
    $param['s4_id'] = $s4_id;
    $param['fen'] = $fen;
    $param['user_id'] = $user_id;
    $param['time'] = time();
    \app\api\model\Cm0fen0log4::create($param, true);
}


if (!function_exists('is_really_writable')) {
    /**
     * 判断文件或文件夹是否可写
     * @param    string $file 文件或目录
     * @return    bool
     */
    function is_really_writable($file)
    {
        if (DIRECTORY_SEPARATOR === '/') {
            return is_writable($file);
        }
        if (is_dir($file)) {
            $file = rtrim($file, '/') . '/' . md5(mt_rand());
            if (($fp = @fopen($file, 'ab')) === false) {
                return false;
            }
            fclose($fp);
            @chmod($file, 0777);
            @unlink($file);
            return true;
        } elseif (!is_file($file) or ($fp = @fopen($file, 'ab')) === false) {
            return false;
        }
        fclose($fp);
        return true;
    }
}

if (!function_exists('rmdirs')) {

    /**
     * 删除文件夹
     * @param string $dirname 目录
     * @param bool $withself 是否删除自身
     * @return boolean
     */
    function rmdirs($dirname, $withself = true)
    {
        if (!is_dir($dirname)) {
            return false;
        }
        $files = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($dirname, RecursiveDirectoryIterator::SKIP_DOTS),
            RecursiveIteratorIterator::CHILD_FIRST
        );

        foreach ($files as $fileinfo) {
            $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
            $todo($fileinfo->getRealPath());
        }
        if ($withself) {
            @rmdir($dirname);
        }
        return true;
    }
}

if (!function_exists('copydirs')) {

    /**
     * 复制文件夹
     * @param string $source 源文件夹
     * @param string $dest 目标文件夹
     */
    function copydirs($source, $dest)
    {
        if (!is_dir($dest)) {
            mkdir($dest, 0755, true);
        }
        foreach (
            $iterator = new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
                RecursiveIteratorIterator::SELF_FIRST
            ) as $item
        ) {
            if ($item->isDir()) {
                $sontDir = $dest . DS . $iterator->getSubPathName();
                if (!is_dir($sontDir)) {
                    mkdir($sontDir, 0755, true);
                }
            } else {
                copy($item, $dest . DS . $iterator->getSubPathName());
            }
        }
    }
}

if (!function_exists('mb_ucfirst')) {
    function mb_ucfirst($string)
    {
        return mb_strtoupper(mb_substr($string, 0, 1)) . mb_strtolower(mb_substr($string, 1));
    }
}

if (!function_exists('addtion')) {

    /**
     * 附加关联字段数据
     * @param array $items 数据列表
     * @param mixed $fields 渲染的来源字段
     * @return array
     */
    function addtion($items, $fields)
    {
        if (!$items || !$fields) {
            return $items;
        }
        $fieldsArr = [];
        if (!is_array($fields)) {
            $arr = explode(',', $fields);
            foreach ($arr as $k => $v) {
                $fieldsArr[$v] = ['field' => $v];
            }
        } else {
            foreach ($fields as $k => $v) {
                if (is_array($v)) {
                    $v['field'] = isset($v['field']) ? $v['field'] : $k;
                } else {
                    $v = ['field' => $v];
                }
                $fieldsArr[$v['field']] = $v;
            }
        }
        foreach ($fieldsArr as $k => &$v) {
            $v = is_array($v) ? $v : ['field' => $v];
            $v['display'] = isset($v['display']) ? $v['display'] : str_replace(['_ids', '_id'], ['_names', '_name'], $v['field']);
            $v['primary'] = isset($v['primary']) ? $v['primary'] : '';
            $v['column'] = isset($v['column']) ? $v['column'] : 'name';
            $v['model'] = isset($v['model']) ? $v['model'] : '';
            $v['table'] = isset($v['table']) ? $v['table'] : '';
            $v['name'] = isset($v['name']) ? $v['name'] : str_replace(['_ids', '_id'], '', $v['field']);
        }
        unset($v);
        $ids = [];
        $fields = array_keys($fieldsArr);
        foreach ($items as $k => $v) {
            foreach ($fields as $m => $n) {
                if (isset($v[$n])) {
                    $ids[$n] = array_merge(isset($ids[$n]) && is_array($ids[$n]) ? $ids[$n] : [], explode(',', $v[$n]));
                }
            }
        }
        $result = [];
        foreach ($fieldsArr as $k => $v) {
            if ($v['model']) {
                $model = new $v['model'];
            } else {
                $model = $v['name'] ? \think\Db::name($v['name']) : \think\Db::table($v['table']);
            }
            $primary = $v['primary'] ? $v['primary'] : $model->getPk();
            $result[$v['field']] = $model->where($primary, 'in', $ids[$v['field']])->column("{$primary},{$v['column']}");
        }

        foreach ($items as $k => &$v) {
            foreach ($fields as $m => $n) {
                if (isset($v[$n])) {
                    $curr = array_flip(explode(',', $v[$n]));

                    $v[$fieldsArr[$n]['display']] = implode(',', array_intersect_key($result[$n], $curr));
                }
            }
        }
        return $items;
    }
}

if (!function_exists('var_export_short')) {

    /**
     * 返回打印数组结构
     * @param string $var 数组
     * @param string $indent 缩进字符
     * @return string
     */
    function var_export_short($var, $indent = "")
    {
        switch (gettype($var)) {
            case "string":
                return '"' . addcslashes($var, "\\\$\"\r\n\t\v\f") . '"';
            case "array":
                $indexed = array_keys($var) === range(0, count($var) - 1);
                $r = [];
                foreach ($var as $key => $value) {
                    $r[] = "$indent    "
                        . ($indexed ? "" : var_export_short($key) . " => ")
                        . var_export_short($value, "$indent    ");
                }
                return "[\n" . implode(",\n", $r) . "\n" . $indent . "]";
            case "boolean":
                return $var ? "TRUE" : "FALSE";
            default:
                return var_export($var, true);
        }
    }
}

if (!function_exists('letter_avatar')) {
    /**
     * 首字母头像
     * @param $text
     * @return string
     */
    function letter_avatar($text)
    {
        $total = unpack('L', hash('adler32', $text, true))[1];
        $hue = $total % 360;
        list($r, $g, $b) = hsv2rgb($hue / 360, 0.3, 0.9);

        $bg = "rgb({$r},{$g},{$b})";
        $color = "#ffffff";
        $first = mb_strtoupper(mb_substr($text, 0, 1));
        $src = base64_encode('<svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="100" width="100"><rect fill="' . $bg . '" x="0" y="0" width="100" height="100"></rect><text x="50" y="50" font-size="50" text-copy="fast" fill="' . $color . '" text-anchor="middle" text-rights="admin" alignment-baseline="central">' . $first . '</text></svg>');
        $value = 'data:image/svg+xml;base64,' . $src;
        return $value;
    }
}

if (!function_exists('hsv2rgb')) {
    function hsv2rgb($h, $s, $v)
    {
        $r = $g = $b = 0;

        $i = floor($h * 6);
        $f = $h * 6 - $i;
        $p = $v * (1 - $s);
        $q = $v * (1 - $f * $s);
        $t = $v * (1 - (1 - $f) * $s);

        switch ($i % 6) {
            case 0:
                $r = $v;
                $g = $t;
                $b = $p;
                break;
            case 1:
                $r = $q;
                $g = $v;
                $b = $p;
                break;
            case 2:
                $r = $p;
                $g = $v;
                $b = $t;
                break;
            case 3:
                $r = $p;
                $g = $q;
                $b = $v;
                break;
            case 4:
                $r = $t;
                $g = $p;
                $b = $v;
                break;
            case 5:
                $r = $v;
                $g = $p;
                $b = $q;
                break;
        }

        return [
            floor($r * 255),
            floor($g * 255),
            floor($b * 255)
        ];
    }
}


//后加的一些方法
if (!function_exists('q')) {
    function q($data = '')
    {
        dump($data);
        die();
    }
}

if (!function_exists('arr')) {
    function arr($data = '')
    {
        $arr = [];
        foreach ($data as $k => $v) {
            $temp = $v->toArray();
            foreach ($temp as $kk => $vv) {
                $arr[$k][$kk] = $vv;
            }
        }
        return $arr;
    }
}

if (!function_exists('img01')) {
    function img01($str, $type = '')
    {
        if ($str == '') {
            return '';
        } else {
            $arr = explode(',', $str);

            //本地的
            if ($type == 'ben') {
                return $arr[0];
            }

            //7牛云的
            if ($type == '') {
                return cdnurl($arr[0], true);
            }

        }
    }
}


//替换富文本中的图片
if (!function_exists('ti0huan0img')) {
    function ti0huan0img($str = '')
    {
//        return str_replace("/uploads/", request()->domain() . '/uploads/', $str);
        return $str;
    }
}


if (!function_exists('changeTimeType')) {
    function changeTimeType($seconds)
    {
        if ($seconds > 3600) {
            $hours = intval($seconds / 3600);
            $time = $hours . ":" . gmstrftime('%M:%S', $seconds);
        } else {
            $time = gmstrftime('%H:%M:%S', $seconds);
        }
        return $time;
    }
}


//统计逗号点的数量
if (!function_exists('dou_num')) {
    function dou_num($str = '')
    {
        if ($str == '') {
            return 0;
        }
        $str_arr = explode(',', $str);
        return sizeof($str_arr);
    }
}

if (!function_exists('ymd')) {
    function ymd($time = 0)
    {
        return date('Y-m-d', $time);
    }
}

//兼容了数组和逗号拼接字符串
if (!function_exists('imgtoarr')) {
    function imgtoarr($str = '')
    {
        if (empty($str)) {
            return [];
        }
        if (is_array($str)) {
            $arr = $str;
        } else {
            $arr = explode(',', $str);
        }
        $arr2 = [];
        foreach ($arr as $k => $v) {
            $arr2[$k] = cdnurl($v, true);
        }
        return $arr2;
    }
}

//生产随机的订单号
if (!function_exists('cmf_get_order_sn')) {
    function cmf_get_order_sn()
    {
        $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L');
        $num2 = substr(date('Y'), -1);
        $num1 = substr("2011", -1);
        $orderSn = $yCode[intval($num2) - $num1] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99)) . rand(10000, 99999);
        return $orderSn;
    }
}


//手机号星号加密
if (!function_exists('tel_xing')) {
    function tel_xing($tel)
    {
        return preg_replace('/(\d{3})\d{4}(\d{4})/', '$1****$2', $tel);
    }
}


if (!function_exists('curl_request')) {
    //使用curl函数库发送请求
    function curl_request($url, $post = true, $params = [], $https = true)
    {
        //初始化请求
        $ch = curl_init($url);
        //默认是get请求。如果是post请求 设置请求方式和请求参数
        if ($post) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        }
        //如果是https协议,禁止从服务器验证本地证书
        if ($https) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        }
        //发送请求,获取返回结果
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $res = curl_exec($ch);
        /*if(!$res){
            $msg = curl_error($ch);
            dump($msg);die;
        }*/
        //关闭请求
        curl_close($ch);
        return $res;
    }

}

//生成毫秒级别的时间戳
if (!function_exists('stime')) {
    function stime()
    {
        list($msec, $sec) = explode(' ', microtime());
        $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        return $msectime;
    }
}


//    截取富文本
if (!function_exists('subfwb')) {
    function subfwb($str)
    {
        //把接收到的的HTML实体转换为字符
        $html_string = htmlspecialchars_decode($str);
        $content = $html_string;
        //将空格替换成空
        $content = str_replace("&nbsp;", "", $html_string);
        //函数剥去字符串中的HTML、XML以及PHP的标签,获取纯文本内容
        $contents = strip_tags($content);
        //返回字符串中的前150字符串长度的字符

        if (mb_strlen($contents) > 60) {
            $text = mb_substr($contents, 0, 60, "utf-8");
            return $text . '...';
        } else {
            $text = mb_substr($contents, 0, 60, "utf-8");
            return $text;
        }

    }
}


//    截取富文本,后台专用
if (!function_exists('subfwb2')) {
    function subfwb2($str)
    {
        //把接收到的的HTML实体转换为字符
        $html_string = htmlspecialchars_decode($str);
        $content = $html_string;
        //将空格替换成空
        $content = str_replace(" ", "", $html_string);
        //函数剥去字符串中的HTML、XML以及PHP的标签,获取纯文本内容
        $contents = strip_tags($content);
        //返回字符串中的前150字符串长度的字符
        $text = mb_substr($contents, 0, 12, "utf-8");
        return $text . '...';
    }
}

//手机号的正则
function ck_phone($phone)
{
    if (!preg_match('/^[1][3,4,5,6,7,8,9][0-9]{9}$/', $phone)) {
        return false;
    }
    return true;
}


if (!function_exists('downloadFile')) {
    function downloadFile($filePath, $readBuffer = 1024)
    {
        //检测下载文件是否存在 并且可读
        if (!is_file($filePath) && !is_readable($filePath)) {
            return false;
        }
        //设置头信息
        //声明浏览器输出的是字节流
        header('Content-Type: application/octet-stream');
        //声明浏览器返回大小是按字节进行计算
        header('Accept-Ranges:bytes');
        //告诉浏览器文件的总大小
        $fileSize = filesize($filePath);//坑 filesize 如果超过2G 低版本php会返回负数
        header('Content-Length:' . $fileSize); //注意是'Content-Length:' 非Accept-Length
        //声明下载文件的名称
        header('Content-Disposition:attachment;filename=' . basename($filePath));//声明作为附件处理和下载后文件的名称
        //获取文件内容
        $handle = fopen($filePath, 'rb');//二进制文件用‘rb’模式读取
        while (!feof($handle)) { //循环到文件末尾 规定每次读取(向浏览器输出为$readBuffer设置的字节数)
            echo fread($handle, $readBuffer);
        }
        fclose($handle);//关闭文件句柄
        exit;
    }
}


function makeZip($datalist = [])
{
    $filename = 'images.zip'; //最终生成的文件名(含路径)
    if (file_exists($filename)) {
        unlink($filename);
    }
//重新生成文件
    $zip = new ZipArchive();
    if ($zip->open($filename, ZIPARCHIVE::CREATE) !== TRUE) {
        exit('无法打开文件,或者文件创建失败');
    }
//    $datalist=array('try.php','zip_class.php');
    foreach ($datalist as $val) {
        if (file_exists($val)) {
            $path_parts = pathinfo($val);
            $zip->addFile($val, $path_parts['basename']);
        }
    }
    $zip->close();//关闭
    if (!file_exists($filename)) {
        exit('无法找到文件'); //即使创建,仍有可能失败
    }
    return $filename;
}

function public_url()
{
    $upload_path = str_replace('\\', '/', ROOT_PATH);
    return $upload_path . 'public';
}

//讲$value加入进$str里[如果不在的话],并返回true;在的话则返回false;
function Inarr($str, $value)
{
    $arr = explode(',', $str);
    if (in_array($value, $arr)) {
        return false;
    } else {
        array_push($arr, $value);
        return implode(',', $arr);
    }
}


//判断$value是否在$str中,在的话返回true,不在的话返回false
function Inarr2($str, $value)
{
    $arr = str2arr($str);
    if (in_array($value, $arr)) {
        return true;
    } else {
        return false;
    }
}

//判断保存目录是否存在
function checkPath($path){
    if (!file_exists($path)) {
        mkdir($path, 0777, true);
    }
}

//将文件路径重命名
function path2($path)
{
    $path_arr = pathinfo($path);
    return $path_arr['dirname'] . '/' . $path_arr['filename'] . '_bak' . '.' . $path_arr['extension'];
}


//发送短信
function sendMsg($mobile, $content)
{
    $date = [
        'username' => 'chemai', //用户名
        'password' => 'Cxz307312', //密码
        'mobile' => $mobile,//手机号码
        'content' => "【同放汽车】" . $content,//短信内容
    ];

    date_default_timezone_set('PRC'); //设置时区
    $url = "https://api.mix2.zthysms.com/v2/sendSms";//提交地址
    $date['tKey'] = time();
    $date['password'] = md5(md5($date['password']) . $date['tKey']);

    $curl = curl_init(); // 启动一个CURL会话
    curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
    curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($date)); // Post提交的数据包
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
    curl_setopt($curl, CURLOPT_HEADER, false); // 显示返回的Header区域内容
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
    curl_setopt($curl, CURLOPT_HEADER, true); //开启header
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json; charset=utf-8'
    )); //类型为json
    $result = curl_exec($curl); // 执行操作
    if (curl_errno($curl)) {
        echo 'Error POST' . curl_error($curl);
    }
    curl_close($curl); // 关键CURL会话
    return $result; // 返回数据
}


function tou_yue()
{
    return mktime(0, 0, 0, date('m'), 1, date('Y'));//本月月初的时间戳
}


//可以带将$k值,的进行数组转化
function arr2($arr = [])
{
    $arr2 = [];
    foreach ($arr as $k => $v) {
        $arr2[$k] = $v->toArray();
    }
    return $arr2;
}


//转换分页数据
if (!function_exists('pageData')) {
    function pageData($data)
    {
        if (empty($data)) {
            return [];
        } else {
            $data2 = $data->toArray();
            return $data2['data'];
        }
    }
}


//对象转数组
function obj2arr($data = [])
{
    if (empty($data)) {
        return [];
    }
    if (is_array($data)) {
        return $data;
    }
    if (is_object($data)) {
        return $data->toArray();
    }
}


//保存到test文件中
function saveTest($data)
{
    \app\admin\model\Test::create([
        'content' => serialize($data),
    ]);
}

//获取保存到test中的数据
function getTest($id)
{
    $data = \app\admin\model\Test::where('id', $id)->value('content');
    return unserialize($data);
}

//获取上一个test中的数据
function getTest1($num = 0)
{
    $limt_num = $num + 1;
    $data = \app\admin\model\Test::order('id desc')->limit($limt_num)->select();
    return unserialize($data[$num]['content']);
}


//将"[1,2]"的转换成数组
function input_arr($str)
{
    if (empty($str)) {
        return [];
    }
    if (is_array($str)) {
        return $str;//是数组就直接返回个数组
    }
    if (substr($str, -1) == ']') {
        $str2 = mb_substr($str, 1);
        $str3 = mb_substr($str2, 0, -1);
        return explode(',', $str3);
    } else {
        return explode(',', $str);
    }
}


//退款
function tui_order($order_no, $shi_pay, $tui_pay, $why)
{
    $shi_pay = $shi_pay * 100;
    $tui_pay = $tui_pay * 100;
    $order = [
        'out_trade_no' => $order_no,//订单号
        'out_refund_no' => time(),
        'total_fee' => $shi_pay,//支付总金额
        'refund_fee' => $tui_pay,//要退款的金额
        'refund_desc' => $why,//退款原因
        'type' => 'miniapp',
    ];

    $pay0wx = new \app\index\controller\Pay0wx();
    return $pay0wx->tui($order);
}


//今日开始的时间戳
function day1()
{
    return mktime(0, 0, 0, date('m'), date('d'), date('Y'));
}

//一周前的时间戳
function zhou1()
{
    return time() - 7 * 24 * 60 * 60;
}

//一个月前的时间戳
function yue1()
{
    return time() - 30 * 24 * 60 * 60;
}


//对二位数组进行排序,默认倒叙排列
function sortData($data, $str, $desc = 'desc')
{
    $timeKey = array_column($data, $str);
    if ($desc == 'desc') {
        array_multisort($timeKey, SORT_DESC, $data);
    } elseif ($desc == 'asc') {
        array_multisort($timeKey, SORT_ASC, $data);
    }
    return $data;
}


//首字母------1
function ziMu($str)
{
    $str0 = mb_substr($str, 0, 1);
    if (!hanZi($str0)) {
        return strtoupper($str0);
    }

    if (empty($str)) {
        return '';
    }

    $fchar = ord($str{0});

    if ($fchar >= ord('A') && $fchar <= ord('z')) return strtoupper($str{0});

    $s1 = iconv('UTF-8', 'GBK', $str);

    $s2 = iconv('GBK', 'UTF-8', $s1);

    $s = $s2 == $str ? $s1 : $str;

    $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;

    if ($asc >= -20319 && $asc <= -20284) return 'A';

    if ($asc >= -20283 && $asc <= -19776) return 'B';

    if ($asc >= -19775 && $asc <= -19219) return 'C';

    if ($asc >= -19218 && $asc <= -18711) return 'D';

    if ($asc >= -18710 && $asc <= -18527) return 'E';

    if ($asc >= -18526 && $asc <= -18240) return 'F';

    if ($asc >= -18239 && $asc <= -17923) return 'G';

    if ($asc >= -17922 && $asc <= -17418) return 'H';

    if ($asc >= -17417 && $asc <= -16475) return 'J';

    if ($asc >= -16474 && $asc <= -16213) return 'K';

    if ($asc >= -16212 && $asc <= -15641) return 'L';

    if ($asc >= -15640 && $asc <= -15166) return 'M';

    if ($asc >= -15165 && $asc <= -14923) return 'N';

    if ($asc >= -14922 && $asc <= -14915) return 'O';

    if ($asc >= -14914 && $asc <= -14631) return 'P';

    if ($asc >= -14630 && $asc <= -14150) return 'Q';

    if ($asc >= -14149 && $asc <= -14091) return 'R';

    if ($asc >= -14090 && $asc <= -13319) return 'S';

    if ($asc >= -13318 && $asc <= -12839) return 'T';

    if ($asc >= -12838 && $asc <= -12557) return 'W';

    if ($asc >= -12556 && $asc <= -11848) return 'X';

    if ($asc >= -11847 && $asc <= -11056) return 'Y';

    if ($asc >= -11055 && $asc <= -10247) return 'Z';
    return rare_words($asc);
    return '';
}


//判断字符串是否为汉子----------2
function hanZi($str)
{
    if (preg_match("/[\x7f-\xff]/", $str)) {  //判断字符串中是否有中文
        return true;//是汉子
    } else {
        return false;//不是汉子
    }
}

//百家姓中的生僻字------------3
function rare_words($asc = '')
{
    $rare_arr = array(
        -3652 => array('word' => "窦", 'first_char' => 'D'),
        -8503 => array('word' => "奚", 'first_char' => 'X'),
        -9286 => array('word' => "酆", 'first_char' => 'F'),
        -7761 => array('word' => "岑", 'first_char' => 'C'),
        -5128 => array('word' => "滕", 'first_char' => 'T'),
        -9479 => array('word' => "邬", 'first_char' => 'W'),
        -5456 => array('word' => "臧", 'first_char' => 'Z'),
        -7223 => array('word' => "闵", 'first_char' => 'M'),
        -2877 => array('word' => "裘", 'first_char' => 'Q'),
        -6191 => array('word' => "缪", 'first_char' => 'M'),
        -5414 => array('word' => "贲", 'first_char' => 'B'),
        -4102 => array('word' => "嵇", 'first_char' => 'J'),
        -8969 => array('word' => "荀", 'first_char' => 'X'),
        -4938 => array('word' => "於", 'first_char' => 'Y'),
        -9017 => array('word' => "芮", 'first_char' => 'R'),
        -2848 => array('word' => "羿", 'first_char' => 'Y'),
        -9477 => array('word' => "邴", 'first_char' => 'B'),
        -9485 => array('word' => "隗", 'first_char' => 'K'),
        -6731 => array('word' => "宓", 'first_char' => 'M'),
        -9299 => array('word' => "郗", 'first_char' => 'X'),
        -5905 => array('word' => "栾", 'first_char' => 'L'),
        -4393 => array('word' => "钭", 'first_char' => 'T'),
        -9300 => array('word' => "郜", 'first_char' => 'G'),
        -8706 => array('word' => "蔺", 'first_char' => 'L'),
        -3613 => array('word' => "胥", 'first_char' => 'X'),
        -8777 => array('word' => "莘", 'first_char' => 'S'),
        -6708 => array('word' => "逄", 'first_char' => 'P'),
        -9302 => array('word' => "郦", 'first_char' => 'L'),
        -5965 => array('word' => "璩", 'first_char' => 'Q'),
        -6745 => array('word' => "濮", 'first_char' => 'P'),
        -4888 => array('word' => "扈", 'first_char' => 'H'),
        -9309 => array('word' => "郏", 'first_char' => 'J'),
        -5428 => array('word' => "晏", 'first_char' => 'Y'),
        -2849 => array('word' => "暨", 'first_char' => 'J'),
        -7206 => array('word' => "阙", 'first_char' => 'Q'),
        -4945 => array('word' => "殳", 'first_char' => 'S'),
        -9753 => array('word' => "夔", 'first_char' => 'K'),
        -10041 => array('word' => "厍", 'first_char' => 'S'),
        -5429 => array('word' => "晁", 'first_char' => 'C'),
        -2396 => array('word' => "訾", 'first_char' => 'Z'),
        -7205 => array('word' => "阚", 'first_char' => 'K'),
        -10049 => array('word' => "乜", 'first_char' => 'N'),
        -10015 => array('word' => "蒯", 'first_char' => 'K'),
        -3133 => array('word' => "竺", 'first_char' => 'Z'),
        -6698 => array('word' => "逯", 'first_char' => 'L'),
        -9799 => array('word' => "俟", 'first_char' => 'Q'),
        -6749 => array('word' => "澹", 'first_char' => 'T'),
        -7220 => array('word' => "闾", 'first_char' => 'L'),
        -10047 => array('word' => "亓", 'first_char' => 'Q'),
        -10005 => array('word' => "仉", 'first_char' => 'Z'),
        -3417 => array('word' => "颛", 'first_char' => 'Z'),
        -6431 => array('word' => "驷", 'first_char' => 'S'),
        -7226 => array('word' => "闫", 'first_char' => 'Y'),
        -9293 => array('word' => "鄢", 'first_char' => 'Y'),
        -6205 => array('word' => "缑", 'first_char' => 'G'),
        -9764 => array('word' => "佘", 'first_char' => 'S'),
        -9818 => array('word' => "佴", 'first_char' => 'N'),
        -9509 => array('word' => "谯", 'first_char' => 'Q'),
        -3122 => array('word' => "笪", 'first_char' => 'D'),
        -9823 => array('word' => "佟", 'first_char' => 'T'),
    );
    if (array_key_exists($asc, $rare_arr) && $rare_arr[$asc]['first_char']) {
        return $rare_arr[$asc]['first_char'];
    } else {
        return '';
    }
}


//查找这个表有木有
function findGuan($where, $table)
{
    $find = \think\Db::name($table)->where($where)->find();
    if (empty($find)) {
        return false;//不存在
    } else {
        return $find;//存在
    }
}

//类似于关注,有则删除,没有则加进去
function guanTb($where, $table)
{
    $res = findGuan($where, $table);
    if ($res) {//删除
        $res2 = \think\Db::name($table)->where($where)->delete();
    } else {//添加
        $res2 = \think\Db::name($table)->insertGetId($where);
    }
    return res2bool($res2);
}


//打印时间
function qtime($time)
{
    $time2 = date('Y-m-d H:i:s', $time);
    q($time2);
}

//清除预绑定的4S店
function clearS4($user_id)
{
    $user = \app\api\model\User::where('id', $user_id)->find();
    $s4_id = \app\api\model\Cm0che::where('id', $user['che_id'])->value('s4_id');
    if ($s4_id != 0) {//如果默认车有绑4s店,则清除预绑定的4S店
        \app\api\model\User::where('id', $user_id)->update([
            's4_id' => 0,
        ]);
    }
}

//改变订单状态,完全抵扣的支付
function orderState($order_no)
{
    $param = [];
//    $param['shi_pay'] = 0;
//    $param['liu_no'] = '';
    $param['state'] = 1;
    $param['back'] = 1;
    \app\api\model\Cm0order::where('order_no', $order_no)->update($param);
    \app\api\model\Cm0s40order::where('order_no', $order_no)->update($param);
}

//生成二维码
function shengCode($width = 430, $scene = 1, $page = '', $type = '')
{
//    $type = 'user';//代表给用户生成的
//    $type = 's4';//代表给s4店生成的
    $access_token = get_accessToken();
    $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $access_token;
//        $scene = 'NO1000000';//用户的user_id
//        $width = 430;
    $data['scene'] = "$scene";
    $data['width'] = intval($width);
//        $data['page'] = "pages/check_status/check_status";//跳转到的页面路径
    if (!empty($page)) {
        $data['page'] = $page;//跳转到的页面路径
    }

    $data = json_encode($data);
    $return = https_request($url, $data);
    if ($type == 'user') {
        $path2 = '/assets/code/user/' .time(). $scene . '.jpg';
    } elseif ($type == 's4') {
        $path2 = '/assets/code/s4/' .time(). $scene. '.jpg';
    }
    $pathfull = public_url() . $path2;
    //判断保存目录是否存在
    $path = substr($pathfull, 0, strrpos($pathfull, "/"));
    checkPath($path);

    file_put_contents($pathfull, $return);
    return $path2;
}

//获取accessToken
function get_accessToken()
{
    $config = \config('wechat');
    $app = new \EasyWeChat\Foundation\Application($config);
    $accessToken = $app->access_token; // EasyWeChat\Core\AccessToken 实例
    $token = $accessToken->getToken(true); // token 字符串
    return $token;
}


//http请求
function https_request($url, $data = null)
{
    if (is_array($data)) {
        $data = json_encode($data);
    }

    $curl = curl_init();
    //curl_setopt ( $curl, CURLOPT_SAFE_UPLOAD, false);
    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;
}

//图片上传7牛云
function qiniu($filePath = '', $fileName = '')
{
    $fileName = substr($fileName, 1);
    $config = get_addon_config('qiniu');
    $policy = array(
        'saveKey' => $fileName,
    );
    $auth = new \addons\qiniu\library\Auth($config['app_key'], $config['secret_key']);
    $token = $auth->uploadToken($config['bucket'], null, $config['expire'], $policy);
    $multipart = [
        ['name' => 'token', 'contents' => $token],
        [
            'name' => 'file',
            'contents' => fopen($filePath, 'r'),
            'filename' => $fileName,
        ]
    ];

    try {
        $client = new \GuzzleHttp\Client();
        $res = $client->request('POST', $config['uploadurl'], [
            'multipart' => $multipart
        ]);
        $code = $res->getStatusCode();
        return true;//上传成功
    } catch (\GuzzleHttp\Exception\ClientException $e) {
        return false;//上传失败
    }
}

//4s店给用户积分
function s4userFen($s4_id, $user_id, $fen, $why_u, $why_s4)
{
    \think\Db::startTrans();
    try {

        $res1 = true;
        $res2 = true;
        $user = \app\api\model\User::where('id', $user_id)->find();
        //必须是当前绑定的4S店和下单的4S店是同一家
        if ($user['s4_id'] == $s4_id) {
            $res1 = add_user4($user_id, $fen);
            fenLog($why_u, $user_id, $fen, $s4_id);//积分记录
        }
//        $res2 = del_fen4($s4_id, $fen);
//        fen_log4($why_s4, $user_id, $s4_id, $fen * (-1));

        if ($res1 && $res2) {
            \think\Db::commit();
        } else {
            \think\Db::rollback();
            return false;
        }

    } catch (\Exception $e) {
        \think\Db::rollback();
        return false;
    }
    return true;
}


//4s店给车积分-----局限于这辆车绑定的就是这个4s店
function s4userFen2($s4_id, $che_id, $fen, $why_u, $why_s4)
{
    \think\Db::startTrans();
    try {
        $res1 = \app\api\model\Cm0che::where('id', $che_id)->setInc('s4_fen', $fen);
        $user_id = \app\api\model\Cm0che0user::where('che_id', $che_id)->value('user_id');
        fenLog($why_u, $user_id, $fen, $s4_id);//积分记录
        $res2 = del_fen4($s4_id, $fen);
        fen_log4($why_s4, $user_id, $s4_id, $fen * (-1));

        if ($res1 && $res2) {
            \think\Db::commit();
        } else {
            \think\Db::rollback();
            return false;
        }

    } catch (\Exception $e) {
        \think\Db::rollback();
        return false;
    }

    return true;
}


function ck_red($red)
{
    if (isset($red['red']['cheap']) && !empty($red['red']['cheap'])) {
        return true;
    } else {
        return false;
    }
}

//统计红包数量,可以使用的红包
function red_num($user_id, $order_no)
{
    $red = new \app\api\controller\Red();
    return $red->orderRed(1, $order_no, $user_id);
}


//数组里的元素是否重复,[1,2,3]这种的
function arr_only($arr = [])
{
    $arr2 = array_unique($arr);
    if (count($arr2) == count($arr)) {
        return true;//没有重复的
    } else {
        return false;//有重复的
    }
}

//将json转换成数组
function json2arr($str)
{
    if (empty($str)) {
        return [];
    } else {
        return json_decode($str, true);
    }
}

//判断一个字符串是否在另一个字符串里
function inStr($str, $v)
{
    if (strpos($str, $v) !== false) {
        return true;//包含
    } else {
        return false;//不包含
    }
}

//计算两个时间的秒数,默认是YY-mm-dd形式
function chaTime($start_time, $stop_time, $type = 1)
{
    if ($type == 1) {
        $start_time = strtotime($start_time);
        $stop_time = strtotime($stop_time);
        return $stop_time - $start_time;
    } elseif ($type == 0) {
        return $stop_time - $start_time;
    }
}


//在这个时间内,这两个时间都是YY-mm-dd形式
function inTime1($start_time, $stop_time)
{
    $time = time();
    $start_time = strtotime($start_time);
    $stop_time = strtotime($stop_time);
    return inShu($time, $start_time, $stop_time);
}

//在这个数字区间内
function inShu($num, $num1, $num2)
{
    if ($num > $num1 && $num < $num2) {
        return true;//在这个区间内
    } else {
        return false;//不在这个区间内
    }
}

//  逗号形式 的 字符串转数组
function str2arr($str, $dou = ',')
{
    if (empty($str)) {
        return [];
    } else {
        return explode($dou, $str);
    }
}


//$arr2 = [
//    [1, 2, 3],
//    [4, 5, 6],
//    [7, 8, 9],
//];
//将$arr2转换成[1,2,3,4,5,6,7,8,9],
function big2arr($big_arr)
{
    $arr = [];
    foreach ($big_arr as $k => $v) {
        $arr = array_merge($arr, $v);
    }
    return $arr;
}


//判断$ck_arr是否在$all_arr中,同时对兼容性处理一下;若$ck_arr为空,则也是在$all_arr中
function in_arr($all_arr, $ck_arr)
{
    if (!is_array($all_arr)) {
        $all_arr = str2arr($all_arr);
    }
    if (!is_array($ck_arr)) {
        $ck_arr = str2arr($ck_arr);
    }
    $c = array_diff($ck_arr, $all_arr);
    $flag = empty($c) ? 1 : 0;
    if ($flag) {
        return true;
    } else {
        return false;
    }
}

//判断用户是否首次登录,只能在login处使用
function loginFrist($user_id)
{
    $logintime = \app\api\model\User::where('id', $user_id)->value('logintime');
    $jointime = \app\api\model\User::where('id', $user_id)->value('jointime');
    if ($logintime == $jointime) {
        return true;//是首次登录
    } else {
        return false;//不是首次登录
    }
}


//秒数转时间
//$t=1637544;
//$d=Sec2Time($t);
//$d为  0年18天 22小时52分24秒
if (!function_exists('sec2time')) {
    function sec2time($time)
    {
        if (is_numeric($time)) {
            $value = array(
                "days" => 0, "hours" => 0,
                "minutes" => 0, "seconds" => 0,
            );
            if ($time >= 86400) {
                $value["days"] = floor($time / 86400);
                $time = ($time % 86400);
            }
            if ($time >= 3600) {
                $value["hours"] = floor($time / 3600);
                $time = ($time % 3600);
            }
            if ($time >= 60) {
                $value["minutes"] = floor($time / 60);
                $time = ($time % 60);
            }
            $value["seconds"] = floor($time);
            $value['milliseconds'] = 0;
            return (array)$value;
        }
    }
}

//秒数转时间
//$t=1637544;
//$d=Sec2Time($t);
//$d为  0年18天 22小时52分24秒
if (!function_exists('sec3time')) {
    function sec3time($time)
    {
        if (is_numeric($time)) {
            $str = '';
            $value = array(
                "hours" => 0,
                "minutes" => 0, "seconds" => 0,
            );
            if ($time >= 3600) {
                $str .= floor($time / 3600) . '小时';
                $time = ($time % 3600);
            }
            if ($time >= 60) {
                $str .= floor($time / 60) . '分钟';
                $time = ($time % 60);
            }
            $str .= floor($time) . '秒';
            return $str;
        }
    }
}

function res2bool($res, $type = 0)
{
    if ($type == 0) {
        if ($res) {
            return true;
        } else {
            return false;
        }
    } elseif ($type == 1) {
        if ($res !== false) {
            return true;
        } else {
            return false;
        }
    }
}

//金钱的正则
function ck_money($money)
{
    if (!preg_match('/(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/', $money)) {
        return false;
    }
    return true;
}

//将复杂的select的对象数组,转为普通的二位数组
function select2arr($data)
{
    $data2 = json_encode($data);
    $data2 = json_decode($data2, true);
    return $data2;
}


function chu_data($field_str, $data)
{
    if (is_array($field_str)) {
        $field_arr = $field_str;
    } else {
        $field_arr = str2arr($field_str);
    }

    $data2 = [];
    foreach ($data as $k => $v) {
        $row = [];
        foreach ($field_arr as $kk => $vv) {
            $row[$vv] = $v[$vv];
        }
        $data2[] = $row;
    }
    return $data2;
}


function chu_find($field_str, $find)
{
    if (is_null($find)) {
        return null;
    }



    if (is_array($field_str)) {
        $field_arr = $field_str;
    } else {
        $field_arr = str2arr($field_str);
    }

    $data = [];
    foreach ($field_arr as $k => $v) {
        $data[$v] = $find[$v];
    }
    return $data;
}


//限制字符串长度
function limit_str($str, $num)
{
    if (mb_strlen($str) > $num) {
        $str = mb_substr($str, 0, $num - 1, "utf-8");
    }
    return $str;
}

//将分页数据  转换成  正常的数组形式
if (!function_exists('pageData2')) {
    function pageData2($data)
    {
        if (empty($data)) {
            return [];
        } else {
            $data2 = $data->toArray();
            return $data2;
        }
    }
}

//现在的日期
function now_date($type = '-')
{
    $time = time();
    $str = "Y{$type}m{$type}d";
    return date($str, $time);
}

//获取try的错误信息
function err_msg($err)
{
    if (isset($err[0]['function']) && $err[0]['function'] == 'result') {
        if (isset($err[0]['args'][0]) && is_string($err[0]['args'][0]) && !empty($err[0]['args'][0])) {
            return $err[0]['args'][0];
        }
    }
    return '系统繁忙';
}

//时间的处理
function chu_time($time)
{
    $now = time();
    $day = intval(floor(($now - $time) / 86400));
    $month = intval(floor(($now - $time) / (86400 * 30)));
    $year = intval(floor(($now - $time) / (86400 * 30 * 12)));
    if ($year >= 1) {
        $time2 = $year . '年前';
    } else if ($month >= 1 && $month < 12) {
        $time2 = $month . '月前';
    } else {
        if ($day >= 1) {
            $time2 = $day . '天前';
        } else if ($day < 1) {
            $hour = intval(floor(($now - $time) / 3600));
            if ($hour >= 1) {
                $time2 = $hour . '小时前';
            } else if ($hour < 1) {
                $minute = intval(floor(($now - $time) / 60));
                if ($minute >= 1) {
                    $time2 = $minute . '分钟前';
                } else if ($minute < 1) {
                    $time2 = '刚刚';
                }
            }
        }
    }
    return $time2;
}

//JS那种List
function js_list($data, $k_name = 'id', $v_name = 'name')
{
    $data2 = [];
    foreach ($data as $k => $v) {
        $data2[$v[$k_name]] = $v[$v_name];
    }
    return $data2;
}

//获取文件后缀
function get_suffix($file)
{
    $info = pathinfo($file);
    return $info['extension'];
}

//检测后缀
function ck_suffix($file, $suffix_arr)
{
    $suffix = get_suffix($file);
    if (in_array($suffix, $suffix_arr)) {
        return true;//符合
    } else {
        return false;//不符合
    }
}

//检查图片类型
function ck_image($file)
{
    $arr = ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf'];
    return ck_suffix($file, $arr);
}

//检测视频类型
function ck_video($file)
{
    $arr = ['avi', 'rmvb', 'rm', 'asf', 'divx', 'mpg', 'mpeg', 'mpe', 'wmv', 'mp4', 'mkv', 'vob'];
    return ck_suffix($file, $arr);
}


//发布MP4  获取mp4的数据
function fa_mp41($mp4)
{
    $path = cdnurl($mp4);
    $path = parse_url($path);
    $path = cdnurl($path['path']);
    $path .= "?avinfo";
    $data = file_get_contents($path);
    $data = json_decode($data, true);
    return $data;
}

//获取视频文件的时长
function get_mp4_long($mp4)
{
    $info = fa_mp41($mp4);
    if (isset($info['format']['duration']) && !empty($info['format']['duration'])) {
        return $info['format']['duration'];
    }
    if (isset($info['streams'][0]['duration']) && !empty($info['streams'][0]['duration'])) {
        return $info['streams'][0]['duration'];
    }
    if (isset($info['streams'][1]['duration']) && !empty($info['streams'][1]['duration'])) {
        return $info['streams'][1]['duration'];
    }
    return 0;
}

/**
 * 通用化API数据格式输出
 * @param $status
 * @param string $message
 * @param array $data
 * @param int $httpStatus
 * @return \think\response\Json
 */
function show($status, $message = "error", $data = [], $httpStatus = 200) {

    $result = [
        "code" => $status,
        "msg" => $message,
        "data" => $data
    ];

    return json($result, $httpStatus);
}

/**
 * 生成随机的唯一核销码
 */
function code()
{
    $uniqueId = uniqid().time();
    return $uniqueId;
}