1253 lines
41 KiB
PHP
1253 lines
41 KiB
PHP
<?php
|
||
|
||
namespace App\Services;
|
||
|
||
use App\Amqp\Producer\UserImOffDelayDirectProducer;
|
||
use App\Constants\HttpEnumCode;
|
||
use App\Model\Area;
|
||
use App\Model\MessageIm;
|
||
use App\Model\OrderInquiry;
|
||
use App\Model\Popup;
|
||
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\UserLocation;
|
||
use App\Model\UserPatient;
|
||
use App\Model\UserShipAddress;
|
||
use App\Model\UserSystem;
|
||
use App\Utils\Log;
|
||
use App\Utils\Mask;
|
||
use App\Utils\PcreMatch;
|
||
use Extend\Tencent\map\Location;
|
||
use Extend\TencentIm\Profile;
|
||
use Extend\Wechat\Wechat;
|
||
use GuzzleHttp\Exception\GuzzleException;
|
||
use Hyperf\Amqp\Producer;
|
||
use Hyperf\Amqp\Result;
|
||
use Hyperf\DbConnection\Db;
|
||
use Hyperf\Redis\Redis;
|
||
use Hyperf\Utils\WaitGroup;
|
||
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
|
||
* @throws GuzzleException
|
||
*/
|
||
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();
|
||
}
|
||
|
||
if ($avatar == $user['avatar']){
|
||
return success();
|
||
}
|
||
|
||
// 匹配去除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(): array
|
||
{
|
||
$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();
|
||
}
|
||
|
||
/**
|
||
* 获取用户收获地址列表
|
||
* @return array
|
||
*/
|
||
public function getUserAddress(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$params = array();
|
||
$params['user_id'] = $user_info['user_id'];
|
||
$user_ship_address = UserShipAddress::getList($params);
|
||
if (empty($user_ship_address)) {
|
||
return success();
|
||
}
|
||
|
||
return success($user_ship_address->toArray());
|
||
}
|
||
|
||
/**
|
||
* 获取用户收获地址详情
|
||
* @return array
|
||
*/
|
||
public function getUserAddressInfo(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$address_id = $this->request->route('address_id');
|
||
|
||
$params = array();
|
||
$params['address_id'] = $address_id;
|
||
$params['user_id'] = $user_info['user_id'];
|
||
$user_ship_address = UserShipAddress::getOne($params);
|
||
if (empty($user_ship_address)) {
|
||
return fail();
|
||
}
|
||
|
||
return success($user_ship_address->toArray());
|
||
}
|
||
|
||
/**
|
||
* 添加用户收获地址
|
||
* @return array
|
||
*/
|
||
public function addUserAddress(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$request_params = $this->request->all();
|
||
|
||
$areaService = new AreaService();
|
||
$req = $areaService->checkAreaById($request_params['province_id'], $request_params['city_id'], $request_params['county_id']);
|
||
if (empty($req)) {
|
||
return fail(HttpEnumCode::HTTP_ERROR, "地区选择错误");
|
||
}
|
||
|
||
$area = $areaService->getAreaById($request_params['province_id'], $request_params['city_id'], $request_params['county_id']);
|
||
|
||
Db::beginTransaction();
|
||
|
||
try {
|
||
if ($request_params['is_default'] == 1) {
|
||
// 获取默认地址
|
||
$params = array();
|
||
$params['user_id'] = $user_info['user_id'];
|
||
$params['is_default'] = $request_params['is_default'];
|
||
$user_ship_address = UserShipAddress::getOne($params);
|
||
if (!empty($user_ship_address)) {
|
||
$data = array();
|
||
$data['is_default'] = 0;
|
||
|
||
$params = array();
|
||
$params['address_id'] = $user_ship_address['address_id'];
|
||
$params['user_id'] = $user_info['user_id'];
|
||
UserShipAddress::edit($params, $data);
|
||
}
|
||
}
|
||
|
||
// 新增
|
||
$data = array();
|
||
$data['user_id'] = $user_info['user_id'];
|
||
$data['province_id'] = $request_params['province_id'];
|
||
$data['province'] = $area['province']['area_name'];
|
||
$data['city_id'] = $request_params['city_id'];
|
||
$data['city'] = $area['city']['area_name'];
|
||
$data['county_id'] = $request_params['county_id'];
|
||
$data['county'] = $area['county']['area_name'];
|
||
$data['address'] = $request_params['address'];
|
||
$data['address_mask'] = Mask::maskAddressStr($request_params['address']);
|
||
$data['consignee_name'] = $request_params['consignee_name'];
|
||
$data['consignee_name_mask'] = Mask::maskNameStr($request_params['consignee_name']);
|
||
$data['consignee_tel'] = $request_params['consignee_tel'];
|
||
$data['consignee_tel_mask'] = Mask::maskPhoneStr($request_params['consignee_tel']);
|
||
$data['is_default'] = $request_params['is_default'];
|
||
$data['tag'] = $request_params['tag'] ?? 4;
|
||
$user_ship_address = UserShipAddress::addUserShipAddress($data);
|
||
if (empty($user_ship_address)) {
|
||
Db::rollBack();
|
||
return fail(HttpEnumCode::SERVER_ERROR);
|
||
}
|
||
|
||
$user_ship_address = $user_ship_address->toArray();
|
||
$user_ship_address['address_id'] = (string)$user_ship_address['address_id'];
|
||
Db::commit();
|
||
} catch (\Exception $e) {
|
||
Db::rollBack();
|
||
return fail(HttpEnumCode::HTTP_ERROR, $e->getMessage());
|
||
}
|
||
|
||
return success($user_ship_address);
|
||
}
|
||
|
||
/**
|
||
* 修改用户收获地址
|
||
* @return array
|
||
*/
|
||
public function putUserAddress(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$request_params = $this->request->all();
|
||
$address_id = $this->request->route('address_id');
|
||
|
||
$params = array();
|
||
$params['address_id'] = $address_id;
|
||
$params['user_id'] = $user_info['user_id'];
|
||
$user_ship_address = UserShipAddress::getOne($params);
|
||
if (empty($user_ship_address)) {
|
||
return fail();
|
||
}
|
||
|
||
$data = array();
|
||
|
||
if ($user_ship_address['province_id'] != $request_params['province_id'] || $user_ship_address['city_id'] != $request_params['city_id'] || $user_ship_address['county_id'] != $request_params['county_id']) {
|
||
$areaService = new AreaService();
|
||
$req = $areaService->checkAreaById($request_params['province_id'], $request_params['city_id'], $request_params['county_id']);
|
||
if (empty($req)) {
|
||
return fail(HttpEnumCode::HTTP_ERROR, "地区选择错误");
|
||
}
|
||
|
||
$area = $areaService->getAreaById($request_params['province_id'], $request_params['city_id'], $request_params['county_id']);
|
||
|
||
$data['province_id'] = $request_params['province_id'];
|
||
$data['province'] = $area['province']['area_name'];
|
||
$data['city_id'] = $request_params['city_id'];
|
||
$data['city'] = $area['city']['area_name'];
|
||
$data['county_id'] = $request_params['county_id'];
|
||
$data['county'] = $area['county']['area_name'];
|
||
}
|
||
|
||
if ($user_ship_address['address'] != $request_params['address']) {
|
||
$data['address'] = $request_params['address'];
|
||
$data['address_mask'] = Mask::maskAddressStr($request_params['address']);
|
||
}
|
||
|
||
if ($user_ship_address['consignee_name'] != $request_params['consignee_name']) {
|
||
$data['consignee_name'] = $request_params['consignee_name'];
|
||
$data['consignee_name_mask'] = Mask::maskNameStr($request_params['consignee_name']);
|
||
}
|
||
|
||
if ($user_ship_address['consignee_tel'] != $request_params['consignee_tel']) {
|
||
$data['consignee_tel'] = $request_params['consignee_tel'];
|
||
$data['consignee_tel_mask'] = Mask::maskPhoneStr($request_params['consignee_tel']);
|
||
}
|
||
|
||
if (isset($request_params['tag'])){
|
||
if ($user_ship_address['tag'] != $request_params['tag']) {
|
||
$data['tag'] = $request_params['tag'];
|
||
}
|
||
}
|
||
|
||
if ($user_ship_address['is_default'] != $request_params['is_default']) {
|
||
$data['is_default'] = $request_params['is_default'];
|
||
}
|
||
|
||
Db::beginTransaction();
|
||
try {
|
||
if ($request_params['is_default'] == 1 && $user_ship_address['is_default'] != $request_params['is_default']) {
|
||
// 获取默认地址
|
||
$params = array();
|
||
$params['user_id'] = $user_info['user_id'];
|
||
$params['is_default'] = $request_params['is_default'];
|
||
$default_user_ship_address = UserShipAddress::getOne($params);
|
||
if (!empty($default_user_ship_address)) {
|
||
$default_data = array();
|
||
$default_data['is_default'] = 0;
|
||
|
||
$params = array();
|
||
$params['address_id'] = $default_user_ship_address['address_id'];
|
||
$params['user_id'] = $user_info['user_id'];
|
||
UserShipAddress::edit($params, $default_data);
|
||
}
|
||
}
|
||
|
||
if (!empty($data)) {
|
||
$params = array();
|
||
$params['address_id'] = $user_ship_address['address_id'];
|
||
UserShipAddress::edit($params, $data);
|
||
}
|
||
|
||
Db::commit();
|
||
} catch (\Exception $e) {
|
||
Db::rollBack();
|
||
return fail(HttpEnumCode::HTTP_ERROR, $e->getMessage());
|
||
}
|
||
|
||
return success();
|
||
}
|
||
|
||
/**
|
||
* 删除用户收获地址
|
||
* @return array
|
||
*/
|
||
public function deleteUserAddress(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$address_id = $this->request->route('address_id');
|
||
|
||
$params = array();
|
||
$params['address_id'] = $address_id;
|
||
$params['user_id'] = $user_info['user_id'];
|
||
$user_ship_address = UserShipAddress::getOne($params);
|
||
if (empty($user_ship_address)) {
|
||
return fail();
|
||
}
|
||
|
||
$res = UserShipAddress::deleteUserShipAddress($params);
|
||
if (!$res){
|
||
return fail(HttpEnumCode::SERVER_ERROR);
|
||
}
|
||
|
||
return success();
|
||
}
|
||
|
||
/**
|
||
* 获取弹窗数据
|
||
* @return array
|
||
*/
|
||
public function getUserPopup(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
$popup_type = $this->request->input('popup_type');
|
||
|
||
if (empty($user_info)){
|
||
// 未登陆
|
||
return success();
|
||
}
|
||
|
||
$params = array();
|
||
$params['user_id'] = $user_info['user_id'];
|
||
$params['popup_type'] = $popup_type;
|
||
$params['status'] = 0;
|
||
$popup = Popup::getList($params);
|
||
if (empty($popup)){
|
||
return success();
|
||
}
|
||
|
||
foreach ($popup as $key => $item){
|
||
$popup[$key]['popup_content'] = json_decode($item['popup_content']);
|
||
|
||
// 修改为已读
|
||
$params = array();
|
||
$params['popup_id'] = $item['popup_id'];
|
||
|
||
$data = array();
|
||
$data['status'] = 1;
|
||
Popup::edit($params,$data);
|
||
}
|
||
|
||
return success($popup->toArray());
|
||
}
|
||
|
||
/**
|
||
* 上报用户地址
|
||
* @return array
|
||
*/
|
||
public function postLocation(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
$request_params = $this->request->all();
|
||
|
||
if (empty($request_params['lon']) || empty($request_params['lat'])){
|
||
return success();
|
||
}
|
||
|
||
// 处理地址逆解析
|
||
try {
|
||
$location = new Location();
|
||
$result = $location->getLocation($request_params['lon'],$request_params['lat']);
|
||
if (empty($result)){
|
||
return success();
|
||
}
|
||
|
||
if (empty($result['address_component'])){
|
||
return success();
|
||
}
|
||
|
||
if (empty($result['address_component']['province']) || empty($result['address_component']['city'])){
|
||
return success();
|
||
}
|
||
|
||
// 处理需修改/新增数据
|
||
$data = array();
|
||
$data['lon'] = $request_params['lon'];
|
||
$data['lat'] = $request_params['lat'];
|
||
$data['province'] = $result['address_component']['province'];
|
||
$data['city'] = $result['address_component']['city'];
|
||
$data['county'] = $result['address_component']['district'] ?: "";
|
||
$data['address'] = $result['address_component']['street_number'] ?: "";
|
||
|
||
// 获取用户地理位置
|
||
$params = array();
|
||
$params['user_id'] = $user_info['user_id'];
|
||
$user_location = UserLocation::getOne($params);
|
||
if (empty($user_location)){
|
||
// 新增
|
||
$data['user_id'] = $user_info['user_id'];
|
||
$user_location = UserLocation::addUserLocation($data);
|
||
if (empty($user_location)){
|
||
return success();
|
||
}
|
||
}else{
|
||
// 修改
|
||
$params = array();
|
||
$params['user_id'] = $user_info['user_id'];
|
||
$res = UserLocation::editUserLocation($params,$data);
|
||
if (!$res){
|
||
return success();
|
||
}
|
||
}
|
||
}catch (\Exception $e){
|
||
dump($e->getMessage());
|
||
return success();
|
||
}
|
||
|
||
return success();
|
||
}
|
||
|
||
/**
|
||
* 获取用户地址
|
||
* @return array
|
||
*/
|
||
public function getLocation(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
// 定义返回数据
|
||
$area = [
|
||
"province_id" => "",
|
||
"province" => "",
|
||
"city_id" => "",
|
||
"city" => "",
|
||
"county_id" => "",
|
||
"county" => "",
|
||
];
|
||
|
||
$params = array();
|
||
$params['user_id'] = $user_info['user_id'];
|
||
$user_location = UserLocation::getOne($params);
|
||
if (empty($user_location)){
|
||
return success($area);
|
||
}
|
||
|
||
// 处理省市区对应
|
||
if (!empty($user_location['province']) && !empty($user_location['city'])){
|
||
$params = array();
|
||
$params['area_name'] = $user_location['province'];
|
||
$params['area_type'] = 2;
|
||
$area_province = Area::getOne($params);
|
||
if (!empty($area_province)){
|
||
$area['province_id'] = $area_province['area_id'];
|
||
$area['province'] = $area_province['area_name'];
|
||
}
|
||
|
||
if (!empty($area['province_id'])){
|
||
$params = array();
|
||
$params['area_name'] = $user_location['city'];
|
||
$params['parent_id'] = $area['province_id'];
|
||
$params['area_type'] = 3;
|
||
$area_city = Area::getOne($params);
|
||
if (!empty($area_city)){
|
||
$area['city_id'] = $area_city['area_id'];
|
||
$area['city'] = $area_city['area_name'];
|
||
}
|
||
}
|
||
}
|
||
|
||
if (!empty($area['city_id']) && !empty($user_location['county'])){
|
||
$params = array();
|
||
$params['area_name'] = $user_location['county'];
|
||
$params['parent_id'] = $area['city_id'];
|
||
$params['area_type'] = 4;
|
||
$area_county = Area::getOne($params);
|
||
if (!empty($area_county)){
|
||
$area['county_id'] = $area_county['area_id'];
|
||
$area['county'] = $area_county['area_name'];
|
||
}
|
||
}
|
||
|
||
return success($area);
|
||
}
|
||
|
||
/**
|
||
* 获取用户配置
|
||
* @return array
|
||
*/
|
||
public function getUserSystem(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
|
||
// 定义返回数据
|
||
$result = array(
|
||
"is_accept_im_message_push" => 0,
|
||
);
|
||
|
||
// 获取用户配置数据
|
||
$params = array();
|
||
$params['user_id'] = $user_info['user_id'];
|
||
$user_system = UserSystem::getOne($params);
|
||
if (empty($user_system)){
|
||
return success($result);
|
||
}
|
||
|
||
$result['is_accept_im_message_push'] = $user_system['is_accept_im_message_push'];
|
||
return success($result);
|
||
}
|
||
|
||
/**
|
||
* 修改用户配置
|
||
* @return array
|
||
*/
|
||
public function putUserSystem(): array
|
||
{
|
||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||
$request_params = $this->request->all();
|
||
|
||
// 获取用户配置数据
|
||
$params = array();
|
||
$params['user_id'] = $user_info['user_id'];
|
||
$user_system = UserSystem::getOne($params);
|
||
if (empty($user_system)){
|
||
// 新增
|
||
$data = array();
|
||
$data['user_id'] = $user_info['user_id'];
|
||
$data['is_accept_im_message_push'] = $request_params['is_accept_im_message_push'];
|
||
$user_system = UserSystem::addUserSystem($data);
|
||
if (empty($user_system)){
|
||
return fail();
|
||
}
|
||
}else{
|
||
// 修改
|
||
$data = array();
|
||
|
||
if ($user_system['is_accept_im_message_push'] != $request_params['is_accept_im_message_push']){
|
||
$data['is_accept_im_message_push'] = $request_params['is_accept_im_message_push'];
|
||
}
|
||
|
||
if (!empty($data)){
|
||
$params = array();
|
||
$params['user_system_id'] = $user_system['user_system_id'];
|
||
|
||
$res = UserSystem::edit($params,$data);
|
||
if (!$res){
|
||
return fail();
|
||
}
|
||
}
|
||
}
|
||
return success();
|
||
}
|
||
|
||
/**
|
||
* 通过user_id获取用户openid
|
||
* @param string|int $user_id
|
||
* @param int|string $user_type
|
||
* @return string
|
||
*/
|
||
public function getOpenIdWithUserId(string|int $user_id, int|string $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;
|
||
}
|
||
|
||
/**
|
||
* 获取用户默认头像oss地址
|
||
* @param int|string $user_type
|
||
* @return string
|
||
*/
|
||
public function getUserDefaultOssAvatar(int|string $user_type): string
|
||
{
|
||
$avatar = "";
|
||
if (empty($user_type)){
|
||
return $avatar;
|
||
}
|
||
|
||
if ($user_type == 1){
|
||
// 患者
|
||
$avatar = "/basic/file/patient_avatar.png";
|
||
}elseif ($user_type == 2){
|
||
// 医生
|
||
$avatar = "/basic/file/doctor_avatar.png";
|
||
}
|
||
|
||
return $avatar;
|
||
}
|
||
|
||
/**
|
||
* 用户im消息通知
|
||
* @param int|string $user_id
|
||
* @param string|int $order_inquiry_id
|
||
* @param array $msg_body
|
||
* @return bool
|
||
*/
|
||
public function userImMessageNotice(int|string $user_id,string|int $order_inquiry_id,array $msg_body): bool
|
||
{
|
||
try {
|
||
// 获取用户数据
|
||
$params = array();
|
||
$params['user_id'] = $user_id;
|
||
$user = User::getOne($params);
|
||
if (empty($user)){
|
||
return false;
|
||
}
|
||
|
||
// 获取用户配置
|
||
$params = array();
|
||
$params['user_id'] = $user['user_id'];
|
||
$user_system = UserSystem::getOne($params);
|
||
if (empty($user_system)){
|
||
$data = array();
|
||
$data['user_id'] = $user['user_id'];
|
||
$data['is_accept_im_message_push'] = 1; // 默认新增一下配置表
|
||
$user_system = UserSystem::addUserSystem($data);
|
||
if (empty($user_system)){
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// 无需发送
|
||
if ($user_system['is_accept_im_message_push'] == 0){
|
||
return true;
|
||
}
|
||
|
||
$params = array();
|
||
$params['order_inquiry_id'] = $order_inquiry_id;
|
||
$order_inquiry = OrderInquiry::getOne($params);
|
||
if (empty($order_inquiry)){
|
||
return false;
|
||
}
|
||
|
||
$message = "您有一条新的问诊消息";
|
||
// 判断消息类型
|
||
if (isset($msg_body[0]['MsgType'])){
|
||
switch ($msg_body[0]['MsgType']) {
|
||
case 'TIMLocationElem':
|
||
$message = "地理位置";
|
||
break;
|
||
case 'TIMFaceElem':
|
||
$message = "表情消息";
|
||
break;
|
||
|
||
case 'TIMSoundElem':
|
||
$message = "[语音]";
|
||
break;
|
||
case 'TIMImageElem':
|
||
$message = "图像消息";
|
||
break;
|
||
case 'TIMFileElem':
|
||
$message = "文件消息";
|
||
break;
|
||
case 'TIMTextElem':
|
||
if (isset($msg_body[0]['MsgContent']['Text'])){
|
||
$message = $msg_body[0]['MsgContent']['Text'];
|
||
}
|
||
break;
|
||
default:
|
||
// code...
|
||
break;
|
||
}
|
||
}
|
||
|
||
$MessagePush = new MessagePush($user_id, $order_inquiry['inquiry_no']);
|
||
|
||
if ($user['user_type'] == 1){
|
||
// 患者
|
||
$MessagePush->patientImMessageNotice($message);
|
||
} elseif ($user['user_type'] == 2){
|
||
// 医师
|
||
$MessagePush->doctorImMessageNotice($message);
|
||
}else{
|
||
return false;
|
||
}
|
||
}catch (\Throwable $e){
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 处理用户im登陆状态
|
||
* @param array $msg_data im消息体
|
||
* {
|
||
"CallbackCommand":"State.StateChange",
|
||
"Info":{
|
||
"To_Account":"516898713896521728",
|
||
"Action":"Disconnect",
|
||
"Reason":"LinkClose"
|
||
},
|
||
"EventTime":1701047839484,
|
||
"ClientIP":"221.216.35.130",
|
||
"OptPlatform":"Web",
|
||
"RequestId":"clhus7ir8lcc6oth83f0-144115242829354858-Disconnect-LinkClose",
|
||
"SdkAppid":"1400798221",
|
||
"contenttype":"json",
|
||
"Sign":"c32ed26bfab2b71ffc4d93f209dade65a7fb8736138d0ca6f44af4c11ba66d2d",
|
||
"RequestTime":"1701047839"
|
||
}
|
||
* @return array
|
||
*/
|
||
public function userImLoginStatus(array $msg_data): array
|
||
{
|
||
$result = array();
|
||
$result['message'] = "";
|
||
$result['code'] = 0;
|
||
|
||
if (empty($msg_data['Info'])){
|
||
$result['message'] = "消息内容错误,缺少Info";
|
||
return $result;
|
||
}
|
||
|
||
if (empty($msg_data['Info']['To_Account'])){
|
||
$result['message'] = "消息内容错误,缺少Info.To_Account";
|
||
return $result;
|
||
}
|
||
|
||
if (empty($msg_data['Info']['Action'])){
|
||
$result['message'] = "消息内容错误,缺少Info.Action";
|
||
return $result;
|
||
}
|
||
|
||
if (empty($msg_data['Info']['Reason'])){
|
||
$result['message'] = "消息内容错误,缺少Info.Reason";
|
||
return $result;
|
||
}
|
||
|
||
if (empty($msg_data['RequestTime'])){
|
||
$result['message'] = "消息内容错误,缺少RequestTime";
|
||
return $result;
|
||
}
|
||
|
||
// 获取用户数据
|
||
$params = array();
|
||
$params['user_id'] = $msg_data['Info']['To_Account'];
|
||
$user = User::getOne($params);
|
||
if (empty($user)){
|
||
$result['message'] = "消息内容错误,接收用户错误";
|
||
return $result;
|
||
}
|
||
|
||
Db::beginTransaction();
|
||
try {
|
||
if ($msg_data['Info']['Action'] == "Login"){
|
||
// 登陆
|
||
$im_login_at = date('Y-m-d H:i:s',$msg_data['RequestTime']);
|
||
|
||
// 修改用户表在线状态
|
||
$params = array();
|
||
$params['user_id'] = $user['user_id'];
|
||
|
||
$data = array();
|
||
if ($user['is_online'] == 0){
|
||
$data['is_online'] = 1;
|
||
}
|
||
$data['im_login_at'] = $im_login_at;
|
||
$res = User::editUser($params,$data);
|
||
if (!$res){
|
||
$result['message'] = "在线状态存储失败";
|
||
return $result;
|
||
}
|
||
|
||
Log::getInstance("UserService-userImLoginStatus")->info("用户已上线");
|
||
} elseif ($msg_data['Info']['Action'] == "Disconnect"){
|
||
// 点右上角退出/断网(如手机开启飞行模式)/微信切后台/杀掉微信进程
|
||
$time = time() - $msg_data['RequestTime'] + 30*60;
|
||
if ($time <= 0){
|
||
$time = 30 * 60;
|
||
}
|
||
|
||
$data = array();
|
||
$data['user_id'] = $user['user_id'];
|
||
|
||
$message = new UserImOffDelayDirectProducer($data);
|
||
$message->setDelayMs(1000 * $time);
|
||
$producer = $this->container->get(Producer::class);
|
||
$res = $producer->produce($message);
|
||
if (!$res) {
|
||
$result['message'] = "添加下线队列失败";
|
||
return $result;
|
||
}
|
||
Log::getInstance("UserService-userImLoginStatus")->info("已添加延迟下线队列");
|
||
} elseif ($msg_data['Info']['Action'] == "Logout"){
|
||
// 主动退出
|
||
// 修改用户表在线状态
|
||
if ($user['is_online'] == 1){
|
||
$params = array();
|
||
$params['user_id'] = $user['user_id'];
|
||
|
||
$data = array();
|
||
$data['is_online'] = 0;
|
||
$res = User::editUser($params,$data);
|
||
if (!$res){
|
||
$result['message'] = "在线状态存储失败";
|
||
return $result;
|
||
}
|
||
}
|
||
|
||
Log::getInstance("UserService-userImLoginStatus")->info("用户已设下线");
|
||
}
|
||
|
||
Db::commit();
|
||
}catch (\Throwable $e){
|
||
Db::rollBack();
|
||
$result['message'] = $e->getMessage();
|
||
return $result;
|
||
}
|
||
|
||
$result['message'] = "成功";
|
||
$result['code'] = 1;
|
||
return $result;
|
||
}
|
||
|
||
/**
|
||
* 处理用户im发送消息后回调
|
||
* @param array $data im消息体
|
||
* {
|
||
"CloudCustomData":"{\"order_inquiry_id\":\"581144270615580673\",\"is_system\":1,\"inquiry_type\":\"2\",\"message_rounds\":0,\"patient_family_data\":{\"patient_name\":\"貂蝉一\",\"patient_sex\":\"2\",\"patient_age\":\"32\"}}",
|
||
"MsgVersion":0,
|
||
"MsgBody":[
|
||
{
|
||
"MsgType":"TIMCustomElem",
|
||
"MsgContent":{
|
||
"Desc":"",
|
||
"Data":"{\"message_type\":11,\"title\":\"患者信息\",\"desc\":\"\",\"data\":{\"order_no\":\"581144270615580672\",\"disease_desc\":\"ᵀᵒᵈᵃʸ ᴵ ʷᵃⁿᵗ ᵗᵒ ᵇᵉ ᵗʰᵉ ʰᵃᵖᵖⁱᵉˢᵗ ᶜʰⁱˡᵈ ⁱⁿ ᵗʰᵉ ʷʰᵒˡᵉ ᵘⁿⁱᵛᵉʳˢᵉ.今天也要做全宇宙最快乐的小朋友 \",\"message_path\":\"\\\/Pages\\\/yishi\\\/case\\\/index?order_inquiry_id=581144270615580673\"}}",
|
||
"Ext":"",
|
||
"Sound":""
|
||
}
|
||
}
|
||
],
|
||
"CallbackCommand":"C2C.CallbackAfterSendMsg",
|
||
"From_Account":"1682282293411975168",
|
||
"To_Account":"581056776246890497",
|
||
"MsgRandom":299670010,
|
||
"MsgSeq":2064411751,
|
||
"MsgTime":1699515713,
|
||
"SupportMessageExtension":0,
|
||
"MsgKey":"2064411751_299670010_1699515713",
|
||
"OnlineOnlyFlag":0,
|
||
"SendMsgResult":0,
|
||
"ErrorInfo":"send msg succeed",
|
||
"UnreadMsgNum":8,
|
||
"EventTime":1699515713924,
|
||
"ClientIP":"139.155.127.177",
|
||
"OptPlatform":"RESTAPI",
|
||
"RequestId":"37677-144115242877083697-1699515713-299670010",
|
||
"SdkAppid":"1400798221",
|
||
"contenttype":"json",
|
||
"Sign":"f6069aeb0c62cf3d77336794f7cf5a8543ddbff8c285f7341419df37ac713ff2",
|
||
"RequestTime":"1699515713"
|
||
}
|
||
* @return array
|
||
*/
|
||
public function userImAfterSendMsg(array $msg_data): array
|
||
{
|
||
$result = array();
|
||
$result['message'] = "";
|
||
$result['code'] = 0;
|
||
|
||
// 验证消息内容
|
||
if (empty($msg_data['MsgBody'])) {
|
||
$result['message'] = "消息内容错误,缺少MsgBody";
|
||
return $result;
|
||
}
|
||
|
||
// 验证消息内容类型
|
||
if (empty($msg_data['MsgBody'][0]['MsgType'])) {
|
||
$result['message'] = "消息内容错误,缺少MsgType";
|
||
return $result;
|
||
}
|
||
|
||
// 验证消息内容详情
|
||
if (empty($msg_data['MsgBody'][0]['MsgContent'])) {
|
||
$result['message'] = "消息内容错误,缺少MsgContent";
|
||
return $result;
|
||
}
|
||
|
||
// 验证接收方user_id
|
||
if (empty($msg_data['To_Account'])) {
|
||
$result['message'] = "消息内容错误,接收用户错误";
|
||
return $result;
|
||
}
|
||
|
||
// 验证消息唯一id
|
||
if (empty($msg_data['MsgKey'])) {
|
||
$result['message'] = "消息内容错误,消息唯一标识错误";
|
||
return $result;
|
||
}
|
||
|
||
// 处理自定义消息
|
||
if ($msg_data['MsgBody'][0]['MsgType'] == "TIMCustomElem"){
|
||
if (empty($msg_data['MsgBody'][0]['MsgContent']['Data'])){
|
||
$result['message'] = "自定义消息数据类型错误";
|
||
return $result;
|
||
}
|
||
|
||
$content = json_decode($msg_data['MsgBody'][0]['MsgContent']['Data'],true);
|
||
if (empty($content)){
|
||
$result['message'] = "自定义消息数据内容错误";
|
||
return $result;
|
||
}
|
||
|
||
if ($content['message_type'] == 4 || $content['message_type'] == 5 || $content['message_type'] == 20){
|
||
// 4/5类型时不进行处理
|
||
$result['message'] = "成功";
|
||
$result['code'] = 1;
|
||
return $result;
|
||
}
|
||
}
|
||
|
||
// 验证消息重复性
|
||
$params = array();
|
||
$params['message_key'] = $msg_data['MsgKey'];
|
||
$message = MessageIm::getExists($params);
|
||
if ($message) {
|
||
$result['message'] = "消息重复";
|
||
return $result;
|
||
}
|
||
|
||
// 处理发送结果
|
||
if ($msg_data['SendMsgResult'] == 0) {
|
||
// im中0表示成功
|
||
$message_send_result = 1;
|
||
}
|
||
|
||
// 验证自定义消息内容
|
||
$is_system = 0;// 是否系统操作发送(0:否 1:是)
|
||
if (!empty($msg_data['CloudCustomData'])) {
|
||
$cloud_custom_data = json_decode($msg_data['CloudCustomData'], true);
|
||
|
||
if (!empty($cloud_custom_data['order_inquiry_id'])) {
|
||
// 获取订单数据
|
||
$params = array();
|
||
$params['order_inquiry_id'] = $cloud_custom_data['order_inquiry_id'];
|
||
$order_inquiry = OrderInquiry::getOne($params);
|
||
if (empty($order_inquiry)) {
|
||
$result['message'] = "消息内容错误,非法订单";
|
||
return $result;
|
||
}
|
||
|
||
$order_inquiry_id = $cloud_custom_data['order_inquiry_id'];
|
||
}
|
||
|
||
if (!empty($cloud_custom_data['is_system'])) {
|
||
if ($cloud_custom_data['is_system'] == 1) {
|
||
// 系统发送
|
||
$is_system = 1;
|
||
}
|
||
}
|
||
}
|
||
|
||
try {
|
||
// 入库
|
||
$data = array();
|
||
if (!empty($msg_data['From_Account'])) {
|
||
// 系统发送时不带参数
|
||
$data['from_user_id'] = $msg_data['From_Account'];
|
||
}
|
||
$data['to_user_id'] = $msg_data['To_Account'];
|
||
$data['message_key'] = $msg_data['MsgKey'];
|
||
$data['message_send_time'] = $msg_data['RequestTime'];
|
||
$data['message_seq'] = $msg_data['MsgSeq'];
|
||
$data['message_send_result'] = $message_send_result ?? 0;
|
||
$data['send_error_info'] = $msg_data['ErrorInfo'];
|
||
$data['message_type'] = $msg_data['MsgBody'][0]['MsgType'];
|
||
|
||
$data['is_system'] = $is_system;
|
||
if (!empty($order_inquiry_id)) {
|
||
$data['order_inquiry_id'] = $order_inquiry_id;
|
||
}
|
||
|
||
$message_content = $msg_data['MsgBody'][0]['MsgContent'];
|
||
$data['message_content'] = json_encode($message_content, JSON_UNESCAPED_UNICODE);
|
||
$data['message_custom_content'] = $msg_data['CloudCustomData'] ?? "";
|
||
$message = MessageIm::addMessage($data);
|
||
if (empty($message)) {
|
||
$result['message'] = "存储数据库失败";
|
||
return $result;
|
||
}
|
||
|
||
// im消息通知
|
||
if ($is_system == 0 && isset($message_send_result) && isset($order_inquiry_id)){
|
||
try {
|
||
$UserService = new UserService();
|
||
$UserService->userImMessageNotice($msg_data['To_Account'],$order_inquiry_id,$msg_data['MsgBody']);
|
||
}catch (\Throwable $e){
|
||
Log::getInstance("UserService-userImAfterSendMsg")->error("im消息通知失败");
|
||
}
|
||
}
|
||
}catch (\Throwable $e){
|
||
$result['message'] = $e->getMessage();
|
||
return $result;
|
||
}
|
||
|
||
$result['message'] = "成功";
|
||
$result['code'] = 1;
|
||
return $result;
|
||
}
|
||
} |