Commit 2f138082 by 赵增煜

短信测试

parent 67236cbb
......@@ -8,24 +8,46 @@
use App\Models\PharmacyModel;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Services\SmsService;
// 药品控制器
class DrugController extends BaseApiController
{
public function test()
{
$search_input = '测试';
$data = PharmacyDrugModel::where('pharmacy_id', 1)
->whereHas('drug', function ($query) use ($search_input) {
$query->where('name', 'LIKE', "%{$search_input}%")
->orWhere('code', 'LIKE', "%{$search_input}%"); // 这里的'%筛选条件%'是你想要匹配的药品名称
})
->paginate(10);
// $drugs = $data->map(function ($row) {
// return $row->drug;
// });
return $this->success($data);
// $id_card = '342224198909070018';
// if (! validateIdCard($id_card)) {
// return $this->failed('身份证格式错误');
// }
// $drug = DrugModel::query()->find(1);
// $drug->limit_buy_7 = 4;
// $drug->save();
// $drug->limit_buy_7 = 5;
// $drug->save();
$mobile = '18321861540';
$templateName = 'verification_code';
$templateData = ['code' => '1234'];
$smsService = new SmsService();
$response = $smsService->sendSms($mobile, $templateName, $templateData);
return response()->json($response);
return $this->success('ok');
// $search_input = '测试';
// $data = PharmacyDrugModel::where('pharmacy_id', 1)
// ->whereHas('drug', function ($query) use ($search_input) {
// $query->where('name', 'LIKE', "%{$search_input}%")
// ->orWhere('code', 'LIKE', "%{$search_input}%"); // 这里的'%筛选条件%'是你想要匹配的药品名称
// })
// ->paginate(10);
// // $drugs = $data->map(function ($row) {
// // return $row->drug;
// // });
// return $this->success($data);
}
// 药品列表
......@@ -55,13 +77,16 @@ public function drugList(Request $request)
// 药品7日内限购
public function drugLimit(Request $request)
{
// 当前药品未设置则使用全局的7日内限购
$site_config = DB::table('admin_settings')->where('slug', 'site_config')->value('value');
$site_config = json_decode($site_config, true);
$limit_num = $site_config['prescription_limit_buy_7'];
$drug_id = $request->input('drug_id');
// 判断当前药品有没有设置7日内限购
$drug = DrugModel::query()->find($drug_id);
// 当前药品未设置则使用全局的7日内限购
$limit_num = 0;
if ($drug->limit_num == 0) {
if ($drug && $drug->limit_buy_7 > 0) {
$limit_num = $drug->limit_buy_7;
}
return $this->success($limit_num);
......
<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
class SmsService
{
protected $username;
protected $password;
protected $apiUrl;
public function __construct()
{
$this->username = config('sms.sms.username');
$this->password = config('sms.sms.password');
$this->apiUrl = 'https://www.sms-cly.cn/v7/msg/submit.json';
}
/**
* 发送短信
*
* @param string $mobile 手机号码,多个号码用半角逗号分隔
* @param string $templateName 模板名称
* @param array $templateData 模板中的变量数据
* @param string|null $seqid 客户自定义消息ID
* @param string|null $dstime 定时时间,格式:yyyy-MM-dd HH:mm:ss
* @param string|null $ext 用户自定义扩展
* @return array 返回接口的响应
*/
public function sendSms($mobile, $templateName, $templateData = [], $seqid = null, $dstime = null, $ext = null)
{
// 获取模板内容并替换变量
$content = $this->getFormattedContent($templateName, $templateData);
if (!$content) {
return ['resultCode' => '0', 'resultMsg' => '无效的模板名称'];
}
// 生成签名
$sign = md5($this->username . $this->password . $mobile . $content);
// 构造请求数据
$payload = [
'userName' => $this->username,
'sign' => $sign,
'mobile' => $mobile,
'content' => $content,
'seqid' => $seqid,
'dstime' => $dstime,
'ext' => $ext
];
# 记录短信日志
Log::info($this->apiUrl, $payload);
// 发送 HTTP 请求
$response = Http::withoutVerifying()->post($this->apiUrl, $payload);
return $response->json();
}
/**
* 获取格式化的模板内容
*
* @param string $templateName 模板名称
* @param array $templateData 模板数据
* @return string|null 返回替换变量后的内容
*/
protected function getFormattedContent($templateName, $templateData)
{
// 从配置文件中获取模板
$template = Config::get("sms.templates.{$templateName}");
if (!$template) {
return null; // 如果模板不存在,返回null
}
// 使用 Laravel 的 str_replace_array 函数替换变量
foreach ($templateData as $key => $value) {
$template = str_replace(":{$key}", $value, $template);
}
return $template;
}
}
\ No newline at end of file
<?php
return [
"sms"=>[
'username' => env('SMS_USERNAME'),
'password' => env('SMS_PASSWORD'),
],
"templates"=>[
'verification_code' => '【同知堂】您的验证码是::code,请在5分钟内使用。',
'order_confirmation' => '【同知堂】您的订单号为::order_id,订单总金额为::amount 元。',
'shipping_notification' => '【同知堂】您的订单已发货,物流单号为::tracking_number。',
// 添加更多模板...
]
];
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment