Member.php 11.0 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/23
 * Time: 18:02
 */

namespace app\home\controller;


use app\common\controller\WechatBase;
use think\Db;

class Member extends WechatBase
{
    protected $user_id;
    function _initialize()
    {
        parent::_initialize();
        //判断是否授权
        $user_id = get_current_user_id();
        if(empty($user_id)){
            $this->redirect('user/authorization_view');
        }
        $this->user_id = $user_id;
    }

    /**
     * 个人中心
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index(){
        $user = Db::name('user')->where(['id'=>$this->user_id])->find();
        if(empty($user)){
            $this->error('查无此人');
        }
        //转换为人民币
        $exp_ratio = Db::name('exp_ratio')->where(['id'=>1])->find();
        $user['rmb'] = round($user['exp']*$exp_ratio['ratio'],2);
        //今日获得的红包券
        $today_exp = Db::name('user_exp_log')->where(['user_id'=>$this->user_id,['type'=>['in',[4,5]]]])->sum('exp');
        $user['today_exp'] = $today_exp;
        $this->assign('user',$user);
        $this->assign('title','个人中心');
        return $this->fetch();
    }

    /**
     * 个人信息
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function my_information(){
        $user = Db::name('user')->where(['id'=>$this->user_id])->find();
        if(empty($user)){
            $this->error('404');
        }
        return $this->fetch();
    }

    /**
     * 更新个人信息
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function update_information(){
        $param = $this->request->param();
        $validate = new \think\Validate([
            'user_id' => 'require',
            'nickname' => 'require',
            'avatar' => 'require',
            'gender' => 'require',
            'year' => 'require',
            'day' => 'require',
            'province' => 'require',
            'city' => 'require',
            'county' => 'require',
        ]);
        $validate->message([
            'user_id' => 'user_id参数错误',
            'nickname' => 'nickname参数错误',
            'avatar.require' => 'avatar参数错误',
            'gender.require' => 'gender参数错误',
            'year' => 'year参数错误',
            'day' => 'day参数错误',
            'province' => 'province参数错误',
            'city' => 'city参数错误',
            'county' => 'county参数错误',
        ]);
        if (!$validate->check($param)) {
            $this->error($validate->getError());
        }
        $user = Db::name('user')->where(['id'=>$param['user_id']])->find();
        $param['birthday'] = "$param[year]-$param[day]";
        unset($param['year']);
        unset($param['day']);
        Db::name('user')->where(['id'=>$param['user_id']])->update($param);
        $this->success('SUCCESS');
    }

    /**
     * 我的钱包
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function my_wallet(){
        $user = Db::name('user')->where(['id'=>$this->user_id])->find();
        if(empty($user)){
            $this->error('查无此人');
        }
        //转换为人民币
        $exp_ratio = Db::name('exp_ratio')->where(['id'=>1])->find();
        $user['rmb'] = round($user['exp']*$exp_ratio['ratio'],2);
        $this->assign('user',$user);
        $this->assign('title','我的钱包');
        return $this->fetch();
    }

    /**
     * 提现页面
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function my_withdraw(){
        $user = Db::name('user')->where(['id'=>$this->user_id])->find();
        if(empty($user)){
            $this->error('查无此人');
        }
        //转换为人民币
        $exp_ratio = Db::name('exp_ratio')->where(['id'=>1])->find();
        $user['rmb'] = round($user['exp']*$exp_ratio['ratio'],2);
        $this->assign('user',$user);
        $this->assign('exp_ratio',$exp_ratio);
        $this->assign('title','提现');
        return $this->fetch();
    }

    /**
     * 提交提现申请
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function withdraw(){
        $user_id = $this->request->param('user_id',0,'intval');
        $exp = $this->request->param('exp',0,'intval');
        if(empty($user_id) || empty($exp)){
            $this->error('404');
        }
        $user = Db::name('user')->where(['id'=>$user_id])->find();
        if($user['exp'] < $exp){
            $this->error("您的红包券不足$exp");
        }
        //判断是否完善提现信息
        if(empty($user['name']) || empty($user['card']) || empty($user['mobile'])){
            $this->error('请完善提现信息','');
        }
        $arr['user_id'] = $user_id;
        $arr['before_exp'] = $user['exp'];
        $arr['exp'] = $exp;
        $arr['after_exp'] = $user['exp'] - $exp;
        $arr['type'] = 1;
        $arr['createtime'] = time();
        $result = Db::name('user_exp_log')->insert();
        if(empty($result)){
            $this->error('sql执行失败');
        }
        $this->success('SUCCESS');
    }

    /**
     * 完善提现信息页面
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function withdraw_information(){
        $user = Db::name('user')->where(['id'=>$this->user_id])->find();
        $this->assign('user',$user);
        $this->assign('title','完善提现信息');
        return $this->fetch();
    }

    /**
     * 更新提现信息
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function update_withdraw_information(){
        $param = $this->request->param();
        $validate = new \think\Validate([
            'user_id' => 'require',
            'name' => 'require',
            'card' => 'require',
            'mobile' => 'require',
        ]);
        $validate->message([
            'user_id' => 'user_id参数错误',
            'name' => 'name参数错误',
            'card' => 'card参数错误',
            'mobile' => 'mobile参数错误',
        ]);
        if (!$validate->check($param)) {
            $this->error($validate->getError());
        }
        Db::name('user')->where(['id'=>$param['user_id']])->update($param);
        $this->success('SUCCESS');
    }

    /**
     * 收益记录页面
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function earnings_log(){
        $data = Db::name('user_exp_log')->where(['type'=>['in',[4,5]]])->order('id desc')->page(1,5)->select();
        foreach($data as $key => $vo){
            $data[$key]['createtime'] = date('Y年m月d日 H:i');
        }
        $this->assign('data',$data);
        $this->assign('title','收益记录');
        return $this->fetch();
    }

    /**
     * 提现记录页面
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function withdraw_log(){
        $data = Db::name('user_exp_log')->where(['type'=>['in',[1,2,3]]])->order('id desc')->page(1,5)->select();
        foreach($data as $key => $vo){
            $data[$key]['createtime'] = date('Y年m月d日 H:i');
        }
        $this->assign('data',$data);
        $this->assign('title','提现记录');
        return $this->fetch();
    }

    /**
     * 商家入驻页面
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function merchant_enter_view(){
        $data = Db::name('page')->where(['id'=>1])->find();
        $this->assign('data',$data);
        $this->assign('title','商家入驻');
        return $this->fetch();
    }

    /**
     * 商家入驻表单页面
     * @return mixed
     */
    public function merchant_enter_form(){
        $data = Db::name('merchant_audit')->where(['user_id'=>$this->user_id])->find();
        $this->assign('user_id',$this->user_id);
        $this->assign('data',$data);
        $this->assign('title','商家入驻');
        return $this->fetch();
    }

    /**
     * 提交申请
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function merchant_enter_submit(){
        $param = $this->request->param();
        $validate = new \think\Validate([
            'user_id' => 'require',
            'merchant_title' => 'require',
            'merchant_name' => 'require',
            'merchant_mobile' => 'require',
            'business_images' => 'require',
        ]);
        $validate->message([
            'user_id' => 'user_id参数错误',
            'merchant_title' => 'merchant_title参数错误',
            'merchant_name' => 'merchant_name参数错误',
            'merchant_mobile' => 'merchant_mobile参数错误',
            'business_images' => 'business_images参数错误',
        ]);
        if (!$validate->check($param)) {
            $this->error($validate->getError());
        }
        $data = Db::name('merchant_audit')->where(['user_id'=>$this->user_id])->find();
        if(!empty($data)){
            if($data['expiration_time'] > time() && $data['status'] == 3){
                $this->error("请于$data[expiration_time]后再来提交");
            }else if($data['status'] == 2){
                $this->error("您已经是商家了");
            }else if($data['status'] == 1){
                $this->error('正在审核中');
            }else{
                $param['status'] = 1;
                Db::name('merchant_audit')->where(['user_id'=>$param['user_id']])->update($param);
            }
        }else{
            $param['createtime'] = time();
            Db::name('merchant_audit')->insert($param);
        }
        $this->success('success');
    }
}