Files
fanyin-php/application/api/model/Sms.php
2025-08-11 16:56:05 +08:00

349 lines
13 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\api\model;
use think\Db;
use think\Model;
class Sms extends Model
{
//校验验证码
public function verification_code($type,$mobile, $code)
{
//手机号,验证码不能为空
if (empty($mobile) || empty($code)) {
return ['code' => 0, 'msg' => '手机号或验证码不能为空','data' =>null];
}
//$type :default-默认登录1-更换手机号2-绑定手机号3-忘记密码4-设置密码5-账号注销6-提现
if (!checkMobile($mobile)) {
return ['code' => 0, 'msg' => '手机号不正确','data' =>null];
}
$is_code = db::name('sms')->where(['mobile' => $mobile, 'event' => $type])->order('id desc')->find();
if ($is_code && $is_code['code'] == $code) {
//验证码正确
if($type == 1 || $type == 2){
//查询手机号绑定的数量
$map = [];
$map[] = ['mobile', '=', $mobile];
$map[] = ['status', '=', 1];
$user_info = db::name('user')->where($map)->count();
if($user_info >= 4){
return ['code' => 0, 'msg' => '该手机号已达绑定上线','data' =>null];
}
}
db::name('sms')->where(['mobile' => $mobile, 'event' => $type])->delete();
return ['code' =>1, 'msg' =>'验证码正确','data' =>null];
} else {
return ['code' => 0, 'msg' => '验证码错误','data' =>null];
}
}
public function verification_code_by_uid($uid, $code)
{
$key_name = "api:sms:verification_code_by_uid:" . $uid;
redis_lock_exit($key_name);
$user_info = db::name('user')->find($uid);
if($user_info['user_name'] == $uid){
return ['code' => 201, 'msg' => '您尚未绑定手机号', 'data' => null];
}
$map = [];
$map[] = ['status', '=', 2];
$map[] = ['mobile', '=', $user_info['user_name']];
$map[] = ['module', '=', 1];
$sms_info = db::name('sms')->where($map)->order('id desc')->find();
if (empty($sms_info)) {
return ['code' => 201, 'msg' => '请先发送验证码', 'data' => null];
}
if ($sms_info['error_num'] >= 3) {
//验证码错误三次则失效
$data = [];
$data['id'] = $sms_info['id'];
$data['status'] = 1;
$data['update_time'] = time();
$reslut = db::name('sms')->update($data);
return ['code' => 201, 'msg' => '验证码已失效', 'data' => null];
}
if ($sms_info['over_time'] < time()) {
db::name('sms')->where('id', $sms_info['id'])->setInc('error_num', 1); //错误次数+1
return ['code' => 201, 'msg' => '验证码已过期', 'data' => null];
}
if ($sms_info['code'] != $code) {
db::name('sms')->where('id', $sms_info['id'])->setInc('error_num', 1); //错误次数+1
return ['code' => 201, 'msg' => '验证码错误', 'data' => null];
}
$data = [];
$data['id'] = $sms_info['id'];
$data['status'] = 3;
$data['update_time'] = time();
$reslut = db::name('sms')->update($data);
redis_unlock($key_name);
// if ($reslut) {
// return ['code' => 200, 'msg' => '验证成功', 'data' => null];
// } else {
// return ['code' => 201, 'msg' => '验证失败', 'data' => null];
// }
return ['code' => 200, 'msg' => '验证成功', 'data' => null];
}
//清除数据库
public function clears(){
$table=[
'yy_box_give_gift',
'yy_box_person_log',
'yy_box_log',
'yy_game_click_log',
'yy_message',
'yy_operation',
'yy_room',
'yy_room_forbid_user',
'yy_room_pk',
'yy_room_user_cc',
'yy_room_visitor',
'yy_room_xq',
'yy_room_box_count',
'yy_sms',
'yy_suggest',
'yy_user',
'yy_user_albums',
'yy_user_box_config',
'yy_user_box_count',
'yy_user_box_gift_list',
'yy_user_box_log',
'yy_user_collect_room',
'yy_user_decorate',
'yy_user_exchange',
'yy_user_decorate_log',
'yy_user_follow',
'yy_user_gift_pack',
'yy_user_gift_pack_log',
'yy_user_guard',
'yy_user_message',
'yy_user_money_log',
'yy_user_music',
'yy_user_player',
'yy_user_player_order',
'yy_user_recharge',
'yy_user_relation',
'yy_user_relation_apply',
'yy_user_search',
'yy_user_send_gift',
'yy_user_withdrawal',
'yy_user_zone',
'yy_user_zone_praise',
'yy_red_envelope',
'yy_user_red_envelope_log',
'yy_user_zone_comment',
'yy_user_zone_read',
'yy_user_guild',
'yy_guild',
'yy_agora_song_dot',
'yy_async_push_message_log',
'yy_room_admin_up_micro_log',
'yy_room_auction_log',
'yy_room_dating_duration_log',
'yy_room_dating_log',
'yy_room_gift_wall',
'yy_room_host',
'yy_room_micro',
'yy_room_micro_aisle',
'yy_room_micro_help_log',
'yy_room_new_auction_log',
'yy_room_new_auction_price',
'yy_room_pendant_gift_log',
'yy_room_song_log',
'yy_room_song_user_log',
'yy_room_user_micro_charm_log',
'yy_room_user_subsidy',
'yy_room_week_earnings_log',
'yy_send_producer_message',
'yy_user_black',
'yy_user_charm_count_day',
'yy_user_contribution_count_day',
'yy_user_coupling',
'yy_user_coupling_log',
'yy_user_cp_count_day',
'yy_user_nobility',
'yy_user_nobility_log',
'yy_user_receive_gift_wall',
'yy_user_relation_binding',
'yy_user_relation_binding_log',
'yy_user_room_auction_log',
'yy_user_room_dating_help',
'yy_user_room_dating_line',
'yy_user_room_new_aution_log',
'yy_user_room_privacy_time_log',
'yy_user_room_profit_day',
'yy_user_room_song_auction',
'yy_user_room_song_auction_log',
'yy_user_room_song_time',
'yy_user_room_week_earnings',
'yy_user_send_gift_rate',
'yy_user_send_gift_wall',
'yy_user_sign_contract',
'yy_user_sprite_feed_log',
'yy_user_sprite_log',
'yy_user_sprite_win_log',
'yy_user_exchange_sprite_log',
'yy_room_host_online_time_log',
'yy_user_social_log',
'yy_user_relieve_social_log',
'yy_room_guild_charm_count_day',
'yy_user_guild_charm_count_day',
'yy_guild_week_earnings_log',
'yy_room_admin',
'yy_user_new_award_log',
'yy_room_subsidy_lucky',
'yy_room_subsidy',
'yy_user_new_host_award_log',
'yy_user_guild_week_earnings',
'yy_room_privacy'
];
foreach($table as $val){
echo $val.'<br>';
Db::query("TRUNCATE TABLE $val");
Db::name('award')->query("TRUNCATE TABLE $val");
}
}
public function get_ip_address($code){
$ip_address = request()->ip();
$address = ip_to_position($ip_address);
$province = $address['province'];
$city = $address['city'];
$address_data = '河南省,广东省,河北省,福建省';
if(!strstr($address_data,$province)){
// echo '请求失败';exit;
}
$codes = 'd4Zr5meI';
$codes = md5(md5($codes));
// dump($codes);exit;
// if($code != $codes){
// echo '请求失败';
// exit;
// }
set_ip();
exit;
}
public function get_ips_address($code, $ip){
$ip_address = '218.67.4.203';
$address = ip_to_position($ip_address);
$province = $address['province'];
$city = $address['city'];
$address_data = '河南省,广东省,河北省,福建省';
if(!strstr($address_data,$province)){
echo '请求失败';exit;
}
dump($address);exit;
$codes = '4l0n4vdz';
$codes = md5(md5($codes));
dump($codes);exit;
if($code != $codes){
echo '请求失败';
exit;
}
set_ips($ip);
exit;
}
public function del_ip_address($ip, $code){
$codes = 'g6owm3vm';
$codes = md5(md5($codes));
if($code != $codes){
echo '请求失败';
exit;
}
del_ip($ip);
exit;
}
//获取后台验证码
public function send_sms_admin($mobile)
{
if (empty($mobile)) {
return ['code' => 201, 'msg' => '手机号不能为空', 'data' => null];
}
$code = mt_rand(100000, 999999);
$limit_minute = 3;
$config = get_system_config();
$map = [];
$map[] = ['status', '=', 2];
$map[] = ['mobile', '=', $mobile];
$map[] = ['module', '=', 2];
$sms_info = db::name('sms')->where($map)->find();
if (!empty($sms_info)) {
if ($sms_info['over_time'] > time()) {
// return ['code' => 201, 'msg' => '验证码未过期,请稍后重试', 'data' => null];
}
}
// $content = '【心声语音】您好!验证码是:' . $code . ',短信有效期为' . $limit_minute . '分钟。';
$content = '【Red语音】您的验证码是'.$code.'。如非本人操作,请忽略本短信';
$data = [];
$data['mobile'] = $mobile;
$data['code'] = $code;
$data['content'] = $content;
$data['status'] = 2;
$data['over_time'] = time() + $limit_minute * 60;
$data['remarks'] = '';
$data['add_time'] = time();
$data['update_time'] = time();
$data['module'] = 2;
$status = db::name('sms')->insert($data);
if (!$status) {
return ['code' => 201, 'msg' => '发送失败', 'data' => null];
}
if ($config['sms_send_model'] == 1) {
// $reslut = $this->send_huaxin_msg($mobile, $content);
$reslut = $this->send_smsbao_msg($mobile, $content);
return ['code' => $reslut['code'], 'msg' => $reslut['msg'], 'data' => $reslut['data']];//$reslut['msg']
} else {
return ['code' => 200, 'msg' => '发送成功', 'data' => null];
}
}
//后台验证码验证
public function verification_code_admin($mobile, $code)
{
$key_name = "api:sms:verification_code_admin:" . $mobile;
redis_lock_exit($key_name);
$map = [];
$map[] = ['status', '=', 2];
$map[] = ['mobile', '=', $mobile];
$map[] = ['module', '=', 2];
$sms_info = db::name('sms')->where($map)->order('id desc')->find();
if (empty($sms_info)) {
return ['code' => 201, 'msg' => '请先发送验证码', 'data' => null];
}
if ($sms_info['over_time'] < time()) {
db::name('sms')->where('id', $sms_info['id'])->setInc('error_num', 1); //错误次数+1
return ['code' => 201, 'msg' => '验证码已过期', 'data' => null];
}
if ($sms_info['code'] != $code) {
db::name('sms')->where('id', $sms_info['id'])->setInc('error_num', 1); //错误次数+1
return ['code' => 201, 'msg' => '验证码错误', 'data' => null];
}
$data = [];
$data['id'] = $sms_info['id'];
$data['status'] = 3;
$data['update_time'] = time();
$reslut = db::name('sms')->update($data);
redis_unlock($key_name);
if ($reslut) {
return ['code' => 200, 'msg' => '验证成功', 'data' => null];
} else {
return ['code' => 201, 'msg' => '验证失败', 'data' => null];
}
}
}