<?php
namespace wstmart\app\controller;
use wstmart\common\model\Orders as OM;
use wstmart\app\model\EctElevenPay as M;
/**
 * ============================================================================
 * 默认控制器
 */
class Ect extends Base{
   
      /**
   * 跳去支付页面
   */
  public function payment(){
        $data = [];
        $data['orderNo'] = input('orderNo');
        $data['isBatch'] = (int)input('isBatch');
        $data['userId'] = (int)session('WST_USER.userId');

        $m = new OM();
        $rs = $m->getOrderPayInfo($data);//获取订单金额以及用户钱包金额
        
        $pay['list'] = $m->getByUnique();// 根据订单唯一流水获取订单信息
        
        if(empty($rs)){//已支付或者找不到此订单
          exit(jsonReturn('读取失败',-1));
        }else{
          $pay['needPay'] = $rs['needPay'];//需付款
          //获取用户钱包
          $user = model('users')->getFieldsById($data['userId'],'userECT,payPwd');
          $pay['userECT'] = $user['userECT'];
          $payPwd = $user['payPwd'];
          $payPwd = empty($payPwd)?0:1;
          $pay['payPwd'] = $payPwd;//1有密码0没设置密码
        }
        exit(jsonReturn('',1,$pay));
     
  }
  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;
  }
  public function getDollerPrice(){
    $now_doller = db('doller')->where('id=1')->cache(true)->value('doller');
    exit(jsonReturn('',1,['now_doller'=>$now_doller]));
  }
  public function getToEctNum(){
    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 = input('total_money');
        $t_usdt_price = $t_info->data->last;
        $now_doller = db('doller')->where('id=1')->value('doller');
        $now_rmb = round($t_usdt_price*$now_doller,2);
        $to_ect_num = round($total_money/$now_rmb,2);
        $ect_discount = Model('payments')->getPaymentDiscount('ect');
        $ect_msg='';
        if($ect_discount >= 0.9 && $ect_discount <1){
          $ect_msg=',ECT支付'.($ect_discount*10).'折';
          $to_ect_num *= $ect_discount;
        }
        $msg='当前ECT价格:'.$now_rmb.',订单价格:'.$total_money.$ect_msg.',共需:'.round($to_ect_num,2).' ECT';
        session('ect_rmb_price',$now_rmb);
        exit(jsonReturn($msg,1,['ect_num'=>$to_ect_num]));
      }
    }catch (\Exception $e) {
      exit(jsonReturn('获取价格失败!',-1));
    }
    

  }

  /**
   * 返回获取双11优惠券所需的ect数量和价格
   * @return [type] [description]
   */
  public function getEctNum(){
      $m = new M();
      $result = $m->getEctNum();
      exit(json_decode($result));
  }
  /**
   * 支付双十一优惠券的ect价格
   */
  public function payElevenEct(){
      $m = new M();
      $result = $m->payElevenEct();
      exit(json_decode($result));
  }
  /**
   * 当前用户的ECT数量
   * @return [type] [description]
   */
  public function userECT(){
     $data['ectNum'] = model('Ectwallet')->getEctNum();
       // $data['eAddressInfo'] = model('Ectwallet')->getDefaultAddress();
       // return WSTReturn('',1,$data);
       exit(jsonReturn('',1,$data));
  }


    /**
   * ECT支付
   */
  public function payByEct(){
    $m = new OM();
    exit(json_encode($m->payByEct()));
  }
    ///**
    //  *查询ect余额
    //  * 
    //  * @return void
    //  */
    // public function get_balance(){
    //   $ect_model = Model('Ect');
    //   $address = $ect_model->getCoinbase();//trim(input('post.address'));
    //   if(strlen($address)<=40){
    //     exit(jsonReturn('地址不正确'));
    //   }
    //   dump($ect_model->getBalance($address));
    // }
    // public function get_mc_Balance(){
    //   $address = $this->getCoinbase();//trim(input('post.address'));      
    //   $this->web3->eth->getBalance($address , function ($err, $balance) {
    //     if ($err !== null) {
    //       exit(jsonReturn($err->getMessage()));
    //         // echo 'Error: ' . $err->getMessage();
    //         //return;
    //     }else{
    //       $my_balance = $balance->toString();
    //       $my_balance = $my_balance/pow(10,$this->wei);
    //       $data['balance'] = $my_balance;
    //       exit(jsonReturn('',1,$data));
    //     }       
    //   });
    // }
    
}