love_php/app/Http/Controllers/Server/H5/InformationController.php
2026-04-02 09:20:51 +08:00

433 lines
20 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Http\Controllers\Server\H5;
use App\Models\MerchantTags;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Server\MerchantInformation as MInformation;
use App\Models\MerchantInformation;
use App\Http\Response\ResponseJson;
use App\Models\CommunityActivity;
use App\Models\ConfigAdvertise;
use App\Models\TouristOrder;
use App\Models\Consultation;
use App\Models\Course\Course;
use App\Models\MerchantShop;
use App\Models\MerchantUsers;
use App\Models\Server\MerchantUser;
use App\Jobs\SendTemplateMsg;
use App\Models\Message;
use App\Services\JpushService;
use App\Models\Server\MerchantAccount;
use App\Models\Server\SaasNotice;
use App\Models\Server\TouristOrder as Order;
use App\Models\User;
use App\Utils\Messenger;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
class InformationController extends Controller
{
use ResponseJson;
/**
* 打赏资讯回调
*
* @param Request $request
* @param int info_id 资讯id
* @param float amount 打赏金额
* @return void
*/
public function callbackRewardOrder(Request $request, $trade_no)
{
try {
\DB::beginTransaction();
//订单信息
$order = Order::where('trade_no', $trade_no)->first();
if (empty($order)) throw new \Exception("订单号" . $trade_no . "回调失败,订单号不存在");
if ($order->pay_status == 1) return;
//是否是咨询打赏订单
if ($order->type != 'reward_info' && $order->type != 'reward_activity') throw new \Exception("订单号" . $trade_no . "回调失败,订单不是打赏订单");
//订单是否支付成功
$result = \WechatService::serverH5OrderPaid($order->trade_no);
if (empty($result)) throw new \Exception("订单号" . $trade_no . "回调失败,订单暂未支付");
$amount = $order->price;
$comment = $order->comment;
$user = MerchantUser::find($order->account_id);
if ($order->type == 'reward_info') {
$info = MInformation::find($order->type_id);
$send_title = $info->title;
$link_url = env('APP_URL') . '/pu/#/dynamicParticulars/' . $order->type_id;
$link_url = urlencode($link_url);
} elseif ($order->type == 'reward_activity') {
$info = CommunityActivity::find($order->type_id);
$send_title = $info->title;
$link_url = env('APP_URL') . '/pu/#/activityDetails/' . $order->type_id;
$link_url = urlencode($link_url);
} else {
throw new \Exception("订单号" . $trade_no . "回调失败,回调;类型不存在");
}
//打赏记录
$result = $user->reward($info, $amount, $comment);
//收益记录
$merchant = MerchantAccount::find($info->merchant_id);
if (empty($merchant)) return $this->failure('打款失败,该资讯暂无所属作者');
$earning = $merchant->addEarning('reward', $amount, $order->id);
if (empty($earning)) throw new \Exception("增加打赏收益失败");
//修改订单状态
$order->pay_status = 1;
$order->save();
$replyxml = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
echo $replyxml;
\DB::commit();
//公众号推送
//打赏人昵称
$reward_pay_name = MerchantUser::where('id', $order->account_id)->value('nickname');
if (mb_strlen($reward_pay_name) > 5) {
$reward_pay_name = mb_substr($reward_pay_name, 0, 5, 'utf-8') . '..';
}
if ($merchant->openid) {
$data['touser'] = $merchant->openid;
$data['template_id'] = 'wR9i6QVHLCY1J9U4fUwXZzi22PwMCSMDmUmycsnNnu4';
$data['url'] = env('APP_URL') . '/api/official/live/wechat/FamilyAuth?merchant_id=' . $info->merchant_id . '&url=' . $link_url;
$data['data'] = [
'first' => '您收到' . $reward_pay_name . '的一笔打赏啦!',
'keyword1' => $info->title,
'keyword2' => $amount,
'keyword3' => $order->created_at,
'reamrk' => '',
];
SendTemplateMsg::dispatch($data)->onQueue('template_message');
}
//【叮打赏来了】昵称昵称昵称…刚刚赞赏了平台的【名称名称名称名称名称…】9元点击ufutx.cn/s/76s查看详情
$info = MerchantInformation::where('id', $order->type_id)->first();
$mobile = MerchantAccount::where('id', $order->merchant_id)->value('mobile');
$jump_url = env('APP_URL') . '/pu_m/#/earningsRecord?merchant_id=' . $order->merchant_id;
$short_url = \CommonUtilsService::shortUrl(env('APP_URL') . '/h5/#/jump_url?url=' . $jump_url);
//短信通知商家
$title = $info->title;
if (mb_strlen($title) > 5) {
$title = mb_substr($title, 0, 5, 'utf-8') . '..';
}
$message = $order->name . '觉得您的《' . $title . '》不错,赞赏了' . $order->price . '元,点击' . $short_url['url'] . ' 查看详情';
Messenger::sendSMS($mobile, $message);
//增加记录
Message::create([
'phone' => $mobile,
'message' => $message,
'confirmed' => 1,
'code' => '打赏通知',
'ip' => request() ? request()->ip() : '127.0.0.1',
]);
//插入购买通知
$pay_name = $order->user ? $order->user->nickname : '匿名用户';
$content = $pay_name . '赞赏了【' . $send_title . '】' . $order->price . '元';
SaasNotice::addRecord($order->merchant_id, $order->account_id, 're_order', $order->id, $content, 0);
// app消息推送
if (isset($merchant->info->android_id)) {
// 推送平台 ios android
$params['platform'] = 'android';
// 推送标题
$params['title'] = '收到打赏提醒通知';
// 推送内容
$params['content'] = $info->title . '收到打赏' . $order->price . '元';
// 通知栏样式 ID
$params['builderId'] = 1;
// 唤醒APP
$params['intent'] = ['url' => 'intent:#Intent;action=com.love.ufutx.saas.ui.main.MainActivity;component=com.love.ufutx.saas/com.love.ufutx.saas.ui.main.MainActivity;end'];
$params['uri_activity'] = 'com.love.ufutx.saas.ui.main.MainActivity';
$params['uri_action'] = 'com.love.ufutx.saas.ui.main.MainActivity';
// 附加字段(这里自定义 Key / value 信息,以供业务使用)
$params['extras'] = [
'url' => 'https://love.ufutx.com/pu_m/#/earningsRecord?equipmentType=android',
];
// 推送类型 1-别名 2-注册id 3-全部
$params['type'] = 2;
// 注册ID 可以是单个 也可以是 数组
$params['registrationId'] = $merchant->info->android_id;
// 开始推送
JpushService::androidOrIosPushByAlias($params);
}
} catch (\Exception $e) {
\DB::rollback();
$this->getError($e);
return $this->success('打赏回调失败');
}
}
/**
* 商户-资讯动态列表
* */
public function informations(Request $request)
{
try {
$result = MInformation::with('tag')->where('merchant_id', $request->merchant_id)->where('status', 1)->orderBy('id', 'desc')->paginate();
foreach ($result as $key => $value) {
# code...
$value->pv = Redis::zscore('information', $value->id) ?? 0;
}
return $this->success('ok', $result);
}catch (\Exception $e){
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* 商户-资讯动态详情
* */
public function information_detail(Request $request)
{
try {
$id = $request->id;
$merchant_user_id = $request->merchant_user_id;
$result = MerchantInformation::withTrashed()->with('tag')->where('id', $id)/*->where('status',1)*/ ->first();
if ($result)
$result->pv = Redis::zincrby('information', 1, $id);
$price = '';
$config = ConfigAdvertise::where('type', 'information')->where('type_id', $id)->first();
if ($config) {
if ($config && $config->choose_type == 'course' && $config->class == 'in') {
$price = Course::where('id', $config->choose_type_id)->value('charge');
$config->links = env('APP_URL') . '/pu/#/courseDetail/' . $config->choose_type_id;
}
if ($config && $config->choose_type == 'activity' && $config->class == 'in') {
$price = CommunityActivity::where('id', $config->choose_type_id)->value('price');
$config->links = env('APP_URL') . '/pu/#/activityDetails/' . $config->choose_type_id;
}
if ($config && $config->choose_type == 'service' && $config->class == 'in') {
$price = CommunityActivity::where('id', $config->choose_type_id)->value('price');
$config->links = env('APP_URL') . '/pu/#/serveDetails/' . $config->choose_type_id;
}
if ($config && $config->choose_type == 'shop' && $config->class == 'in') {
$price = MerchantShop::where('id', $config->choose_type_id)->value('price');
$config->links = env('APP_URL') . '/pu/#/mallDetail/' . $config->choose_type_id;
}
if ($config && $config->choose_type == 'consult' && $config->class == 'in') {
$price = Consultation::where('id', $config->choose_type_id)->value('price');
$config->links = env('APP_URL') . '/pu/#/consultingDetail/' . $config->choose_type_id;
}
$config->price = $price;
}
$reward_count = TouristOrder::where('type', 'reward_info')->where('type_id', $id)->whereIn('pay_status', [1, 4])->get()->count();
$result->reward_count = $reward_count??0;
$result->config = $config;
$jump_url = urlencode(env('APP_URL') . '/pu/#/dynamicParticulars/' . $id);
$url = env('APP_URL') . '/api/official/live/wechat/FamilyAuth?merchant_id=' . $result->merchant_id . '&anchor_openid=&url=' . $jump_url;
$qr_code = Redis::get('InforDetail_S' . $id);
if (!$qr_code) {
$qr_code = $this->getPreviewQrcode($url);
Redis::setex('InforDetail_S' . $id, 60 * 60 * 24 * 30, $qr_code);
$qr_code = Redis::get('InforDetail_S' . $id);
}
$result->share_qr_code = $qr_code;
// 评论数
$result->comment_num = $result->totalCommentsCount();
// 点赞数
$result->liker_num = $result->likers()->count();
// 是否已经点赞
$merchant_user_id = $request->merchant_user_id;
$result->liker_status = false;
if ($merchant_user_id) {
$merchant_user = MerchantUser::where('id', $request->merchant_user_id)->first();
$result->liker_status = $merchant_user->hasLiked($result);
if (config('app.env') == 'production') {
$merchant_user->addlog($result);
}
}
// $result = $merchant_user->loglist()->get();
//下架或删除状态 0下架 1正常上架 2已删除
$publish_state = 0;
if ($result->deleted_at) {
$publish_state = 2;
} elseif ($result->status == 1) {
$publish_state = 1;
} else {
$publish_state = 0;
}
$result->publish_state = $publish_state;
$merchant_user = MerchantUser::where('id', $merchant_user_id)->first();
if ($merchant_user) {
$result['share_nickname'] = $merchant_user->nickname ?? '匿名用户';
$result['share_avatar'] = $merchant_user->pic ?? User::DefaultAvatar;
}
return $this->success('ok', $result);
}catch (\Exception $e){
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* 咨讯(文章)评论
* */
public function informationComment(Request $request, $comment_id)
{
try {
$content = $request->input('content');
if (!$content) return $this->failure('评论失败');
$reply_id = $request->reply_id ?? 0;
$information = MerchantInformation::where('id', $comment_id)->first();
if (!$information) return $this->failure('该记录不存在或已删除');
$merchant_user = MerchantUsers::where('id', $request->merchant_user_id)->first();
$result = $merchant_user->comment($information, $content);
$result->reply_id = $reply_id;
$result->save();
$content = $merchant_user->nickname . '在您的文章【' . $information->title . '】发表了评论';
SaasNotice::addRecord($information->merchant_id, $request->merchant_user_id, 'comment', $result->id, $content, 0);
return $this->success('ok', $result);
}catch (\Exception $e){
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* 咨讯(文章)评论列表
* */
public function commentList(Request $request, $id)
{
try {
$information = MerchantInformation::withTrashed()->where('id', $id)->first();
$comments = $information->comments()->select('id', 'commentable_id', 'commented_id', 'reply_id', 'comment', 'created_at')->where('reply_id', 0)->orderBy('id', 'desc')->paginate();
foreach ($comments as $key => $value) {
# code...
$merchant_user = MerchantUsers::select('nickname', 'pic')->where('id', $value->commented_id)->first();
$value->nickname = $merchant_user->nickname ?? '匿名用户';
$value->pic = $merchant_user->pic ?? User::DefaultAvatar;
$reply = $information->comments()->select('id', 'commentable_id', 'commented_id', 'reply_id', 'comment', 'created_at')->where('reply_id', $value->id)->orderBy('id', 'desc')->get();
foreach ($reply as $k => $v) {
$merchant_user = MerchantUsers::select('nickname', 'pic')->where('id', $v->commented_id)->first();
$v->nickname = $merchant_user->nickname ?? '匿名用户';
$v->pic = $merchant_user->pic ?? User::DefaultAvatar;
}
$value->reply = $reply;
}
return $this->success('ok', $comments);
}catch (\Exception $e){
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* 删除评论
* **/
public function delComment(Request $request)
{
try {
$comment_id = $request->comment_id;
$information_id = $request->information_id;
$information = MerchantInformation::select('id', 'title', 'merchant_id')->where('id', $information_id)->first();
$information->comments()->where(function ($sql) use ($comment_id) {
$sql->where('id', $comment_id)
->orWhere('reply_id', $comment_id);
})->delete();
return $this->success('ok');
}catch (\Exception $e){
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* 点赞
* */
public function like(Request $request, $id)
{
try {
$merchant_user = MerchantUser::where('id', $request->merchant_user_id)->first();
$Information = MerchantInformation::where('id', $id)->first();
if ($merchant_user->hasLiked($Information)) {
$merchant_user->dislike($Information, 'like');
} else {
$result = $merchant_user->like($Information, 'like');
$content = $merchant_user->nickname . '点赞了您的文章【' . $Information->title . '】';
SaasNotice::addRecord($Information->merchant_id, $merchant_user->id, 'like', $result->id, $content, 0);
}
return $this->success('ok');
}catch (\Exception $e){
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* 点赞列表
* */
public function likes(Request $request, $id)
{
try {
$Information = MerchantInformation::select('id', 'title', 'pic')->where('id', $id)->first();
$comments = $Information->likers()->select('id', 'likedable_id', 'created_at')->orderBy('id', 'desc')->paginate();
foreach ($comments as $key => $value) {
$user = MerchantUser::select('id', 'nickname', 'pic')->where('id', $value->likedable_id)->first();
$value->nickname = $user->nickname ?? '匿名用户';
$value->pic = $user->pic ?? User::DefaultAvatar;
}
return $this->success('ok', $comments);
}catch (\Exception $e){
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* 标签详情
* */
public function tag(Request $request, $tag_id)
{
try {
if (!isset($request->tag_id)) {
return $this->failure('标签id必填');
}
$tag = MerchantTags::find($tag_id);
$tag->pv = Redis::zincrby('merchant_tag', 1, $tag_id);
$tag->count = MerchantInformation::where('tag_id', $tag_id)->count();
return $this->success('ok', $tag);
} catch (\Exception $e) {
Log::error($e->getMessage());
return $this->failure('数据错误');
}
}
/**
* 标签文章列表
* */
public function tagInformations(Request $request)
{
try {
if (!isset($request->tag_id)) {
return $this->failure('标签id必填');
}
if (!isset($request->merchant_id)) {
return $this->failure('商户id必填');
}
$informations = MerchantInformation::where('merchant_id', $request->merchant_id)->where('status', 1)->where('tag_id', $request->tag_id);
if ($request->tag_id == 42) {
$informations = $informations->orderBy('id', 'asc')->paginate();
}else {
$informations = $informations->orderBy('id', 'desc')->paginate();
}
foreach ($informations as $key => $value) {
# code...
$value->pv = Redis::zscore('information', $value->id) ?: 0;
$value->type = 'information';
}
return $this->success('ok', $informations);
} catch (\Exception $e) {
Log::error($e->getMessage());
return $this->failure('数据错误');
}
}
}