对接短信

main
yanzai 7 days ago
parent a414d2160b
commit 658bbc1027

@ -0,0 +1,71 @@
<?php
namespace App\Http\Controllers\API;
use App\Services\TencentSmsApiService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class SmsController
{
public $Lifespan=1;//验证码有效期单位分钟
public function SendCode(Request $request)
{
$phoneNumber = $request->input('tel');
$code = $this->generateCode();
$service = new TencentSmsApiService();
$cha=DB::table('sms')->where(['phone'=>$phoneNumber])->orderBy('id','desc')->first();
if(!!$cha){
$specificTimeTimestamp = strtotime($cha->created_at);
// 计算一分钟之后的时间戳
$oneMinuteLaterTimestamp = strtotime('+1 minute', $specificTimeTimestamp);
if($oneMinuteLaterTimestamp>time()){
return \Yz::echoError1('请稍后再试');
}
}
$end_time = strtotime('+'.$this->Lifespan.' minute', time());
$in=DB::table('sms')->insert([
'code'=>$code,
'phone'=>$phoneNumber,
'status'=>0,
'lifespan'=>$this->Lifespan,
'end_time'=>date('Y-m-d H:i:s', $end_time)
]);
if($in){
$params = [$code,(string)$this->Lifespan];
$ss= $service->send($phoneNumber, $params);
return \Yz::Return(true,'短信验证码发送成功');
}
}
//校验短信验证码
public function CheckCode($phone,$code)
{
$nowtime=date('Y-m-d H:i:s');
$cha=DB::table('sms')->where(['phone'=>$phone,'code'=>$code,'status'=>0,['end_time','>',$nowtime]])->first();
if(!!$cha){
$u=DB::table('sms')->where(['id'=>$cha->id])->update([
'status'=>1
]);
if($u){
return true;
}
}else{
return false;
}
}
// 生成随机验证码
function generateCode($length = 4) {
$characters = '0123456789';
$code = '';
for ($i = 0; $i < $length; $i++) {
$code .= $characters[rand(0, strlen($characters) - 1)];
}
return $code;
}
}

@ -0,0 +1,90 @@
<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
class TencentSmsApiService
{
protected string $secretId;
protected string $secretKey;
protected string $sdkAppId;
protected string $signName;
protected string $templateId;
protected string $endpoint = 'sms.tencentcloudapi.com';
protected string $service = 'sms';
protected string $host = 'sms.tencentcloudapi.com';
protected string $region = 'ap-guangzhou'; // 根据你的需求调整
protected string $version = '2021-01-11';
public function __construct()
{
$this->secretId ="AKIDvxuqiU3OYbR3RcZJrBIDuOegU5YxEQWa";
$this->secretKey = "xnUyGK9lAnHYcS3SecOyLi2GOOOH4h7K";
$this->sdkAppId = "1401058974";
$this->signName ="秦皇岛安尔然";
$this->templateId = "2564483";
}
public function send(string $phoneNumber, array $params = [])
{
$action = 'SendSms';
$timestamp = time();
$date = gmdate('Y-m-d', $timestamp);
// 构造请求体
$body = [
'PhoneNumberSet' => [$phoneNumber],
'SmsSdkAppId' => $this->sdkAppId,
'SignName' => $this->signName,
'TemplateId' => $this->templateId,
];
if (!empty($params)) {
$body['TemplateParamSet'] = $params;
}
$payload = json_encode($body, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
// Canonical Request
$canonicalUri = '/';
$canonicalQueryString = '';
$canonicalHeaders = "content-type:application/json; charset=utf-8\nhost:{$this->host}\n";
$signedHeaders = 'content-type;host';
$hashedPayload = hash('sha256', $payload);
$canonicalRequest = "POST\n{$canonicalUri}\n{$canonicalQueryString}\n{$canonicalHeaders}\n{$signedHeaders}\n{$hashedPayload}";
// String to Sign
$algorithm = 'TC3-HMAC-SHA256';
$date = gmdate('Y-m-d', $timestamp);
$credentialScope = "{$date}/{$this->service}/tc3_request";
$hashedCanonicalRequest = hash('sha256', $canonicalRequest);
$stringToSign = "{$algorithm}\n{$timestamp}\n{$credentialScope}\n{$hashedCanonicalRequest}";
// Signature
$kSecret = 'TC3' . $this->secretKey;
$kDate = hash_hmac('sha256', $date, $kSecret, true);
$kService = hash_hmac('sha256', $this->service, $kDate, true);
$kSigning = hash_hmac('sha256', 'tc3_request', $kService, true);
$signature = hash_hmac('sha256', $stringToSign, $kSigning, true);
$signature = bin2hex($signature); // 转为小写 hex 字符串!
// Authorization
$authorization = "{$algorithm} Credential={$this->secretId}/{$credentialScope}, SignedHeaders={$signedHeaders}, Signature={$signature}";
// 发送请求:必须用 $payload 字符串,不能传数组!
$response = Http::withHeaders([
'Authorization' => $authorization,
'Content-Type' => 'application/json; charset=utf-8',
'Host' => $this->host,
'X-TC-Action' => $action,
'X-TC-Timestamp' => $timestamp,
'X-TC-Version' => $this->version,
'X-TC-Region' => $this->region,
])->withBody($payload, 'application/json; charset=utf-8')
->post("https://{$this->endpoint}");
return $response->json();
}
}

@ -11,7 +11,8 @@
"intervention/image": "^2.7",
"laravel/framework": "^8.75",
"laravel/sanctum": "^2.11",
"laravel/tinker": "^2.5"
"laravel/tinker": "^2.5",
"tencentcloud/tencentcloud-sdk-php": "^3.0"
},
"require-dev": {
"facade/ignition": "^2.5",

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "1294fb7cbbb3f3211351829a6a4deedb",
"content-hash": "0c0f90884756722f08429ef2724c6fae",
"packages": [
{
"name": "asm89/stack-cors",
@ -5697,6 +5697,56 @@
],
"time": "2023-07-13T07:32:46+00:00"
},
{
"name": "tencentcloud/tencentcloud-sdk-php",
"version": "3.0.1497",
"source": {
"type": "git",
"url": "https://github.com/TencentCloud/tencentcloud-sdk-php.git",
"reference": "63691e7e57062726c81e5c05913cd5d67da8ae9d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/TencentCloud/tencentcloud-sdk-php/zipball/63691e7e57062726c81e5c05913cd5d67da8ae9d",
"reference": "63691e7e57062726c81e5c05913cd5d67da8ae9d",
"shasum": ""
},
"require": {
"guzzlehttp/guzzle": "^6.3 || ^7.0",
"php": ">=5.6.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
},
"type": "library",
"autoload": {
"psr-4": {
"TencentCloud\\": "./src/TencentCloud"
},
"classmap": [
"src/QcloudApi/QcloudApi.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"Apache-2.0"
],
"authors": [
{
"name": "coolli",
"email": "tencentcloudapi@tencent.com",
"homepage": "https://cloud.tencent.com/document/sdk/PHP",
"role": "Developer"
}
],
"description": "TencentCloudApi php sdk",
"homepage": "https://github.com/TencentCloud/tencentcloud-sdk-php",
"support": {
"issues": "https://github.com/TencentCloud/tencentcloud-sdk-php/issues",
"source": "https://github.com/TencentCloud/tencentcloud-sdk-php/tree/3.0.1497"
},
"time": "2025-12-01T20:09:08+00:00"
},
{
"name": "tijsverkoyen/css-to-inline-styles",
"version": "2.2.6",

@ -50,7 +50,7 @@
/* */
"mp-weixin" : {
/* */
"appid" : "wx9ba939d2590a8103",
"appid" : "wx166d139c3183e1e0",
"setting" : {
"urlCheck" : false
},

@ -49,7 +49,7 @@ const handleLogin = async () => {
try {
const res = await wx.login()
if (res.code) {
console.log(res.code)
Login({ code: res.code }).then(res => {
if(res.status){
uni.setStorageSync("openid",res.data.openid)

File diff suppressed because one or more lines are too long

@ -6916,9 +6916,9 @@ function isConsoleWritable() {
return isWritable;
}
function initRuntimeSocketService() {
const hosts = "192.168.0.101,127.0.0.1";
const hosts = "192.168.0.103,127.0.0.1";
const port = "8090";
const id = "mp-weixin__KJ8Nq";
const id = "mp-weixin_1frMf2";
const lazy = typeof swan !== "undefined";
let restoreError = lazy ? () => {
} : initOnError();

@ -16,7 +16,6 @@ const _sfc_main = {
try {
const res = await common_vendor.wx$1.login();
if (res.code) {
common_vendor.index.__f__("log", "at pages/index/index.vue:52", res.code);
api_index.Login({ code: res.code }).then((res2) => {
if (res2.status) {
common_vendor.index.setStorageSync("openid", res2.data.openid);

@ -13,7 +13,7 @@
},
"compileType": "miniprogram",
"libVersion": "",
"appid": "wx9ba939d2590a8103",
"appid": "wx166d139c3183e1e0",
"projectname": "xiaochengxu",
"condition": {
"search": {

Loading…
Cancel
Save