Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
赵增煜
/
tzt-admin
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
2f138082
authored
Nov 15, 2024
by
赵增煜
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
短信测试
parent
67236cbb
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
141 additions
and
15 deletions
+141
-15
app/Api/Controllers/DrugController.php
+40
-15
app/Services/SmsService.php
+86
-0
config/sms.php
+15
-0
No files found.
app/Api/Controllers/DrugController.php
View file @
2f138082
...
...
@@ -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
);
...
...
app/Services/SmsService.php
0 → 100644
View file @
2f138082
<?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
config/sms.php
0 → 100644
View file @
2f138082
<?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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment