当前位置: 移动技术网 > IT编程>开发语言>c# > Unity登录注册时限制发送验证码次数功能的解决方法

Unity登录注册时限制发送验证码次数功能的解决方法

2020年03月09日  | 移动技术网IT编程  | 我要评论

当我们需要在unity客户端做一个限制功能,比如按钮 (最好是发送验证码按钮)要求每天只能点击三次,等到第二天又有三次机会,这个过程不涉及到服务端消息,只涉及到本地存储,以下是我的解决方案:

直接上代码:

using system.collections;
using system.collections.generic;
using unityengine;
using unityengine.ui;
using unirx;
using system;
using system.io;
using system.text;
using system.globalization;
public class registerpanel : monobehaviour
  {
    private loginuipanel mloginuipanel;
    streamwriter writer;
    streamreader reader;
 //本地存储手机号
    private string set_phonenum;
 //同一个手机号码使用次数
    private int usenum=1;
    fileinfo file;
    private button btn_getmsgcode;
/**倒计时 */
    private text txt_countdowntimer;
 
 
}
 private void awake()
  {
   //获取验证码按钮
      btn_getmsgcode = input_msgcode.transform.find("btn_getverficationcode").getcomponent<button>();
btn_getmsgcode.onclick.addlistener(ongetmsgcodeclick);
 txt_countdowntimer = btn_getmsgcode.transform.find("text").getcomponent<text>();
 
}
 private void onenable()
    {
 
 resetgetmsgcode();
}
/**
     * 获取验证码 
     */
    private void ongetmsgcodeclick()
    {
     
      if (!mloginuipanel.checkgetmsgcode(input_account.text))
      {
        debug.log("没有输入");
        return;
      }
     
      set_phonenum = input_account.text.tostring();
 
      if (limit())
      {
        debug.log("返回true");
        return;
      }
      else
      {
        int timer = 60;
        intervalstream = observable.interval(timespan.fromseconds(1))
          .where(value => { return timer > 1; })
          .subscribetotext(txt_countdowntimer, value =>
          {
 
            btn_getmsgcode.interactable = false;
            
            return (timer--).tostring() + "秒";
          });
        //.addto(this);  //绑定生命周期
 
        timestream = observable.timer(timespan.fromseconds(60))
           .subscribetotext(txt_countdowntimer, _ =>
           {
             btn_getmsgcode.interactable = true;
             return "获取验证码";
           });
        //.addto(this);
      }
 
     
    }
 private bool limit()
    {
      if (playerprefs.haskey(set_phonenum))
      {
        mloginuipanel.showtoast("号码已经被注册过!");
        return true;
      }
      playerprefs.setstring(set_phonenum, set_phonenum);
      //application.streamingassetspath
 
      //获取当前时间天数
      datetime now = datetime.now;
      datetimeoffset nowtimesss = datetimeoffset.now;
      string nowtime = now.day.tostring();
      string filenames = application.datapath+"/num.txt";
     
      //写入当前次数
      //第一次创建文件
      fileinfo file = new fileinfo(filenames);
      if (!file.exists)
      {
      
        file.createtext().close();
        file.creationtimeutc = nowtimesss.utcdatetime;
        debug.log("第一次创建" + file.creationtimeutc);
        string bb = file.creationtime.tostring();
        char[] ss = new char[] {'/'};
        string[] nnn = bb.split(ss);
        debug.log(nnn[1]);
        playerprefs.setstring("filetime", nnn[1]);
        playerprefs.setint("i", 0);
      }
 
    
      if (convert.toint32(nowtime)!=(convert.toint32(playerprefs.getstring("filetime"))))
      {
        //刷新次数
        debug.log("刷新");
        filestream stream = file.open(filenames, filemode.openorcreate, fileaccess.write);
        stream.seek(0, seekorigin.begin);
        stream.setlength(0);
        stream.close();
        //重置次数和时间
        playerprefs.setint("i", 0);
        playerprefs.setstring("filetime", nowtime);
      }
      //再判断次数
      //如果是当天
      if ((convert.toint32(playerprefs.getstring("filetime")))== convert.toint32(nowtime))
      {
        debug.log("执行");
       
 
 
        if (playerprefs.getint("i") > 2)
        {
          debug.log("次数已达上限");
          mloginuipanel.showtoast("次数已达上限,请明天再来!");
 
          return true;
        }
        writeintotxt(usenum, filenames, file);
        //读取本地数据
        readouttxt(filenames);
        //排序
        allmytxt.sort();
        //对当前号码取最大值存入
        playerprefs.setint("i", allmytxt[allmytxt.count - 1]);
        usenum++;
        int a = 3 - allmytxt[allmytxt.count - 1];
        mloginuipanel.showtoast("今天还剩下"+ a+ "次注册机会");
        debug.log(playerprefs.getint("i"));
        // debug.log("当前文件日期" + convert.toint32(playerprefs.getstring("filetime")));
        //先判断时间
        return false;
      }
      
      return false;
    }
 /**
     * reset重置倒计时·
     */
    public void resetgetmsgcode()
    {
      btn_getmsgcode.interactable = true;
      txt_countdowntimer.text = "获取验证码";
    }
 //把所有的数据写入文本中
    public void writeintotxt(int message,string filename,fileinfo file)
    {
 
      // fileinfo file = new fileinfo(filename);
 
      //最后一次修改日期并存储
      
      
 
      if (!file.exists)
      {
        writer = file.createtext();
      }
      else
      {
        writer = file.appendtext();
      }
      writer.writeline(message);
      writer.flush();
      writer.dispose();
      writer.close();
 
      string bb = file.lastaccesstime.tostring();
      char[] ss = new char[] { '/' };
      string[] nnn = bb.split(ss);
 
      debug.log(nnn[1]);
       
        playerprefs.setstring("filetime", nnn[1]);
 
    }
    //读取次数 存储到列表中
    public void readouttxt(string filename)
    {
      allmytxt.clear();
      reader = new streamreader(filename, encoding.utf8);
      string text;
      int line = 0;
      while ((text = reader.readline()) != null)
      {
        ++line;
        
       // allmytxt.add(int.parse(text));
      }
      //利用文本的行数来判断次数
      allmytxt.add(line);
      debug.log(line);
      reader.dispose();
      reader.close();
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网