CenterController.php 4.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;

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

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

    /**
     * @title 我的资料
     * @description 默认访问接口(post方式更改用户信息需要填写参数,post以外是模板渲染用户信息)
     * @author sᴏᴜʟ ᴏғ ᴄɪɴᴅᴇʀ
     * @url /user/Center/myInfo
     * @method GET
     *
     * @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();
            }

        } else {
            $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();
        }
    }

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

    /**
     * @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();
        echo json_encode(['data' => $res, 'code' => 20000]);
        exit();
    }

}