CenterController.php 9.6 KB
<?php
/**
 * Created by PhpStorm.
 * User: yhbr
 * Date: 2018/9/5
 * Time: 10:48
 */

namespace app\user\controller;
use app\coupons\model\CouponModel;
use cmf\controller\HomeBaseController;
use Think\Db;
use app\coupons\model\DiscountCouponModel;
use app\order\model\OrderModel;

/**
 * @title 个人中心模块
 */
class CenterController extends HomeBaseController
{

    function _initialize()
    {
        if (empty(session('user.id'))) {
            echo json_encode(['msg' => '登陆失败', 'code' => 40001]);
            exit();
        }
    }

    /**
     * @title 个人中心首页
     * @description 默认访问接口
     * @author sᴏᴜʟ ᴏғ ᴄɪɴᴅᴇʀ
     * @url /user/Center/index
     * @method POST
     */
    public function index()
    {
        $userInfo = Db::name('user')->field('user_nickname,avatar')->where(['id' => session('user.id')])->find();
        if (request()->isPost()) {
            echo json_encode(['user_info' => $userInfo, 'code' => 20000]);
            exit();
        } else {
            return $this->fetch(':index', [
                'user_info' => $userInfo
            ]);
        }
    }

    /**
     * @title 我的资料
     * @description 默认访问接口(post方式更改用户信息需要填写参数,put模板渲染用户信息)
     * @author sᴏᴜʟ ᴏғ ᴄɪɴᴅᴇʀ
     * @url /user/Center/myInfo
     * @method PUT
     *
     * @param name:realname  type:char require:1 default:李涵 desc:真实姓名
     * @param name:tel  type:char require:1 default:15122554644 desc:手机号
     * @param name:wechat  type:char require:1 default:xxxxxx desc:微信号
     * @param name:identity  type:char require:1 default:12010319960501291X desc:身份证号
     *
     * @return version:版本号
     * @return code:错误码
     */
    public function myInfo()
    {
        $request = request();
        if ($request->isPost()) {
            $post = $request->param();
            if ($post['realname'] == null) {
                echo json_encode(['msg' => '请填写真实姓名', 'code' => 40000]);
                exit();
            }
            if ($post['tel'] == null) {
                echo json_encode(['msg' => '请填写手机号', 'code' => 40000]);
                exit();
            }
            if ($post['wechat'] == null) {
                echo json_encode(['msg' => '请填写微信号', 'code' => 40000]);
                exit();
            }
            if ($post['identity'] == null) {
                echo json_encode(['msg' => '请填写身份证信息', 'code' => 40000]);
                exit();
            }
            $post['id'] = session('user.id');
            if (Db::name('user')->update($post)) {
                echo json_encode(['data' => '更改成功', 'code' => 20000]);
                exit();
            } else {
                echo json_encode(['data' => '您未做出任何修改', 'code' => 40000]);
                exit();
            }

        } elseif ($request->isPut()) {
            $userInfo = Db::name('user')->field('avatar,user_nickname,realname,tel,wechat,identity')
                ->where(['id' => session('user.id')])
                ->find();
            echo json_encode(['data' => $userInfo, 'code' => 20000]);
            exit();
        } else {
            return $this->fetch(':edit', [
                'info' => Db::name('user')->field('avatar,user_nickname,realname,tel,wechat,identity')
                    ->where(['id' => session('user.id')])
                    ->find()
            ]);
        }
    }

    /**
     * @title 我的优惠券
     * @description 默认访问接口
     * @author sᴏᴜʟ ᴏғ ᴄɪɴᴅᴇʀ
     * @url /user/Center/discountCoupon
     * @method POST
     *
     * @return version:版本号
     * @return code:错误码
     * @return id:优惠券id
     * @return reduce:减
     * @return discount_coupon_name:优惠券名称
     * @return deadline:截止日期
     */
    public function discountCoupon()
    {
        $request = request();
        $user_id = session('user.id');
        $model = new CouponModel;
        $data = $model->getAllDiscountCouponByUniqueIdAndByStatus($user_id);
        if ($request->isPost()) {
            if (empty($data)) {
                echo json_encode(['msg' => '暂无数据', 'code' => 40000]);
                exit();
            } else {
                echo json_encode(['data' => $data, 'code' => 20000]);
                exit();
            }
        } else {
            return $this->fetch(':coupon', [
                'data' => $data
            ]);
        }
    }

    /**
     * @title 我要充值
     * @description 默认访问接口
     * @author sᴏᴜʟ ᴏғ ᴄɪɴᴅᴇʀ
     * @url /user/Center/charge
     * @method POST
     *
     * @param name:is_bonus  type:int require:1 default:0 desc:0普通充值1活动专享
     *
     * @return version:版本号
     * @return code:错误码
     * @return id:充值卡id
     * @return denomination:面值
     * @return bonus:赠送金额
     */
    public function charge()
    {
        $is_bonus = request()->param('is_bonus');
        $where['is_on_sale'] = ['eq', 1];
        $is_bonus = (empty($is_bonus)) ? 0 : 1;
        $where['is_bonus'] = ['eq', $is_bonus];
        $field = 'id,denomination';
        if ($is_bonus == 1) {
            $field .= ',bonus';
        }
        $res = Db::name('charge')->field($field)->where($where)->select()->toArray();
        if (request()->isPost()) {
            echo json_encode(['data' => $res, 'code' => 20000]);
            exit();
        } else {
            if ($is_bonus == 0) {
                $view = ':charge';
            } else {
                $view = ':charge2';
            }
            return $this->fetch($view, [
                'data' => $res
            ]);
        }
    }

    /**
     * @title 订单列表
     * @description 默认访问接口
     * @author sᴏᴜʟ ᴏғ ᴄɪɴᴅᴇʀ
     * @url /user/Center/orderList
     * @method POST
     *
     * @param name:status  type:int require:1 default: desc:订单状态
     * @param name:order_sn  type:char require:1 default:201809091314 desc:订单号
     * @return version:版本号
     * @return code:错误码
     */
    public function orderList()
    {
        $request = request();
        $order['status'] = $request->param('status');
        $order['order_sn'] = $request->param('order_sn');
        $model = new OrderModel;
        $list = $model->getOrderListByUserId($order['status'], $order['order_sn'], session('user.id'));
        if (!empty($list)) {
            echo json_encode(['order_list' => $list, 'code' => 20000]);
            exit();
        } else {
            echo json_encode(['msg' => '暂无数据', 'code' => 40000]);
            exit();
        }
    }

    /**
     * @title 订单详情
     * @description 默认访问接口
     * @author sᴏᴜʟ ᴏғ ᴄɪɴᴅᴇʀ
     * @url /user/Center/orderDetail
     * @method POST
     *
     * @param name:oid  type:int require:1 default: desc:订单id
     * @return version:版本号
     * @return code:错误码
     */
    public function orderDetail()
    {
        $request = request();
        //订单基本信息
        $oid = $request->param('oid');
        $info = Db::name('order_info')->alias('o')
            ->field('o.id as oid,o.order_sn,a.name,a.thumb,o.status,a.down_price,s.price')
            ->join('activity a', 'a.id=o.activity_id')
            ->join('activity_schedule s', 's.id=o.schedule_id')
            ->where(['o.id' => $oid])
            ->order('add_time DESC')
            ->find();
        $info['status_text'] = getOrderStatusText($info['status']);
        $info['count'] = Db::name('order_detail')->where(['oid' => $oid])->count();
        $escort = Db::name('order_detail')->alias('d')
            ->field('d.id,d.status,e.name,e.tel')
            ->join('escort e', 'e.id=d.escort_id')
            ->where(['oid' => $oid])
            ->select();
        foreach ($escort as $k => $v) {
            $v['count'] = 1;
            $v['down_price'] = $info['down_price'];
            $escort[$k] = $v;
        }
        $return = [
            'baseInfo' => $info,
            'escort' => $escort
        ];
        echo json_encode(['data' => $return, 'code' => 20000]);
        exit();
    }

    /**
     * @title 钱包记录
     * @description 默认访问接口
     * @author sᴏᴜʟ ᴏғ ᴄɪɴᴅᴇʀ
     * @url /user/Center/walletLog
     * @method POST
     */
    public function walletLog()
    {
        $balance = Db::name('user')->where(['id' => session('user.id')])->value('balance');
        $log = Db::name('my_wallet')->field('type,cost,create_time')->where(['user_id' => session('user.id')])->order('create_time DESC')->select()->toArray();
        foreach ($log as $k => $v) {
            $v['create_time'] = date('Y.m.d H:i:s', $v['create_time']);
            if ($v['type'] == 0) {
                $v['type_text'] = '账户消费';
            } elseif ($v['type'] == 1) {
                $v['type_text'] = '账户充值';
            } else {
                $v['type_text'] = '退款订单';
            }
            $log[$k] = $v;
        }
        $return = [
            'balance' => $balance,
            'log' => $log
        ];
        if (request()->isPost()) {
            echo json_encode(['data' => $return, 'code' => 20000]);
            exit();
        } else {
            return $this->fetch(':log', [
                'data' => $return
            ]);
        }
    }

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

}