2022-03-04 14:33:24
0
一. 创建工程
二. 示例代码
index.hml代码如下:
- <div class="container">
- <div class="body">
- <div class="stack">
- <div class="left">
- </div>
- <div class="right">
- <text class="header">注册</text>
- <input class="input" type="text" value="" maxlength="20" enterkeytype="next"
- placeholder="user name" onchange="onUserNameChange">
- </input>
- <div class="safety-factor-div">
- <text id="safe-head" class="notice-txt" style="color : black;"
- show="{{ safeHeadShow }}">安全系数</text>
- <div class="safe" style="background-color : red;" show="{{ safeD1Show }}"></div>
- <div class="safe" style="background-color : #F0F028;" show="{{ safeD2Show }}"></div>
- <div class="safe" style="background-color : green;" show="{{ safeD3Show }}"></div>
- </div>
- <input class="input" type="password" value="" maxlength="20" enterkeytype="next"
- placeholder="password" onchange="onPswChange">
- </input>
- <div class="safety-factor-div">
- <text class="notice-txt doudong">{{ pswInnerErr }}</text>
- </div>
- <input class="input" type="password" value="" maxlength="20" enterkeytype="next"
- placeholder="password again" onchange="onRePswChange">
- </input>
- <div class="safety-factor-div">
- <text class="notice-txt doudong">{{ repswInnerErr }}</text>
- </div>
- <stack class="div-code">
- <input class="input" type="text" value="" maxlength="4" enterkeytype="go"
- placeholder="safe code" onchange="onSafeCodeChange"
- onenterkeyclick="register">
- </input>
- <canvas ref="code_canvas" class="canvas-code" on:click="canvasClick"/>
- </stack>
- <div class="safety-factor-div">
- <text class="notice-txt" style="color : black;">温馨提示:可点击刷新验证码</text>
- </div>
- <input type="button" class="btn" value="注册" on:click="register"></input>
- </div>
- </div>
- </div>
- </div>
复制代码
index.css代码如下:
index.js代码如下:
- import prompt from '@system.prompt'
- var code;
- var canvas;
- var username = null;
- var password;
- var repassword;
- var inputCode = "";
- export default {
- data: {
- safeHeadShow: true,
- safeD1Show: false,
- safeD2Show: false,
- safeD3Show: false,
- pswInnerErr: '',
- repswInnerErr: '',
- },
- createCode: function () {
- let code = "";
- var codeLength = 4;
- var selectChar = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
- for (var i = 0; i < codeLength; i++) {
- var charIndex = Math.floor(Math.random() * 36);
- code += selectChar[charIndex];
- }
- return code;
- },
- checkPwd: function (str) {
- var pattern1 = /([0-9]+)/i;
- var pattern2 = /([a-z]+)/i;
- if (str.length < 6 || str.length > 20) {
- return 0;
- }
- if (pattern1.exec(str)) {
- if (pattern2.exec(str)) {
- return 2
- }
- return 1;
- }
- },
- draw_canvas: function (code) {
- if (canvas) {
- var ctx = canvas.getContext('2d', {
- antialias: true
- });
- ctx.clearRect(0, 0, 60, 30);
- ctx.font = "16px Verdana";
- let leftX = (60 - ctx.measureText(code).width) / 2;
- if (leftX >= 20) {
- leftX = 10;
- }
- ctx.strokeText(code, leftX, 20);
- } else {
- console.error("没有找到canvas");
- }
- },
- onShow() {
- canvas = this.$refs.code_canvas;
- this.canvasClick();
- },
- canvasClick() {
- code = this.createCode();
- canvas = this.$refs.code_canvas;
- this.draw_canvas(code);
- },
- onUserNameChange(e) {
- username = e.value;
- },
- onPswChange(e) {
- password = e.value;
- //this.safeHeadShow = true;
- switch (this.checkPwd(password)) {
- case 0:
- {
- this.pswInnerErr = "密码不能小于6位";
- this.safeD1Show = true;
- this.safeD2Show = false;
- this.safeD3Show = false;
- break;
- }
- case 1:
- {
- this.pswInnerErr = "";
- this.safeD1Show = true;
- this.safeD2Show = true;
- this.safeD3Show = false;
- break;
- }
- case 2:
- {
- this.pswInnerErr = "";
- this.safeD1Show = true;
- this.safeD2Show = true;
- this.safeD3Show = true;
- break;
- }
- default:
- {
- this.pswInnerErr = "";
- }
- }
- },
- onRePswChange(e) {
- repassword = e.value;
- if (repassword != password) {
- this.repswInnerErr = "两次密码不相同";
- } else {
- this.repswInnerErr = "";
- }
- },
- onSafeCodeChange(e) {
- inputCode = e.value;
- },
- register() {
- if (username) {
- if (password) {
- if (password.length >= 6) {
- if (repassword) {
- if (repassword == password) {
- if (code == inputCode.toUpperCase()) {
- this.alert("恭喜您注册成功,现在开始体验吧!", "#00E5EE", "开始体验");
- } else {
- this.alertDef("验证码错误");
- }
- } else {
- this.alertDef("两次密码不相同");
- }
- } else {
- this.alertDef("请输入重复密码");
- }
- } else {
- this.alertDef("请输入符合规范的密码");
- }
- } else {
- this.alertDef("请输入密码");
- }
- } else {
- this.alertDef("请输入用户名");
- }
- },
- alertDef(msg) {
- this.alert(msg, "#FF0000", "");
- },
- alert(msg, btnColor, toastMsg) {
- prompt.showDialog({
- title: "",
- message: msg,
- buttons: [{
- "text": "确定",
- "color": btnColor
- }],
- success: function (e) {
- if (e.index == 0 && toastMsg.length != 0) {
- //鸿蒙的提示框
- prompt.showToast({
- message: toastMsg
- });
- }
- }
- });
- },
- }
复制代码
三. 效果案例
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容图片侵权或者其他问题,请联系本站作侵删。
侵权投诉