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

222 lines
8.9 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\Admin;
use App\Http\Response\ResponseJson;
use App\Models\Server\MerchantAccount;
use App\Models\Server\TouristOrder;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Server\MEarning;
use App\Models\Server\MerchantShareChannel as ShareChannel;
use App\Models\Server\MerchantUser;
class ShareChannelController extends Controller
{
use ResponseJson;
/**
* 分享渠道列表
* @param Request $request
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\Http\JsonResponse|\Illuminate\View\View
*/
public function channels(Request $request)
{
try {
$merchant_id = $request->account_id;
$keyword = trim($request->keyword);
$merchant = MerchantAccount::find($merchant_id);
if (empty($merchant)) return $this->failure("暂无商户信息");
$channels = ShareChannel::with('bindMerchantAnchor:m_id,name,pic', 'bindMerchantUser:id,nickname,pic')->withCount('paidOrders')->where('m_id', $merchant_id)->where('created_at', '>', '2022-01-06');
if($keyword){
$channels = $channels->where('title','like',"%$keyword%");
}
$channels = $channels->orderBy('id','desc')->paginate();
return $this->success('ok', $channels);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure("服务器休息,请稍后再试");
}
}
/**
* 创建渠道
* @param Request $request
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\Http\JsonResponse|\Illuminate\View\View|void
*/
public function storeChannel(Request $request)
{
try {
$title = $request->input('title');
if (empty($title)) return $this->failure("请输入渠道名称");
$merchant_id = $request->account_id;
//随机分享吗
$code = $this->makeCode();
$channel = new ShareChannel();
$channel->m_id = $merchant_id;
$channel->title = $title;
$channel->code = $code;
$serve_tab = $request->serve_tab;
$channel->ratio = $request->input('ratio',0);
if ($channel->ratio < 0 || $channel->ratio > 1) return $this->failure("分成比例取值范围在0~1之间");
$channel->save();
//分享码
$url = env('APP_URL').'/api/official/live/wechat/FamilyAuth?merchant_id='.$merchant_id.'&share_channel_id='.$channel->id.'&serve_tab='.$serve_tab.'&url='.urlencode(env('APP_URL').'/pu/#/');
$qrcode = $this->getUrlqrcode($url,300);
$channel->qrcode = $qrcode;
// //商户绑定码
// $bind_url = env('APP_URL').'/api/official/bind/merchant/share/channels/'.$channel->id;
//商户绑定码
$bind_url = env('APP_URL').'/api/official/bind/merchant/share/channels/'.$channel->id.'/v2';
$bind_qrcode = $this->getUrlqrcode($bind_url,300);
$channel->bind_qrcode = $bind_qrcode;
$channel->save();
return $this->success("ok", $channel);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure("服务器休息,请稍后再试");
}
}
/**
* 修改渠道
* @param Request $request
* @param $id
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\Http\JsonResponse|\Illuminate\View\View
*/
public function updateChannel(Request $request, $id)
{
try {
$channel = ShareChannel::find($id);
if (empty($channel)) return $this->failure('未获取到渠道信息');
$ratio = $request->input('ratio');
if ($ratio && $ratio != $channel->ratio) {
if ($ratio < 0 || $ratio > 1) return $this->failure("分成比例取值范围在0~1之间");
$channel->ratio = $ratio;
}
$title = $request->input('title');
if ($title && $title != $channel->c) {
$channel->title = $title;
}
$channel->save();
return $this->success("ok", $channel);
}catch (\Exception $e) {
$this->getError($e);
return $this->failure("服务器休息,请稍后再试");
}
}
/**
* 渠道详情
* @param Request $request
* @param $id
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\Http\JsonResponse|\Illuminate\View\View
*/
public function channel(Request $request, $id)
{
try {
$channel = ShareChannel::find($id);
$channel->bind_anchor = $channel->bindMerchantAnchor()->where('m_id', $channel->bind_m_id)->select('id', 'm_id', 'name', 'pic')->first();
$channel->bind_user = $channel->bindMerchantUser()->where('id', $channel->bind_m_user_id)->select('id', 'nickname', 'pic')->first();
if (empty($channel)) return $this->failure('未获取到渠道信息');
return $this->success('ok',$channel);
}catch (\Exception $e) {
$this->getError($e);
return $this->failure("服务器休息,请稍后再试");
}
}
/**
* 删除渠道
* @param Request $request
* @param $id
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\Http\JsonResponse|\Illuminate\View\View
*/
public function deleteChannel(Request $request, $id)
{
try {
$channel = ShareChannel::find($id);
if (empty($channel)) return $this->failure('未获取到渠道信息');
$channel->delete();
return $this->success('ok',$channel);
}catch (\Exception $e) {
$this->getError($e);
return $this->failure("服务器休息,请稍后再试");
}
}
public function channelOrders(Request $request, $id)
{
try {
$keyword = $request->keyword;
$channel = ShareChannel::find($id);
if (empty($channel)) return $this->failure('暂无渠道信息');
$orders = TouristOrder::with('user:id,nickname,pic')->where('share_channel_id', $id)->whereIn('pay_status',[1,4]);
$type = $request->input('type');
if ($type) {
if ($type == 'activity'){
$orders = $orders->where('type', 'community')->whereHas('activity');
}elseif ($type == 'service') {
$orders =$orders->where('type', 'community')->whereHas('service');
}else{
$orders =$orders->where('type', $type);
}
$orders->with($type.':id,title');
}
if($keyword){
$keyword = trim($keyword);
$ids = MerchantUser::where('nickname','like','%'.$keyword.'%')->pluck('id');
$orders = $orders->where(function($sql) use($keyword,$ids){
$sql->where('desc','like','%'.$keyword.'%')
->orWhere('name','like','%'.$keyword.'%')
->orWhereIn('account_id',$ids);
});
}
$orders = $orders->orderBy('id', 'desc')->paginate();
foreach ($orders as $order)
{
$order->linkmen =json_decode($order->linkmen)?:[];
if ($type) {
//订单名称
$order->goods_name = $order->$type?$order->$type->title:null;
}
//渠道分成金额
$channel_earning = MEarning::where('m_order_id',$order->id)->where('sharer','channel')->value('value');
$order->channel_earning = $channel_earning ?? '0.00';
}
return $this->success('ok', $orders);
}catch (\Exception $e) {
$this->getError($e);
return $this->failure('服务器休息,请稍后再试');
}
}
/**
* 生成随机渠道码
* @return string
*/
function makeCode(){
try {
$code = "ABCDEFGHIGKLMNOPQRSTUVWXYZ";
$rand = $code[rand(0, 25)] . strtoupper(dechex(date('m')))
. date('d') . substr(time(), -5)
. substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
for (
$a = md5($rand, true),
$s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',
$d = "",
$f = 0;
$f < 6;
$g = ord($a[$f]), // ord函数获取首字母的 的 ASCII值
$d .= $s[($g ^ ord($a[$f + 8])) - $g & 0x1F], //按位亦或,按位与。
$f++
) ;
return $d;
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
}