微信小程序 login.js 登录文件 图形验证码

本文介绍了微信小程序中实现登录功能的关键部分,重点讲解了如何在login.js文件中结合mcaptcha.js库生成图形验证码,并利用httpclient.js进行HTTP请求验证用户输入的验证码,确保登录的安全性。
摘要由CSDN通过智能技术生成

图形验证码
mcaptcha.js

module.exports = class Mcaptcha {
   
  constructor(options) {
   
    this.options = options;
    this.fontSize = options.height * 3 / 4;
    this.init();
    this.refresh(this.options.code);
  }
  init() {
   
    this.ctx = wx.createCanvasContext(this.options.el);
    this.ctx.setTextBaseline("middle");
    this.ctx.setFillStyle(this.randomColor(180, 240));
    this.ctx.fillRect(0, 0, this.options.width, this.options.height);
  }
  refresh(code) {
   
    let arr = (code + '').split('');
    if (arr.length === 0) {
   
      arr = ['e', 'r', 'r', 'o', 'r'];
    };
    let offsetLeft = this.options.width * 0.6 / (arr.length - 1);
    let marginLeft = this.options.width * 0.2;
    arr.forEach((item, index) => {
   
      this.ctx.setFillStyle(this.randomColor(0, 180));
      let size = this.randomNum(24, this.fontSize);
      this.ctx.setFontSize(size);
      let dis = offsetLeft * index + marginLeft - size * 0.3;
      let deg = this.randomNum(-30, 30);
      this.ctx.translate(dis, this.options.height * 0.5);
      this.ctx.rotate(deg * Math.PI / 180);
      this.ctx.fillText(item, 0, 0);
      this.ctx.rotate(-deg * Math.PI / 180);
      this.ctx.translate(-dis, -this.options.height * 0.5);
    })
    this.ctx.draw();
  }
  randomNum(min, max) {
   
    return Math.floor(Math.random() * (max - min) + min);

  }
  randomColor(min, max) {
   
    let r = this.randomNum(min, max);
    let g = this.randomNum(min, max);
    let b = this.randomNum(min, max);
    return "rgb(" + r + "," + g + "," + b + ")";
  }
}

HTTP请求,httpclient.js

const app = getApp()
var utiltime = require('time.js')
var utilsign = require('getsign.js')
function doRequest(url,postData,doSuccess,doFail,doComplete){
   
 
  wx.request({
   
    url: app.globalData.APIUrl+url,
    data:postData,
    header: {
   
         'content-type': 'application/x-www-form-urlencoded' // 默认值
       },
    method: 'POST', 
    success: function(res){
   
      typeof doSuccess == "function" && doSuccess(res.data)
    },
    fail: function(res) {
   
      // console.log(res);
      wx.hideLoading();
      wx.showToast({
   
        title: '服务器异常,请求超时!', //提示的内容,
        icon: "none",
        duration: 3000, //延迟时间,
      });
      typeof doFail == "function" && doFail()
    },
    complete: function() {
   
      typeof doComplete == "function" && doComplete()
    }
  })
  return
}


function doRequestService(url, postData, doSuccess, doFail, doComplete) {
   

  var _time = utiltime.getTimems();
  var jsonstr = objKeySort(postData); //函数执行
  // console.log(jsonstr)
  var _sign = utilsign.getSign(_time, jsonstr);
  var dateStr = JSON.stringify(postData);
  // console.log("验签参数:" + jsonstr);
  var map = {
    paramJson: '{"data":\'' + dateStr + '\',"sign":"' + _sign + &#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值