[文章]HarmonyOS应用开发-新样式注册表单体验

阅读量0
0
2
1.png
一. 创建工程
二. 示例代码
index.hml代码如下:
  1. <div class="container">
  2.     <div class="body">
  3.         <div class="stack">
  4.             <div class="left">
  5.             </div>
  6.             <div class="right">
  7.                 <text class="header">注册</text>
  8.                 <input class="input" type="text" value="" maxlength="20" enterkeytype="next"
  9.                        placeholder="user name" onchange="onUserNameChange">
  10.                 </input>
  11.                 <div class="safety-factor-div">
  12.                     <text id="safe-head" class="notice-txt" style="color : black;"
  13.                           show="{{ safeHeadShow }}">安全系数</text>
  14.                     <div class="safe" style="background-color : red;" show="{{ safeD1Show }}"></div>
  15.                     <div class="safe" style="background-color : #F0F028;" show="{{ safeD2Show }}"></div>
  16.                     <div class="safe" style="background-color : green;" show="{{ safeD3Show }}"></div>
  17.                 </div>
  18.                 <input class="input" type="password" value="" maxlength="20" enterkeytype="next"
  19.                        placeholder="password" onchange="onPswChange">
  20.                 </input>
  21.                 <div class="safety-factor-div">
  22.                     <text class="notice-txt doudong">{{ pswInnerErr }}</text>
  23.                 </div>
  24.                 <input class="input" type="password" value="" maxlength="20" enterkeytype="next"
  25.                        placeholder="password again" onchange="onRePswChange">
  26.                 </input>
  27.                 <div class="safety-factor-div">
  28.                     <text class="notice-txt doudong">{{ repswInnerErr }}</text>
  29.                 </div>
  30.                 <stack class="div-code">
  31.                     <input class="input" type="text" value="" maxlength="4" enterkeytype="go"
  32.                            placeholder="safe code" onchange="onSafeCodeChange"
  33.                            onenterkeyclick="register">
  34.                     </input>
  35.                     <canvas ref="code_canvas" class="canvas-code" on:click="canvasClick"/>
  36.                 </stack>
  37.                 <div class="safety-factor-div">
  38.                     <text class="notice-txt" style="color : black;">温馨提示:可点击刷新验证码</text>
  39.                 </div>
  40.                 <input type="button" class="btn" value="注册" on:click="register"></input>
  41.             </div>
  42.         </div>
  43.     </div>
  44. </div>
复制代码

index.css代码如下:
  1. .container {
  2.     flex-direction: column;
  3.     background-color: #5e7c85;
  4. /*    padding-top: 52px;*/
  5.     height: 100%;
  6. }

  7. .body {
  8.     background: linear-gradient(to left bottom, #DD6455, #a18cd1);
  9.     width: 100%;
  10.     height: 100%;
  11. }

  12. .stack {
  13.     width: 100%;
  14.     background-color: white;
  15.     border-radius: 10px;
  16.     height: 400px;
  17.     box-shadow: 0 14px 28px rgba(0,0,0,0.25);
  18.     overflow: hidden;
  19.     margin-top: 10px;
  20.     margin-start: 10px;
  21.     margin-end: 10px;
  22. }

  23. .left {
  24.     width: 40%;
  25.     height: 100%;
  26.     float: left;
  27.     background: linear-gradient(to left bottom, #b2d235, #84bf96);
  28. }

  29. .right {
  30.     float: right;
  31.     width: 60%;
  32.     height: 100%;
  33.     flex-direction: column;
  34.     padding-start: 10px;
  35. }

  36. .header {
  37.     font-size: 20px;
  38.     font-weight: bold;
  39.     margin-top: 20px;
  40.     margin-bottom: 10px;
  41. }

  42. .input {
  43.     height: 36px;
  44.     width: 185px;
  45.     background-color: #EEEEEE;
  46.     placeholder-color: #99000000;
  47.     align-items: center;
  48.     color: black;
  49.     border-radius: 8px;
  50.     border: 1px solid red;
  51.     font-size: 15px;
  52. }

  53. .safety-factor-div {
  54.     height: 30px;
  55.     flex-direction: row;
  56.     align-items: center;
  57. }

  58. .safe {
  59.     width: 24px;
  60.     height: 6px;
  61.     border-radius: 3px;
  62.     margin-start: 5px;
  63. }

  64. .div-code {
  65.     width: 185px;
  66.     align-items: center;
  67. }

  68. .canvas-code {
  69.     position: absolute;
  70.     right: 3px;
  71.     top: 3px;
  72.     height: 30px;
  73.     width: 60px;
  74.     background-color: #CE6D39;
  75.     border-radius: 8px;
  76. }

  77. .btn {
  78.     width: 105px;
  79.     font-size: 15px;
  80.     height: 36px;
  81.     margin-start: 40px;
  82.     background-color: #CE6D39;
  83.     border-radius: 18px;
  84.     color: white;
  85. }

  86. .notice-txt {
  87.     margin-start: 5px;
  88.     font-size: 13px;
  89.     color: red;
  90. }

  91. .doudong {
  92.     animation: shake 1s ease-out infinite;
  93. }

  94. @keyframes shake {
  95.     10% {
  96.         transform: translateX(3px) rotate(2deg);
  97.     }

  98.     20% {
  99.         transform: translateX(-3px) rotate(-2deg);
  100.     }

  101.     30% {
  102.         transform: translateX(3px) rotate(2deg);
  103.     }

  104.     40% {
  105.         transform: translateX(-3px) rotate(-2deg);
  106.     }

  107.     50% {
  108.         transform: translateX(2px) rotate(1deg);
  109.     }

  110.     60% {
  111.         transform: translateX(-2px) rotate(-1deg);
  112.     }

  113.     70% {
  114.         transform: translateX(2px) rotate(1deg);
  115.     }

  116.     80% {
  117.         transform: translateX(-2px) rotate(-1deg);
  118.     }

  119.     90% {
  120.         transform: translateX(1px) rotate(0deg);
  121.     }

  122.     100% {
  123.         transform: translateX(-1px) rotate(0deg);
  124.     }
  125. }
复制代码

index.js代码如下:
  1. import prompt from '@system.prompt'
  2. var code;
  3. var canvas;
  4. var username = null;
  5. var password;
  6. var repassword;
  7. var inputCode = "";

  8. export default {
  9.     data: {
  10.         safeHeadShow: true,
  11.         safeD1Show: false,
  12.         safeD2Show: false,
  13.         safeD3Show: false,
  14.         pswInnerErr: '',
  15.         repswInnerErr: '',
  16.     },
  17.     createCode: function () {
  18.         let code = "";
  19.         var codeLength = 4;
  20.         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');
  21.         for (var i = 0; i < codeLength; i++) {
  22.             var charIndex = Math.floor(Math.random() * 36);
  23.             code += selectChar[charIndex];
  24.         }
  25.         return code;
  26.     },
  27.     checkPwd: function (str) {
  28.         var pattern1 = /([0-9]+)/i;
  29.         var pattern2 = /([a-z]+)/i;
  30.         if (str.length < 6 || str.length > 20) {
  31.             return 0;
  32.         }
  33.         if (pattern1.exec(str)) {
  34.             if (pattern2.exec(str)) {
  35.                 return 2
  36.             }
  37.             return 1;
  38.         }
  39.     },
  40.     draw_canvas: function (code) {
  41.         if (canvas) {
  42.             var ctx = canvas.getContext('2d', {
  43.                 antialias: true
  44.             });
  45.             ctx.clearRect(0, 0, 60, 30);
  46.             ctx.font = "16px Verdana";
  47.             let leftX = (60 - ctx.measureText(code).width) / 2;
  48.             if (leftX >= 20) {
  49.                 leftX = 10;
  50.             }
  51.             ctx.strokeText(code, leftX, 20);
  52.         } else {
  53.             console.error("没有找到canvas");
  54.         }
  55.     },
  56.     onShow() {
  57.         canvas = this.$refs.code_canvas;
  58.         this.canvasClick();
  59.     },
  60.     canvasClick() {
  61.         code = this.createCode();
  62.         canvas = this.$refs.code_canvas;
  63.         this.draw_canvas(code);
  64.     },
  65.     onUserNameChange(e) {
  66.         username = e.value;
  67.     },
  68.     onPswChange(e) {
  69.         password = e.value;

  70.         //this.safeHeadShow = true;
  71.         switch (this.checkPwd(password)) {
  72.             case 0:
  73.             {
  74.                 this.pswInnerErr = "密码不能小于6位";
  75.                 this.safeD1Show = true;
  76.                 this.safeD2Show = false;
  77.                 this.safeD3Show = false;
  78.                 break;
  79.             }
  80.             case 1:
  81.             {
  82.                 this.pswInnerErr = "";
  83.                 this.safeD1Show = true;
  84.                 this.safeD2Show = true;
  85.                 this.safeD3Show = false;
  86.                 break;
  87.             }
  88.             case 2:
  89.             {
  90.                 this.pswInnerErr = "";
  91.                 this.safeD1Show = true;
  92.                 this.safeD2Show = true;
  93.                 this.safeD3Show = true;
  94.                 break;
  95.             }
  96.             default:
  97.                 {
  98.                     this.pswInnerErr = "";
  99.                 }
  100.         }
  101.     },
  102.     onRePswChange(e) {
  103.         repassword = e.value;
  104.         if (repassword != password) {
  105.             this.repswInnerErr = "两次密码不相同";
  106.         } else {
  107.             this.repswInnerErr = "";
  108.         }
  109.     },
  110.     onSafeCodeChange(e) {
  111.         inputCode = e.value;
  112.     },
  113.     register() {
  114.         if (username) {
  115.             if (password) {
  116.                 if (password.length >= 6) {
  117.                     if (repassword) {
  118.                         if (repassword == password) {
  119.                             if (code == inputCode.toUpperCase()) {
  120.                                 this.alert("恭喜您注册成功,现在开始体验吧!", "#00E5EE", "开始体验");
  121.                             } else {
  122.                                 this.alertDef("验证码错误");
  123.                             }
  124.                         } else {
  125.                             this.alertDef("两次密码不相同");
  126.                         }
  127.                     } else {
  128.                         this.alertDef("请输入重复密码");
  129.                     }
  130.                 } else {
  131.                     this.alertDef("请输入符合规范的密码");
  132.                 }
  133.             } else {
  134.                 this.alertDef("请输入密码");
  135.             }
  136.         } else {
  137.             this.alertDef("请输入用户名");
  138.         }
  139.     },
  140.     alertDef(msg) {
  141.         this.alert(msg, "#FF0000", "");
  142.     },
  143.     alert(msg, btnColor, toastMsg) {
  144.         prompt.showDialog({
  145.             title: "",
  146.             message: msg,
  147.             buttons: [{
  148.                           "text": "确定",
  149.                           "color": btnColor
  150.                       }],
  151.             success: function (e) {
  152.                 if (e.index == 0 && toastMsg.length != 0) {
  153.                     //鸿蒙的提示框
  154.                     prompt.showToast({
  155.                         message: toastMsg
  156.                     });
  157.                 }
  158.             }
  159.         });
  160.     },
  161. }
复制代码

三. 效果案例
1.png
附件:

回帖

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容图片侵权或者其他问题,请联系本站作侵删。 侵权投诉
链接复制成功,分享给好友