<?php
namespace wstmart\app\model;
use think\Db;
use think\Loader;
/**
 * 双十一优惠券
 */
class EctElevenPay extends Base{
	/**
   * 返回获取双11优惠券所需的ect数量和价格
   * @return [type] [description]
   */
  public function getEctNum(){
    try{
      $url ='https://api.tokencan.net/exchange-open-api/open/api/get_ticker?symbol=ectusdt';
      $t_info = $this->getHTTPS($url);
      $t_info = json_decode($t_info);
      if(is_object($t_info)){
        $total_money = 11.11;//input('total_money');
        $t_usdt_price = $t_info->data->last;
        $now_doller = Db::name('doller')->where('id=1')->value('doller');
        $now_rmb = round($t_usdt_price*$now_doller,2);
        $to_ect_num = round($total_money/$now_rmb,2);
        $msg='';
        session('ect_rmb_price',$now_rmb);
        exit(jsonReturn($msg,1,['now_rmb'=>$now_rmb,'ect_num'=>$to_ect_num]));
      }
    }catch (\Exception $e) {
      exit(jsonReturn('获取价格失败!',-1));
    }
  }
  
  private function getHTTPS($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
  }


  /**
   * 支付双十一优惠券的ect价格
   */
  public function payElevenEct(){
    try{
      $userId = (int)session('WST_USER.userId');
      if(!$userId) exit(jsonReturn('请先登录!',-999));
      //获取用户钱包
      $user = model('users')->get($userId);

      // if($user->payPwd=='')return WSTReturn('您未设置支付密码,请先设置密码',-1);
      // if($user->payPwd!=md5($payPwd.$user->loginSecret))return WSTReturn('您的支付密码不正确',-1);
      $ect_rmb_price = session('ect_rmb_price');
      if($ect_rmb_price<0.05){        
        exit(jsonReturn('ECT价格出错!',-1));
      }
      $price = 11.11;
      $needPay = round($price/$ect_rmb_price,2);
      
      $needPay = $needPay > 0 ? $needPay : 0;
      if($needPay > $user->userECT) exit(jsonReturn('您的钱包ECT不足!',-2));

      $cfgm = Db::name('coupon_record')->where(['couponId'=>11,'userId'=>$userId])->find();
      if($cfgm){
        exit(jsonReturn('您已购买过该优惠券,不可重复购买!',-3));
      }

      //创建一条支出流水记录
      // $lm = [];
      // $lm['targetType'] = 0;
      // $lm['targetId'] = $userId;
      // $lm['dataId'] = 0;
      // $lm['dataSrc'] = 1;
      // $lm['remark'] = '购买双十一神券支出¥'.$needPay;
      // $lm['moneyType'] = 0;
      // $lm['money'] = $needPay;
      // $lm['payType'] = 'ect';
      // $a = model('common/LogMoneys')->add($lm);
      
      ectLog($userId,$needPay,11,'11.11神券支出',['userECT'=>['exp','userECT-'.$needPay]],2);


      $m = new \addons\hyhcouponset\model\Hyhcouponset();

      $data= $m->giftBag($userId,$secTypeId = 104);
      // dump($data);die;
      // Db::commit();
      exit(jsonReturn('扣款成功!',1));
    }catch(\Exception $e){
      // Db::rollback();errLog($e);
      exit(jsonReturn('扣款失败!',-1));
    }

  }

}