ProductAction.class.php页面
[code=php]<?php
class ProductAction extends BaseAction
{
    public $token;
    public $wecha_id;
    public $product_model;
    public $product_cat_model;
    public $isDining;
    public function __construct()
    {
        $agent = $_SERVER['HTTP_USER_AGENT'];
        //if (!strpos($agent, "MicroMessenger")) {
        //    echo '此功能只能在微信浏览器中使用';
        //    exit;
        //}
        
        $this->token = $this->_get('token');
        $this->assign('token', $this->token);
        $this->wecha_id = $this->_get('wecha_id');
        if (!$this->wecha_id) {
            //$this->wecha_id='';
        }
        $this->assign('wecha_id', $this->wecha_id);
        $this->product_model     = M('Product');
        $this->product_cat_model = M('Product_cat');
        //define('RES',THEME_PATH.'common');
        //define('STATICS',TMPL_PATH.'static');
        $this->assign('staticFilePath', str_replace('./', '/', THEME_PATH . 'common/css/product'));
        //购物车
        $calCartInfo = $this->calCartInfo();
        $this->assign('totalProductCount', $calCartInfo[0]);
        $this->assign('totalProductFee', $calCartInfo[1]);
        //是否是餐饮
        if (isset($_GET['dining']) && intval($_GET['dining'])) {
            $this->isDining = 1;
            $this->assign('isDining', 1);
        }
        //pay info
        $pay_m_alipay_config = M('Pay_m_alipay_config')->where(array(
            'token' => $this->token
        ))->find();
        $this->assign('pay_m_alipay_config', $pay_m_alipay_config);
        //统计购物车类型
        $carts      = $this->_getCart();
        $cartsCount = 0;
        $isGroupon  = 0;
        $productids = array();
        foreach ($carts as $k => $c) {
            array_push($productids, $k);
        }
        //把团购、普通购物和餐饮分开
        $normalCart    = array();
        $grouponCart   = array();
        $diningCart    = array();
        $productsByKey = array();
        if (count($productids)) {
            $products = $this->product_model->where(array(
                'id' => array(
                    'in',
                    $productids
                )
            ))->select();
            if ($products) {
                foreach ($products as $p) {
                    $productsByKey[$p['id']] = $p;
                }
            }
            foreach ($carts as $k => $c) {
                $thisProduct = $productsByKey[$k];
                if ($thisProduct['groupon'] == 1) {
                    $grouponCart[$k]    = $c;
                    $carts[$k]['type']  = 'groupon';
                    $carts[$k]['rtype'] = 'Groupon';
                } else {
                    if ($thisProduct['dining'] == 1) {
                        $diningCart[$k]     = $c;
                        $carts[$k]['type']  = 'dining';
                        $carts[$k]['rtype'] = 'Dining';
                    } else {
                        $normalCart[$k]     = $c;
                        $carts[$k]['type']  = 'normal';
                        $carts[$k]['rtype'] = 'Shop';
                    }
                }
                $cartsCount++;
            }
        }
        foreach ($carts as $k => $v) {
            $array[$k] = $v['rtype'];
        }
        $newarray = array_values($array);
        foreach ($newarray as $key => $val) {
            $biz_pay         = M('Reply_info')->where(array(
                'token' => $this->token,
                'infotype' => $val
            ))->find();
            $pay_array[$key] = $biz_pay['if_pay'];
        }
        $this->pay_array = $pay_array;
        if (in_array(1, $pay_array)) {
            $this->assign('if_pay', 1);
        } else {
            $this->assign('if_pay', 0);
        }
    }
    function remove_html_tag($str) //清除HTML代码、空格、回车换行符
    {
        //trim 去掉字串两端的空格
        //strip_tags 删除HTML元素
        
        $str = trim($str);
        $str = @preg_replace('/<script[^>]*?>(.*?)<\/script>/si', '', $str);
        $str = @preg_replace('/<style[^>]*?>(.*?)<\/style>/si', '', $str);
        $str = @strip_tags($str, "");
        $str = @ereg_replace("\t", "", $str);
        $str = @ereg_replace("\r\n", "", $str);
        $str = @ereg_replace("\r", "", $str);
        $str = @ereg_replace("\n", "", $str);
        $str = @ereg_replace(" ", "", $str);
        $str = @ereg_replace("&nbsp;", "", $str);
        return trim($str);
    }
    public function cats()
    {
        $catWhere = array(
            'parentid' => 0,
            'token' => $this->token
        );
        if (isset($_GET['parentid'])) {
            $parentid             = intval($_GET['parentid']);
            $catWhere['parentid'] = $parentid;
            
            $thisCat = $this->product_cat_model->where(array(
                'id' => $parentid
            ))->find();
            $this->assign('thisCat', $thisCat);
            $this->assign('parentid', $parentid);
        }
        if ($this->isDining) {
            $catWhere['dining'] = 1;
        } else {
            $catWhere['dining'] = 0;
        }
        $cats = $this->product_cat_model->where($catWhere)->order('id asc')->select();
        $this->assign('cats', $cats);
        $this->assign('metaTitle', '商品分类');
        $this->display();
    }
    public function products()
    {
        $where = array(
            'token' => $this->token
        );
        if (isset($_GET['catid'])) {
            $catid          = intval($_GET['catid']);
            $where['catid'] = $catid;
            
            $thisCat = $this->product_cat_model->where(array(
                'id' => $catid
            ))->find();
            $this->assign('thisCat', $thisCat);
        }
        if (IS_POST) {
            $key      = $this->_post('search_name');
            $wecha_id = $this->_post('search_wecha_id');
            $this->redirect(U('Wap/Product/products', array(
                'token' => $this->token,
                'wecha_id' => $wecha_id,
                'keyword' => $key
            )));
        }
        if (isset($_GET['keyword'])) {
            $where['name|intro|keyword'] = array(
                'like',
                "%" . $_GET['keyword'] . "%"
            );
            $this->assign('isSearch', 1);
        }
        $count = $this->product_model->where($where)->count();
        $this->assign('count', $count);
        //排序方式
        $method = isset($_GET['method']) && ($_GET['method'] == 'DESC' || $_GET['method'] == 'ASC') ? $_GET['method'] : 'DESC';
        $orders = array(
            'time',
            'discount',
            'price',
            'salecount'
        );
        $order  = isset($_GET['order']) && in_array($_GET['order'], $orders) ? $_GET['order'] : 'time';
        $this->assign('order', $order);
        $this->assign('method', $method);
        
        $products = $this->product_model->where($where)->order($order . ' ' . $method)->limit('5')->select();
        $this->assign('products', $products);
        $this->assign('metaTitle', '商品');
        $this->display();
    }
    public function ajaxProducts()
    {
        $where = array(
            'token' => $this->token
        );
        if (isset($_GET['catid'])) {
            $catid          = intval($_GET['catid']);
            $where['catid'] = $catid;
        }
        $page     = isset($_GET['page']) && intval($_GET['page']) > 1 ? intval($_GET['page']) : 2;
        $pageSize = isset($_GET['pagesize']) && intval($_GET['pagesize']) > 1 ? intval($_GET['pagesize']) : 5;
        $start    = ($page - 1) * $pageSize;
        if ($_SESSION['last'] == $page) {
            unset($_SESSION['last']);
            $str = '{"products":[]}';
            $this->show($str);
            exit;
        }
        $_SESSION['last'] = $page;
        $products         = $this->product_model->where($where)->order('id desc')->limit($start . ',' . $pageSize)->select();
        $str              = '{"products":[';
        if ($products) {
            $comma = '';
            foreach ($products as $p) {
                $str .= $comma . '{"id":"' . $p['id'] . '","catid":"' . $p['catid'] . '","storeid":"' . $p['storeid'] . '","name":"' . $p['name'] . '","price":"' . $p['price'] . '","intro":"' . $p['intro'] . '","token":"' . $p['token'] . '","keyword":"' . $p['keyword'] . '","salecount":"' . $p['salecount'] . '","logourl":"' . $p['logourl'] . '","time":"' . $p['time'] . '","oprice":"' . $p['oprice'] . '"}';
                $comma = ',';
            }
        }
        $str .= ']}';
        $this->show($str);
    }
    public function header()
    {
        $this->display();
    }
    public function product()
    {
        $where = array(
            'token' => $this->token
        );
        if (isset($_GET['id'])) {
            $id          = intval($_GET['id']);
            $where['id'] = $id;
        }
        $product = $this->product_model->where($where)->find();
        $this->assign('product', $product);
        if ($product['endtime']) {
            $leftSeconds = intval($product['endtime'] - time());
            $this->assign('leftSeconds', $leftSeconds);
        }
        $this->assign('metaTitle', $product['name']);
        $product['intro'] = str_replace(array(
            '&lt;',
            '&gt;'
        ), array(
            '<',
            '>'
        ), $product['intro']);
        $intro            = $this->remove_html_tag($product['intro']);
        $intro            = mb_substr($intro, 0, 30, 'utf-8');
        $this->assign('intro', $intro);

解决方案 »

  1.   

            //分店信息
            $company_model    = M('Company');
            $branchStoreCount = $company_model->where(array(
                'token' => $this->token,
                'isbranch' => 1
            ))->count();
            $this->assign('branchStoreCount', $branchStoreCount);
            //销量最高的商品
            $sameCompanyProductWhere = array(
                'token' => $this->token,
                'id' => array(
                    'neq',
                    $product['id']
                )
            );
            if ($product['dining']) {
                $sameCompanyProductWhere['dining'] = 1;
            }
            if ($product['groupon']) {
                $sameCompanyProductWhere['groupon'] = 1;
            }
            if (!$product['groupon'] && !$product['dining']) {
                $sameCompanyProductWhere['groupon'] = array(
                    'neq',
                    1
                );
                $sameCompanyProductWhere['dining']  = array(
                    'neq',
                    1
                );
            }
            $products = $this->product_model->where($sameCompanyProductWhere)->limit('salecount DESC')->limit('0,5')->select();
            $this->assign('products', $products);
            $this->display();
        }
        public function productDetail()
        {
            $where = array(
                'token' => $this->token
            );
            if (isset($_GET['id'])) {
                $id          = intval($_GET['id']);
                $where['id'] = $id;
            }
            $product          = $this->product_model->where($where)->find();
            $product['intro'] = str_replace(array(
                '&lt;',
                '&gt;'
            ), array(
                '<',
                '>'
            ), $product['intro']);
            $this->assign('product', $product);
            $this->assign('metaTitle', $product['name']);
            
            $this->display();
        }
        public function company($display = 1)
        {
            //店铺信息
            $company_model = M('Company');
            $where         = array(
                'token' => $this->token
            );
            if (isset($_GET['companyid'])) {
                $where['id'] = intval($_GET['companyid']);
            }
            
            $thisCompany = $company_model->where($where)->find();
            $this->assign('thisCompany', $thisCompany);
            //分店信息
            $branchStores     = $company_model->where(array(
                'token' => $this->token,
                'isbranch' => 1
            ))->order('taxis ASC')->select();
            $branchStoreCount = count($branchStores);
            $this->assign('branchStoreCount', $branchStoreCount);
            $this->assign('branchStores', $branchStores);
            $this->assign('metaTitle', '公司信息');
            if ($display) {
                $this->display();
            }
        }
        public function companyMap()
        {
            $this->apikey = C('baidu_map_api');
            $this->assign('apikey', $this->apikey);
            $this->company(0);
            $this->display();
        }
        public function addProductToCart() //商品id|商品价格|商品数量,
        {
            $count = isset($_GET['count']) ? intval($_GET['count']) : 1;
            $carts = $this->_getCart();
            $id    = intval($_GET['id']);
            if (key_exists($id, $carts)) {
                $carts[$id]['count']++;
            } else {
                $carts[$id] = array(
                    'count' => 1,
                    'price' => floatval($_GET['price'])
                );
            }
            $_SESSION['session_cart_products'] = serialize($carts);
            $calCartInfo                       = $this->calCartInfo();
            echo $calCartInfo[0] . '|' . $calCartInfo[1];
        }
        public function calCartInfo($carts = '')
        {
            $totalFee   = 0;
            $totalCount = 0;
            if (!$carts) {
                $carts = $this->_getCart();
            }
            if ($carts) {
                foreach ($carts as $c) {
                    if ($c) {
                        $totalFee += floatval($c['price']) * $c['count'];
                        $totalCount += intval($c['count']);
                    }
                }
            }
            return array(
                $totalCount,
                $totalFee
            );
        }
        function _getCart()
        {
            if (!isset($_SESSION['session_cart_products']) || !strlen($_SESSION['session_cart_products'])) {
                $carts = array();
            } else {
                $carts = unserialize($_SESSION['session_cart_products']);
            }
            return $carts;
        }
        public function cart()
        {
            $totalFee   = 0;
            $totalCount = 0;
            $products   = array();
            $ids        = array();
            $carts      = $this->_getCart();
            foreach ($carts as $k => $c) {
                if (is_array($c)) {
                    $productid = $k;
                    $price     = $c['price'];
                    $count     = $c['count'];
                    //
                    if (!in_array($productid, $ids)) {
                        array_push($ids, $productid);
                    }
                    $totalFee += $price * $count;
                    $totalCount += $count;
                }
            }
            if (count($ids)) {
                $list = $this->product_model->where(array(
                    'id' => array(
                        'in',
                        $ids
                    )
                ))->select();
            }
            //判断是不是餐饮
            $isDining = 0;
            if ($list) {
                $i = 0;
                foreach ($list as $p) {
                    $list[$i]['count'] = $carts[$p['id']]['count'];
                    if ($p['dining']) {
                        $isDining = 1;
                    }
                    $i++;
                }
            }
            $this->assign('cartIsDining', $isDining);
            $this->assign('products', $list);
            //
            $this->assign('totalFee', $totalFee);
            $this->assign('metaTitle', '购物车');
            $this->display();
        }
        public function deleteCart()
        {
            $products = array();
            $ids      = array();
            $carts    = $this->_getCart();
            foreach ($carts as $k => $c) {
                $i = 0;
                if (strlen($c)) {
                    $productid = $k;
                    $price     = $c['price'];
                    $count     = $c['count'];
                    //
                    if ($_GET['id'] != $productid) {
                        $products[$productid] = array(
                            'price' => $price,
                            'count' => $count
                        );
                    }
                    $i++;
                }
            }
            $_SESSION['session_cart_products'] = serialize($products);
            $this->redirect(U('Product/cart', array(
                'token' => $_GET['token'],
                'wecha_id' => $_GET['wecha_id']
            )));
        }
        public function ajaxUpdateCart()
        {
            $carts   = $this->_getCart();
            $g_id    = intval($_GET['id']);
            $g_count = intval($_GET['count']);
            if ($carts) {
                foreach ($carts as $k => $c) {
                    if ($g_id == $k) {
                        $carts[$k]['count'] = $g_count;
                    }
                }
            }
            $_SESSION['session_cart_products'] = serialize($carts);
            $calCartInfo                       = $this->calCartInfo();
            echo $calCartInfo[0] . '|' . $calCartInfo[1];
        }
        public function orderCart()
        {
            $calCartInfo         = $this->calCartInfo();
            $cart_num            = $calCartInfo[0];
            $cart_price          = $calCartInfo[1];
            $pay_m_alipay_config = M('Pay_m_alipay_config')->where(array(
                'token' => $this->token
            ))->find();
            
            if (isset($_GET['cartIsDining']) && intval($_GET['cartIsDining'])) {
                $cartIsDining = 1;
                $this->assign('cartIsDining', 1);
            }
            $userinfo_model = M('Userinfo');
            $thisUser       = $userinfo_model->where(array(
                'token' => $this->token,
                'wecha_id' => $this->wecha_id
            ))->find();
            $this->assign('thisUser', $thisUser);
            if (IS_POST) {
                $row        = array();
                $carts      = $this->_getCart();
                $cartsCount = 0;
                $isGroupon  = 0;
                $productids = array();
                foreach ($carts as $k => $c) {
                    array_push($productids, $k);
                }
                //把团购、普通购物和餐饮分开
                $normalCart    = array();
                $grouponCart   = array();
                $diningCart    = array();
                $productsByKey = array();
                if (count($productids)) {
                    $products = $this->product_model->where(array(
                        'id' => array(
                            'in',
                            $productids
                        )
                    ))->select();
       
      

  2.   

    //短信+邮件
                $info            = M('Wxuser')->where(array(
                    'token' => $this->token
                ))->find();
                $phone           = $info['sms_plat_reply'];
                $user            = $info['sms_plat_user'];
                $pass            = md5($info['sms_plat_pass']);
                $if_sms          = $info['sms_plat_status'];
                $sms_order_feed  = $info['sms_plat_order_feed'];
                $if_smtp         = $info['smtp_plat_status'];
                $smtp_order_feed = $info['smtp_plat_order_feed'];
                $content         = $this->content();
                if ($if_sms == 1 && $sms_order_feed == 1) {
                    if (!empty($content)) {
                        $this->sendSMS($user, $pass, $phone, $content);
                    }
                }
                $to                    = $info['smtp_plat_reply'];
                $name                  = $info['wxname'];
                $subject               = '您有新订单,请注意查收!';
                $body                  = $this->content();
                $attachment            = NULL;
                $config['SMTP_HOST']   = $info['smtp_plat_host'];
                $config['SMTP_PORT']   = $info['smtp_plat_port'];
                $config['SMTP_USER']   = $info['smtp_plat_send'];
                $config['SMTP_PASS']   = $info['smtp_plat_pass'];
                $config['FROM_EMAIL']  = $info['smtp_plat_send'];
                $config['FROM_NAME']   = $info['wxname'];
                $config['REPLY_EMAIL'] = $info['smtp_plat_reply'];
                $config['REPLY_NAME']  = $info['wxname'];
                $ssl                   = $info['smtp_plat_ssl'];
                if ($if_smtp == 1 && $smtp_order_feed == 1) {
                    if (!empty($content)) {
    $this->sendEMAIL($to, $name, $subject, $body, $attachment, $config, $ssl);
                    }
                }

                if (in_array(1, $this->pay_array)) {
                    $this->order_pay($payinfo, $this->wecha_id, $this->token);
                } else {
                    $this->redirect(U('Product/my', array(
                        'token' => $_GET['token'],
                        'wecha_id' => $_GET['wecha_id'],
                        'success' => 1
                    )));
                }
            } else {
                //如果是订餐