<?php
// +----------------------------------------------------------------------
// | WindSNS 国内领先企业级社交开源SNS框架系统
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2028 http://windsns.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 常州微诺信息科技有限公司 http://www.weinuo.net
// +----------------------------------------------------------------------
// | E-mail: open@weinuo.net
// +----------------------------------------------------------------------
namespace app\index\controller;
use app\common\controller\Frontend;
use think\Config;
use think\Hook;
use think\Db;
use think\Request;
use think\Session;
class Windsnsrechargeable extends Frontend
{
    protected $layout = 'default';
    protected $noNeedLogin = ['login', 'register', 'third'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $auth = $this->auth;

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

        //监听注册登录注销的事件
        Hook::add('user_login_successed', function ($user) use ($auth) {
            $expire = input('post.keeplogin') ? 30 * 86400 : 0;
            Cookie::set('uid', $user->id, $expire);
            Cookie::set('token', $auth->getToken(), $expire);
        });
        Hook::add('user_register_successed', function ($user) use ($auth) {
            Cookie::set('uid', $user->id);
            Cookie::set('token', $auth->getToken());
        });
        Hook::add('user_delete_successed', function ($user) use ($auth) {
            Cookie::delete('uid');
            Cookie::delete('token');
        });
        Hook::add('user_logout_successed', function ($user) use ($auth) {
            Cookie::delete('uid');
            Cookie::delete('token');
        });
    }

    /**
     * 空的请求
     * @param $name
     * @return mixed
     */
    public function _empty($name)
    {
        $data = Hook::listen("user_request_empty", $name);
        foreach ($data as $index => $datum) {
        $this->view->assign($datum);
        }
        return $this->view->fetch('user/' . $name);
    }

	/**
	 * 协议
	 * @author   扣扣小子
	 * @blog     http://www.weinuo.net
	 * @version  0.0.1
	 * @datetime 2020-12-20
	 */
    public function index()
    {
    $this->view->assign('title', __('点卡充值-用户中心'));
    $windsns_userid = $this->auth->getUserinfo()['user_id'];
	return $this->view->fetch('windsns_rechargeable');

    }

    public function chongzhido()
    {
    $this->view->assign('title', __('点卡充值-用户中心'));
        $time = time();
		$paytimess = date('Y-m-d H:i:s',$time);
		$thistimes = strtotime($paytimess);
		$userid = $this->auth->getUserinfo()['user_id'];
		$windsns_getkahao =  input('post.windsnsrechargeablehao'); 
	    $windsns_getmima =  input('post.windsnsrechargeablemima'); 
		$sqkahao = Db::name('windsnsrechargeable')->where(['windsnsrechargeablehao'=>$windsns_getkahao])->find();
		$sqinfosid = Db::name('windsnsrechargeable')->where(['windsnsrechargeablehao'=>$windsns_getkahao,'windsnsrechargeablemima'=>$windsns_getmima,'windsnsrechargeablepaytype'=>1])->find();
		$cardid =  $sqinfosid['windsnsrechargeableid'];	
        $cardmoney =  $sqinfosid['windsnsrechargeablejine'];
        $cardtype =  $sqinfosid['windsnsrechargeabletype'];
          if(!$sqkahao){
          	return $this->view->fetch('windsns_rechargeabledono');
          }elseif(!$sqinfosid){
        	return $this->view->fetch('windsns_rechargeabledonoa');
        }else{
        	
        	if($cardtype == 1){
        		$usermoney = Db::name('user')->where('id', $userid)->setInc('money', $cardmoney);
        		$data['user_id'] = $userid;
        		$data['windsnsrechargeablepaytimes'] = $thistimes;
        		$data['windsnsrechargeablepaytype'] =  2; 
        		$xiugaizhuangtai = Db::name('windsnsrechargeable')->where('windsnsrechargeableid', $cardid)->update($data);// 
                return $this->view->fetch('windsns_rechargeabledook');
        		
        	}else{
        		$usermoney = Db::name('user')->where('id', $userid)->setInc('score', $cardmoney);
        		$data['user_id'] = $userid;
        		$data['windsnsrechargeablepaytimes'] = $thistimes;
        		$data['windsnsrechargeablepaytype'] =  2; 
        		$xiugaizhuangtai = Db::name('windsnsrechargeable')->where('windsnsrechargeableid', $cardid)->update($data);// 
        		return $this->view->fetch('windsns_rechargeabledook');
        	}

        }  
   

   

    }



}
