Evaluate.php 16.9 KB
<?php
/**
 * Created by PhpStorm.
 * User: wxd1061
 * Date: 2017-05-02
 * Time: 11:31
 */
namespace app\mobile\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\response\Json;
use think\Session;
use think\Cookie;

class Evaluate extends MobileBase
{
//MobileBase   Controller
    public $user_id = 0;
//19534
    public $user = array();

    /**
     * 析构流函数
     */
    public function __construct()
    {
        parent::__construct();
        if (session('?user')) {
            $user = session('user');
            $user = M('users')->where("user_id", $user['user_id'])->find();
            session('user', $user);  //覆盖session 中的 user
            $this->user = $user;
            $this->user_id = $user['user_id'];
            $this->assign('user', $user); //存储用户信息


        } else {
            $uid = Cookie::get('user_id');
            if ($uid) {
                $user = M('users')->where("user_id", $uid)->where("store_id", getMobileStoId())->find();
                session('user', $user);  //覆盖session 中的 user
                $this->user = $user;
                $this->user_id = $user['user_id'];
                $this->assign('user', $user); //存储用户信息
            }
        }
        $nologin = array(
            'getNum',
        );
        if ($this->user_id == 0) {
            $this->error('请先登录', U('Mobile/User/login', array('stoid' => getMobileStoId())));
        }
        if (!in_array(ACTION_NAME, $nologin)) {
            if (($this->pm_erpid && (!$user || !$user['erpvipid'])) || (empty($this->pm_erpid) && !$user || !$user['mobile'])) {
                $this->redirect(U('mobile/User/login', array('stoid' => getMobileStoId())));
                exit;
            }
        }

    }

    /**推送打开评论页面**/
    public function first()
    {
        $state = I('state');
        $POSNo = I('POSNo');
        $user_info = M('users')->alias('a')
            ->join('store b', 'a.store_id=b.store_id', 'left')
            ->field('a.*,b.api_token,b.store_logo,b.store_name')
            ->where("a.user_id", $this->user_id)->find();//调取api_token,商家LOGO,商家名称
        if ($user_info) {
            if (empty($user_info['erpvipno'])) {
                $sto_erpid = $user_info['ERPId'];
                if ($user_info['erpvipid']) {
                    $whereId['VIPId'] = $user_info['erpvipid'];
                    $rs_one = getApiData_java_p("/api/erp/vip/info/page", $sto_erpid, $whereId, 1, 10, null, "GET");
                    if ($rs_one) {
                        $rs_one_data = json_decode($rs_one, true);
                        if ($rs_one_data['code'] == 0) {
                            $rs_one1datainfo = $rs_one_data['data']['pageData'][0];
                            if ($rs_one1datainfo) {
                                $user_info['erpvipno'] = $rs_one1datainfo['VIPNo'];
                                M('users')->where(array('store_id' => $user_info['store_id'], 'user_id' => $user_info['user_id']))->save(array('erpvipno' => $rs_one1datainfo['VIPNo']));
                            }
                        }
                    }
                }
            }

            $mapuser['VIPNo'] = $user_info['erpvipno'];//token验证
            //第一次进入
            mlog("第一次进入:" . json_encode($state), "evaluate_state/" . getMobileStoId());
            if ($state == 'STATE' || $state == '') {
                $where = array(
                    'IsEvaluate' => 0,
                    'vipno' => $user_info['erpvipno'],
                    'posno' => ''
                );
            } else if ($state == 'add') {
                //列表add进入
                $where = array(
                    'vipno' => '',
                    'posno' => $POSNo,
                );
            }
            mlog("搜索条件:" . json_encode($where), "evaluate_state/" . getMobileStoId());
            $user_rs = getApiData('wxd.pos.prc.posevaluate.list', $user_info['api_token'], null, $where, '', '1', 'BillDate desc');
            mlog("接口返回:" . $user_rs, "evaluate_state/" . getMobileStoId());
            $user_rs = json_decode($user_rs, 'true');
            if ($user_rs && $user_rs['code'] == 1 && $user_rs['data'][0]['POSNo']) {
                //还有订单未评价,调取最新未评价订单
                $order_info = $user_rs['data'][0];
                $other = $user_rs['data'][0]['StarLabelname'];
                $order_info['StarLabelname'] = json_decode($other, 'true');
                $order_info['store_logo'] = $user_info['store_logo'];
                return json(["code" => 1, "msg" => "还有多条订单", "data" => $order_info]);
            } else {
                //所有订单已经评价跳转至列表页
                $this->redirect(U('Mobile/Evaluate/evaluate_list', array('stoid' => getMobileStoId())));
            }
        }
    }


    /** 评论页面状态**/
    public function evaluate()
    {

        //是否关闭免费使用优惠券
        $switch = tpCache('shopping.switch_list', getMobileStoId());//获取开关json
        $switch = json_decode($switch, true);
        $is_closecoupon = $switch['is_closecoupon'];
        $this->assign('is_closecoupon', $is_closecoupon);


        mlog("取会员Id:" . $this->user_id, "evaluate/" . getMobileStoId());
        $state = I('state');
        $POSNo = I('POSNo');
        $user_info = M('users')->alias('a')
            ->join('store b', 'a.store_id=b.store_id', 'left')
            ->field('a.*,b.api_token,b.store_logo,b.store_name,b.ERPId')
            ->where("a.user_id", $this->user_id)->find();//调取api_token,商家LOGO,商家名称
        if ($user_info) {
            if (empty($user_info['erpvipno']) && $user_info['erpvipid']) {
                $sto_erpid = $user_info['ERPId'];
                $whereId['VIPId'] = $user_info['erpvipid'];
                $rs_one = getApiData_java_p("/api/erp/vip/info/page", $sto_erpid, $whereId, 1, 10, null, "GET");

                if ($rs_one) {
                    $rs_one_data = json_decode($rs_one, true);
                    if ($rs_one_data['code'] == 0 ) {
                        $rs_one1datainfo = $rs_one_data['data']['pageData'][0];
                        $user_info['erpvipno'] = $rs_one1datainfo['VIPNo'];
                        M('users')->where(array('store_id' => $user_info['store_id'], 'user_id' => $user_info['user_id']))->save(array('erpvipno' => $rs_one1datainfo['VIPNo']));
                    }
                }
            }
            $mapuser['VIPNo'] = $user_info['erpvipno'];//token验证
            if ($state == 'edit' && !empty($POSNo)) {
                //列表点击进入,根据订单号查询
                if ($state == 'add' || $state == 'edit') {
                    $where = array(
                        'vipno' => '',
                        'posno' => $POSNo,
                    );
                    $order_info = getApiData('wxd.pos.prc.posevaluate.list', $user_info['api_token'], null, $where);//内含主表星级以及自定义评价
                    $order_info = json_decode($order_info, 'true');
                    if ($order_info && $order_info['code'] == 1) {
                        $other = $order_info['data'][0]['StarLabelname'];
                        $other = json_decode($other, 'true');
                        $order_info = $order_info['data'][0];
                        $goods_info = $other['goodinfo'];
                        $star_bs = $other['data2'];
                        $star_bs = json_encode($star_bs);
                        //所有星级的评价
                        $star = $other['data'];
                    }
                    //查询状态调取对应的星级
                    $where = array(
                        'POSNo' => $POSNo,
                    );
                    $fixpro = getApiData('wxd.pos.evaluatelist.list', $user_info['api_token'], null, $where);//查询从表固定评价
                    $fixpro = json_decode($fixpro, 'true');
                    $fixpro = $fixpro['data'];
//                        $onlyorder = I('onlyorder');  //(只有一条订单时,提交评价后判断)且不显示按钮(评价其他订单)
//                        if (!empty($onlyorder)) {
//                            $this->assign('onlyorder', $onlyorder);//判断按钮显示
//                        };
                    if ($order_info['$order_info'] == '0' || $order_info['$order_info'] == '') {
                        $onlyorder = '2';
                        $this->assign('onlyorder', $onlyorder);//判断按钮显示
                    };
                    $this->assign('fixpro', $fixpro);//渲染该单固定评价
                }
            }
            $this->assign('star_edit', json_encode($star));//所有星级评价信息
            $this->assign('star_bs', $star_bs);//所有星级标识
            $this->assign('goods_info', $goods_info);//所有商品信息
            $this->assign('order_info', $order_info);//渲染订单信息
            $this->assign('store_logo', $user_info['store_logo']);//渲染店铺头像
            $this->assign('state', $state);//传递状态add||edit
            $this->assign('POSNo', $POSNo);//传订单编号
        }
        return $this->fetch();
    }

    /**提交评价内容**/
    public function refer_eva()
    {
        $data = I('post.');
        $data['EvaluateDatetime'] = $time = date("Y-m-d H:i:s", time());//添加评价时间
        $user_info = M('users')->alias('a')
            ->join('store b', 'a.store_id=b.store_id', 'left')
            ->field('a.*,b.api_token,b.store_logo,b.store_name')
            ->where("a.user_id", $this->user_id)->find();//调取api_token,商家LOGO,商家名称
        if ($user_info) {
            //添加评价
            $data['LabelName'] = explode(',', $data['LabelName']);
            $data['LabelName1'] = '';
            foreach ($data['LabelName'] as $k => $vo) {
                $data['LabelName1'][$k] = array(
                    'LabelName' => $data['LabelName'][$k],
                    'POSNo' => $data['POSNo']
                );
            }
            $data['LabelName'] = $data['LabelName1'];
            unset($data['LabelName1']);
            $data_main = $data;
            mlog("0条件:" . json_encode($data_main), "refer_eva/" . getMobileStoId());
            $main_table = getApiData('wxd.pos.evaluateparaformlist.add', $user_info['api_token'], $data_main);//插入主表
            mlog("1条件:" . $main_table, "refer_eva/" . getMobileStoId());
            $main_table = json_decode($main_table, 'true');
            if (isset($main_table['code'])) {
                if ($main_table && $main_table['code'] == '1') {//插入成功后判断该VIPNo是否存在其他订单,是:返回列表,否:跳转该订单详情页
                    $where = array(
                        'VIPNo' => $user_info['erpvipno'],
                        'IsEvaluate' => 0
                    );
                    mlog("4条件:" . json_encode($where), "refer_eva/" . getMobileStoId());
                    $sur_order = getApiData('wxd.pos.vealuateform.list', $user_info['api_token'], null, $where);//查询是否还有订单未评价
                    mlog("5条件:" . json_encode($sur_order), "refer_eva/" . getMobileStoId());
                    $sur_order = json_decode($sur_order, 'true');
                    if ($sur_order && $sur_order['code'] == 1 && $sur_order['count'] && $sur_order['count'] == 1) {
                        return json(["code" => 1, "msg" => "评价成功", "count" => 1]);//评价插入成功(订单条数=1)
                    } else {
                        return json(["code" => 1, "msg" => "评价成功", "count" => 2]);//评价插入成功(订单条数>1)
                    }
                } else {
                    return json(["code" => -1, "msg" => "系统繁忙,请稍后"]);//(从)未成功添加信息
                }
//                if ($main_table && $main_table['code'] == '1' ) {
//                    //从表
//                    $slave_main = $data;
//                    unset($slave_main['Remark']);
//                    unset($slave_main['Star']);
//                    unset($slave_main['EvaluateDatetime']);
//                    mlog("2条件:".json_encode($slave_main),"refer_eva/".getMobileStoId());
//                    $slave_table = getApiData('wxd.pos.evaluatelist.add', $user_info['api_token'], array($slave_main));
//                    mlog("3条件:".json_encode($slave_table),"refer_eva/".getMobileStoId());
//                    $slave_table = json_decode($slave_table, 'true');
//
//
//                } else {
//                    return json(["code" => -1, "msg" => "系统繁忙,请稍后"]);//(主)未成功添加信息
//                }
            }
        }
        return $this->fetch();
    }

    /** 评论列表**/
    public
    function evaluate_list()
    {
        $IsEvaluate = I('IsEvaluate');
        $user_info = M('users')->alias('a')
            ->join('store b', 'a.store_id=b.store_id', 'left')
            ->field('a.*,b.api_token,b.store_logo,b.store_name')
            ->where("a.user_id", $this->user_id)->find();//调取api_token,商家LOGO,商家名称
        if ($user_info) {
            $mapuser['VIPNo'] = $user_info['erpvipno'];//token验证
            //加载订单信息
            if ($IsEvaluate != '') {
                $where = array(
                    'VIPNo' => $user_info['erpvipno'],
                    'IsEvaluate' => $IsEvaluate
                );
            } else {
                $where = array(
                    'VIPNo' => $user_info['erpvipno'],
                );
            }
            mlog("1条件:" . json_encode($where), "evaluate_list/" . getMobileStoId());
            //查询用户历史订单
            $user_rs = getApiData('wxd.pos.vealuateform.list', $user_info['api_token'], null, $where, '', '', 'BillDate desc ,IsEvaluate asc');
            mlog("2返回:" . $user_rs, "evaluate_list/" . getMobileStoId());

            $user_rs = json_decode($user_rs, 'true');
            if ($user_rs && $user_rs['code'] == 1 && $user_rs['count'] && $user_rs['count'] = 1) {
                $order_num = $user_rs['data'];
                $order_info = $user_rs['data'];
                $this->assign('order_info', $order_info);//渲染订单信息
                $this->assign('store_logo', $user_info['store_logo']);//渲染店铺头像
//              $this->assign('goods_name', $goods_name);//渲染商品名称以及数量
            } else {
                $order_null = '1';
                $this->assign('order_null', $order_null);//暂无订单
            }

        }
        return $this->fetch();
    }

    /** 评论列表调取商品信息**/
    public
    function goods_list()
    {
        $POSNo = I('POSNo');
        $user_info = M('users')->alias('a')
            ->join('store b', 'a.store_id=b.store_id', 'left')
            ->field('a.*,b.api_token,b.store_logo,b.store_name')
            ->where("a.user_id", $this->user_id)->find();//调取api_token,商家LOGO,商家名称
        if ($user_info) {
            //调取一条订单的所有商品信息
            $where = array(
                'POSNo' => $POSNo,
            );
            $goods_info = getApiData('wxd.pos.vposdetail.list', $user_info['api_token'], null, $where);
            $goods_info = json_decode($goods_info, 'true');
            if ($goods_info && $goods_info['code'] == 1 && $goods_info['count'] && $goods_info['count'] > 0) {
//                $goods_info = $goods_info['data'];
                return json($goods_info);
            }
        }
        return $this->fetch();
    }

    /**评价其他订单**/
    public
    function backlist()
    {
        $user_info = M('users')->alias('a')
            ->join('store b', 'a.store_id=b.store_id', 'left')
            ->field('a.*,b.api_token,b.store_logo,b.store_name')
            ->where("a.user_id", $this->user_id)->find();//调取api_token,商家LOGO,商家名称
        if ($user_info) {
            $mapuser['VIPNo'] = $user_info['erpvipno'];//token验证
            $where = array(
                'VIPNo' => $user_info['erpvipno'],
                'IsEvaluate' => 0
            );
            $user_rs = getApiData('wxd.pos.vealuateform.list', $user_info['api_token'], null, $where, '', '', 'BillDate desc ,IsEvaluate asc');
            $user_rs = json_decode($user_rs, 'true');
            if ($user_rs && $user_rs['code'] == 1 && $user_rs['count'] && $user_rs['count'] == 1) {
                $only_order = $user_rs['data']['0']['POSNo'];
                return json(["code" => 1, "msg" => "只剩下一条订单", 'data' => $only_order]);
            } else if ($user_rs && $user_rs['code'] == 1 && $user_rs['count'] && $user_rs['count'] > 1) {
                return json(["code" => 2, "msg" => "还有多条订单"]);
            } else {
                $order_null = 1;
                return json(["code" => 3, "msg" => "没有订单"]);
            }
        }
    }

}