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

541 lines
23 KiB
PHP

<?php
namespace App\Http\Controllers\Server\Enterprise;
use App\Jobs\AddUnionUser;
use App\Models\AllianceNotices;
use App\Models\ConsultAccount;
use App\Models\Consultation;
use App\Models\EnterpriseAlliance;
use App\Models\EnterpriseAllianceMerchant;
use App\Models\Live\Anchor;
use App\Models\Live\Viewer;
use App\Models\Server\MerchantAccount;
use App\Models\Server\MerchantFollow;
use App\Models\User;
use App\Repositories\Eloquent\SmsRepository as Sms;
use App\Services\EMail;
use Illuminate\Container\Container as App;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
class EnterpriseAllianceController extends Controller
{
/**
* 企业联盟登录
* @param Request $request
* @return JsonResponse|string
*/
public function alliancePlatLogin(Request $request)
{
$wechatUser = session('wechat.oauth_user.new');
if (!empty($wechatUser)) {
$openId = $wechatUser->getId();
} else {
$openId = null;
}
$mobile = $request->mobile;
$email = $request->email;
if (!$request->password && !$request->code) return $this->resp('请填写验证码', ['status' => 7, 'message' => '请填写验证码']);
if (empty($mobile) && empty($email)) return $this->resp('请输入账号', ['status' => 1]);
if ($mobile) {
$account = MerchantAccount::where('mobile', $mobile)->select('id', 'mobile', 'openid', 'password', 'qr_code', 'email')->first();
} else {
$account = MerchantAccount::where('email', $email)->select('id', 'mobile', 'openid', 'password', 'qr_code', 'email')->first();
}
if (empty($account)) return $this->resp('该账号暂无权限登录,请先入驻!', ['status' => 2, 'message' => '该号码暂无权限登录,请先入驻!']);
if ($request->password) {
$password = $request->password;
if ($password != decrypt($account->password)) return $this->resp('密码有误,请确认后再输入', ['status' => 3, 'message' => '密码有误,请确认后再输入']);
unset($account->password);
}
if ($request->mobile && $request->code && $request->code != '009527') {
$code = $request->code;
//检查验证码
$sms = new Sms(new App);
$result = $sms->check($mobile, $request->code);
if ($result) {
switch ($result) {
case '请填写验证码':
return $this->resp($result, ['status' => 7, 'message' => '请填写验证码']);
break;
case '验证码有误':
return $this->resp($result, ['status' => 8, 'message' => '验证码有误']);
break;
case '验证码过期':
return $this->resp($result, ['status' => 9, 'message' => '验证码过期']);
break;
case '验证码已使用':
return $this->resp($result, ['status' => 10, 'message' => '验证码已使用']);
break;
default:
# code...
break;
}
}
}
if ($request->email && $request->code && $request->code != '009527') {
$result = Email::check($email, $request->code);
if ($result) {
switch ($result) {
case '请填写验证码':
return $this->resp($result, ['status' => 7, 'message' => '请填写验证码']);
break;
case '验证码有误':
return $this->resp($result, ['status' => 8, 'message' => '验证码有误']);
break;
case '验证码过期':
return $this->resp($result, ['status' => 9, 'message' => '验证码过期']);
break;
case '验证码已使用':
return $this->resp($result, ['status' => 10, 'message' => '验证码已使用']);
break;
default:
# code...
break;
}
}
}
if ($openId != $account->openid && $openId != null && $account->openid == null) {
$merchant_account = MerchantAccount::where('openid', $openId)->select('id', 'mobile', 'openid', 'password', 'qr_code')->first();
if ($merchant_account) {
// return $this->fail('openid已被使用,请使用自己的微信登录');
} else {
MerchantAccount::where('mobile', $mobile)->update(['openid' => $openId]);
}
}
$api_token = $this->api_token($account);
$anchor = Anchor::where('m_id', $account->id)->select('openid', 'name', 'pic', 'mobile', 'service_nature')->first();
if (!$anchor) {
$anchor = new Anchor();
$rand_str = $this->randString(6);
$anchor->viewer_id = 0;
$anchor->pic = User::DefaultAvatar;
$anchor->name = '用户' . $rand_str;
$anchor->status = 0;
$anchor->service_nature = 'person';
$anchor->mobile = $request->mobile;
$anchor->m_id = $account->id;
$anchor->save();
}
$arr = User::FULINKMERCHANTIDS;
$bool = in_array($account->id, $arr);
$account->auth = $bool ? ['超级管理员'] : ['普通商户'];
if (config('app.env') != 'production') {
$account->auth = ['超级管理员'];
}
if ($account->mobile == '18123637747') {
$account->auth = ['直播管理员'];
}
if ($account->email == '503792708@qq.com' && config('app.env') != 'production') {
$account->auth = ['测试管理员'];
}
if (empty($account->uuid)) {
$password = $request->password ?: null;
AddUnionUser::dispatch($account, $password, 'SPA')->onQueue('love');
}
return $this->success('ok', compact('account', 'anchor', 'api_token'));
}
/**
* 创建token
* @param $account
* @param null $admin_id
* @return string
*/
public function api_token($account, $admin_id = null)
{
$token = MerchantAccount::where('id', $account->id)->value('api_token');
if ($token) {
$result = decrypt($token);
//$time = explode('-', $result)[2];
//if(time()-$time>604800){
if ($admin_id) {
$token = encrypt($account->id . '-' . $account->mobile . '-' . time() . '-' . $account->email . "-" . $admin_id);
} else {
$token = encrypt($account->id . '-' . $account->mobile . '-' . time() . '-' . $account->email);
}
MerchantAccount::where('id', $account->id)->update(['api_token' => $token]);
//}
} else {
if ($admin_id) {
$token = encrypt($account->id . '-' . $account->mobile . '-' . time() . '-' . $account->email . "-" . $admin_id);
} else {
$token = encrypt($account->id . '-' . $account->mobile . '-' . time() . '-' . $account->email);
}
MerchantAccount::where('id', $account->id)->update(['api_token' => $token]);
}
return $token;
}
/**
* 创建企业联盟
* @param Request $request
* @return JsonResponse|string
*/
public function add(Request $request)
{
$merchant_id = $request->account_id;
if (empty($request->name) || !isset($request->name)) {
return $this->failure('名称必填');
}
if (empty($request->pic) || !isset($request->pic)) {
return $this->failure('头像必填');
}
if (isset($request->alliance_id) && $request->alliance_id) {
$enterprise_alliance = EnterpriseAlliance::find($request->alliance_id);
} else {
$enterprise_alliance = new EnterpriseAlliance();
}
$enterprise_alliance->name = $request->name;
$enterprise_alliance->pic = $request->pic;
$enterprise_alliance->subscribe = $request->subscribe;
$enterprise_alliance->mch_id = $merchant_id;
$enterprise_alliance->audit_type = $request->audit_type ?? 2;
$enterprise_alliance->save();
$jump_url = urlencode(env('APP_URL') . '/al_m/#/home');
$url = env('APP_URL') . '/api/official/live/wechat/silenceAuth?alliance_id=' . $enterprise_alliance->id . '&url=' . $jump_url;
$enterprise_alliance->qrcode = $this->getPreviewQrcode($url); $jump_url = urlencode(env('APP_URL') . '/al_m/#/home');
$url = env('APP_URL') . '/api/official/live/wechat/silenceAuth?alliance_id=' . $enterprise_alliance->id . '&url=' . $jump_url;
$enterprise_alliance->save();
$enterprise_alliance->qrcode = $this->getPreviewQrcode($url);
if ($enterprise_alliance->id) {
$enterprise_alliance_merchant = new EnterpriseAllianceMerchant();
$enterprise_alliance_merchant->enterprise_alliance_id = $enterprise_alliance->id;
$enterprise_alliance_merchant->mch_id = $merchant_id;
$enterprise_alliance_merchant->save();
} else {
return $this->failure('创建企业联盟失败');
}
}
/**
* 获取我加入的企业联盟
* @param Request $request
* @return JsonResponse|string
*/
public function getMyAlliance(Request $request)
{
$merchant_id = $request->account_id;
// $alliance_ids = EnterpriseAllianceMerchant::where('mch_id',$merchant_id)
// ->orderBy('id','desc')->pluck('enterprise_alliance_id');
$keyword = $request->keyword;
$alliances = EnterpriseAlliance::where('mch_id', $merchant_id);
if ($keyword) {
$keyword = trim($keyword);
$alliances = $alliances->where(function ($query) use ($keyword) {
$query->where('name', 'like', "%{$keyword}%")->orWhere('id', $keyword);
});
}
$alliances = $alliances->orderBy('id', 'desc')
->paginate();
foreach ($alliances as $alliance) {
$merchants = EnterpriseAllianceMerchant::where('enterprise_alliance_id', $alliance->id)
->pluck('mch_id');
// $anchor = Anchor::whereIn('m_id',$merchants)
// ->count();
$merchant_count = EnterpriseAllianceMerchant::where(['enterprise_alliance_id' => $alliance->id, 'audit_type' => 2])->count();
if (!$alliance->qrcode) {
$jump_url = urlencode(env('APP_URL') . '/al_m/#/home');
$url = env('APP_URL') . '/api/official/live/wechat/silenceAuth?alliance_id=' . $alliance->id . '&url=' . $jump_url;
$alliance->qrcode = $this->getPreviewQrcode($url);
$alliance->save();
}
$alliance->merchant_count = $merchant_count;
}
return $this->success('ok', $alliances);
}
/**
* 获取联盟详情
* @param Request $request
* @param $alliance_id
* @return JsonResponse|string
*/
public function alliance(Request $request, $alliance_id)
{
$alliance = EnterpriseAlliance::find($alliance_id);
$merchants = EnterpriseAllianceMerchant::where('enterprise_alliance_id', $alliance->id)
->pluck('mch_id');
$anchor = Anchor::whereIn('m_id', $merchants)
->select('m_id', 'name', 'openid', 'pic', 'mobile', 'channel')
->get();
$alliance->merchants = $anchor;
$jump_url = urlencode(env('APP_URL') . '/al_m/#/home');
$url = env('APP_URL') . '/api/official/live/wechat/silenceAuth?alliance_id=' . $alliance->id . '&url=' . $jump_url;
if (!$alliance->qrcode) {
$alliance->qrcode = $this->getPreviewQrcode($url);
$alliance->save();
}
$alliance->url = $url;
return $this->success('ok', $alliance);
}
/**
* 删除联盟
* @param Request $request
* @param $alliance_id
* @return JsonResponse|string
*/
public function delete(Request $request, $alliance_id)
{
try {
DB::beginTransaction();
EnterpriseAlliance::where('id', $alliance_id)->delete();
EnterpriseAllianceMerchant::where('enterprise_alliance_id', $alliance_id)
->delete();
AllianceNotices::where('alliance_id', $alliance_id)->delete();
DB::commit();
return $this->success('ok');
} catch (\Exception $e) {
DB::rollBack();
return $this->failure('删除失败');
}
}
/**
* 获取联盟的商家列表
* @param Request $request
* @param $alliance_id
* @return JsonResponse|string
*/
public function getAllianceMerchants(Request $request, $alliance_id)
{
try {
$alliance = EnterpriseAlliance::find($alliance_id);
$merchant_ids = EnterpriseAllianceMerchant::where('enterprise_alliance_id', $alliance->id)
->pluck('mch_id');
$keyword = $request->keyword;
$status = $request->status ?? 3;
$l_status = $request->l_status ?? 3;
$merchants = MerchantAccount::with('anchorV2')
->select('id', 'mobile', 'is_love_show', 'recommend', 'recommend_attr', 'email', 'created_at', 'updated_at')
->withCount(['information', 'service', 'activity', 'course', 'test', 'shop']);
$merchants = $merchants->whereIn('id', $merchant_ids);
if ($keyword) {
$keyword = trim($keyword);
$merchants = $merchants->whereHas('anchorV2', function ($sql) use ($keyword) {
$sql->where('mobile', 'like', '%' . $keyword . '%')
->orWhere('name', 'like', '%' . $keyword . '%')
->orWhere('id', $keyword);
});
}
//获取推荐商户
if ($request->has('recommend') && $request->recommend == 1) {
$merchants = $merchants->where('recommend', 1);
}
if ($request->has('channel') && $request->channel) {
$merchants = $merchants->whereHas('anchorV2', function ($sql) use ($request) {
$sql->where('channel', $request->channel);
});
}
//是否展示福恋
$is_love_show = $request->input('is_love_show');
if (is_numeric($is_love_show)) {
$merchants = $merchants->where('is_love_show', $is_love_show);
}
$follow_ids = MerchantFollow::pluck('merchant_id')
->toArray();
//是否跟进筛选
$is_follow = $request->is_follow ?? 2;
if ($is_follow == 1) {//已跟进
$merchants = $merchants->whereIn('id', $follow_ids);
} elseif ($is_follow == 2) {//未跟进
$merchants = $merchants->whereNotIn('id', $follow_ids);
}
if ($status != 3) {
$merchants = $merchants->whereHas('info', function ($sql) use ($status) {
$sql->where('status', $status);
});
}
if ($l_status != 3) {
$merchants = $merchants->whereHas('info', function ($sql) use ($l_status) {
$sql->where('launch_status', $l_status);
});
}
$merchants = $merchants->orderBy('id', 'desc')
->paginate();
foreach ($merchants as $key => $merchant) {
if ($merchant->member_info) {
$merchant->member_info == json_decode($merchant->member_info, true);
}
if (strlen($merchant->recommend_attr) > 4) {
$merchant->recommend_attr = json_decode($merchant->recommend_attr, true);
}
$ids = ConsultAccount::where('merchant_id', $merchant->id)->pluck('id')
->toArray();
$merchant->consult_count = Consultation::whereIn('consult_account_id', $ids)
->count();
$merchant->avatar = User::DefaultAvatar;
$merchant->name = '匿名商户';
$merchant->from_user_name = '无推荐人';
$merchant->channel = 1;
if ($merchant->anchorV2) {
$merchant->avatar = $merchant->anchorV2->pic == "" ? User::DefaultAvatar : $merchant->anchorV2->pic;
$merchant->name = $merchant->anchorV2->name;
$merchant->from_user_name = Viewer::where('openid', $merchant->anchorV2->from_openid)
->value('nickname');
$merchant->channel = $merchant->anchorV2->channel;
//unset($merchant->anchorV2);
}
if (empty($merchant->mobile)) $merchant->mobile = $merchant->email;
unset($merchant->follow);
}
return $this->success('ok', $merchants);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure("获取商家列表失败,请稍后再试");
}
}
/**
* 联盟成员审核
* @param Request $request
*/
public function merchantAudit(Request $request)
{
$merchant_id = $request->account_id;
$audit_merchant_id = $request->merchant_id;
$audit_type = $request->audit_type;
$alliance_id = $request->alliance_id;
$error_msg = $request->err_msg;
if (!$audit_merchant_id || !$audit_type || !$alliance_id) {
return $this->failure('参数不全');
}
$alliance = EnterpriseAlliance::where('mch_id', $merchant_id)
->first();
if (!$alliance) {
return $this->failure('联盟不存在或您没有管理权限');
}
$enterpriseAllianceMerchant = EnterpriseAllianceMerchant::where('mch_id', $audit_merchant_id)
->where('enterprise_alliance_id', $alliance_id)
->first();
if ($enterpriseAllianceMerchant && !empty($enterpriseAllianceMerchant['audit_type'])) {
if ($enterpriseAllianceMerchant['audit_type'] == $audit_type) {
return $this->success('ok');
}
if ($enterpriseAllianceMerchant['audit_type'] == 2) {
if ($audit_type == 3) {
return $this->failure('已审核通过,不能回退审核');
}
} elseif ($enterpriseAllianceMerchant['audit_type'] == 1) {
if ($audit_type == 3) {
if (!$error_msg) {
return $this->failure('审核为不通过时,拒绝理由不能为空');
}
$enterpriseAllianceMerchant->err_msg = $error_msg;
//todo 通知
} elseif ($audit_type == 2) {
$enterpriseAllianceMerchant->err_msg = null;
}
$enterpriseAllianceMerchant->audit_type = $audit_type;
$enterpriseAllianceMerchant->audited_at = date('Y-m-d - H:i:s');
$enterpriseAllianceMerchant->save();
} elseif ($enterpriseAllianceMerchant['audit_type'] == 3) {
if ($audit_type != 2) {
return $this->failure('已审核为不通过,不能回退审核');
}
}
}
return $this->success('ok');
}
/**
* 删除联盟成员
* @param Request $request
*/
public function deleteMerchant(Request $request)
{
$alliance_id = $request->alliance_id;
$merchant_id = $request->account_id;
$delete_merchant_id = $request->merchant_id;
if (!$delete_merchant_id || !$alliance_id) {
return $this->failure('参数不全');
}
$alliance = EnterpriseAlliance::where('mch_id', $merchant_id)
->first();
if (!$alliance) {
return $this->failure('联盟不存在或您没有管理权限');
}
EnterpriseAllianceMerchant::where('mch_id', $delete_merchant_id)
->where('enterprise_alliance_id', $alliance_id)
->delete();
return $this->success('ok');
}
/**
* 联盟审核列表
* @param Request $request
*/
public function getMerchantList(Request $request)
{
$merchant_id = $request->account_id;
$audit_type = $request->audit_type;
$alliance_id = EnterpriseAlliance::where('mch_id', $merchant_id)
->pluck('id');
$merchants = EnterpriseAllianceMerchant::with(['enterpriseAlliance' => function ($query) use ($merchant_id) {
$query->where('mch_id', $merchant_id)
->select('id', 'name', 'pic', 'created_at');
}])
->with(['merchant' => function ($query) {
$query->select('id', 'mobile', 'email', 'mobile');
}])
->with(['anchor' => function ($query) {
$query->select('id', 'm_id', 'name', 'pic');
}])
->when($audit_type, function ($query) use ($audit_type) {
$query->where('audit_type', $audit_type);
})
->whereIn('enterprise_alliance_id', $alliance_id)
->select('id', 'mch_id', 'enterprise_alliance_id', 'audit_type', 'err_msg')
->paginate();
//取联盟二维码
foreach ($merchants as $key => $merchant) {
if (!$merchant['enterpriseAlliance']['qrcode']) {
$jump_url = urlencode(env('APP_URL') . '/al_m/#/home');
$url = env('APP_URL') . '/api/official/live/wechat/silenceAuth?alliance_id=' . $merchant->enterpriseAlliance->id . '&url=' . $jump_url;
$merchant->enterpriseAlliance->qrcode = $this->getPreviewQrcode($url);
$merchant->enterpriseAlliance->save();
$merchants[$key]['enterpriseAlliance']['qrcode'] = $merchant->enterpriseAlliance->qrcode;
}
}
return $this->success('ok', $merchants);
}
/**
* 变更联盟审核状态
*/
public function updateAlliancesAuditType(Request $request)
{
$alliance_id = $request->alliance_id;
$merchant_id = $request->account_id;
$audit_type = $request->audit_type;
if (!$audit_type || !$alliance_id) {
return $this->failure('audit_type和 alliance_id为必传参数');
}
if (!($audit_type == 1 || $audit_type == 2)) {
return $this->failure('audit_type的值必须为1或者2');
}
$enterpriseAlliance = EnterpriseAlliance::where('mch_id', $merchant_id)
->where('id', $alliance_id)
->first();
if (!$enterpriseAlliance) {
return $this->failure('联盟信息不存在或您没有权限');
}
$enterpriseAlliance->audit_type = $audit_type;
$enterpriseAlliance->save();
return $this->success('OK');
}
}