创蓝闪验php手机号一键登录

创蓝闪验php手机号一键登录

注意:对外接口需要两个必要参数,flash_token、type(安卓或ios)
flash_token从哪里来:
是安卓和ios端集成创蓝闪验sdk以后通过调用sdk相关接口获得,参数名为token。
例如:
===================oneKeyLoginListener: <CLCompleteResult: 0x282759130> { authPagePresented = 1; code = 1000; data = { token = "A2-TbeW2bEc8sWcqa9X2_R3GJK4rKulBIEQhyfRn0NWpi_MBoxslDC3c22RByVMAcJddO5X291FBpBshtwcXBp-giIDFXc-7uw-t7t36qhV6HUnWsanbQkVFYH1wFbelVU_uNKk3BwbTtmZ44z7qZIdQHZfYZfsqHTyQGLV7aCkUbGQk0ax_p_xtWzqRTyqpN3HglJmPnsaeATXH_8cn5iJYN8bx6U2-XrkCVw4xxv2seF8oJ82MKD70BHGG776F6vU" }; error = <nil>; innerCode = 0; innerData = <nil>; innerDesc = <nil>; innerError = <nil>; message = "SDK获取Token成功" } ==================

少废话,上代码

对外(安卓、ios)一键登录接口:

    /**
     * 闪验手机号一键登录接口
     * @param $flash_token
     * @param $type 1安卓、2ios
     */
    public function flashLogin()
    {
        // 接值 $params
        ···
        ···
        
		// 校验参数
        if ( !isset($params['flash_token']) ){
            throw new Exception(71018);
        }

        if ( !isset($params['type']) ){
            throw new Exception(71018);
        }

        // 判断是安卓还是ios
        $platform = $params['type'];

        if ($platform == 'android'){
            $type = 1;
        }elseif($platform == 'ios'){
            $type = 2;
        }else{
            throw new Exception(71018);
        }
        // 调用封装的公共方法 返回手机号
        $phone = getFlashPhone( $params['flash_token'] , $type  );
        // 判断手机号
        if (empty($phone)){
            throw new Exception(71019);
        }
		// 写库登录操作
        ···
        ···

    	// 返回接口登录成功或失败
    	···
    	···
    	
    }

公共方法:

/**
 * 创蓝闪验 一键登录取号
 * @param $tonen
 * @param $type 1安卓、2ios
 * @param string $massage
 * @return $return_phone 返回手机号,失败则返回false
 * @throws Exception
 */
function getFlashPhone( $token ,$type = 1 )
{

    $clapi = new \service\ChuanglanFlashSms();
    // 获取手机号
    $return_phone = $clapi->getFlashPhone( $token , $type );
    return $return_phone;

}

创蓝闪验封装类:

<?php
namespace service;

/* *
 * 创蓝闪验(手机号一键登录)
 * smt
 */

class ChuanglanFlashSms
{

    // 创蓝手机号一键登录 闪验地址
    protected $flash_query_url;
    // 安卓对应的appid
    protected $ad_flash_appid;
    // 安卓对应的appkey
    protected $ad_flash_appkey;
    // ios对应的appid
    protected $ios_flash_appid;
    // ios对应的appkey
    protected $ios_flash_appkey;
    // 应用私钥,可选,如用RSA解密,必须填写
    protected $flash_private_key;
    //  AES 1 RSA  , 默认0 AES
    protected $flash_encrypt_type;

    public function __construct()
    {
        $this->flash_query_url    = config('cl_flash_config.flash_query_url');
        $this->ad_flash_appid     = config('cl_flash_config.ad_flash_appid');
        $this->ad_flash_appkey    = config('cl_flash_config.ad_flash_appkey');
        $this->ios_flash_appid    = config('cl_flash_config.ios_flash_appid');
        $this->ios_flash_appkey   = config('cl_flash_config.ios_flash_appkey');
        $this->flash_private_key  = config('cl_flash_config.flash_private_key');
        $this->flash_encrypt_type = config('cl_flash_config.flash_encrypt_type');
    }

    /**
     * 闪验短信取号校验
     * @param $token
     * @param $type 1安卓、2ios
     */
    public function getFlashPhone($token, $type = 1)
    {

        if ($type == 1) {
            // android
            // 生成sign串
            $content = 'appId' . $this->ad_flash_appid . 'token' . $token;
            $sign    = bin2hex(hash_hmac('sha256', $content, $this->ad_flash_appkey, true));

            $params = [
                'appId' => $this->ad_flash_appid,
                'token' => $token,
                'sign'  => $sign //签名
            ];
        } elseif ($type == 2) {
            // ios
            // 生成sign串
            $content = 'appId' . $this->ios_flash_appid . 'token' . $token;
            $sign    = bin2hex(hash_hmac('sha256', $content, $this->ios_flash_appkey, true));

            $params = [
                'appId' => $this->ios_flash_appid,
                'token' => $token,
                'sign'  => $sign //签名
            ];

        }


        $return_phone = $this->flashCurlPost($this->flash_query_url, $params, $type);
        return $return_phone;
    }

    /**
     * 闪验curl
     * @param $url
     * @param $data
     * @param $type 1安卓、2ios
     */
    private function flashCurlPost($url, $params, $type = 1)
    {
        // CURL 模拟 post 请求
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        $resultJson = curl_exec($ch);
        $curlInfo   = curl_getinfo($ch);

        // CURL 错误码
        $errNo = curl_errno($ch);
        if ($errNo > 0) {
            // 通信失败处理逻辑,请自己填充
            // CURL 错误信息
            $errMsg = curl_error($ch);
            logs($errMsg, 'flash_login_error');
        } elseif (!empty($curlInfo) && intval($curlInfo['http_code']) != 200) {
            // http 状态码不是 200,请求失败处理逻辑
            $httpCode = $curlInfo['http_code'];
            logs($httpCode, 'flash_login_error');
        } else {
            // 拿到请求结果,使用返回结果逻辑
            $requestData = json_decode($resultJson, true);
            if ($requestData['code'] == 200000) {
                $chargeStatus = $requestData['chargeStatus']; // 是否收费,枚举值:1 :收费 0:不收费
                $mobile       = $requestData['data']['mobileName']; // 手机号

                if ('0' == $this->flash_encrypt_type) { //AES解密 ,默认方式
                    if ($type == 1) {
                        // android
                        $key = md5($this->ad_flash_appkey);
                    } elseif ($type == 2) {
                        // ios
                        $key = md5($this->ios_flash_appkey);
                    } else {
                        return false;
                    }

                    $mobile = openssl_decrypt(hex2bin($mobile), 'AES-128-CBC', substr($key, 0, 16), OPENSSL_RAW_DATA, substr($key, 16));
                    // 如解密失败 请检查$appKey是否正确
                } elseif ('1' == $this->flash_encrypt_type) { //RSA解密
                    $pi_key = openssl_pkey_get_private($this->flash_private_key);
                    openssl_private_decrypt(hex2bin($mobile), $mobile, $pi_key);//私钥解密
                    // 如解密失败 请检查$private_key是否正确
                }

                // $tradeNo = $requestData['data']['tradeNo']; // 流水号
                // 拿到返回数据继续处理逻辑
                return $mobile;
            } else {
                // 响应异常处理逻辑
                logs($resultJson, 'flash_login_error');
                // return $resultJson;
                return false;
            }
        }
    }


}


?>

完 ···

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我来回答你的问题。实现闪验功能需要以下步骤: 1. 在创蓝官网申请闪验的AppID和AppKey。 2. 在Spring Boot项目中引入创蓝的SDK依赖,可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.chuanglan</groupId> <artifactId>clwlbusiness-sdk</artifactId> <version>1.0.0</version> </dependency> ``` 3. 创建一个发送验证码的Controller接口。 ```java @RestController @RequestMapping("/sms") public class SmsController { @Value("${chuanglan.appId}") private String appId; @Value("${chuanglan.appKey}") private String appKey; @PostMapping("/sendCode") public String sendCode(@RequestParam("phone") String phone) { CLWLBussinessService clwlBussinessService = new CLWLBussinessServiceImpl(appId, appKey); String code = RandomStringUtils.randomNumeric(6); String msg = String.format("【闪验科技】您的验证码是:%s,5分钟内有效。如非本人操作,请忽略本短信。", code); String result = clwlBussinessService.sendSMS(phone, msg, "", ""); // 处理发送结果 return result; } } ``` 4. 在application.properties或application.yml文件中配置创蓝的AppID和AppKey。 ```yaml chuanglan: appId: your_app_id appKey: your_app_key ``` 5. 在前端页面中添加闪验的SDK代码。 ```javascript <!-- 引入闪验SDK --> <script src="https://ssl.captcha.qq.com/TCaptcha.js"></script> <!-- 创建一个div用于显示闪验验证码 --> <div id="TencentCaptcha"></div> <!-- 在需要触发闪验的事件中调用sdk的方法 --> <button onclick="showCaptcha()">触发闪验</button> <script> // 创建一个变量保存验证码的ticket和randStr var verifyParams = {}; function showCaptcha() { // 调用sdk的方法获取ticket和randStr window.TencentCaptcha.init({ // 替换成你在腾讯云上面申请的appid appId: 'your_app_id', // 回调函数 callback: function (res) { // 保存ticket和randStr verifyParams.ticket = res.ticket; verifyParams.randStr = res.randstr; // 发送验证请求 $.post("/sms/sendCode", {phone: "your_phone_number", verifyParams: JSON.stringify(verifyParams)}, function (data) { console.log(data); }) } }).show(); } </script> ``` 以上就是Spring Boot使用创蓝闪验实现闪验功能的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

布尼卡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值