<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use app\common\library\Sms;

/**
 *
 */
class Userbank extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];




    /**
     * 绑定银行卡
     */
    public function bindBank() {

        $captcha = input('captcha');
        if (!$captcha) {
            $this->error(__('Invalid parameters'));
        }
        $result = Sms::check($this->auth->mobile, $captcha, 'changemobile');
        if (!$result) {
            $this->error(__('Captcha is incorrect'));
        }

        //
        $bank_no = $this->request->request('bank_no');// 银行账号
        $open_bank = $this->request->request('open_bank');// 开户行
        if(!$bank_no || !$open_bank ) {
            $this->error("请将信息填写完整");
        }

        $userId = $this->auth->id;

        //检测实名认证
        $userAuthWhere['user_id'] = $userId;
        $userAuth = Db::name('user_idconfirm')->where($userAuthWhere)->find();
        if (empty($userAuth)) {
            $this->error('请先实名认证');
        }
        if ($userAuth['status'] != 1) {
            $this->error('请先实名认证通过');
        }
        $realname = $userAuth['realname'];


        // 查询是否有过绑定
        $bankInfo = Db::name('user_bank')->where(["user_id"=>$userId])->find();
        $data = [];
        $data["realname"] = $realname;
        $data["bank_no"] = $bank_no;
        $data["open_bank"] = $open_bank;

        if($bankInfo) {
            $res = Db::name('user_bank')->where(["user_id"=>$userId])->update($data);
        } else {
            $data["user_id"] = $userId;
            $res = Db::name('user_bank')->insertGetId($data);
        }


        $this->success("银行卡绑定成功!");

    }

    /**
     * 获取绑定银行卡信息
     */
    public function getBankInfo() {
        // 查询是否有过绑定
        $bankInfo = Db::name('user_bank')->where(["user_id"=>$this->auth->id])->find();

        $this->success("获取成功!",$bankInfo);
    }

    /**
     * 绑定支付宝
     */
    public function bindAlipay() {

        $captcha = input('captcha');
        if (!$captcha) {
            $this->error(__('Invalid parameters'));
        }
        $result = Sms::check($this->auth->mobile, $captcha, 'changemobile');
        if (!$result) {
            $this->error(__('Captcha is incorrect'));
        }

        //
        $payNo = $this->request->request('pay_no');//支付宝账号
        if(!$payNo) {
            $this->error("请将信息填写完整");
        }

        $userId = $this->auth->id;

        //检测实名认证
        $userAuthWhere['user_id'] = $userId;
        $userAuth = Db::name('user_idconfirm')->where($userAuthWhere)->find();
        if (empty($userAuth)) {
            $this->error('请先实名认证');
        }
        if ($userAuth['status'] != 1) {
            $this->error('请先实名认证通过');
        }


        // 查询是否有过绑定
        $bankInfo = Db::name('user_alipay')->where(["user_id"=>$userId])->find();
        $data = [];
        $data["realname"] = $userAuth['realname'];
        $data["pay_no"] = $payNo;
        if($bankInfo) {
            $res = Db::name('user_alipay')->where(["user_id"=>$userId])->update($data);
        } else {
            $data["user_id"] = $userId;
            $res = Db::name('user_alipay')->insertGetId($data);
        }
        if($res) {
            $this->success("支付宝绑定成功!");
        } else {
            $this->error("网络异常,请稍后重试!");
        }
    }

    /**
     * 获取绑定银行卡信息
     */
    public function getAlipayInfo() {
        // 查询是否有过绑定
        $alipayInfo = Db::name('user_alipay')->where(["user_id"=>$this->auth->id])->find();
        $this->success("获取成功!",$alipayInfo);
    }


    /**
     * 绑定微信
     */
    public function bindWechat() {

        $captcha = input('captcha');
        if (!$captcha) {
            $this->error(__('Invalid parameters'));
        }
        $result = Sms::check($this->auth->mobile, $captcha, 'changemobile');
        if (!$result) {
            $this->error(__('Captcha is incorrect'));
        }

        //
        $payNo = $this->request->request('pay_no');//支付宝账号
        if(!$payNo) {
            $this->error("请将信息填写完整");
        }

        $userId = $this->auth->id;

        //检测实名认证
        $userAuthWhere['user_id'] = $userId;
        $userAuth = Db::name('user_idconfirm')->where($userAuthWhere)->find();
        if (empty($userAuth)) {
            $this->error('请先实名认证');
        }
        if ($userAuth['status'] != 1) {
            $this->error('请先实名认证通过');
        }


        // 查询是否有过绑定
        $bankInfo = Db::name('user_wechat')->where(["user_id"=>$userId])->find();
        $data = [];
        $data["realname"] = $userAuth['realname'];
        $data["pay_no"] = $payNo;
        if($bankInfo) {
            $res = Db::name('user_wechat')->where(["user_id"=>$userId])->update($data);
        } else {
            $data["user_id"] = $userId;
            $res = Db::name('user_wechat')->insertGetId($data);
        }
        if($res) {
            $this->success("绑定成功!");
        } else {
            $this->error("网络异常,请稍后重试!");
        }
    }

    /**
     * 获取绑定银行卡信息
     */
    public function getWechatInfo() {
        // 查询是否有过绑定
        $alipayInfo = Db::name('user_wechat')->where(["user_id"=>$this->auth->id])->find();
        $this->success("获取成功!",$alipayInfo);
    }


}