<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 绑定支付宝或银行
 */
class Userbank extends Api
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [''];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    //绑定支付宝
    public function addalipayaccount() {
        $id = input('id', 0, 'intval'); //账号id
        $type = input('type', 0, 'intval'); //账号类型:1=支付宝,2=银行卡
//        $type = 1;
        $realname = input('realname', '', 'trim'); //账户真实姓名
        $banknumber = input('banknumber', '', 'trim'); //卡号或账号
        $bankname = input('bankname', '', 'trim'); //银行名称

//        if ($id) {
//            $this->error('您已经拥有该类型账号,请联系后台解绑');
//        }
        if (!in_array($type, [1, 2])) {
            $this->error('参数错误');
        }
        /*if ($type == 1) {
            $this->error('暂不支持绑定支付宝');
        }*/
        if ($realname === '' || iconv_strlen($realname, 'utf-8') > 30) {
            $this->error('账号姓名1-30位');
        }
        if ($banknumber === '' || iconv_strlen($banknumber, 'utf-8') > 50) {
            $this->error('账号1-50位');
        }
        if ($type == 1) {
            if (iconv_strlen($bankname, 'utf-8') != 18) {
                $this->error('请输入正确身份证号');
            }
        }
        if ($type == 2) {
            if ($bankname === '' || iconv_strlen($bankname, 'utf-8') > 50) {
                $this->error('开户行名称1-50位');
            }
        }

        $user_bank = Db::name('user_bank');
        if ($id) {
            //编辑
            //查询账号是否存在
            $info = $user_bank->where(['id' => $id, 'user_id' => $this->auth->id])->find();
            if (!$info) {
                $this->error('账号不存在');
            }
            if ($info['type'] != $type) {
                $this->error('账号类型错误');
            }
            //查询是否未通过提现记录
            $count = Db::name('take_cash')->where(['user_id' => $this->auth->id, 'alipay_account' => $info['banknumber'], 'status' => 0])->count('id');
            if ($count) {
                $this->error('该账户有未审核的提现记录,暂不可修改');
            }

            $data['realname'] = $realname;
            $data['banknumber'] = $banknumber;
            $data['updatetime'] = time();
//            if ($type == 2) {
            $data['bankname'] = $bankname;
//            }

            $rs = $user_bank->where(['id' => $id, 'user_id' => $this->auth->id])->setField($data);
            if ($rs === false) {
                $this->error('修改失败');
            }

            $this->success('修改成功');
        } else {
            //添加
            //查询是否已经拥有该类型账号
            $count = $user_bank->where(['user_id' => $this->auth->id, 'type' => $type])->count('id');
            if ($count) {
                $this->error('您已经拥有该类型账号,请联系后台解绑');
            }

            $data['user_id'] = $this->auth->id;
            $data['type'] = $type;
            $data['realname'] = $realname;
            $data['banknumber'] = $banknumber;
//            if ($type == 2) {
            $data['bankname'] = $bankname;
//            }
            $data['createtime'] = time();

            $rs = $user_bank->insertGetId($data);
            if (!$rs) {
                $this->error('添加失败');
            }

            $this->success('添加成功');
        }
    }

    //查询支付宝/银行卡信息
    public function alipayinfo() {
        $type = input('type', 0, 'intval'); //账号类型:1=支付宝,2=银行卡
        $info = Db::name('user_bank')->field('id, realname, banknumber, bankname')->where(['user_id' => $this->auth->id, 'type' => $type])->find();
        if (!$info) {
            $info = (object)[];
        }

        $this->success('success', $info);
    }

    //绑定支付宝账号信息
    public function bindalipayaccount() {
        $mt_user_bank = Db::name('user_bank');
        $count = $mt_user_bank->where(['user_id' => $this->auth->id, 'type' => 1])->count();
        if ($count) {
            $this->error('您已绑定支付宝啦');
        }

        $auth_code = input('auth_code', '', 'trim');
        if (!$auth_code) {
            $this->error('授权码缺失');
        }
        //获取支付宝用户信息
        $rs = getAlipayInfo($auth_code);
        if ($rs['status'] == 0) {
            $this->error($rs['info']);
        }
        $rs = $rs['data'];
        /*
         * Array(
            [alipay_user_info_share_response] => Array
                (
                    [code] => 10000
                    [msg] => Success
                    [avatar] => https://tfs.alipayobjects.com/images/partner/T1BQJsXhhbXXXXXXXX  头像
                    [city] => 临沂市       市名称。
                    [gender] => m       【注意】只有is_certified为T的时候才有意义,否则不保证准确性. 性别(F:女性;M:男性)。
                    [is_certified] => T   是否通过实名认证。T是通过 F是没有实名认证。
                    [is_student_certified] => F  是否是学生 T是 F否
                    [nick_name] => 风的追求         用户昵称
                    [province] => 山东省        省份名称
                    [user_id] => 2088902918001020  支付宝用户的userId
                    [user_status] => T  用户状态(Q/T/B/W)。 Q代表快速注册用户 T代表已认证用户 B代表被冻结账户 W代表已注册,未激活的账户
                    [user_type] => 2  用户类型(1/2) 1代表公司账户2代表个人账户
                )
            )
        */

        $data['banknumber'] = $rs['user_id']; //支付宝用户id
        if (!$data['banknumber']) {
            $this->error('获取用户信息失败');
        }
        $bank_number_count = $mt_user_bank->where(['banknumber' => $data['banknumber'], 'type' => 1])->count();
        if ($bank_number_count) {
            $this->error('该支付宝账号已经绑定用户, 请先解除绑定');
        }

        if (isset($rs['nick_name'])) {
            $data['realname'] = $rs['nick_name'];  //支付宝用户昵称
        }

        $data['user_id'] = $this->auth->id;
        $data['createtime'] = time();
        $data['type'] = 1;

        $rt = $mt_user_bank->insertGetId($data);
        if (!$rt) {
            $this->error('绑定失败');
        }

        $this->success('绑定成功');
    }



}