VerifyCode.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. *
  6. *
  7. * ----------------------------------------------------------------------------
  8. *
  9. * 数据层模型
  10. */
  11. class VerifyCode extends BaseModel
  12. {
  13. public $page_info;
  14. /**
  15. * 获取验证码列表
  16. * @access public
  17. * @author csdeshang
  18. * @param type $condition
  19. * @param type $pagesize
  20. * @param type $order
  21. * @return type
  22. */
  23. public function getVerifyCodeList($condition, $pagesize = '', $order = 'verify_code_id desc')
  24. {
  25. if ($pagesize) {
  26. $result = Db::name('VerifyCode')->where($condition)->order($order)->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
  27. $this->page_info = $result;
  28. return $result->items();
  29. } else {
  30. return Db::name('VerifyCode')->where($condition)->order($order)->limit(10)->select()->toArray();
  31. }
  32. }
  33. /**
  34. * 取得验证码信息
  35. * @access public
  36. * @author csdeshang
  37. * @param array $condition 检索条件
  38. * @param string $fields 字段
  39. * @param string $order 排序
  40. * @return array
  41. */
  42. public function getVerifyCodeInfo($condition = array(), $fields = '*')
  43. {
  44. return Db::name('VerifyCode')->where($condition)->field($fields)->order('verify_code_id desc')->find();
  45. }
  46. /**
  47. * 添加验证码信息
  48. * @access public
  49. * @author csdeshang
  50. * @param array $data 参数数据
  51. * @return type
  52. */
  53. public function addVerifyCode($data)
  54. {
  55. return Db::name('VerifyCode')->insertGetId($data);
  56. }
  57. /**
  58. * 编辑验证码信息
  59. * @access public
  60. * @author csdeshang
  61. * @param array $data 更新数据
  62. * @param array $condition 条件
  63. * @return bool
  64. */
  65. public function editVerifyCode($data, $condition = array())
  66. {
  67. return Db::name('VerifyCode')->where($condition)->update($data);
  68. }
  69. /**
  70. * 获取验证码数量
  71. * @access public
  72. * @author csdeshang
  73. * @param array $condition 条件
  74. * @return bool
  75. */
  76. public function getVerifyCodeCount($condition = array())
  77. {
  78. return Db::name('VerifyCode')->where($condition)->count();
  79. }
  80. /*
  81. * 发送频率
  82. * @param int $verify_code_type 验证码类型
  83. * @param int $verify_code_user_type 用户类型
  84. * @return array
  85. */
  86. public function isVerifyCodeFrequant($verify_code_type, $verify_code_user_type)
  87. {
  88. $ip = request()->ip();
  89. if ($this->getVerifyCodeCount(array(array('verify_code_ip', '=', $ip), array('verify_code_type', '=', $verify_code_type), array('verify_code_user_type', '=', $verify_code_user_type), array('verify_code_add_time', '>', TIMESTAMP - 60)))) {
  90. return ds_callback(false, '请60秒以后再发');
  91. }
  92. if ($this->getVerifyCodeCount(array(array('verify_code_ip', '=', $ip), array('verify_code_type', '=', $verify_code_type), array('verify_code_user_type', '=', $verify_code_user_type), array('verify_code_add_time', '>', strtotime(date('Y-m-d 0:0:0'))))) > 15) {
  93. return ds_callback(false, '今天验证码已超15条,不能再发送');
  94. }
  95. return ds_callback(true);
  96. }
  97. /*
  98. * 生成验证码
  99. * @param int $verify_code_type 验证码类型
  100. * @param int $verify_code_user_type 用户类型
  101. * @return array
  102. */
  103. public function genVerifyCode($verify_code_type, $verify_code_user_type)
  104. {
  105. $verify_code = str_pad(strval(rand(0, 999999)), 6, '0', STR_PAD_LEFT);
  106. $i = 0;
  107. while ($i < 100 && $this->getVerifyCodeCount(array(array('verify_code', '=', $verify_code), array('verify_code_type', '=', $verify_code_type), array('verify_code_user_type', '=', $verify_code_user_type), array('verify_code_add_time', '>', TIMESTAMP - VERIFY_CODE_INVALIDE_MINUTE * 60)))) {
  108. $verify_code = str_pad(strval(rand(0, 999999)), 6, '0', STR_PAD_LEFT);
  109. $i++;
  110. }
  111. if ($i < 100) {
  112. return $verify_code;
  113. }
  114. return false;
  115. }
  116. }