VerifyCode.php 4.4 KB

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