126 lines
3.7 KiB
PHP

<?php
namespace App\Services;
use App\Constants\HttpEnumCode;
use App\Model\SubTemplate;
use App\Model\SubUser;
use App\Model\User as UserModel;
use App\Model\UserDoctor;
use App\Model\UserDoctorInfo;
use App\Model\UserPatient;
use Extend\Wechat\Wechat;
use Hyperf\Amqp\Result;
use Hyperf\DbConnection\Db;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class UserService extends BaseService
{
/**
* 添加订阅消息
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ClientExceptionInterface
* @throws DecodingExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*/
public function addSubMessage(): array
{
// 下载消息模版
$weChat = new Wechat(1);
$result = $weChat->getTemplate();
if (empty($result)){
Db::rollBack();
return fail();
}
$template = json_decode($result,true);
foreach ($template['data'] as $item){
$params = array();
$params['wx_template_id'] = $item['priTmplId'];
$sub_template = SubTemplate::getOne($params);
if (empty($sub_template)){
// 新增模版
$data = array();
$data['client_type'] = 1;
$data['wx_template_id'] = $item['priTmplId'];
$data['template_title'] = $item['title'];
$data['template_type'] = $item['type'];
$data['template_content'] = $item['content'];
$sub_template = SubTemplate::addSubTemplate($data);
if (empty($sub_template)){
Db::rollBack();
return fail();
}
}
}
return success();
}
/**
* 通过user_id获取用户openid
* @param string|int $user_id
* @param int $user_type
* @return string
*/
public function getOpenIdWithUserId(string|int $user_id,int $user_type): string
{
$open_id = '';
if ($user_type == 1){
// 患者
$params = array();
$params['user_id'] = $user_id;
$user_patient = UserPatient::getOne($params);
if (empty($user_patient)){
return "";
}
if (empty($user_patient['open_id'])){
return "";
}
$open_id = $user_patient['open_id'];
}elseif ($user_type == 2){
// 医生
$params = array();
$params['user_id'] = $user_id;
$user_doctor = UserDoctor::getOne($params);
if (empty($user_doctor)){
return "";
}
if (empty($user_doctor['open_id'])){
return "";
}
$open_id = $user_doctor['open_id'];
}elseif ($user_type == 3){
// 药师
$params = array();
$params['user_id'] = $user_id;
$user_pharmacist = UserPatient::getOne($params);
if (empty($user_pharmacist)){
return "";
}
if (empty($user_pharmacist['open_id'])){
return "";
}
$open_id = $user_pharmacist['open_id'];
}
return $open_id;
}
}