327 lines
9.6 KiB
PHP
327 lines
9.6 KiB
PHP
<?php
|
||
|
||
namespace App\Services;
|
||
|
||
use App\Constants\HttpEnumCode;
|
||
use App\Model\SubTemplate;
|
||
use App\Model\User;
|
||
use App\Model\User as UserModel;
|
||
use App\Model\UserDoctor;
|
||
use App\Model\UserDoctorInfo;
|
||
use App\Model\UserPatient;
|
||
use App\Utils\PcreMatch;
|
||
use Extend\TencentIm\Profile;
|
||
use Extend\Wechat\Wechat;
|
||
use Hyperf\Amqp\Result;
|
||
use Hyperf\DbConnection\Db;
|
||
use Hyperf\Redis\Redis;
|
||
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();
|
||
}
|
||
|
||
/**
|
||
* 修改用户头像
|
||
* @return array
|
||
*/
|
||
public function putUserAvatar(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$avatar = $this->request->input('avatar');
|
||
|
||
// 获取用户信息
|
||
$params = array();
|
||
$params['user_id'] = $user_info['user_id'];
|
||
$user = User::getOne($params);
|
||
if (empty($user)){
|
||
return fail();
|
||
}
|
||
|
||
// 匹配去除oss网址
|
||
$avatar = PcreMatch::pregRemoveOssWebsite($avatar);
|
||
|
||
Db::beginTransaction();
|
||
try {
|
||
// 修改用户表
|
||
$data = array();
|
||
$data['avatar'] = $avatar;
|
||
|
||
$params = array();
|
||
$params['user_id'] = $user_info['user_id'];
|
||
User::editUser($params,$data);
|
||
|
||
// 获取对应用户信息
|
||
if ($user['user_type'] == 1){
|
||
// 患者
|
||
$params = array();
|
||
$params['user_id'] = $user_info['user_id'];
|
||
$user_patient = UserPatient::getOne($params);
|
||
if (empty($user_patient)){
|
||
Db::rollBack();
|
||
return fail();
|
||
}
|
||
|
||
if ($user_patient['avatar'] != $avatar) {
|
||
$data = array();
|
||
$data['avatar'] = $avatar;
|
||
|
||
$params = array();
|
||
$params['patient_id'] = $user_patient['patient_id'];
|
||
UserPatient::editUserPatient($params,$data);
|
||
}
|
||
}elseif ($user['user_type'] == 2){
|
||
// 医生
|
||
$params = array();
|
||
$params['user_id'] = $user_info['user_id'];
|
||
$user_doctor = UserDoctor::getOne($params);
|
||
if (empty($user_doctor)){
|
||
Db::rollBack();
|
||
return fail();
|
||
}
|
||
|
||
if ($user_doctor['avatar'] != $avatar) {
|
||
$data = array();
|
||
$data['avatar'] = $avatar;
|
||
|
||
$params = array();
|
||
$params['doctor_id'] = $user_doctor['doctor_id'];
|
||
UserDoctor::editUserDoctor($params,$data);
|
||
}
|
||
}elseif ($user['user_type'] == 3){
|
||
// 药师
|
||
$params = array();
|
||
$params['user_id'] = $user_info['user_id'];
|
||
$user_pharmacist = UserPatient::getOne($params);
|
||
if (empty($user_pharmacist)){
|
||
Db::rollBack();
|
||
return fail();
|
||
}
|
||
|
||
if ($user_pharmacist['avatar'] != $avatar) {
|
||
$data = array();
|
||
$data['avatar'] = $avatar;
|
||
|
||
$params = array();
|
||
$params['pharmacist_id'] = $user_pharmacist['pharmacist_id'];
|
||
UserPatient::editUserPatient($params,$data);
|
||
}
|
||
}
|
||
|
||
// 修改im头像
|
||
$profile = new Profile();
|
||
$arg = array();
|
||
$arg['Tag_Profile_IM_Image'] = addAliyunOssWebsite($avatar);
|
||
$profile->setProfile($user_info['user_id'],$arg);
|
||
|
||
Db::commit();
|
||
} catch (\Exception $e) {
|
||
Db::rollBack();
|
||
return fail(HttpEnumCode::HTTP_ERROR, $e->getMessage());
|
||
}
|
||
|
||
return success();
|
||
}
|
||
|
||
/**
|
||
* 修改用户名
|
||
* @return array
|
||
*/
|
||
public function putUserName(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$user_name = $this->request->input('user_name');
|
||
|
||
// 获取用户信息
|
||
$params = array();
|
||
$params['user_id'] = $user_info['user_id'];
|
||
$user = User::getOne($params);
|
||
if (empty($user)){
|
||
return fail();
|
||
}
|
||
|
||
if ($user['user_type'] != 1){
|
||
return fail(HttpEnumCode::HTTP_ERROR,"禁止修改");
|
||
}
|
||
|
||
if ($user['user_name'] == $user_name){
|
||
return success();
|
||
}
|
||
|
||
Db::beginTransaction();
|
||
try {
|
||
// 修改用户表
|
||
$data = array();
|
||
$data['user_name'] = $user_name;
|
||
|
||
$params = array();
|
||
$params['user_id'] = $user['user_id'];
|
||
User::editUser($params,$data);
|
||
|
||
// 修改对应用户表
|
||
if ($user['user_type'] == 1){
|
||
// 患者
|
||
$data = array();
|
||
$data['user_name'] = $user_name;
|
||
|
||
$params = array();
|
||
$params['user_id'] = $user['user_id'];
|
||
UserPatient::editUserPatient($params,$data);
|
||
}
|
||
|
||
Db::commit();
|
||
} catch (\Exception $e) {
|
||
Db::rollBack();
|
||
return fail(HttpEnumCode::SERVER_ERROR, $e->getMessage());
|
||
}
|
||
|
||
return success();
|
||
}
|
||
|
||
/**
|
||
* 退出登陆
|
||
* @return array
|
||
* @throws ContainerExceptionInterface
|
||
* @throws NotFoundExceptionInterface
|
||
*/
|
||
public function putLoginout(){
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
// 获取用户信息
|
||
$params = array();
|
||
$params['user_id'] = $user_info['user_id'];
|
||
$user = User::getOne($params);
|
||
if (empty($user)){
|
||
return fail();
|
||
}
|
||
|
||
// 获取token
|
||
$bearer_token = $this->request->getHeader('Authorization');
|
||
if (empty($bearer_token)){
|
||
return fail();
|
||
}
|
||
|
||
$token = explode(' ', $bearer_token[0]);
|
||
if (!isset($token[1])){
|
||
return fail();
|
||
}
|
||
|
||
$redis = $this->container->get(Redis::class);
|
||
|
||
// 旧token加入黑名单 5天有效期,5天内,无法继续进行访问
|
||
$res = $redis->set('jwt_black_' . $token[1], time(), 60*60*24*5);
|
||
if (!$res) {
|
||
return fail(HttpEnumCode::SERVER_ERROR);
|
||
}
|
||
|
||
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;
|
||
}
|
||
} |