更新记录

1.1.4(2024-09-06)

解决BUG,完善日志输出。

1.1.3(2024-07-27)

1、补齐SDK Version 2.4.10.1版本新的的密码认证、令牌认证等多种辅助认证(二次认证)方式 2、修复SDK Version 2.4.10.1引出的BUG

1.1.2(2024-07-25)

新版SDK动态令牌认证接口代码调整

查看更多

平台兼容性

Vue2 Vue3
App 快应用 微信小程序 支付宝小程序 百度小程序 字节小程序 QQ小程序
HBuilderX 3.98,Android:5.0,iOS:9,HarmonyNext:不确定 × × × × × ×
钉钉小程序 快手小程序 飞书小程序 京东小程序
× × × ×
H5-Safari Android Browser 微信浏览器(Android) QQ浏览器(Android) Chrome IE Edge Firefox PC-Safari
× × × × × × × × ×

sanfor-atrust

插件说明

        本插件支持 uni-app 和 uni-app x 项目,uni-app x 的uts语法上有细微的区别。请先下载示例(uni-app)项目进行学习、试用,使用 HBuilderX 导入示例项目后,点击插件页面“试用”按钮引入插件,打包自定义基座就可以进行使用了。深信服SDK仅支持armeabi-v7a架构, arm64-v8a架构模拟器, 不支持X86架构模拟器,建议在真机环境下运行调试。

获取插件对象

import * as aTrustVPN from "@/uni_modules/sanfor-atrust";

演示界面

<template>
    <view style="padding: 5px 10px;">
        <view class="uni-padding-wrap uni-common-mt">
            <view class="uni-form-item uni-column">
                <view class="row-item"><text class="title">服务器:</text>
                <uni-easyinput v-model="mVpnAddress" placeholder="请输入服务器地址" />
                </view>
            </view>
            <view class="uni-form-item uni-column">
                <view class="row-item"><text class="title">账  号:</text>
                <uni-easyinput  v-model="mUserName" placeholder="请输入账号" />
                </view>
            </view>
            <view class="uni-form-item uni-column">
                <view class="row-item"><text class="title">密   码:</text>
                <uni-easyinput type="password" v-model="mUserPassword" placeholder="请输入密码" />
                </view>
            </view>
            <view class="uni-btn-v">
                <button type="default" @click="loginVPN()" plain="true">连接(主认证)</button>
            </view>
            <view v-if="showRand">
                <view class="uni-form-item uni-column">
                    <view class="row-item"><text class="title">校验码:</text>
                    <uni-easyinput  v-model="randCode" placeholder="请输入图形校验码" />
                        <view @click="getRandCode()" style="width: 80px; background-color: black; height: 38px; border: solid 1 px red;"><image :src="randCodeBase64" style="width: 80px; height: 38px; "></image></view>
                    </view>
                </view>
                <view class="uni-btn-v">
                    <button type="default" @click="doRandAuth()" plain="true">图形辅助认证</button>
                </view>
            </view>
            <view v-if="showRepw">
                <view class="uni-form-item uni-column">
                    <view class="row-item"><text class="title">旧密码:</text>
                    <uni-easyinput type="password" v-model="oldPassword" placeholder="请输入旧密码" />
                    </view>
                </view>
                <view class="uni-form-item uni-column">
                    <view class="row-item"><text class="title">新密码:</text>
                    <uni-easyinput type="password" v-model="newPassword" placeholder="请输入新密码" />
                    </view>
                </view>
                <view class="uni-btn-v">
                    <button type="default" @click="doRenewPassword()" plain="true">更新密码</button>
                </view>
            </view>
            <view class="uni-form-item uni-column">
                <view class="row-item"><text class="title">服务器:</text>
                <uni-easyinput v-model="mVpnAddress" placeholder="请输入服务器地址" />
                </view>
            </view>
            <view class="uni-form-item uni-column">
                <view class="row-item"><text class="title">SPA:</text>
                <uni-easyinput v-model="mSpa" placeholder="请输入SPA安全码" />
                </view>
            </view>
            <view class="uni-btn-v">
                <button type="default" @click="setSpa()" plain="true">配置SPA安全码</button>
            </view>

            <view class="uni-form-item uni-column">
                <view class="row-item"><text class="title">服务器:</text>
                <uni-easyinput v-model="mVpnAddress" placeholder="请输入服务器地址" />
                </view>
            </view>
            <view class="uni-form-item uni-column">
                <view class="row-item"><text class="title">手机号:</text>
                    <uni-easyinput  v-model="phoneNumber" placeholder="请输入手机号(86-185xxxxxx8626@sms)" />
                </view>
            </view>
            <view class="uni-btn-v">
                <button type="default" @click="loginSMS()" plain="true">短信主认证</button>
            </view>
            <view class="uni-form-item uni-column">
                <view class="row-item"><text class="title">验证码:</text>
                <uni-easyinput  v-model="smsCode" placeholder="请输入验证码" />
                    <csr-button :count-down="true" :count-down-num="99" count-down-text="重新获取" class="btn" width="200rpx" height="70rpx" type="hollow" @click="regetSmsCode()" >获取验证码</csr-button>
                </view>
            </view>

            <view class="uni-btn-v">
                <button style="margin-bottom: 20rpx;" type="default" @click="loginAutoTicket()" plain="true">免密认证</button>
                <button style="margin-bottom: 20rpx;" type="default" @click="getAuthStatusShow()" plain="true">登陆状态</button>
                <button style="margin-bottom: 20rpx;" type="default" @click="logoutVPN()" plain="true">注销</button>
            </view>
        </view>
    </view>
</template>

初始化

data() {
    return {
        showRand:false,
        showRepw:false,
        oldPassword:'旧密码',
        newPassword:'新密码',
        mSpa:'安全码',
        mVpnAddress: 'https://IP:端口/',
        phoneNumber: '86-185xxxxxx8626@sms',
        mUserName: '账号',
        mUserPassword: '密码',
        smsCode:'',
        randCode:'',
        randCodeBase64:''
    }
},
created() {
    this.initSDK();//SDK初始化,并设置监听
},
methods: {
    initSDK(){
        aTrustVPN.initSDK()
    },
}

设置SPA安全码

methods: {
    setSpa() {
        if(!aTrustVPN.isSpaSeedExist(this.mVpnAddress)){
            const config={"loginAddress":this.mVpnAddress, "spaSecret":this.mSpa}
            aTrustVPN.setSpaConfig(JSON.stringify(config), result => {
                console.log('前端接收结果',result)
                uni.hideLoading();
                uni.showModal({
                    title: '提示',
                    content: result.message,
                    showCancel:false
                });

            })
        }else{
            //已经登陆
            uni.showToast({
                title: 'SPA已设置!',
                duration: 2000
            });
        }
    },
}

VPN账号主认证

loginVPN() {
    if(this.getAuthStatus()!="SFAuthStatusAuthOk"){
        aTrustVPN.startPasswordAuth(this.mVpnAddress,this.mUserName,this.mUserPassword, result => {
            if(result.message=='AUTH_TYPE_SMS'){
                //需要短信二次辅助认证时,IOS的图型验证码可能不会隐藏,需要弹出窗口输入短信验证码进行验证
                uni.showModal({
                    title: '提示',
                    content: '处理二次认证-短信认证',
                    editable:true,
                    placeholderText:'请输入短信验证码',
                    success: function (res) {
                        if (res.confirm) {
                            console.log('用户点击确定',res.content);
                            //短信验证
                            aTrustVPN.doSecondaryAuth({
                                code: res.content
                            }, result => {
                                uni.showModal({
                                    title: '提示',
                                    content: result.message,
                                    showCancel:false
                                });
                                console.log(result)
                            })

                        } else if (res.cancel) {
                            console.log('用户点击取消');
                        }
                    }
                });
            }else if(result.message=='AUTH_TYPE_TOKEN'){
                //需要短信二次辅助认证时,IOS的图型验证码可能不会隐藏,需要弹出窗口输入短信验证码进行验证
                uni.showModal({
                    title: '提示',
                    content: '处理二次认证-令牌认证',
                    editable:true,
                    placeholderText:'请输入令牌动态口令',
                    success: function (res) {
                        if (res.confirm) {
                            console.log('用户点击确定',res.content);
                            //动态口令
                            aTrustVPN.doSecondaryAuth({
                                code: res.content
                            }, result => {
                                uni.showModal({
                                    title: '提示',
                                    content: result.message,
                                    showCancel:false
                                });
                            })

                        } else if (res.cancel) {
                            console.log('用户点击取消');
                        }
                    }
                });
            }else if(result.message=='AUTH_TYPE_RADIUS'){
                //需要短信二次辅助认证时,IOS的图型验证码可能不会隐藏,需要弹出窗口输入短信验证码进行验证
                uni.showModal({
                    title: '提示',
                    content: '处理二次认证-Radius认证',
                    editable:true,
                    placeholderText:'请输入Radius认证码',
                    success: function (res) {
                        if (res.confirm) {
                            console.log('用户点击确定',res.content);
                            //  挑战认证或者Radius认证
                            aTrustVPN.doSecondaryAuth({
                                code: res.content
                            }, result => {
                                uni.showModal({
                                    title: '提示',
                                    content: result.message,
                                    showCancel:false
                                });
                            })

                        } else if (res.cancel) {
                            console.log('用户点击取消');
                        }
                    }
                });
            }else if(result.message=='AUTH_TYPE_RAND'){
                this.getRandCode();//
                this.showRand=true;
            }else if(result.message=='AUTH_TYPE_RENEW_PASSWORD'){
                this.showRepw=true;
            }else{
                uni.showModal({
                    title: '提示',
                    content: result.message,
                    showCancel:false
                });
            }
            // console.log(result)
        })
    }else{
        //已经登陆
        uni.showToast({
            title: 'VPN已认证!',
            duration: 2000
        });
    }
},

短信主验证

loginSMS() {
    if(this.getAuthStatus()!="SFAuthStatusAuthOk"){
        aTrustVPN.startPrimarySmsAuth(this.mVpnAddress,this.phoneNumber, result => {
            console.log(result)
            if(result.message=='AUTH_TYPE_PRIMARY_SMS'){
                //需要短信二次辅助认证时,IOS的图型验证码可能不会隐藏,需要弹出窗口输入短信验证码进行验证
                uni.showModal({
                    title: '提示',
                    content: '处理二次认证-短信认证',
                    editable:true,
                    placeholderText:'请输入短信验证码',
                    success: function (res) {
                        if (res.confirm) {
                            console.log('用户点击确定',res.content);
                            //短信验证
                            aTrustVPN.doSecondaryAuth({
                                code: res.content
                            }, result => {
                                uni.showModal({
                                    title: '提示',
                                    content: result.message,
                                    showCancel:false
                                });
                                console.log(result)
                            })

                        } else if (res.cancel) {
                            console.log('用户点击取消');
                        }
                    }
                });
            }
        })
    }else{
        //已经登陆
        uni.showToast({
            title: 'VPN已认证!',
            duration: 2000
        });
    }
},

免密登陆

loginAutoTicket() {
    //自动免密上线接口,该接口可以实现免密认证。
    if(this.getAuthStatus()!="SFAuthStatusAuthOk"){
        // console.log("startAutoTicket");
        //Android  true代表免密调用成功,false代表免密调用失败。
        //IOS  YES代表免密调用成功,NO代表免密调用失败
        let res=aTrustVPN.startAutoTicket();
        console.log(res);
        uni.showModal({
            title: '提示',
            content: ''+res,
            showCancel:false
        });
        return res;
    }else{
        //已经登陆
        uni.showToast({
            title: 'VPN已认证!',
            duration: 2000
        });
    }
},

重新获取短信验证码

regetSmsCode() {
    aTrustVPN.regetSmsCode({},result => {
        uni.showModal({
            title: '提示',
            content: result.message,
            showCancel:false
        });
         console.log(result)
    })
},

重新获取图形验证码

getRandCode(){
    const that=this;
    aTrustVPN.regetRandCode(function(result){
        console.log('验证码',result);
        if(result){
            that.randCodeBase64="data:image/png;base64," + result.message;
        }else{
             uni.showModal({title:"getRandCode-fail",content:'获取图形验证码失败',showCancel:false});
        }
    });
},

图形验证/更新密码

doRandAuth(){
    //图形验证码验证
    aTrustVPN.doSecondaryAuth({
        u: this.mUserName,
        p: this.mUserPassword,
        randCode: this.randCode
    }, result => {
        console.log(result)
        if(result.message){
            uni.showModal({
                title: '提示',
                content: result.message,
                showCancel:false
            });
        }else{
            uni.showModal({
                title: '提示',
                content: result,
                showCancel:false
            });
        }
    })
},
doRenewPassword(){
    //更新密码
    aTrustVPN.doSecondaryAuth({
        oldPw: this.oldPassword,
        newPw: this.newPassword
    }, result => {
        console.log(result)
        if(result.message){
            uni.showModal({
                title: '提示',
                content: result.message,
                showCancel:false
            });
        }else{
            uni.showModal({
                title: '提示',
                content: result,
                showCancel:false
            });
        }
    })
},

获取登陆状态

getAuthStatus() {
    // case 0:
    //  return SFAuthStatusNone;
    // case 1:
    //  return SFAuthStatusLogining;
    // case 2:
    //  return SFAuthStatusPrimaryAuthOK;
    // case 3:
    //  return SFAuthStatusAuthOk;//已登陆
    // case 4:
    //  return SFAuthStatusLogouting;
    // case 5:
    //  return SFAuthStatusLogouted;//已经注销(登出)
    let res = aTrustVPN.getAuthStatus();
    console.log(res);
    return res;
},

登出、注销

logoutVPN() {
    if(this.getAuthStatus()=="SFAuthStatusAuthOk"){
        uni.showLoading({
            title:'正在注销,请稍候...'
        })
        aTrustVPN.logout(result => {
            uni.hideLoading();
            uni.showModal({
                title: '提示',
                content: result.message,
                showCancel:false
            });
             console.log(result)
        })
    }else{
        //已经登陆
        uni.showToast({
            title: 'VPN未登陆!',
            duration: 2000
        });
    }   
}

常见问题原因排查

        常见错误码信息请查阅:https://bbs.sangfor.com.cn/atrustdeveloper/appsdk/android/error.html

隐私、权限声明

1. 本插件需要申请的系统权限列表:

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据,插件相关功能调用深信服提供的原生SDK,参考其官方网站 https://bbs.sangfor.com.cn/atrustdeveloper/appsdk/

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问