Member.php 11.7 KB
<?php


namespace app\index\controller;

use app\admin\model\Faq;
use app\admin\model\Feedback;
use app\admin\model\Vipdesc;
use app\common\controller\Frontend;
use app\index\model\Goodscollect;
use app\index\model\OrderInfo;
use app\index\model\Price;
use app\index\model\Store;
use app\index\model\Storecollect;
use think\Db;
use think\Validate;

/**
 * 个人中心
 * Class Member
 * @package app\index\controller
 */
class Member extends Frontend
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->view->assign('is_search', 0);
        $this->view->assign('is_active', 0);
    }

    // 基本信息
    public function index()
    {
        $this->view->assign('is_nav', 1);
        return $this->view->fetch();
    }

    // 修改基本信息
    public function change_inform()
    {
        if($this->request->isAjax()) {
            $param = $this->request->param();
            $validate = new Validate([
                'nickname' => 'require',
                'mobile' => 'require|regex:^1\d{10}$|unique:user,mobile,'.$this->auth->id,
                'work_address' => 'require'
            ]);
            $validate->message([
                'nickname.require' => '请填写昵称',
                'mobile.require' => '请填写电话',
                'mobile.regex' => '电话格式错误',
                'mobile.unique' => '电话已存在',
                'work_address.require' => '请填写办公地址'
            ]);
            if (!$validate->check($param)) {
                $this->error($validate->getError());
            }
            $user = $this->auth->getUser();
            $user->nickname = $param['nickname'];
            $user->mobile = $param['mobile'];
            $user->work_address = $param['work_address'];
            $user->updatetime = time();
            $result = $user->save();
            if(!$result) {
                $this->error('修改失败');
            }
            $this->success('修改成功');
        }
    }

    // 我的订单
    public function order()
    {
        $this->view->assign('is_nav', 2);
        return $this->view->fetch();
    }

    // 订单列表
    public function order_list() {
        if($this->request->isAjax()) {
            $param = $this->request->param();
            $order_detail_model = new \app\index\model\OrderDetail();
            $order_info_model = new OrderInfo();
            $store_model = new Store();
            $where = [
                'user_id' => $this->auth->id
            ];
            if($param['status']) {
                $where['status'] = $param['status'];
            }
            $order = $order_detail_model
                ->where($where)
                ->order('createtime','DESC')
                ->paginate($param['pageNum'],false,['page'=>$param['page']]);
            $list = $order->items();
            $status = [
                '1' => '待支付',
                '2' => '待发货',
                '3' => '待收货',
                '4' => '待评价',
                '5' => '售后',
                '6' => '已完成',
            ];
            foreach ($list as $k=>$v) {
                $order[$k]['store_name'] = $store_model->where('id',$v['store_id'])->value('name');
                $order[$k]['goods'] = $order_info_model->where('order_id',$v['id'])->select();
                $order[$k]['operate'] = $this->get_operate($v['status']);
                $order[$k]['status'] = $status[$v['status']];
            }
            $return = [
                'list' => $list,
                'totalSize' => $order->total(),
                'current_page' => $order->currentPage(),
                'totalPage' => $order->lastPage(),
                'pageNum' => $param['pageNum'],
            ];
            $this->success('成功','',$return);
        }
    }

    private function get_operate($status) {
        $operate = '';
        switch ($status) {
            case '1':
                $operate = '<button type="button" class="btn operateBtn operateBtnGreen topay">付款</button>' .
                    '           <button type="button" class="btn operateBtn operateBtnGrey detail">查看订单</button>' .
                    '           <button type="button" class="btn operateBtn operateBtnGrey cancel" >取消订单</button>';
                break;
            case '2':
                $operate = '<button type="button" class="btn operateBtn operateBtnGreen refund">退款</button>' .
                    '           <button type="button" class="btn operateBtn operateBtnGrey detail">查看订单</button>';
                break;
            case '3':
                $operate = '<button type="button" class="btn operateBtn operateBtnGreen topay">付款</button>' .
                    '           <button type="button" class="btn operateBtn operateBtnGrey detail">查看订单</button>' .
                    '           <button type="button" class="btn operateBtn operateBtnGrey cancel" >取消订单</button>';
                break;
            case '4':
                $operate = '<button type="button" class="btn operateBtn operateBtnGreen confirm">确认收货</button>' .
                    '           <button type="button" class="btn operateBtn operateBtnGreen refund">退款</button>' .
                    '           <button type="button" class="btn operateBtn operateBtnGrey detail" >查看订单</button>';
                break;
            case '5':
                $operate = '<button type="button" class="btn operateBtn operateBtnGreen comment">去评价</button>' .
                    '           <button type="button" class="btn operateBtn operateBtnGrey detail">查看订单</button>';
                break;
            case '6':
                $operate = '<button type="button" class="btn operateBtn operateBtnGrey delete">删除</button>';
                break;
            case '7':
                $operate = '<button type="button" class="btn operateBtn operateBtnGrey detail">查看订单</button>';
                break;
            case '8':
                $operate = '<button type="button" class="btn operateBtn operateBtnGrey detail">查看订单</button>';
                break;
        }
        return $operate;
    }

    // 我的会员
    public function member()
    {
        $store_model = new Store();
        $store = $store_model->where('user_id',$this->auth->id)->find();
        $is_vip = 0;
        if($store) {
            if($store['svip_passtime'] > time()) {
                $is_vip = 1;
                $passtime = date('Y-m-d');
            }
        }
        $this->view->assign('is_nav', 3);
        return $this->view->fetch();
    }

    public function vip() {
        $price_model = new Price();
        $vipdesc_model = new Vipdesc();
        $price = $price_model->where('id',1)->find();
        $vipdesc = $vipdesc_model->where('id',1)->find();
        $this->view->assign('price',$price);
        $this->view->assign('vipdesc',$vipdesc);
        return $this->view->fetch();
    }

    public function svip() {
        $price_model = new Price();
        $vipdesc_model = new Vipdesc();
        $price = $price_model->where('id',1)->find();
        $vipdesc = $vipdesc_model->where('id',1)->find();
        $this->view->assign('price',$price);
        $this->view->assign('vipdesc',$vipdesc);
        return $this->view->fetch();
    }

    // 我的收藏
    public function favorite()
    {
        $this->view->assign('is_nav', 4);
        return $this->view->fetch();
    }

    // 收藏的商品
    public function goodscollect() {
        if($this->request->isAjax()) {
            $param = $this->request->param();
            $goodscollect_model = new Goodscollect();
            $store_model = new Store();
            $where = [
                'goodscollect.user_id' => $this->auth->id,
                'goods.status' => '1'
            ];
            $goodscollect = $goodscollect_model
                ->with(['goods'])
                ->where($where)
                ->paginate($param['pageNum'],false,['page'=>$param['page']]);
            $collect_list = $goodscollect->items();
            $list = [];
            foreach ($collect_list as $k=>$v) {
                $store = $store_model->field('name,property')->where('id',$v['goods']['store_id'])->find();
                $list[] = [
                    'href' => url('goods/detail',['goods_id'=>$v['goods']['id']]),
                    'goods_id' => $v['goods']['id'],
                    'goodsname' => $v['goods']['goodsname'],
                    'thumbnail' => $v['goods']['thumbnail'],
                    'store_name' => $store['name'],
                    'price' => $v['goods']['price'],
                    'property' => explode(',',$store['property']),
                ];
            }
            $return = [
                'list' => $list,
                'totalSize' => $goodscollect->total(),
                'current_page' => $goodscollect->currentPage(),
                'totalPage' => $goodscollect->lastPage(),
                'pageNum' => $param['pageNum'],
            ];
            $this->success('成功','',$return);
        }
    }

    // 收藏的店铺
    public function storecollect() {
        if($this->request->isAjax()) {
            $param = $this->request->param();
            $storecollect_model = new Storecollect();
            $where = [
                'storecollect.user_id' => $this->auth->id
            ];
            $storecollect = $storecollect_model
                ->with(['store'])
                ->where($where)
                ->paginate($param['pageNum'],false,['page'=>$param['page']]);
            $collect_list = $storecollect->items();
            $list = [];
            foreach ($collect_list as $k=>$v) {
                $list[] = [
                    'href' => url('store/detail',['store_id'=>$v['store']['id']]),
                    'store_id' => $v['goods']['id'],
                    'name' => $v['store']['name']
                ];
            }
            $return = [
                'list' => $list,
                'totalSize' => $storecollect->total(),
                'current_page' => $storecollect->currentPage(),
                'totalPage' => $storecollect->lastPage(),
                'pageNum' => $param['pageNum'],
            ];
            $this->success('成功','',$return);
        }
    }

    // 常见问题
    public function question()
    {
        $faq_model = new Faq();
        $faq = $faq_model->where('id',1)->find();
        $this->view->assign('faq',$faq);
        $this->view->assign('is_nav', 5);
        return $this->view->fetch();
    }

    // 问题反馈
    public function feedback()
    {
        $this->view->assign('is_nav', 6);
        return $this->view->fetch();
    }

    // 问题反馈提交
    public function feedback_submit()
    {
        if($this->request->isAjax()) {
            $param = $this->request->param();
            $validate = new Validate([
                'name' => 'require',
                'content' => 'require',
                'phone' => 'require|regex:^1\d{10}$'
            ]);
            $validate->message([
                'nickname.require' => '请填写昵称',
                'content.require' => '请填写留言内容',
                'phone.require' => '请填写手机',
                'phone.regex' => '手机格式错误'
            ]);
            if (!$validate->check($param)) {
                $this->error($validate->getError());
            }
            $feedback_model = new Feedback();
            $param['user_id'] = $this->auth->id;
            $result = $feedback_model->save($param);
            if(!$result) {
                $this->error('反馈提交失败');
            }
            $this->success('反馈提交成功');
        }
    }
}