Files
xk-api-yii/admin/controllers/OpenApiController.php
2026-07-03 09:38:52 +08:00

185 lines
6.6 KiB
PHP

<?php
namespace admin\controllers;
use admin\components\BaseAdminController;
use common\events\PrescriptionEvent;
use common\events\ProductOrderEvent;
use common\forms\ProductCancelForm;
use common\forms\ProductRefundForm;
use common\helpers\FuncHelper;
use common\jobs\ProductOrderSyncJob;
use common\jobs\Sms\WaitApprovalMessageJob;
use common\jobs\templateMessage\PrescriptionCreated;
use common\models\oldDb\Prescription;
use common\models\oldDb\PrescriptionLog;
use common\models\oldDb\ProductOrder;
use common\models\oldDb\ProductOrderLog;
use common\models\oldDb\Reconciliation;
use common\models\oldDb\Register;
use common\models\oldDb\Store;
use common\models\oldDb\User;
use common\services\JacErpService;
use common\services\WeappService;
use common\services\WechatService;
use yii\db\ActiveRecord;
use yii\db\Exception;
use yii\helpers\Json;
class OpenApiController extends BaseAdminController
{
public $optional = [
'notify-order-generation',
'send-order',
];
public function actionNotifyOrderGeneration()
{
$prescription = \Yii::$app->request->post('prescription');
//处方开具待支付订阅消息通知
$this->sendUserNote($prescription);
// //处方开具待支付订阅消息通知
// \Yii::$app->queue->push(new PrescriptionCreated([
// 'orderId' => $prescription,
// ]));
//处方生成短信通知药师审方
// \Yii::$app->queue->push(new WaitApprovalMessageJob([
// 'orderId' => $prescription,
// ]));
return ['ok'];
}
public function sendUserNote($orderId)
{
PrescriptionLog::saveLog($orderId,'处方订单生成通知用户支付订阅消息发送队列 start');
$t = \Yii::$app->db->beginTransaction();
try {
$this->setRequest();
$Prescription = Prescription::find()->where([
'id' => $orderId
])->with(['user','doctorInfo','userPatient'])->one();
if (!$Prescription||!$Prescription->user||!$Prescription->doctorInfo) {
throw new \yii\base\Exception('处方不存在或已取消');
}
PrescriptionLog::saveLog($orderId,'处方订单生成通知用户支付订阅消息发送队列 load');
PrescriptionLog::saveLog($orderId,json_encode([
'touser' => $Prescription->user->openid,
'template_id' => \Yii::$app->params['template']['user']['prescription_created'],
'page' => 'subPackages/my/myrecord-detail?id='.$Prescription->id.'&no='.$Prescription->prescription_no,
'data' => [
'name1' => [
'value' => $Prescription->userPatient->name,
],
'thing2' => [
'value' => $Prescription->clinical_diagnose,
],
'name4' => [
'value' => $Prescription->doctorInfo->name,
],
'time9' => [
'value' => date('Y-m-d H:i:s',$Prescription->created_at),
],
'thing11' => [
'value' => '医生已为您开具处方,请尽快前往支付!',
]
]
]));
PrescriptionLog::saveLog($orderId,'处方订单生成通知用户支付订阅消息发送队列 load end');
$result = WechatService::getInstance()->app->subscribe_message->send([
'touser' => $Prescription->user->openid,
'template_id' => \Yii::$app->params['template']['user']['prescription_created'],
'page' => 'subPackages/my/myrecord-detail?id='.$Prescription->id.'&no='.$Prescription->prescription_no,
'data' => [
'name1' => [
'value' => $Prescription->userPatient->name,
],
'thing2' => [
'value' => $Prescription->clinical_diagnose,
],
'name4' => [
'value' => $Prescription->doctorInfo->name,
],
'time9' => [
'value' => date('Y-m-d H:i:s',$Prescription->created_at),
],
'thing11' => [
'value' => '医生已为您开具处方,请尽快前往支付!',
]
]
]);
if(isset($result['errcode']) && $result['errcode']){
throw new Exception('发送失败,失败原因:'.$result['errmsg']);
}
PrescriptionLog::saveLog($orderId,'处方订单生成通知用户支付订阅消息发送队列 end');
} catch (\Exception $exception) {
$t->rollBack();
PrescriptionLog::saveLog($orderId,'处方订单生成通知用户支付订阅消息发送队列异常:'.$exception->getMessage());
return;
}
}
/**
* 处理处方过期
* @return string[]
* @throws \yii\base\Exception
*/
public function prescriptionOver(): array
{
$orderId = \Yii::$app->request->post('order_id');
$userId = \Yii::$app->request->post('user_id');
$type = \Yii::$app->request->post('type');
if($type === 1){
// 自动退款
$ProductRefundForm = new ProductRefundForm();
$ProductRefundForm->refund([
'order_id' => $orderId,
'user_id' => $userId
]);
} else {
// 订单自动失效
$ProductCancelForm = new ProductCancelForm();
$ProductCancelForm->cancel([
'order_id' => $orderId,
'user_id' => $userId
], '处方失效,订单自动失效');
}
return ['ok'];
}
public function actionSendOrder()
{
// 订单自动同步任务
\Yii::$app->queue->push(new ProductOrderSyncJob([
'orderId' => 126,
]));
}
public function getWeChatQrCode()
{
$id = \Yii::$app->request->post('id');
$store = Store::find()->where(['id' => $id])->one();
if (!$store) {
return ['error' => '店铺不存在'];
}
if (!$store->qr_code) {
$scene = 'STORE_QR_CODE'.'_'.$this->id;
$weappService = new WeappService();
$url= $weappService->getQrcode($scene);
$store->qr_code=$url;
$store->saveOrFail();
return ['ok'];
}
return [''];
}
}