CenterController.php 13.0 KB
<?php

namespace app\user\controller;
use cmf\controller\HomeBaseController;
use think\Db;
use app\order\model\OrderModel;

class CenterController extends HomeBaseController
{

    /**
     * 个人中心首页
     */
    public function index()
    {
        $userInfo = Db::name('user')->field('user_nickname,avatar,balance')->where(['id' => session('user.id')])->find();
        return $this->fetch('index', [
            'data' => $userInfo
        ]);
    }

    /**
     * 个人信息
     */
    public function userInfo()
    {
        $userInfo = Db::name('user')->field('user_nickname,avatar,sex,mobile')->where(['id' => session('user.id')])->find();
        return $this->fetch('info', [
            'data' => $userInfo
        ]);
    }

    public function editName()
    {
        $request = request();
        if ($request->isAjax()) {
            if (Db::name('user')->update(['id' => session('user.id'), 'user_nickname' => $request->param('user_nickname')])) {
                $this->success('编辑成功', url('user/Center/userInfo'));
            }
        } else {
            return $this->fetch('edit_name', [
                'nickname' => Db::name('user')->where(['id' => session('user.id')])->value('user_nickname')
            ]);
        }
    }

    public function editPhone()
    {
        return $this->fetch('edit_phone');
    }

    public function editPhone2()
    {
        return $this->fetch('edit_phone2');
    }

    public function aboutUs()
    {
        $request = request();
        if ($request->isAjax()) {
            $data = [
                'uid' => session('user.id'),
                'content' => $request->param('content'),
                'create_time' => time()
            ];
            if (Db::name('zj_msg')->insert($data)) {
                $this->success('感谢您的反馈', url('user/Center/userInfo'));
            }
        } else {
            return $this->fetch('about_us', [
                'data' => html_entity_decode(Db::name('zj_system')->value('about')),
                'mobile' => Db::name('zj_system')->value('mobile')
            ]);
        }
    }

    /**
     * 地址管理
     */
    public function addressManage()
    {
        $address = Db::name('zj_user_place')
            ->field('id,name,province,city,county,mobile,is_sta,place')
            ->where(['uid' => session('user.id')])
            ->select()
            ->toArray();
        return $this->fetch('address_manage', [
            'data' => $address
        ]);
    }

    public function addAddress()
    {
        $request = request();
        if ($request->isAjax()) {
            $post = $request->param();
            $code = $post['code'];
            $label = $post['label'];
            $arr1 = explode(',', $code);
            $post['province_id'] = $arr1[0];
            $post['city_id'] = $arr1[1];
            $post['county_id'] = $arr1[2];
            $arr2 = explode(' ', $label);
            $post['province'] = $arr2[0];
            $post['city'] = $arr2[1];
            $post['county'] = $arr2[2];
            unset($post['code']);
            unset($post['label']);
            $post['uid'] = session('user.id');
            if (Db::name('zj_user_place')->insert($post)) {
                $this->success('', url('user/Center/addressManage'), true);
            }
        } else {
            return $this->fetch('add_address');
        }
    }

    public function delAddress()
    {
        if (Db::name('zj_user_place')->delete(request()->param('id'))) {
            $this->success('', '', true);
        }
    }

    public function editAddress()
    {
        $request = request();
        if ($request->isAjax()) {
            $post = $request->param();
            $code = $post['code'];
            $label = $post['label'];
            $arr1 = explode(',', $code);
            $post['province_id'] = $arr1[0];
            $post['city_id'] = $arr1[1];
            $post['county_id'] = $arr1[2];
            $arr2 = explode(' ', $label);
            $post['province'] = $arr2[0];
            $post['city'] = $arr2[1];
            $post['county'] = $arr2[2];
            unset($post['code']);
            unset($post['label']);
            $post['uid'] = session('user.id');
            if (Db::name('zj_user_place')->update($post)) {
                $this->success('', url('user/Center/addressManage'), true);
            }
        } else {
            return $this->fetch('edit_address', [
                'data' => Db::name('zj_user_place')
                    ->field('id,name,mobile,place,province_id,city_id,county_id,province,city,county')
                    ->where(['id' => $request->param('id')])
                    ->find()
            ]);
        }
    }

    public function setDefault()
    {
        Db::name('zj_user_place')->where(['uid' => session('user.id')])->update(['is_sta' => 1]);
        Db::name('zj_user_place')->where(['id' => request()->param('id')])->update(['is_sta' => request()->param('is_sta')]);
    }

    public function myBalance()
    {
        return $this->fetch('my_balance', [
            'balance' => Db::name('user')->where(['id' => session('user.id')])->value('balance')
        ]);
    }

    public function upload()
    {
        upload();
    }

    public function upload2()
    {
        $this->success('', '', upload2());
    }

    public function orderList()
    {
        $order = new OrderModel;
        require_once EXTEND_PATH . '/WeChatCommon.php';
        $wx = new \WeChatCommon();
        $data = $order->getAllOrders();
        return $this->fetch('order_list', [
            'data' => $data,
            'js_sdk' => $wx->js_sdk()
        ]);
    }

    public function evaluate()
    {
        $request = request();
        $oid = $request->param('oid');
        if ($request->isAjax()) {
            Db::startTrans();
            if (Db::name('zj_order')->where(['id' => $oid])->value('is_evaluate') == 0) {
                $res = Db::name('zj_order_goods')->field('gid')->where(['oid' => $oid])->select();
                $data = [];
                foreach ($res as $key => $item) {
                    $data[$key] = [
                        'oid' => $oid,
                        'gid' => $item['gid'],
                        'uid' => session('user.id'),
                        'is_sta' => 1,
                        'is_shove' => 1,
                        'create_time' => time(),
                        'rank' => $request->param('rank'),
                        'more' => substr($request->param('more'), 0, strlen($request->param('more')) - 1),
                        'content' => $request->param('content')
                    ];
                }
                if (Db::name('zj_evaluate')->insertAll($data)) {
                    //变为已评价
                    $evaluate = [
                        'id' => $oid,
                        'is_evaluate' => 1
                    ];
                    if (Db::name('zj_order')->update($evaluate)) {
                        Db::commit();
                        $this->success('感谢您的支持!', url('user/Center/orderList'), true);
                    } else {
                        Db::rollback();
                        $this->success('评价失败', '', false);
                    }
                }
            } else {
                Db::rollback();
                $this->success('您已评价过此订单啦', '', false);
            }
        } else {
            return $this->fetch('', [
                'oid' => $oid
            ]);
        }
    }

    public function cancelOrder()
    {
        $request = request();
        if ($request->isAjax()) {
            $oid = $request->param('oid');
            //如果是组合支付,需要吧之前支付的积分一并返还
            $info = Db::name('zj_order')->field('pay_type,whole_num')->where(['id' => $oid])->find();
            Db::startTrans();
            if (Db::name('zj_order')->where(['id' => $oid])->value('step') == 1) {
                if (Db::name('zj_order')->delete($oid)) {
                    if (Db::name('zj_order_goods')->where(['oid' => $oid])->delete()) {
                        if ($info['pay_type'] == 2) {
                            Db::name('user')->where(['id' => session('user.id')])->setInc('balance', $info['whole_num']);
                        }
                        Db::commit();
                        $this->success('订单已取消', '', true);
                    } else {
                        Db::rollback();
                        $this->success('取消失败', '', false);
                    }
                } else {
                    Db::rollback();
                    $this->success('取消失败', '', false);
                }
            } else {
                $this->success('该订单禁止取消', '', false);
            }
        }
    }

    //填写退货申请
    public function refund()
    {
        $request = request();
        $oid = $request->param('oid');
        if ($request->isAjax()) {
            $data = [
                'id' => $request->param('oid'),
                'cause' => $request->param('cause'),
                'step' => 6
            ];
            if (Db::name('zj_order')->update($data)) {
                $this->success('请等待审核', url('user/Center/orderList'), true);
            } else {
                $this->success('提交失败', '', false);
            }
        } else {
            $order_sn = Db::name('zj_order')->where(['id' => $oid])->value('order_num');
            $list = Db::name('zj_order_goods')->alias('o')
                ->field('g.name,g.intro,o.price,g.thumb,o.num')
                ->join('zj_goods g', 'g.id=o.gid')
                ->where(['o.oid' => $oid])
                ->select();
            foreach ($list as $k => $v) {
                $v['thumb'] = cmf_get_image_url($v['thumb']);
                $list[$k] = $v;
            }
            return $this->fetch('refund', [
                'order_sn' => $order_sn,
                'list' => $list,
                'oid' => $oid
            ]);
        }
    }

    //填写退款申请
    public function refundPay()
    {
        $request = request();
        $oid = $request->param('oid');
        if ($request->isAjax()) {
            $data = [
                'id' => $request->param('oid'),
                'company' => $request->param('company'),
                'com_order' => $request->param('com_order'),
                'step' => 8
            ];
            if (Db::name('zj_order')->update($data)) {
                $this->success('请等待审核', url('user/Center/orderList'), true);
            } else {
                $this->success('提交失败', '', false);
            }
        } else {
            $order = Db::name('zj_order')->field('order_num,cause')->where(['id' => $oid])->find();
            $list = Db::name('zj_order_goods')->alias('o')
                ->field('g.name,g.intro,o.price,g.thumb,o.num')
                ->join('zj_goods g', 'g.id=o.gid')
                ->where(['o.oid' => $oid])
                ->select();
            foreach ($list as $k => $v) {
                $v['thumb'] = cmf_get_image_url($v['thumb']);
                $list[$k] = $v;
            }
            return $this->fetch('refund_pay', [
                'order_sn' => $order['order_num'],
                'cause' => $order['cause'],
                'list' => $list,
                'oid' => $oid
            ]);
        }
    }

    //物流跟踪
    public function tracking()
    {
        $oid = request()->param('oid');
        $info = Db::name('zj_order')->alias('o')
            ->field('k.code,o.order_num,o.kd_num,k.name')
            ->join('zj_kd k', 'o.kid=k.id')
            ->where(['o.id' => $oid])
            ->find();
        require EXTEND_PATH . '/KdApiSubscribeDemo.php';
        $requestData = [
            "OrderCode" => $info['order_num'],
            "ShipperCode" => $info['code'],
            "LogisticCode" => $info['kd_num']
        ];
        $logisticResult = json_decode(orderTracesSubByJson(json_encode($requestData)), true);
        return $this->fetch('', [
            'kd_name' => $info['name'],
            'tracking' => $logisticResult
        ]);
    }

    //确认收货
    public function confirm()
    {
        $request = request();
        if ($request->isAjax()) {
            $data = [
                'id' => $request->param('oid'),
                'step' => 5,
                'end_time' => time()
            ];
            if (Db::name('zj_order')->update($data)) {
                $this->success('', '', true);
            }
        }
    }

    public function integralLog()
    {
        $type = request()->param('type');
        if ($type == 1) {
            $title = '积分获取记录';
            $where['type'] = ['in', "1,3"];
        } else {
            $title = '积分使用记录';
            $where['type'] = ['in', "2,4"];
        }
        $where['uid'] = ['eq', session('user.id')];
        $log = Db::name('zj_integral_log')->where($where)->order('create_time DESC')->select();
        return $this->fetch('integral_log', [
            'log' => $log,
            'title' => $title
        ]);
    }

}