Weapp.php 77.4 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
<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司,并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: JY
 * Date: 2015-09-23
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Page;
use qcloudcos\Conf;
use qcloudcos\Myqcloudcos;
use think\AjaxPage;


class Weapp extends Base
{

    function __construct()
    {
        parent::__construct();
        $nologin = ['not_xcx'];
        if (!in_array(ACTION_NAME, $nologin)) {
            $rs = M("store_module_endtime")->where('store_id', getAdmStoId())->where('type', 5)->find();
            if ($rs) {
                if ($rs['end_time'] < time()) $this->redirect(U('Admin/weapp/not_xcx'));
                $this->assign('end_time', $rs['end_time']);
            } else {
                $this->redirect(U('Admin/weapp/not_xcx'));
            }
        }
    }

    //settings
    public function settings()
    {
        $getstoid = getAdmStoId();
        $weres = M('weapp')->where(' store_id=' . $getstoid)->find();
        $this->assign('getstoid', $getstoid);
        if ($weres) {
            $this->redirect($_SERVER['HTTP_HOST'] . '/Admin/Weapp/call?appid=' . $weres['appid'], 302);
        } else {

            return $this->fetch('', getAdmStoId());
        }

        //查询是否已授权,如果已授权直接到定向到小程序详情页

    }


    //小程序授权
    public function call($appid)
    {

        $m_end=M("store_module_endtime")->where("type",5)->where("store_id",getAdmStoId())->find();
        $this->assign("m_end",$m_end);


        $getadmstoid = getAdmStoId();
        //小程序二维码生成
        $resl = M('weapp')->where('appid', $appid)->find();

        $default_pageValue = "pages/index/index/index";
        $weapp_auditinfo = M('weapp_audit')->where(array('store_id' => $getadmstoid, 'status' => 0))->order('id desc')->find();
        if ($weapp_auditinfo) {
            $weapp_qrcode = M('weapp_qrcode')->where(array('store_id' => $getadmstoid, 'pageValue' => $default_pageValue))->find();
            if (empty($weapp_qrcode)) {
                $wepp_api = WEAPPAPIURL . "api/wx/open/app/user/createQrcode/" . $getadmstoid . "?pageValue=pages/index/index/index";
                $wepp_info = httpRequest($wepp_api);
                if ($wepp_info) {
                    $wepp_info = json_decode($wepp_info, true);
                    if ($wepp_info['code'] == 0) {
                        $weapp_qrcode['qrcodeurl'] = $wepp_info['data'];


                        //新增data
                        $qrcode_adddata['sceneValue'] = "";
                        $qrcode_adddata['store_id'] = $getadmstoid;
                        $qrcode_adddata['date'] = time();
                        $qrcode_adddata['pageValue'] = $default_pageValue;
                        $qrcode_adddata['qrcodeurl'] = $wepp_info['data'];
                        M('weapp_qrcode')->save($qrcode_adddata);


                    }
                }
            } else {

                if ($weapp_qrcode['date'] < $weapp_auditinfo['succtime']) {

                    $wepp_api = WEAPPAPIURL . "api/wx/open/app/user/createQrcode/" . $getadmstoid . "?pageValue=pages/index/index/index";

                    $wepp_info = httpRequest($wepp_api);

                    if ($wepp_info) {
                        $wepp_info = json_decode($wepp_info, true);
                        if ($wepp_info['code'] == 0) {
                            $weapp_qrcode['qrcodeurl'] = $wepp_info['data'];


                            //新增data
                            $qrcode_adddata['sceneValue'] = "";
                            $qrcode_adddata['store_id'] = $getadmstoid;
                            $qrcode_adddata['date'] = time();
                            $qrcode_adddata['pageValue'] = $default_pageValue;
                            $qrcode_adddata['qrcodeurl'] = $wepp_info['data'];
                            M('weapp_qrcode')->where(array('id' => $weapp_qrcode['id']))->save($qrcode_adddata);


                        }
                    }
                }
            }


            $this->assign('weapp_qrcode', $weapp_qrcode);
        }
        $wxuser = M('wx_user')->where(' store_id=' . $getadmstoid)->find();

        switch ($resl['service_type_info']) {
            case 2:
                $type = "服务号";
                break;

            default:
                $type = "订阅号";
                break;
        }

        //处理订阅号和服务号
        switch ($resl['verify_type_info']) {
            case -1:
                $verify_type_info = "未认证";
                break;
            default:
                $verify_type_info = "已认证";
                break;
        }


        $sq_func_info = $resl[sq_func_info];
        $func_info = explode(',', $sq_func_info);
        for ($i = 0; $i < (count($func_info) - 1); $i++) {
            switch ($func_info[$i]) {
                case 1:
                    $func_info[$i] = "消息管理权限";
                    break;
                case 2:
                    $func_info[$i] = "用户管理权限";
                    break;
                case 3:
                    $func_info[$i] = "帐号服务权限";
                    break;
                case 4:
                    $func_info[$i] = "网页服务权限";
                    break;
                case 5:
                    $func_info[$i] = "微信小店权限";
                    break;
                case 6:
                    $func_info[$i] = "微信多客服权限";
                    break;
                case 7:
                    $func_info[$i] = "群发与通知权限";
                    break;
                case 8:
                    $func_info[$i] = "微信卡券权限";
                    break;
                case 9:
                    $func_info[$i] = "微信扫一扫权限";
                    break;
                case 10:
                    $func_info[$i] = "微信连WIFI权限";
                    break;
                case 11:
                    $func_info[$i] = "素材管理权限";
                    break;
                case 12:
                    $func_info[$i] = "微信摇周边权限";
                    break;
                case 13:
                    $func_info[$i] = "微信门店权限";
                    break;
                case 14:
                    $func_info[$i] = "微信支付权限";
                    break;
                case 15:
                    $func_info[$i] = "自定义菜单权限";
                    break;
                case 16:
                    $func_info[$i] = "";
                    break;
                case 17:
                    $func_info[$i] = "帐号管理权限";
                    break;
                case 18:
                    $func_info[$i] = "开发管理权限";
                    break;
                case 19:
                    $func_info[$i] = "客服消息管理权限";
                    break;
                default:
                    $func_info[$i] = "";
                    break;
            }
        }
        $this->assign('func_info', $func_info);

        $this->assign('verify_type_info', $verify_type_info);

        $this->assign('type', $type);

        $this->assign('res', $resl);

        $this->assign('appid', $appid);
        $this->assign('wxuser', $wxuser);

        $storeid = getAdmStoId();
        $this->assign('getstoid', $storeid);

        return $this->fetch('call');
    }

    //支付配置
    public function payset()
    {

        $id = getAdmStoId();
        $dir = ROOT_PATH . '/public/cert/' . $id . '/weapp/apiclient_cert.pem';
        $isup = 0;
        //创建目录失败
        if (file_exists($dir)) {
            $this->assign('isup', 1);
            $isup = 1;
        }

        $weapp = M('weapp')->where(array('store_id' => $id))->find();
        if (!$weapp) {
            $this->error('请先进行授权', U('Admin/Weapp/settings'));
            exit;
        }
        if (IS_POST) {
            $post_data = input('post.');
            foreach ($post_data as $k => $v) {
                $post_data[$k] = trim($v);
            }

            $row = M('weapp')->where(array('store_id' => $id))->update($post_data);

            if ($row !== false) {
                //有上传证书要校验一遍
                if ($isup) {
                    $getcheck_json = $this->check_zip(1);
                    if ($getcheck_json['code'] != 1) {
                        $this->error($getcheck_json['msg']);
                        exit();
                    }
                }

                $this->success("修改成功");
            } else {
                $this->error("修改失败");
            }
        }
        $this->assign('weapp', $weapp);
        return $this->fetch('', getAdmStoId());
    }

    public function check_zip($ind = 0)
    {
        include_once "plugins/payment/weixin/weixin.class.php";
        $wx = new \weixin();
        $stoid = getAdmStoId();
        $ordno = $total_fee = $refund_fee = 1;
        $path = BASE_PATH . 'public/cert/' . $stoid . '/weapp/';
        $json = null;
        $json['time'] = time();
        try {
            $rs = $wx->refund2($ordno, $total_fee, $refund_fee, $path, $stoid,"weapp",1);
            if ($rs['return_code'] == 'FAIL') {
                $json['code'] = -1;
                $json['msg'] = $rs['return_msg'];
                $str = urldecode(urldecode($rs['return_msg']));
                if (strpos($str, 'certificate not match') !== false) {
                    $json['msg'] = "证书已失效,请到微信商户平台更新证书,重新下载后上传";
                }
                if (strpos($str, 'mch_id') !== false) {
                    $json['msg'] = "微信支付商户号错误,请重新编辑";
                }
                if (strpos($str, '签名错误') !== false) {
                    $json['msg'] = "微信支付KEY错误,请重新编辑";
                }
                if (strpos($str, '订单不存在') !== false) {
                    $json['code'] = 1;
                    $json['msg'] = '校验成功';
                }
            } else {
                $json['code'] = 1;
                $json['msg'] = '校验成功';
            }
        }catch (\Exception $e) {
            $json['code'] = 1;
            $json['msg'] = '校验成功';
            mlog($e->getMessage(), "check_zipwepp/" . getAdmStoId());
            $str = $e->getMessage();
            if (strpos($str, '52') !== false) {
                $json['code'] = -1;
                $json['msg'] = '证书路径错误,请重新上传';
            }
            if (strpos($str, '58') !== false) {
                $json['code'] = -1;
                $json['msg'] = '证书未上传或者证书路径错误,请重新上传';
            }
        }

        M('weapp')->where('store_id', $stoid)
            ->save(['check_json' => json_encode($json)]);
        if ($ind == 1) return $json;
        return json($json);

    }
    //提交代码 1
    public function sumbitcode()
    {


        $getadmstoid = getAdmStoId();
        $weapp = M('weapp')->where(array('store_id' => $getadmstoid))->find();
        if (!$weapp) {
            $this->error('请先进行授权', U('Admin/Weapp/settings'));
            exit;
        }
        $man_res = M('manager_config')->find();

        if (IS_POST) {
            $post_data = input('post.');


//            $res = httpRequest("https://api.weixin.qq.com/wxa/gettemplatedraftlist?access_token=".getcomponent_access_token());
//            var_dump($res);die;

            $getweptoken = weapp_get_access_token($weapp, $getadmstoid);

            $getweptoken = json_decode($getweptoken, true);

            if ($getweptoken['errcode'] != 0) {
                $this->error('请重新授权', U('Admin/Weapp/settings'));
                exit;
            }

            $getauthorizer_access_token = $getweptoken["authorizer_access_token"];

            $PostData_str = '{"user_version":"' . $post_data["user_version"] . '","user_desc":"' . $post_data['user_desc'] . '","ext_json":"{\"extAppid\":\"'.WEAPPID.'\",';
            $PostData_str .= '\"ext\":{\"extAppid\":\"'.WEAPPID.'\",\"appCode\":\"UNION_PAY\",\"appNickName\":\"万信达商城\",\"shopId\":\"'.WEAPPSTOREID.'\",\"preDomain\":\"'.WEAPPAPIURL.'\"},';
            $PostData_str .= '\"extPages\":{},\"pages\":' . $man_res["weapp_ext_json"] . ',';
            $PostData_str .= ' \"window\":{},\"networkTimeout\":{},\"tabBar\":{\"color\":\"#7f8389\",\"selectedColor\":\"#ff9a0f\",\"borderStyle\":\"white\",';
            $PostData_str .= '\"list\":[{\"pagePath\":\"pages/index/index/index\",\"text\":\"首页\",\"iconPath\":\"images/bar/index.png\",\"selectedIconPath\":\"images/bar/index_on.png\"},{\"pagePath\":\"pages/goods/categoryList/categoryList\",\"text\":\"分类\",\"iconPath\":\"images/bar/fl.png\",\"selectedIconPath\":\"images/bar/fl_on.png\"},{\"pagePath\":\"pages/cart/cart/cart\",\"text\":\"购物车\",\"iconPath\":\"images/bar/car.png\",\"selectedIconPath\":\"images/bar/car_on.png\"},{\"pagePath\":\"pages/user/index/index\",\"text\":\"我的\",\"iconPath\":\"images/bar/user.png\",\"selectedIconPath\":\"images/bar/user_on.png\"}]}}"}';


            //如下是正确格式
            //  $PostData_str='{\"extAppid\":\"wx2ea7cdd3ef52cdff\",\"ext\":{\"extAppid\":\"wx2ea7cdd3ef52cdff\",\"appCode\":\"UNION_PAY\",\"appNickName\":\"万信达商城\",\"shopId\":\"1\",\"preDomain\":\"https://test.yolipai.net/\"},\"extPages\":{},\"pages\":[\"pages/index/index/index\",\"pages/goods/categoryList/categoryList\",\"pages/activity/group_list/group_list\",\"pages/activity/coupon_list/coupon_list\",\"pages/cart/cart/cart\",\"pages/cart/cart2/cart2\",\"pages/cart/cart4/cart4\",\"pages/goods/integralMall/integralMall\",\"pages/goods/brandstreet/brandstreet\",\"pages/goods/goodsInfo/goodsInfo\",\"pages/goods/goodsList/goodsList\",\"pages/goods/search/search\",\"pages/order/refund_order/refund_order\",\"pages/payment/payment/payment\",\"pages/user/index/index\",\"pages/user/order_list/order_list\",\"pages/user/order_detail/order_detail\",\"pages/user/visit_log/visit_log\",\"pages/user/address_list/address_list\",\"pages/user/userinfo/userinfo\",\"pages/user/account/account\",\"pages/user/comment/comment\",\"pages/user/return_goods_list/return_goods_list\",\"pages/user/userinfo_edit/userinfo_edit\",\"pages/user/collect_list/collect_list\",\"pages/user/coupon/coupon\",\"pages/user/checkcoupon/checkcoupon\",\"pages/user/add_address/add_address\",\"pages/user/account_list/account_list\",\"pages/user/points_list/points_list\",\"pages/user/recharge_list/recharge_list\",\"pages/user/withdrawals_list/withdrawals_list\",\"pages/user/add_comment/add_comment\",\"pages/user/return_goods_info/return_goods_info\",\"pages/user/return_goods/return_goods\",\"pages/user/withdrawals/withdrawals\",\"pages/user/recharge/recharge\",\"pages/user/message_notice/message_notice\",\"pages/user/express/express\",\"pages/user/binding_info/binding_info\",\"pages/user/get_user_info/get_user_info\",\"pages/virtual/buy_step/buy_step\",\"pages/virtual/virtual_list/virtual_list\",\"pages/distribut/index/index\",\"pages/distribut/distribut_list/distribut_list\",\"pages/distribut/good_list/good_list\",\"pages/distribut/order_list/order_list\",\"pages/distribut/my_store/my_store\",\"pages/distribut/set_store/set_store\",\"pages/distribut/team/team\",\"pages/index/webview/webview\",\"pages/cart/integral/integral\",\"pages/team/index/index\",\"pages/team/team_info/team_info\",\"pages/team/team_detail/team_detail\",\"pages/team/team_confirm/team_confirm\",\"pages/team/team_coupon/team_coupon\",\"pages/team/team_order/team_order\",\"pages/activity/seckill_list/seckill_list\"],\"window\":{\"backgroundTextStyle\":\"light\",\"navigationBarTextStyle\":\"black\",\"navigationBarTitleText\":\"加载中...\",\"navigationBarBackgroundColor\":\"#ffffff\",\"backgroundColor\":\"#eeeeee\",\"enablePullDownRefresh\":true},\"networkTimeout\":{},\"tabBar\":{\"color\":\"#7f8389\",\"selectedColor\":\"#ff9a0f\",\"borderStyle\":\"white\",\"list\":[{\"pagePath\":\"pages/index/index/index\",\"text\":\"首页\",\"iconPath\":\"images/bar/index.png\",\"selectedIconPath\":\"images/bar/index_on.png\"},{\"pagePath\":\"pages/goods/categoryList/categoryList\",\"text\":\"分类\",\"iconPath\":\"images/bar/fl.png\",\"selectedIconPath\":\"images/bar/fl_on.png\"},{\"pagePath\":\"pages/cart/cart/cart\",\"text\":\"购物车\",\"iconPath\":\"images/bar/car.png\",\"selectedIconPath\":\"images/bar/car_on.png\"},{\"pagePath\":\"pages/user/index/index\",\"text\":\"我的\",\"iconPath\":\"images/bar/user.png\",\"selectedIconPath\":\"images/bar/user_on.png\"}]}}';

            $res = httpRequest("https://api.weixin.qq.com/wxa/commit?access_token=" . $getauthorizer_access_token, "POST", $PostData_str);
            return $res;

        }
        $this->assign('weapp', $weapp);
        return $this->fetch('', getAdmStoId());
    }

    //获取体验小程序的体验二维码 2
    public function get_qrcode()
    {
        $getstoid = I('stoid/d', 0);
        if (empty($getstoid)) {
            return json(array('errcode' => -1, 'errmsg' => '请选择商家'));

        }


        $weapp = M('weapp')->where(array('store_id' => $getstoid))->find();
        if (empty($weapp)) {
            return json(array('errcode' => -1, 'errmsg' => '该商家未授权'));
        }
        $getweptoken = weapp_get_access_token($weapp, $getstoid);
        $getweptoken = json_decode($getweptoken, true);
        if ($getweptoken['errcode'] != 0) {
            return json(array('errcode' => -1, 'errmsg' => '该商家取消授权,请重新授权'));
        }

        if ($weapp['qrcode_urlimg']) {
            return json(array('errcode' => 0, 'qrcode_urlimg' => QCLOUD_IMGURL . $weapp['qrcode_urlimg'], 'errmsg' => 'ok'));
        }

        $getauthorizer_access_token = $getweptoken["authorizer_access_token"];
        $res = httpRequest("https://api.weixin.qq.com/wxa/get_qrcode?access_token=" . $getauthorizer_access_token, 'get');
        $localpath = "/public/upload/weappqrcode/" . $getstoid . "/";
        $picname = time() . ".jpg";
        if (!file_exists(ROOT_PATH . $localpath)) {
            mkdir(ROOT_PATH . $localpath, 0777, true);
        }
        $ret = file_put_contents(ROOT_PATH . $localpath . $picname, $res, true);

        vendor('qcloudcos.myqcloudcos');
        $resfolder = Myqcloudcos::statFolder('wxd', $localpath);

        if ($resfolder && $resfolder['code'] != 0)//不存在创建
        {
            Myqcloudcos::createFolder('wxd', $localpath);

        }
        //上传到腾讯云
        $res = Myqcloudcos::upload('wxd', ROOT_PATH . $localpath . $picname, $localpath . $picname);
        if ($res && $res['code'] == 0)//
        {            //
            $savedata['qrcode_urlimg'] = $localpath . $picname;
            M('weapp')->where(array('store_id' => $getstoid))->save($savedata);
            mdelFile($localpath);
            return json(array('errcode' => 0, 'qrcode_urlimg' => QCLOUD_IMGURL . $localpath . $picname, 'errmsg' => 'ok'));
        } else {
            return json(array('errcode' => -1, 'errmsg' => '生成失败'));
        }
    }

    //获取小程序的第三方提交代码的页面配置(仅供第三方开发者代小程序调用)3
    public function get_page()
    {
        $getstoid = I('stoid/d', 0);
        if (empty($getstoid)) {
            return json(array('errcode' => -1, 'errmsg' => '请选择商家'));

        }
        $weapp = M('weapp')->where(array('store_id' => $getstoid))->find();
        if (empty($weapp)) {
            return json(array('errcode' => -1, 'errmsg' => '该商家未授权'));
        }
        $getweptoken = weapp_get_access_token($weapp, $getstoid);
        $getweptoken = json_decode($getweptoken, true);
        if ($getweptoken['errcode'] != 0) {
            return json(array('errcode' => -1, 'errmsg' => '该商家取消授权,请重新授权'));
        }
        $getauthorizer_access_token = $getweptoken["authorizer_access_token"];
        $res = httpRequest("https://api.weixin.qq.com/wxa/get_page?access_token=" . $getauthorizer_access_token, 'get');
        return $res;
    }

    //获取草稿箱内的所有临时代码草稿 4
    public function gettemplatedraftlist()
    {
        $res = httpRequest("https://api.weixin.qq.com/wxa/gettemplatedraftlist?access_token=" . getcomponent_access_token(), 'get');
        return $res;
    }

    //获取代码模版库中的所有小程序代码模版 5
    public function gettemplatelist()
    {

        $res = httpRequest("https://api.weixin.qq.com/wxa/gettemplatelist?access_token=" . getcomponent_access_token(), 'get');
        return $res;

    }

    //将草稿箱的草稿选为小程序代码模版 6
    public function addtotemplate()
    {
        $draft_id = I('draft_id');
        if ($draft_id == "") {
            return json(array('errcode' => -1, 'errmsg' => '请选择草稿模板'));
        }
        $postdata['draft_id'] = $draft_id;
        $res = httpRequest("https://api.weixin.qq.com/wxa/addtotemplate?access_token=" . getcomponent_access_token(), 'POST', json_encode($postdata));
        return $res;
    }

    //将草稿箱的草稿选为小程序代码模版 7
    public function deletetemplate()
    {
        $template_id = I('template_id');
        if ($template_id == "") {
            return json(array('code' => -1, 'msg' => '请选择要删除的模版ID'));
        }
        $postdata['template_id'] = $template_id;
        $res = httpRequest("https://api.weixin.qq.com/wxa/deletetemplate?access_token=" . getcomponent_access_token(), 'POST', json_encode($postdata));
        return $res;
    }

    //设置小程序业务域名(仅供第三方代小程序调用) 8
    public function setwebviewdomain()
    {
        $getstoid = I('stoid/d', 0);
        $getaction = I('action', 'get');
        if (empty($getstoid)) {
            return json(array('errcode' => -1, 'errmsg' => '请选择商家'));

        }
        $weapp = M('weapp')->where(array('store_id' => $getstoid))->find();
        if (empty($weapp)) {
            return json(array('errcode' => -1, 'errmsg' => '该商家未授权'));
        }
        $getweptoken = weapp_get_access_token($weapp, $getstoid);
        $getweptoken = json_decode($getweptoken, true);
        if ($getweptoken['errcode'] != 0) {
            return json(array('errcode' => -1, 'errmsg' => $getweptoken['errmsg']));
        }
        $getauthorizer_access_token = $getweptoken["authorizer_access_token"];

        $getpostr = "POST";
        //add添加, delete删除, set覆盖, get获取
        switch ($getaction) {
            case "add":
                $postdata['webviewdomain'] = "[\"https://test-weshop.yolipai.net\"]";
                break;
            case "delete":
                $postdata['webviewdomain'] = "[\"https://test-weshop.yolipai.net\"]";
                break;
            case "set":
                $postdata['webviewdomain'] = "[\"https://test-weshop.yolipai.net\"]";
                break;
            case "get":
                break;
            default:
                break;
        }
        $postdata['action'] = $getaction;


        $postdatastr = json_encode($postdata);

        $res = httpRequest("https://api.weixin.qq.com/wxa/setwebviewdomain?access_token=" . $getauthorizer_access_token, $getpostr, $postdatastr);
        return $res;
    }


    //设置小程序服务器域名 9
    public function modify_domain()
    {
        $getstoid = I('stoid/d', 0);
        $getaction = I('action', 'get');
        if (empty($getstoid)) {
            return json(array('errcode' => -1, 'errmsg' => '请选择商家'));

        }
        $weapp = M('weapp')->where(array('store_id' => $getstoid))->find();
        if (empty($weapp)) {
            return json(array('errcode' => -1, 'errmsg' => '该商家未授权'));
        }
        $getweptoken = weapp_get_access_token($weapp, $getstoid);
        $getweptoken = json_decode($getweptoken, true);
        if ($getweptoken['errcode'] != 0) {
            return json(array('errcode' => -1, 'errmsg' => $getweptoken['errmsg']));
        }
        $getauthorizer_access_token = $getweptoken["authorizer_access_token"];

        $getpostr = "POST";
        //add添加, delete删除, set覆盖, get获取
        $postdata['action'] = $getaction;
        switch ($getaction) {
            case "add":
                $postdata['requestdomain'] = "[\"https://test-weshop.yolipai.net\"]";
                $postdata['wsrequestdomain'] = "[\"https://test-weshop.yolipai.net\"]";
                $postdata['uploaddomain'] = "[\"https://test-weshop.yolipai.net\"]";
                $postdata['downloaddomain'] = "[\"https://test-weshop.yolipai.net\"]";
                break;
            case "delete":
                $postdata['requestdomain'] = "[\"https://test-weshop.yolipai.net\"]";
                $postdata['wsrequestdomain'] = "[\"https://test-weshop.yolipai.net\"]";
                $postdata['uploaddomain'] = "[\"https://test-weshop.yolipai.net\"]";
                $postdata['downloaddomain'] = "[\"https://test-weshop.yolipai.net\"]";
                break;
            case "set":
                $postdata['requestdomain'] = "[\"https://test-weshop.yolipai.net\"]";
                $postdata['wsrequestdomain'] = "[\"https://test-weshop.yolipai.net\"]";
                $postdata['uploaddomain'] = "[\"https://test-weshop.yolipai.net\"]";
                $postdata['downloaddomain'] = "[\"https://test-weshop.yolipai.net\"]";
                break;
            case "get":
                break;
            default:
                break;
        }


        $postdatastr = json_encode($postdata);

        $res = httpRequest("https://api.weixin.qq.com/wxa/modify_domain?access_token=" . $getauthorizer_access_token, $getpostr, $postdatastr);
        return $res;
    }

    //

    //获取授权小程序帐号的可选类目10
    public function get_category()
    {
        $getstoid = I('stoid/d', 0);
        $getaction = I('action', 'get');
        if (empty($getstoid)) {
            return json(array('errcode' => -1, 'errmsg' => '请选择商家'));

        }
        $weapp = M('weapp')->where(array('store_id' => $getstoid))->find();
        if (empty($weapp)) {
            return json(array('errcode' => -1, 'errmsg' => '该商家未授权'));
        }
        $getweptoken = weapp_get_access_token($weapp, $getstoid);
        $getweptoken = json_decode($getweptoken, true);
        if ($getweptoken['errcode'] != 0) {
            return json(array('errcode' => -1, 'errmsg' => $getweptoken['errmsg']));
        }
        $getauthorizer_access_token = $getweptoken["authorizer_access_token"];
        $res = httpRequest("https://api.weixin.qq.com/wxa/get_category?access_token=" . $getauthorizer_access_token, "GET");
        return $res;
    }

    //将第三方提交的代码包提交审核11
    public function submit_audit()
    {
        $getstoid = I('stoid/d', 0);
        $getaction = I('action', 'get');
        if (empty($getstoid)) {
            return json(array('errcode' => -1, 'errmsg' => '请选择商家'));

        }
        $weapp = M('weapp')->where(array('store_id' => $getstoid))->find();
        if (empty($weapp)) {
            return json(array('errcode' => -1, 'errmsg' => '该商家未授权'));
        }
        $getweptoken = weapp_get_access_token($weapp, $getstoid);
        $getweptoken = json_decode($getweptoken, true);
        if ($getweptoken['errcode'] != 0) {
            return json(array('errcode' => -1, 'errmsg' => $getweptoken['errmsg']));
        }
        $getauthorizer_access_token = $getweptoken["authorizer_access_token"];

        $tag = I('tag');
        $first_class = I('first_class');
        $second_class = I('second_class');
        $third_class = I('third_class');
        $title = I('title');
        if ($tag && $first_class && $second_class && $third_class && $title) {
            $postdata['item_list']['tag'] = $first_class;
            $postdata['item_list']['first_class'] = $second_class;
            $postdata['item_list']['second_class'] = $third_class;
            $postdata['item_list']['third_class'] = $third_class;
            $postdata['item_list']['title'] = $title;


            $res = httpRequest("https://api.weixin.qq.com/wxa/submit_audit?access_token=" . $getauthorizer_access_token, "POST");
            return $res;
        } else {
            return json(array('errcode' => -1, 'errmsg' => '参数有误'));
        }
    }

    //设置小程序隐私设置(是否可被搜索)12
    public function changewxasearchstatus()
    {
        $getstoid = I('stoid/d', 0);
        //  "status":0, //1表示不可搜索,0表示可搜索
        $getstatus = I('status/d', 0);
        if (empty($getstoid)) {
            return json(array('errcode' => -1, 'errmsg' => '请选择商家'));

        }
        $weapp = M('weapp')->where(array('store_id' => $getstoid))->find();
        if (empty($weapp)) {
            return json(array('errcode' => -1, 'errmsg' => '该商家未授权'));
        }
        $getweptoken = weapp_get_access_token($weapp, $getstoid);
        $getweptoken = json_decode($getweptoken, true);
        if ($getweptoken['errcode'] != 0) {
            return json(array('errcode' => -1, 'errmsg' => $getweptoken['errmsg']));
        }
        $getauthorizer_access_token = $getweptoken["authorizer_access_token"];


        $postdata['status'] = $getstatus;
        $postdatastr = json_encode($postdata);
        $res = httpRequest("https://api.weixin.qq.com/wxa/changewxasearchstatus?access_token=" . $getauthorizer_access_token, "POST", $postdatastr);
        return $res;

    }

    //查询小程序当前隐私设置(是否可被搜索)13
    public function getwxasearchstatus()
    {
        $getstoid = I('stoid/d', 0);
        //  "status":0, //1表示不可搜索,0表示可搜索
        $getstatus = I('status/d', 0);
        if (empty($getstoid)) {
            return json(array('errcode' => -1, 'errmsg' => '请选择商家'));

        }
        $weapp = M('weapp')->where(array('store_id' => $getstoid))->find();
        if (empty($weapp)) {
            return json(array('errcode' => -1, 'errmsg' => '该商家未授权'));
        }
        $getweptoken = weapp_get_access_token($weapp, $getstoid);
        $getweptoken = json_decode($getweptoken, true);
        if ($getweptoken['errcode'] != 0) {
            return json(array('errcode' => -1, 'errmsg' => $getweptoken['errmsg']));
        }
        $getauthorizer_access_token = $getweptoken["authorizer_access_token"];
        $res = httpRequest("https://api.weixin.qq.com/wxa/getwxasearchstatus?access_token=" . $getauthorizer_access_token, "get");
        return $res;
    }

    //获取小程序模板库标题列表14
    public function template_library_list()
    {
        $getstoid = I('stoid/d', 0);
        //  "status":0, //1表示不可搜索,0表示可搜索
        $getp = I('p/d', 1);
        if (empty($getstoid)) {
            return json(array('errcode' => -1, 'errmsg' => '请选择商家'));

        }
        $weapp = M('weapp')->where(array('store_id' => $getstoid))->find();
        if (empty($weapp)) {
            return json(array('errcode' => -1, 'errmsg' => '该商家未授权'));
        }
        $getweptoken = weapp_get_access_token($weapp, $getstoid);
        $getweptoken = json_decode($getweptoken, true);
        if ($getweptoken['errcode'] != 0) {
            return json(array('errcode' => -1, 'errmsg' => $getweptoken['errmsg']));
        }
        $getcount = 20;
        $getoffset = 0;
        if ($getp > 1) {
            $getoffset = $getp * $getcount;
        }
        $postdata['offset'] = $getoffset;
        $postdata['count'] = $getcount;
        $postdatastr = json_encode($postdata);
        $getauthorizer_access_token = $getweptoken["authorizer_access_token"];
        $res = httpRequest("https://api.weixin.qq.com/cgi-bin/wxopen/template/library/list?access_token=" . $getauthorizer_access_token, "POST", $postdatastr);
        return $res;
    }

    //获取模板库某个模板标题下关键词库15
    public function template_library_get()
    {
        $getstoid = I('stoid/d', 0);
        $getid = I('id');
        if (empty($getstoid)) {
            return json(array('errcode' => -1, 'errmsg' => '请选择商家'));
        }
        if (empty($getid)) {
            return json(array('errcode' => -1, 'errmsg' => '请输入模板标题'));
        }
        $weapp = M('weapp')->where(array('store_id' => $getstoid))->find();
        if (empty($weapp)) {
            return json(array('errcode' => -1, 'errmsg' => '该商家未授权'));
        }
        $getweptoken = weapp_get_access_token($weapp, $getstoid);
        $getweptoken = json_decode($getweptoken, true);
        if ($getweptoken['errcode'] != 0) {
            return json(array('errcode' => -1, 'errmsg' => $getweptoken['errmsg']));
        }
        $postdata['id'] = $getid;
        $postdatastr = json_encode($postdata);
        $getauthorizer_access_token = $getweptoken["authorizer_access_token"];
        $res = httpRequest("https://api.weixin.qq.com/cgi-bin/wxopen/template/library/get?access_token=" . $getauthorizer_access_token, "POST", $postdatastr);
        return $res;
    }

    //组合模板并添加至帐号下的个人模板库16
    public function template_add()
    {
        $getstoid = I('stoid/d', 0);
        $getid = I('id');
        $keyword_id_list = I('keyword_id_list');

        if (empty($getstoid)) {
            return json(array('errcode' => -1, 'errmsg' => '请选择商家'));
        }
        if (empty($getid)) {
            return json(array('errcode' => -1, 'errmsg' => '请输入模板标题'));
        }
        $weapp = M('weapp')->where(array('store_id' => $getstoid))->find();
        if (empty($weapp)) {
            return json(array('errcode' => -1, 'errmsg' => '该商家未授权'));
        }
        $getweptoken = weapp_get_access_token($weapp, $getstoid);
        $getweptoken = json_decode($getweptoken, true);
        if ($getweptoken['errcode'] != 0) {
            return json(array('errcode' => -1, 'errmsg' => $getweptoken['errmsg']));
        }
        $postdata['id'] = $getid;
        $postdata['keyword_id_list'] = $keyword_id_list;
        $postdatastr = json_encode($postdata);

        $getauthorizer_access_token = $getweptoken["authorizer_access_token"];
        $res = httpRequest("https://api.weixin.qq.com/cgi-bin/wxopen/template/add?access_token=" . $getauthorizer_access_token, "POST", $postdatastr);
        return $res;
    }


    //获取帐号下已存在的模板列表17
    public function template_list()
    {
        $getstoid = I('stoid/d', 0);
        $getp = I('p/d', 1);
        if (empty($getstoid)) {
            return json(array('errcode' => -1, 'errmsg' => '请选择商家'));

        }
        $weapp = M('weapp')->where(array('store_id' => $getstoid))->find();
        if (empty($weapp)) {
            return json(array('errcode' => -1, 'errmsg' => '该商家未授权'));
        }
        $getweptoken = weapp_get_access_token($weapp, $getstoid);
        $getweptoken = json_decode($getweptoken, true);
        if ($getweptoken['errcode'] != 0) {
            return json(array('errcode' => -1, 'errmsg' => $getweptoken['errmsg']));
        }
        $getcount = 20;
        $getoffset = 0;
        if ($getp > 1) {
            $getoffset = $getp * $getcount;
        }
        $postdata['offset'] = $getoffset;
        $postdata['count'] = $getcount;
        $postdatastr = json_encode($postdata);
        $getauthorizer_access_token = $getweptoken["authorizer_access_token"];
        $res = httpRequest("https://api.weixin.qq.com/cgi-bin/wxopen/template/list?access_token=" . $getauthorizer_access_token, "POST", $postdatastr);
        return $res;
    }

    //组合模板并添加至帐号下的个人模板库18
    public function template_del()
    {
        $getstoid = I('stoid/d', 0);
        $template_id = I('template_id');

        if (empty($getstoid)) {
            return json(array('errcode' => -1, 'errmsg' => '请选择商家'));
        }
        if (empty($template_id)) {
            return json(array('errcode' => -1, 'errmsg' => '请选择删除模板ID'));
        }
        $weapp = M('weapp')->where(array('store_id' => $getstoid))->find();
        if (empty($weapp)) {
            return json(array('errcode' => -1, 'errmsg' => '该商家未授权'));
        }
        $getweptoken = weapp_get_access_token($weapp, $getstoid);
        $getweptoken = json_decode($getweptoken, true);
        if ($getweptoken['errcode'] != 0) {
            return json(array('errcode' => -1, 'errmsg' => $getweptoken['errmsg']));
        }
        $postdata['template_id'] = $template_id;
        $postdatastr = json_encode($postdata);

        $getauthorizer_access_token = $getweptoken["authorizer_access_token"];
        $res = httpRequest("https://api.weixin.qq.com/cgi-bin/wxopen/template/del?access_token=" . $getauthorizer_access_token, "POST", $postdatastr);
        return $res;
    }

    //查询某个指定版本的审核状态19
    public function get_auditstatus()
    {
        $getstoid = I('stoid/d', 0);
        $auditid = I('auditid');

        if (empty($getstoid)) {
            return json(array('errcode' => -1, 'errmsg' => '请选择商家'));
        }
        if (empty($auditid)) {
            return json(array('errcode' => -1, 'errmsg' => '请选择提交审核时获得的审核id'));
        }
        $weapp = M('weapp')->where(array('store_id' => $getstoid))->find();
        if (empty($weapp)) {
            return json(array('errcode' => -1, 'errmsg' => '该商家未授权'));
        }
        $getweptoken = weapp_get_access_token($weapp, $getstoid);
        $getweptoken = json_decode($getweptoken, true);
        if ($getweptoken['errcode'] != 0) {
            return json(array('errcode' => -1, 'errmsg' => $getweptoken['errmsg']));
        }
        $postdata['auditid'] = $auditid;
        $postdatastr = json_encode($postdata);

        $getauthorizer_access_token = $getweptoken["authorizer_access_token"];
        $res = httpRequest("https://api.weixin.qq.com/wxa/get_auditstatus?access_token=" . $getauthorizer_access_token, "POST", $postdatastr);
        return $res;
    }

    //查询最新一次提交的审核状态20
    public function get_latest_auditstatus()
    {
        $getstoid = I('stoid/d', 0);

        if (empty($getstoid)) {
            return json(array('errcode' => -1, 'errmsg' => '请选择商家'));
        }
        $weapp = M('weapp')->where(array('store_id' => $getstoid))->find();
        if (empty($weapp)) {
            return json(array('errcode' => -1, 'errmsg' => '该商家未授权'));
        }
        $getweptoken = weapp_get_access_token($weapp, $getstoid);
        $getweptoken = json_decode($getweptoken, true);
        if ($getweptoken['errcode'] != 0) {
            return json(array('errcode' => -1, 'errmsg' => $getweptoken['errmsg']));
        }
        $getauthorizer_access_token = $getweptoken["authorizer_access_token"];
        $res = httpRequest("https://api.weixin.qq.com/wxa/get_latest_auditstatus?access_token=" . $getauthorizer_access_token, "get");
        return $res;
    }

    //发布已通过审核的小程序21
    public function release()
    {

        $getstoid = I('stoid/d', 0);
        if (empty($getstoid)) {
            return json(array('errcode' => -1, 'errmsg' => '请选择商家'));
        }

        $weapp = M('weapp')->where(array('store_id' => $getstoid))->find();
        if (empty($weapp)) {
            return json(array('errcode' => -1, 'errmsg' => '该商家未授权'));
        }
        $getweptoken = weapp_get_access_token($weapp, $getstoid);
        $getweptoken = json_decode($getweptoken, true);
        if ($getweptoken['errcode'] != 0) {
            return json(array('errcode' => -1, 'errmsg' => $getweptoken['errmsg']));
        }
        $getauthorizer_access_token = $getweptoken["authorizer_access_token"];
        $postdatastr = "{}";
        $res = httpRequest("https://api.weixin.qq.com/wxa/release?access_token=" . $getauthorizer_access_token, "POST", $postdatastr);
        return $res;
    }

    //修改小程序线上代码的可见状态(仅供第三方代小程序调用)22
    public function change_visitstatus()
    {

        $getstoid = I('stoid/d', 0);
        $getaction = I('action');//close为不可见,open为可见

        if (empty($getstoid)) {
            return json(array('errcode' => -1, 'errmsg' => '请选择商家'));
        }
        if (empty($getaction)) {
            return json(array('errcode' => -1, 'errmsg' => '请输入可访问状态'));
        }
        $weapp = M('weapp')->where(array('store_id' => $getstoid))->find();
        if (empty($weapp)) {
            return json(array('errcode' => -1, 'errmsg' => '该商家未授权'));
        }
        $getweptoken = weapp_get_access_token($weapp, $getstoid);
        $getweptoken = json_decode($getweptoken, true);
        if ($getweptoken['errcode'] != 0) {
            return json(array('errcode' => -1, 'errmsg' => $getweptoken['errmsg']));
        }
        $postdata['action'] = $getaction;
        $postdatastr = json_encode($postdata);

        $getauthorizer_access_token = $getweptoken["authorizer_access_token"];
        $res = httpRequest("https://api.weixin.qq.com/wxa/change_visitstatus?access_token=" . $getauthorizer_access_token, "POST", $postdatastr);
        return $res;
    }

    //没有支付小程序
    public function not_xcx()
    {

        $yy = M("storage_recharge")->where("store_id", getAdmStoId())->where('recharge_state', 1)
            ->where("wxapp_price_id>0")->field('dj_price_id')->select();
        $aarr = M("wxapp_price")->where(array('id'=>array('neq',1)))->order('type desc,money desc')->select();

        $barr = null;
        if ($yy) {
            foreach ($aarr as $k => $v) {
                if ( $v['type'] == 0) {
                    if ($v['money'] <= 0)
                        break;
                    else {
                    }
                    $barr[] = $v;
                } else {
                    if ($v['type'] == 1)
                        $barr[] = $v;
                    else
                        break;
                }
            }
        } else {
            $barr = $aarr;
        }
        if ($barr) {
            $this->assign('price_arr', $barr);
        }

        //先判断缴费记录是否有该商家的记录
        $rss = M('store_module_endtime')->where('store_id=' . getAdmStoId())->where('type', 5)
            ->where('is_sy', 0)->find();
        if ($rss) {
            $end_time = strtotime('+1 year', $rss['end_time']);
            $end_time = date("Y-m-d H:i:s", $end_time);
            $this->assign('t_end_time', $end_time);

        } else {
            $end_time = strtotime('+1 year', time());
            $end_time = date("Y-m-d H:i:s", $end_time);
            $this->assign('t_end_time', $end_time);
        }


        return $this->fetch('', getAdmStoId());
    }


    /***
     * 自定义模板有关
     */
    /***小程序模板列表***/
    public function miniTemplateList()
    {
        $pagenum = I('pagenum/d', 10);
        $key_word = I('key_word') ? trim(I('key_word')) : ''; // 关键词搜索
        $cur_page = I('p/d', 0);//当前页数
        $this->assign('key_word', $key_word);
        $this->assign('pagenum', $pagenum);
        $this->assign('cur_page', $cur_page);
        return $this->fetch('', getAdmStoId());
    }

    /***小程序模板列表的加载更多***/
    public function ajaxMiniTemplateList()
    {
        $key_word = I('key_word') ? trim(I('key_word')) : ''; // 关键词搜索
        $cur_page = I('p/d', 0);//当前页数
        if ($cur_page <= 0) {
            $cur_page = 1;
        }
        $pagenum = I('pagenum/d', 10);
        $where = " a.type=4 and a.store_id=" . getAdmStoId();
        $this->assign('stoid', getAdmStoId());

        if ($key_word) {
            $where .= " and (a.page_title like '%$key_word%' or c.page_title like '%$key_word%' or a.template_sn like '%$key_word%')";
        }

        $model = M('store_module');
        $count = $model->alias('a')
            ->join('store_module c', 'a.from_id=c.id', 'left')
            ->where($where)->count();
        $Page = new AjaxPage($count, $pagenum);
        $show = $Page->show();

        $List = $model->alias('a')
            ->join('store_module c', 'a.from_id=c.id', 'left')
            ->where($where)
            ->field('a.*,c.page_title as page_title_type')
            ->order("a.billdate desc")
            ->limit($Page->firstRow . ',' . $Page->listRows)->select();

        $oldurl = U('admin/template/templatelist', array(
            "key_word" => $key_word,
            "pagenum" => $pagenum,
            "p" => $cur_page,
        ));

        $this->assign('oldurl', urlencode($oldurl));
        $this->assign('key_word', $key_word);
        $this->assign('pagenum', $pagenum);

        $this->assign('cur_page', $cur_page);
        $this->assign('temList', $List);
        $this->assign('page', $show);// 赋值分页输出
        $this->assign('pager', $Page);
//      upload_ylp_log('模块列表');
        return $this->fetch("", getAdmStoId());
    }

    //小程序搜索选择模板
    public function templatesearch_mini()
    {
        $p = I('p/d', 1);
        $type = I('type');
        $type_id = I('type_id/d');
        $kw = I('keywords');
        $this->assign('keywords', $kw);

        $sto_id = getAdmStoId();

        $where = " (type=0 or type=3)";
        if ($type_id) {
            $where .= " and type_id=" . $type_id;
        } else {
            $type_id = 0;
        }
        $this->assign('gettypeid', $type_id);
        if (!empty($kw)) {
            $where .= " and (page_title like '%" . $kw . "%' )";
        }

        $count = M('store_module')->where($where)->where("(store_id=" . $sto_id . " or store_id is null)")->count();


        $Page = new Page($count, 10);
        $mList = M('store_module')->where($where)
            ->where("(store_id=" . $sto_id . " or store_id is null)")
            ->order('type desc,billdate DESC')
            ->limit($Page->firstRow . ',' . $Page->listRows)->select();

        /*--数组顶部插入--*/
        $show = $Page->show();//分页显示输出

        foreach ($mList as $k => $v) {
            $check = M('store_renew_module')->where(['module_id' => $v['id'], 'store_id' => $sto_id])->where('end_time>' . time() . ' or end_time=0')->field('id')->find();
            if ($check) {
                $mList[$k]['money'] = 0;
                $mList[$k]['isshow'] = 1;
            }

        }

        $this->assign('page', $show);//赋值分页输出
        $this->assign('mList', $mList);
        $this->assign('pager', $Page);//赋值分页输出

        return $this->fetch("", $sto_id);
    }

    //小程序模板处理
    public function templatehandle_mini()
    {
        /*--获取id--*/
        $id = I('id');
        $new = I("new");
        $this->assign("new", $new);
        $this->assign('is_mini', 1);//是小程序的页面

        $this->initEditor();
        $stoid = getAdmStoId();
        /*-------模块读取-------*/
        $rs = M("module")->order('sort')->where('is_use', 1)->select();
        //元素  {"content":citem,"ename":data.data.eng_name,"name":"","icon":"",type:"1"}; type:1表示自定义模块
        foreach ($rs as $k => $v) {
            //小程序的不需要富文本
            if ($v['eng_name'] == "richText") continue;
            if ($v['eng_name'] == "groupbuy") continue;

            $str = file_get_contents(ROOT_PATH . $v['controljson']);
            $jdata = json_decode($str, true);
            $data[] = [
                "content" => $jdata['content'],
                "ename" => $v['eng_name'],
                "name" => $v['name'],
                "icon" => getimg($v['module_icon'], NOIMG),
                "type" => 0
            ];
        }

        /*---------如果有自定义模块-------*/
        $rrs = M('custommodule_list')->where('store_id', $stoid)->select();
        if ($rrs) {
            $data[] = [
                "content" => ['ename' => "", 'name' => ""],
                "ename" => "custom",
                "name" => "自定义",
                "icon" => "/public/static/images/model/custom_img.png",
                "type" => 1
            ];
            $this->assign("customlist", $rrs);
        }

        if (empty($data)) $data = -1;
        $this->assign("module", json_encode($data));

        if (!empty($id)) {
            /*--模板读取--*/
            $temdata = M("store_module")->where('id', $id)->find();
            /*--3是空模板--*/
            if ($temdata['type'] <> 3) {
                /*--要读取content--*/
                if ($temdata['type'] == 0 || $temdata['type'] == 4)
                    $p = ROOT_PATH . "/public/template/feature/" . $stoid . '/' . $temdata['template_sn'] . ".json";
                else
                    $p = ROOT_PATH . "/public/template/feature/" . $temdata['template_sn'] . ".json";
                if (file_exists($p)) {
                    $str = file_get_contents($p);
                    if ($temdata['type'] != 0 && $new == 1) {
                        $str = str_replace('stoid/1.html', "stoid/" . $stoid . "html", $str);
                        $str = str_replace('stoid\/1.html', "stoid\/" . $stoid . ".html", $str);
                        $str = str_replace('stoid\/1\/', "stoid\/" . $stoid . "\/", $str);
                        $str = str_replace('stoid/1/', "stoid/" . $stoid . "/", $str);
                    }
                    $jjdata = json_decode($str, true);
                    if ($temdata['type'] != 0 && $temdata['type'] != 4) {
                        foreach ($jjdata as $kh => $vh) {
                            $content = $vh['content'];
                            if ($content['moudletype'] == 3 || $content['moudletype'] == 2 || $content['classstyle'] == 2) {
                                $content['data'] = [];
                            }
                            $jjdata[$kh]['content'] = $content;
                        }
                    }

                } else {
                    $this->assign("temdata", -1);
                    $this->assign("config", -1);
                    $this->assign("err", "未找到模板文件");
                    return $this->fetch();
                }
                $this->assign("temdata", json_encode($jjdata));
                /*--config--{"title":"","descr":"","istop":0,"isright":0,"money":0};*/
                $data = [
                    "title" => $temdata['page_title'],
                    "descr" => $temdata['page_describe'],
                    "istop" => $temdata['istop'],
                    "isright" => $temdata['isright'],
                    "money" => $temdata['money'],
                    "bkcolor" => $temdata['bkcolor'],
                ];
                $this->assign("config", json_encode($data));

                //商家要对from_id进行映射,如果是收费模板,要进行记录
                if ($temdata['type'] == 2 || $temdata['type'] == 1) {
                    $this->assign("from_id", $id);
                } else if ($temdata['type'] == 0) {
                    $this->assign("from_id", $temdata['from_id']);
                }
            } else {
                $this->assign("temdata", -1);
                $this->assign("config", -1);
            }

        } else {
            $this->assign("temdata", -1);
            $this->assign("config", -1);
        }
        return $this->fetch("", getAdmStoId());
    }

    /*-------小程序模板的保存------------*/
    public function temlatesave_mini()
    {
        /*--清除缓存--*/
        ClearALLCache();
        delFile(TEMP_PATH . "/" . getAdmStoId());

        $data = I("post.");
        $id = I('id');
        $from_id = I('from_id/d', 0);
        $stoid = getAdmStoId();
        /*---创建模板存放目录---*/
        if (!is_dir(ROOT_PATH . '/public/template/feature/' . $stoid . '/'))
            mkdir(ROOT_PATH . '/public/template/feature/' . $stoid . '/');
        /*----页面基础配置----*/
        $conf = $data[1];
        /*----json文件保存----*/
        $str = json_encode($data[0]);

        mlog("文字:" . $str, "temlatesave/" . $stoid);

        if (!empty($id)) {
            $r10 = $rs = M('store_module')->where('id', $id)->find();
        }

        /*----------------新增,或者是空模板,或者是从商家商城模板那里选择的-----------------*/
        if (empty($id) || $r10['type'] == 3 || $r10['type'] == 0) {
            $mr = get_total_millisecond();
            $sn = date('YmdHis') . $mr . rand(1000, 9999);
            /*--json文件操作--*/
            $filename = '/public/template/feature/' . $stoid . '/' . $sn . '.json';
            if (file_exists(ROOT_PATH . $filename) && $filename) {
                mdelFile(ROOT_PATH . $filename);
            }
            file_put_contents(ROOT_PATH . $filename, $str);

            /*---调用风格表存储手机端显示文件---*/
            $filename2 = '/public/template/feature/' . $stoid . '/' . $sn . '.html';
            if (file_exists(ROOT_PATH . $filename2) && $filename2) {
                mdelFile(ROOT_PATH . $filename2);
            }
            /*---手机前端要显示保存的内容---*/
            mlog("文字122:" . json_encode($data[0]), "temlatesave/" . $stoid);
            $htm = "";
            foreach ($data[0] as $k => $v) {
                if ($v['type'] == 1) {
                    $htmlfile = '/public/template/custom/' . getAdmStoId() . '/' . $v['content']['ename'] . '.html';
                    if (file_exists(ROOT_PATH . $htmlfile)) {
                        $cstr = file_get_contents(ROOT_PATH . $htmlfile);
                        $htm .= $cstr;
                    } else {
                        return json(['code' => -1, 'msg' => '未找自定义模板的文件']);
                    }
                } else {
                    /*--风格类型--*/
                    $type = $v['content']['style'];
                    $r0 = M("module")->where('eng_name', $v['ename'])->find();
                    $r1 = M("module_list")->where('parent_id', $r0['id'])->where('type', $type)->find();
                    /*--获取手机前端要显示html--*/
                    $htmlfile = "/public/template/html/" . $r0['eng_name'] . "/mobliehtm" . $r1['class_eng_name'] . ".html";
                    if (file_exists(ROOT_PATH . $htmlfile)) {
                        $cstr = file_get_contents(ROOT_PATH . $htmlfile);
                    } else {
                        return json(['code' => -1, 'msg' => '未找到手机端展示文件']);
                    }
                    /*--根据字段替换--*/
                    $fields = explode(",", $r1['fields']);
                    foreach ($fields as $kk => $vv) {
                        $val = $v['content'][$vv];
                        if ($vv == 'no') {
                            $val = uniqid();
                        }
                        if ($vv == 'scoll_length') {
                            $val = $v['content']['length'];
                            if (count($v['content']['data']) < $val) {
                                $val = count($v['content']['data']);
                            }
                        }
                        if (gettype($val) == 'boolean') {
                            $val = (int)$val;
                        }
                        $replacestr = "{{" . $vv . "}}";
                        $cstr = str_replace($replacestr, $val . "", $cstr);
                    }

                    /*--如果有循环体--*/
                    if (!empty($v['content']['data'])) {
                        $rhtm = "";
                        $width = 100 / count($v['content']['data']);
                        /*--截取循环里面的循环体--*/
                        $start1 = strpos($cstr, '<foreach>', 0);
                        if ($start1) {
                            $end1 = strpos($cstr, '</foreach>', 0);
                            /* $str = "<foreach><span>{{name}}</span></foreach>"; */
                            $repeat = substr($cstr, $start1 + 9, $end1 - $start1 - 9);

                            $cstr = str_replace('<foreach>', "", $cstr);
                            $cstr = str_replace('</foreach>', "", $cstr);
                            $cstr = str_replace($repeat, "{{the_repeat}}", $cstr);

                            foreach ($v['content']['data'] as $kk => $vv) {
                                $vv['width'] = $width;
                                $rp = $repeat;
                                foreach ($vv as $mk => $mv) {
                                    $replacestr = "{{" . $mk . "}}";
                                    $rp = str_replace($replacestr, $mv . "", $rp);
                                }
                                $rhtm .= $rp;
                            }
                            $cstr = str_replace("{{the_repeat}}", $rhtm, $cstr);
                        } else {
                            /*--如果有按数组替换的--*/
                            if (strpos($cstr, '{{data[0]', 0)) {
                                /*--根据字段替换--*/
                                foreach ($v['content']['data'] as $kk => $vv) {
                                    foreach ($vv as $km => $vm) {
                                        $replacestr = "{{data[" . $kk . "]." . $km . "}}";
                                        $cstr = str_replace($replacestr, $vm . "", $cstr);
                                    }
                                }
                            } else {
                                /*---如果是商品分组的手动选择商品---*/
                                if (
                                    $v['content']['classstyle'] == 1
                                    || $v['content']['moudletype'] == 2
                                    || $v['content']['moudletype'] == 3
                                    || $v['content']['moudletype'] == 4
                                ) {
                                    $idlist = "";
                                    foreach ($v['content']['data'] as $kk => $vv) {
                                        $idlist .= $vv['goodsid'] . ",";
                                    }
                                    $idlist = substr($idlist, 0, strlen($idlist) - 1);
                                    $replacestr = "{{idlist}}";
                                    $cstr = str_replace($replacestr, $idlist . "", $cstr);
                                }
                            }
                        }
                    }
                    $htm .= $cstr;
                }
            }

            mlog("文字2:" . $htm, "temlatesave/" . $stoid);
            /*---写入文件---*/
            file_put_contents(ROOT_PATH . $filename2, $htm);

            /*--存储数据--*/
            $sdata = [
                "type" => 4,//设置成了小程序模板
                "page_title" => $conf['title'],
                "page_describe" => $conf['descr'],
                "bkcolor" => $conf['bkcolor'],
                "istop" => $conf['istop'],
                "isright" => $conf['isright'],
                "money" => $conf['money'],
                "template_sn" => $sn,
                "billdate" => time(),
                "store_id" => getAdmStoId(),
                "from_id" => $from_id,
                "json_str" => $str
            ];
            $u = M('store_module')->save($sdata);
            if ($u) {
                upload_ylp_log('D01模板添加/确认提交');
                return json(['code' => 1]);
            } else
                return json(['code' => -1, 'msg' => '保存失败']);
        } /*----------------编辑-----------------*/
        else {
            $rs = M('store_module')->where('id', $id)->where('store_id', getAdmStoId())->find();
            if (!$rs) {
                return json(['code' => -1, 'msg' => '未找到模板']);
            }
            $sn = $rs['template_sn'];
            /*--json文件操作--*/
            $filename = '/public/template/feature/' . $stoid . '/' . $sn . '.json';
            if (file_exists(ROOT_PATH . $filename) && $filename) {
                mdelFile(ROOT_PATH . $filename);
            }
            file_put_contents(ROOT_PATH . $filename, $str);

            /*---调用风格表存储手机端显示文件---*/
            $filename2 = '/public/template/feature/' . $stoid . '/' . $sn . '.html';
            if (file_exists(ROOT_PATH . $filename2) && $filename2) {
                mdelFile(ROOT_PATH . $filename2);
            }
            /*---手机前端要显示保存的内容---*/
            $htm = "";

            mlog("文字12:" . json_encode($data[0]), "temlatesave/" . $stoid);
            foreach ($data[0] as $k => $v) {
                if ($v['type'] == 1) {
                    $htmlfile = '/public/template/custom/' . getAdmStoId() . '/' . $v['content']['ename'] . '.html';
                    if (file_exists(ROOT_PATH . $htmlfile)) {
                        $cstr = file_get_contents(ROOT_PATH . $htmlfile);
                        $htm .= $cstr;
                    } else {
                        return json(['code' => -1, 'msg' => '未找自定义模板的文件']);
                    }
                } else {
                    /*--风格类型--*/
                    $type = $v['content']['style'];
                    $r0 = M("module")->where('eng_name', $v['ename'])->find();
                    $r1 = M("module_list")->where('parent_id', $r0['id'])->where('type', $type)->find();
                    /*--获取手机前端要显示html--*/
                    $htmlfile = "/public/template/html/" . $r0['eng_name'] . "/mobliehtm" . $r1['class_eng_name'] . ".html";
                    if (file_exists(ROOT_PATH . $htmlfile)) {
                        $cstr = file_get_contents(ROOT_PATH . $htmlfile);
                    } else {
                        return json(['code' => -1, 'msg' => '未找到手机端展示文件']);
                    }
                    /*--根据字段替换--*/
                    $fields = explode(",", $r1['fields']);
                    foreach ($fields as $kk => $vv) {
                        $val = $v['content'][$vv];
                        if ($vv == 'no') {
                            $val = uniqid();
                        }
                        if ($vv == 'scoll_length') {
                            $val = $v['content']['length'];
                            if (count($v['content']['data']) < $val) {
                                $val = count($v['content']['data']);
                            }
                        }
                        if (gettype($val) == 'boolean') {
                            $val = (int)$val;
                        }

                        $replacestr = "{{" . $vv . "}}";
                        $cstr = str_replace($replacestr, $val . "", $cstr);
                    }

                    /*--如果有循环体--*/
                    if (!empty($v['content']['data'])) {
                        $rhtm = "";
                        $width = 100 / count($v['content']['data']);
                        /*--截取循环里面的循环体--*/
                        $start1 = strpos($cstr, '<foreach>', 0);
                        if ($start1) {
                            $end1 = strpos($cstr, '</foreach>', 0);
                            /* $str = "<foreach><span>{{name}}</span></foreach>"; */
                            $repeat = substr($cstr, $start1 + 9, $end1 - $start1 - 9);

                            $cstr = str_replace('<foreach>', "", $cstr);
                            $cstr = str_replace('</foreach>', "", $cstr);
                            $cstr = str_replace($repeat, "{{the_repeat}}", $cstr);

                            foreach ($v['content']['data'] as $kk => $vv) {
                                $vv['width'] = $width;
                                $rp = $repeat;
                                foreach ($vv as $mk => $mv) {
                                    $replacestr = "{{" . $mk . "}}";
                                    $rp = str_replace($replacestr, $mv . "", $rp);
                                }
                                $rhtm .= $rp;
                            }
                            $cstr = str_replace("{{the_repeat}}", $rhtm, $cstr);
                        } else {
                            /*--如果有按数组替换的--*/
                            if (strpos($cstr, '{{data[0]', 0)) {
                                /*--根据字段替换--*/
                                foreach ($v['content']['data'] as $kk => $vv) {
                                    foreach ($vv as $km => $vm) {
                                        $replacestr = "{{data[" . $kk . "]." . $km . "}}";
                                        $cstr = str_replace($replacestr, $vm . "", $cstr);
                                    }
                                }
                            } else {
                                /*---如果是商品分组的手动选择商品---*/
                                if (
                                    $v['content']['classstyle'] == 1
                                    || $v['content']['classstyle'] == 2
                                    || $v['content']['classstyle'] == 3
                                    || $v['content']['moudletype'] == 2
                                    || $v['content']['moudletype'] == 3
                                    || $v['content']['moudletype'] == 4
                                ) {
                                    $idlist = "";
                                    foreach ($v['content']['data'] as $kk => $vv) {
                                        $idlist .= $vv['goodsid'] . ",";
                                    }
                                    $idlist = substr($idlist, 0, strlen($idlist) - 1);
                                    $replacestr = "{{idlist}}";
                                    $cstr = str_replace($replacestr, $idlist . "", $cstr);
                                }
                            }
                        }
                    }
                    $htm .= $cstr;
                }
            }

            mlog("文字2:" . $htm, "temlatesave/" . $stoid);
            /*---写入文件---*/
            file_put_contents(ROOT_PATH . $filename2, $htm);

            $mid = session('admin_id');
            $rr = M("admin")->where("admin_id", $mid)->find();
            /*--存储数据--*/
            $sdata = [
                "type" => 4,//设置成了小程序模板
                "page_title" => $conf['title'],
                "page_describe" => $conf['descr'],
                "bkcolor" => $conf['bkcolor'],
                "istop" => $conf['istop'],
                "isright" => $conf['isright'],
                "money" => $conf['money'],
                "editdate" => time(),
                "editip" => getIP(),
                "editman" => $rr['ERPName'],
                "store_id" => getAdmStoId(),
                "from_id" => $from_id,
                "json_str" => $str
            ];
            $u = M('store_module')->where('id', $id)->save($sdata);
            if ($u) {
                return json(['code' => 1]);
            } else
                return json(['code' => -1]);
        }
    }

    /*---小程序模板设置为首页---*/
    public function sethomepage_mini()
    {
        /*--清除缓存--*/
        ClearALLCache();
        delFile(TEMP_PATH . "/" . getAdmStoId());
        $id = I('id');
        $stoid = getAdmStoId();
        /*--查找模板--*/
        $r0 = M('store_module')->where(['id' => $id, 'type' => 4, 'store_id' => $stoid])->find();
        if ($r0) {
            M('store_module')->where(['type' => 4, 'store_id' => $stoid])->save(['isdefault' => 0]);
            M('store_module')->where(['id' => $id, 'type' => 4, 'store_id' => $stoid])->save(['isdefault' => 1]);
            return json(['code' => 1]);
        } else {
            return json(['code' => -1, 'msg' => '未找到该模板']);
        }
    }

    /*--小程序模板取消首页--*/
    public function canclehomepage_mini()
    {
        /*--清除缓存--*/
        ClearALLCache();
        delFile(TEMP_PATH . "/" . getAdmStoId());
        $id = I('id');
        $stoid = getAdmStoId();
        /*--查找模板--*/
        $r0 = M('store_module')->where(['id' => $id, 'type' => 4, 'store_id' => $stoid])->find();
        if ($r0) {
            M('store_module')->where(['id' => $id, 'type' => 4, 'store_id' => $stoid])->save(['isdefault' => 0]);
            return json(['code' => 1]);
        } else {
            return json(['code' => -1, 'msg' => '未找到该模板']);
        }
    }

    /**
     * 初始化编辑器链接
     * 本编辑器参考 地址 http://fex.baidu.com/ueditor/
     */
    private function initEditor()
    {
        $this->assign("URL_upload", U('admin/Ueditor/imageUp', array('savepath' => 'goods', 'savepath1' => getERPId()))); // 图片上传目录
        $this->assign("URL_imageUp", U('admin/Ueditor/imageUp', array('savepath' => 'goods', 'savepath1' => getERPId()))); //  不知道啥图片
        $this->assign("URL_fileUp", U('admin/Ueditor/fileUp', array('savepath' => 'goods', 'savepath1' => getERPId()))); // 文件上传s
        $this->assign("URL_scrawlUp", U('admin/Ueditor/scrawlUp', array('savepath' => 'goods', 'savepath1' => getERPId())));  //  图片流
        $this->assign("URL_getRemoteImage", U('admin/Ueditor/getRemoteImage', array('savepath' => 'goods', 'savepath1' => getERPId()))); // 远程图片管理
        $this->assign("URL_imageManager", U('admin/Ueditor/imageManager', array('savepath' => 'goods', 'savepath1' => getERPId()))); // 图片管理
        $this->assign("URL_getMovie", U('admin/Ueditor/getMovie', array('savepath' => 'goods', 'savepath1' => getERPId()))); // 视频上传
        $this->assign("URL_Home", "");
    }

    //获取续费的信息
    public function getxufeimoney()
    {
        $id = I("id");
        $rs = M('store_module')->alias('a')->join('store_module b', 'a.from_id=b.id', 'left')
            ->field('a.from_id,b.money,b.days')->where('a.id', $id)->find();
        if ($rs) {
            return json(['code' => 1, 'id' => $rs['from_id'], 'money' => $rs['money'], 'days' => $rs['days']]);
        } else {
            return json(['code' => 0]);
        }

    }


    //微信触发消息
    public function wxsendlist()
    {
        $pagenum = 10;//每页显示多少条
        if ((int)I('pagenum/s') > 0) {
            $pagenum = I('pagenum/s');
        }
        $key_word = I('key_word') ? trim(I('key_word')) : '';
        if ($key_word) {
            $where['b.typename|a.typeid'] = array('like', '%' . $key_word . '%');
        }
        $where['store_id'] = getAdmStoId();
        $model = M('weapp_sendlist');
        $count = $model->alias('a')->join('weapp_sendtype b', ' a.typeid=b.typeid')->where($where)->count();
        $Page = new Page($count, $pagenum);
        $list = $model->alias('a')->join('weapp_sendtype b', ' a.typeid=b.typeid')->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->order('a.typeid asc')->select();
        $show = $Page->show();//分页显示输出
        $this->assign('page', $show);//赋值分页输出
        $this->assign('list', $list);
        $this->assign('pager', $Page);//赋值分页输出
        $this->assign('pagenum', $pagenum);
        $this->assign('oldurl', urlencode(curPageURL()));
//        $oldurl = urldecode(urldecode($oldurl));
//        upload_ylp_log('微信消息设置');
        return $this->fetch('', getAdmStoId());
    }

    //添加推送消息
    public function addwxsend()
    {

        $typwhere = "1=1";
        $erpid = getERPId();
        $oldurl = I('oldurl/s');
        if ($oldurl) {
            $oldurl = urldecode(urldecode($oldurl));
        }


        $getstoid = getAdmStoId();
        $id = I('sendid');
        //修改
        if ($id) {
            $wxsend = M('weapp_sendlist')->alias('a')
                ->join('weapp_sendtype b', 'a.typeid=b.typeid', 'left')
                ->where(array('a.store_id' => $getstoid, 'a.sendid' => $id))
                ->field('a.*,b.remark as type_remark,b.typestate')
                ->find();
            if ($wxsend) {
                if (empty($wxsend['typestate'])) {
                    $this->error('该模板微信官网已失效无法修改', U('Weapp/wxsendlist'));
                    exit;
                }
                if ($wxsend['store_remark']) {
                    $store_remark = json_decode($wxsend['store_remark'], true);

                    $wxsend['first'] = $store_remark['first'];
                    $wxsend['remark'] = $store_remark['remark'];
                }
            }
            //

            //
        } else {
            $wxsend['state'] = 1;
            $typwhere .= " and typeid not in(select typeid from wxd_weapp_sendlist where  store_id=" . $getstoid . ") and typestate=1 ";
        }
        $typelist = M('weapp_sendtype')->where($typwhere)->order('typeid asc')->select();

        if (IS_POST) {
            $data = input('post.');

            $typeid = I('post.typeid');
            $data['store_id'] = $getstoid;
            unset($data['wx_first']);
            unset($data['wx_remark']);

            $data['template_id'] = trim(I('template_id'));
            $data['htmlurl'] = trim(I('htmlurl'));
            if (I('post.wx_first') && I('post.wx_remark')) {
                $store_remark['first'] = I('post.wx_first');
                $store_remark['remark'] = I('post.wx_remark');
                $data['store_remark'] = json_encode($store_remark);
            } else {
                $data['store_remark'] = "";
            }
            if ($id) {
                $data['sendid'] = $id;
                M("weapp_sendlist")->update($data);
            } else {
                $data['add_time'] = time();
                $sendlist = M('weapp_sendlist')->where(array('store_id' => $getstoid, 'typeid' => $typeid))->find();
                if ($sendlist) {
                    $this->error('该模板已存在!', U('Admin/Weapp/addwxsend'));
                    exit();
                } else {

                    M("weapp_sendlist")->insert($data);
                }
            }
            if ($oldurl) {

                $this->success("操作成功!!!", $oldurl);
            } else {
                $this->success("操作成功!!!", U('Admin/Weapp/wxsendlist'));
            }
            exit;
        }

        $this->assign('wxsend', $wxsend);
        $this->assign('typelist', $typelist);
        return $this->fetch('', getAdmStoId());

    }


    //获取模板类型的格式
    public function getwxremark(){
        $model = M("weapp_sendtype");
        $res= $model->where(array('typeid' => $_GET['typeid']))->find();
        if ($model)
        {

            $return_arr = array('status' => 1, 'msg' => '操作成功', 'remark' =>$res['remark']);
        }
        else{
            $return_arr = array('status' => -1, 'msg' => '获取失败', 'data' => '');
        }

        $this->ajaxReturn($return_arr);
    }

    //消息模板测试
    public function sendtest()
    {

        $getstoid = getAdmStoId();
        $gid = I('post.sendid');
        if (!empty($gid)) {
            $wxsend = M('weapp_sendlist')->where(array('store_id' => $getstoid, 'sendid' => $gid))->find();
        }
        $this->assign('wxsend', $wxsend);
        return $this->fetch('', getAdmStoId());
    }


    public function sumbitsendtest()
    {

        $getstoid = getAdmStoId();
        $gid = I('post.sendid');
        $mobile = I('post.mobile');

        $userinfo = M('users')->alias('a')
            ->join('weapp b', 'a.store_id=b.store_id')
            ->field("a.*,b.appid")
            ->where(array('a.store_id' => $getstoid, 'a.mobile' => $mobile))->find();
        if (empty($userinfo)) {
            $return_arr = array('code' => -1, 'msg' => '会员手机号不存在', 'data' => '');
            $this->ajaxReturn($return_arr);
        }


        if (empty($userinfo['weapp_openid'])) {
            $return_arr = array('code' => -1, 'msg' => '该手机号未绑定线上会员', 'data' => '');
            $this->ajaxReturn($return_arr);
        }

        $wxsend = M('weapp_sendlist')->alias('a')->where(array('a.store_id' => $getstoid, 'a.sendid' => $gid))->find();
        $wxsendtype = M('weapp_sendtype')->where('typeid', $wxsend['typeid'])->find();


        if (empty($wxsend)) {
            $return_arr = array('code' => -1, 'msg' => '未设置该模板', 'data' => '');
            $this->ajaxReturn($return_arr);
        }
        $colorlist = urlencode("#FF0000|#173177|#173177|#173177|#173177|#173177|#FF0000|#ca003a");
        $pd['colorlist'] = $colorlist;
        $pd['title'] = "测试标题";
        $pd['key1'] = "关键字1";
        $pd['key2'] = "关键字2";
        $pd['key3'] = "关键字3";;
        $pd['key4'] = "关键字4";
        $pd['key5'] = "关键字5";
        $pd['remark'] = "备注";

        $mdataarr = explode(",", $wxsendtype["myfieldlist"]);
        $postdata = null;
        /*--双数组,确定推送内容--*/
        foreach ($mdataarr as $ku => $vu) {

            $postdata[] = $pd[$vu];
        }
        $postdata[] = $colorlist;
        $backurl = "";

        $return_arr = array('code' => 0, 'msg' => '发送成功', 'data' => '',);
        $this->ajaxReturn($return_arr);
    }

    //删除推送消息
    public function delsendlist()
    {
        $model = M("weapp_sendlist");
        $model->where(array('sendid' => $_GET['id'], 'store_id' => getAdmStoId()))->delete();
        $return_arr = array('status' => 1, 'msg' => '操作成功', 'data' => '',);
        $this->ajaxReturn($return_arr);
    }
}