VerifyCode.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  9. * 网站地址: https://www.valimart.net/
  10. * ----------------------------------------------------------------------------
  11. *
  12. * ============================================================================
  13. * 数据层模型
  14. */
  15. class VerifyCode extends BaseModel {
  16. public $page_info;
  17. /**
  18. * 获取验证码列表
  19. * @access public
  20. * @author csdeshang
  21. * @param type $condition
  22. * @param type $pagesize
  23. * @param type $order
  24. * @return type
  25. */
  26. public function getVerifyCodeList($condition, $pagesize = '', $order = 'verify_code_id desc') {
  27. if ($pagesize) {
  28. $result = Db::name('VerifyCode')->where($condition)->order($order)->paginate(['list_rows'=>$pagesize,'query' => request()->param()],false);
  29. $this->page_info = $result;
  30. return $result->items();
  31. } else {
  32. return Db::name('VerifyCode')->where($condition)->order($order)->limit(10)->select()->toArray();
  33. }
  34. }
  35. /**
  36. * 取得验证码信息
  37. * @access public
  38. * @author csdeshang
  39. * @param array $condition 检索条件
  40. * @param string $fields 字段
  41. * @param string $order 排序
  42. * @return array
  43. */
  44. public function getVerifyCodeInfo($condition = array(), $fields = '*') {
  45. return Db::name('VerifyCode')->where($condition)->field($fields)->order('verify_code_id desc')->find();
  46. }
  47. /**
  48. * 添加验证码信息
  49. * @access public
  50. * @author csdeshang
  51. * @param array $data 参数数据
  52. * @return type
  53. */
  54. public function addVerifyCode($data) {
  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. return Db::name('VerifyCode')->where($condition)->update($data);
  67. }
  68. /**
  69. * 获取验证码数量
  70. * @access public
  71. * @author csdeshang
  72. * @param array $condition 条件
  73. * @return bool
  74. */
  75. public function getVerifyCodeCount($condition = array()) {
  76. return Db::name('VerifyCode')->where($condition)->count();
  77. }
  78. /*
  79. * 发送频率
  80. * @param int $verify_code_type 验证码类型
  81. * @param int $verify_code_user_type 用户类型
  82. * @return array
  83. */
  84. public function isVerifyCodeFrequant($verify_code_type, $verify_code_user_type) {
  85. $ip = request()->ip();
  86. 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)))) {
  87. return ds_callback(false, '请60秒以后再发');
  88. }
  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','>', strtotime(date('Y-m-d 0:0:0'))))) > 15) {
  90. return ds_callback(false, '今天验证码已超15条,不能再发送');
  91. }
  92. return ds_callback(true);
  93. }
  94. /*
  95. * 生成验证码
  96. * @param int $verify_code_type 验证码类型
  97. * @param int $verify_code_user_type 用户类型
  98. * @return array
  99. */
  100. public function genVerifyCode($verify_code_type, $verify_code_user_type) {
  101. $verify_code = str_pad(strval(rand(0, 999999)), 6, '0', STR_PAD_LEFT);
  102. $i = 0;
  103. 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)))) {
  104. $verify_code = str_pad(strval(rand(0, 999999)), 6, '0', STR_PAD_LEFT);
  105. $i++;
  106. }
  107. if ($i < 100) {
  108. return $verify_code;
  109. }
  110. return false;
  111. }
  112. }
  113. ?>