791 lines
32 KiB
PHP
791 lines
32 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Server\EnterpriseH5;
|
|
|
|
use App\Jobs\AddUnionUser;
|
|
use App\Jobs\NewMerchantDefaultService;
|
|
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\Live;
|
|
use App\Models\MEarningRules;
|
|
use App\Models\MerchantInfo;
|
|
use App\Models\MerchantUsers;
|
|
use App\Models\Message;
|
|
use App\Models\Server\MerchantAccount;
|
|
use App\Models\Server\MerchantUser;
|
|
use App\Models\User;
|
|
use App\Repositories\Eloquent\SmsRepository as Sms;
|
|
use App\Services\EMail;
|
|
use App\Utils\Messenger as Messengers;
|
|
use Illuminate\Container\Container as App;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Redis;
|
|
|
|
class EnterpriseAllianceController extends Controller
|
|
{
|
|
/**
|
|
* 企业联盟登录,open_id换取token
|
|
* @param Request $request
|
|
* @return \Illuminate\Http\JsonResponse|string
|
|
*/
|
|
public function alliancePlatLogin(Request $request)
|
|
{
|
|
$wechatUser = session('wechat.oauth_user.new');
|
|
if (!empty($wechatUser)) {
|
|
$openId = $wechatUser->getId();
|
|
} else {
|
|
$openId = $request->openid;
|
|
}
|
|
if($openId){
|
|
$account = MerchantUser::where('openid',$openId)
|
|
->select('id', 'mobile', 'openid','password')
|
|
->first();
|
|
if($account){
|
|
$api_token = $this->api_token($account);
|
|
}else{
|
|
$rand_str = $this->randString(8);
|
|
$account = new MerchantUser();
|
|
$account->openid = $openId;
|
|
$account->rand_str = $rand_str;
|
|
$account->pic = User::DefaultAvatar;
|
|
$account->nickname = '用户' . $rand_str;
|
|
$account->save();
|
|
$api_token = $this->api_token($account);
|
|
}
|
|
}else{
|
|
$account = [];
|
|
$api_token = null;
|
|
}
|
|
return $this->success('ok', compact('account', 'api_token'));
|
|
}
|
|
|
|
/**
|
|
* 创建token
|
|
* *
|
|
* @param $account
|
|
* @return string
|
|
*/
|
|
public function api_token($account)
|
|
{
|
|
$token = MerchantUser::where('id', $account->id)
|
|
->value('api_token');
|
|
if ($token) {
|
|
$result = decrypt($token);
|
|
$time = explode('-', $result)[2];
|
|
if (time() - $time > 604800) {
|
|
$token = encrypt($account->id . '-' . $account->mobile . '-' . time());
|
|
MerchantUser::where('id', $account->id)
|
|
->update(['api_token' => $token]);
|
|
}
|
|
} else {
|
|
$token = encrypt($account->id . '-' . $account->mobile . '-' . time());
|
|
MerchantUser::where('id', $account->id)
|
|
->update(['api_token' => $token]);
|
|
}
|
|
return $token;
|
|
}
|
|
|
|
/**
|
|
* 企业联盟h5首页信息
|
|
* @param Request $request
|
|
* @return JsonResponse|string
|
|
*/
|
|
public function getAllianceInfo(Request $request)
|
|
{
|
|
try {
|
|
$alliance_id = $request->alliance_id;
|
|
if (!$alliance_id) {
|
|
return $this->failure('联盟ID必传');
|
|
}
|
|
//用戶是否关注公众号
|
|
$wechatUser = session('wechat.oauth_user.new');
|
|
if (empty($wechatUser)) {
|
|
$openId = $request->openid;
|
|
} else {
|
|
$openId = $wechatUser->getId();
|
|
}
|
|
if(!$openId){
|
|
return $this->failure('未获取到openid');
|
|
}
|
|
$is_subscribe = $this->getSubscribeStatus($openId);
|
|
//是否需要加入商家用户
|
|
$merchant_user = MerchantUsers::where('openid', $openId)->first();
|
|
if (!$merchant_user) {
|
|
$rand_str = $this->randString(8);
|
|
$merchantUser = new MerchantUser();
|
|
$merchantUser->openid = $openId;
|
|
$merchantUser->rand_str = $rand_str;
|
|
$merchantUser->pic = User::DefaultAvatar;
|
|
$merchantUser->nickname = '用户' . $rand_str;
|
|
$merchantUser->save();
|
|
}
|
|
//联盟信息
|
|
$enterprise_alliance = EnterpriseAlliance::find($alliance_id);
|
|
// 创建联盟的商户信息
|
|
$create_merchant = Anchor::where('m_id', $enterprise_alliance->mch_id)->select('name', 'mobile', 'pic', 'designation')->first();
|
|
$enterprise_alliance->creater = $create_merchant;
|
|
//分享码
|
|
$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;
|
|
|
|
if (!$enterprise_alliance->qrcode) {
|
|
$enterprise_alliance->qrcode = $this->getPreviewQrcode($url);
|
|
$enterprise_alliance->save();
|
|
}
|
|
$qr_codes = $enterprise_alliance->qrcode;
|
|
$merchants = EnterpriseAllianceMerchant::where('enterprise_alliance_id', $alliance_id)
|
|
->pluck('mch_id');
|
|
$settle_count = Anchor::whereIn('m_id', $merchants)->count();
|
|
|
|
//当前用户openid是否加入联盟
|
|
$merchant = MerchantAccount::where('openid', $openId)->first();
|
|
if ($merchant) {
|
|
$is_openid_join = EnterpriseAllianceMerchant::where('enterprise_alliance_id', $alliance_id)
|
|
->where('mch_id', $merchant->id)
|
|
->where('audit_type', 2)
|
|
->first() ? 1 : 0;
|
|
} else {
|
|
$is_openid_join = 0;
|
|
}
|
|
|
|
// 当前用户是否有未读通知
|
|
if ($is_openid_join) {
|
|
$has_unread_notice_ids = AllianceNotices::where('alliance_id', $alliance_id)
|
|
->where('notice_mch_id', $merchant->id)
|
|
->where('is_read', 0)
|
|
->pluck('join_mch_id');
|
|
$has_unread_notice = Anchor::whereIn('m_id', $has_unread_notice_ids)
|
|
->count() ? 1 : 0;
|
|
} else {
|
|
$has_unread_notice = 0;
|
|
}
|
|
|
|
//获取最新几条商家加盟信息
|
|
$notices = EnterpriseAllianceMerchant::where('enterprise_alliance_id', $alliance_id)
|
|
->orderBy('id', 'desc')
|
|
->limit(3)
|
|
->select('mch_id', 'created_at')
|
|
->get();
|
|
foreach ($notices as $notice) {
|
|
$merchant = Anchor::where('m_id', $notice->mch_id)
|
|
->select('name', 'pic', 'mobile')
|
|
->first();
|
|
if (!$merchant) {
|
|
$notice->name = '已删除商户';
|
|
} else {
|
|
$notice->name = $merchant->name;
|
|
}
|
|
}
|
|
|
|
return $this->success('ok', compact('is_subscribe', 'notices', 'enterprise_alliance', 'qr_codes', 'settle_count', 'is_openid_join', 'url', 'has_unread_notice'));
|
|
|
|
} catch (\Exception $e) {
|
|
Log::error($e->getMessage());
|
|
return $this->failure('数据错误');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取联盟商户
|
|
* @param Request $request
|
|
* @return JsonResponse|string
|
|
*/
|
|
public function allianceMerchants(Request $request)
|
|
{
|
|
try {
|
|
$keyword = $request->keyword;
|
|
$alliance_id = $request->alliance_id;
|
|
if (!$alliance_id) {
|
|
return $this->failure('联盟ID必传');
|
|
}
|
|
//加入联盟的商户
|
|
$merchant_ids = EnterpriseAllianceMerchant::where('enterprise_alliance_id', $alliance_id)->where('audit_type', 2)
|
|
->orderBy('created_at', 'desc')->pluck('mch_id');
|
|
|
|
//$merchant_ids = array_column($merchant_ids,'mch_id');
|
|
$merchant_info = MerchantAccount::with(['anchorV2', 'enterprise'])
|
|
->select('id', 'mobile', 'created_at')
|
|
->withCount(['information', 'service', 'activity', 'course', 'test', 'shop', 'video'])
|
|
->whereIn('id', $merchant_ids);
|
|
if ($keyword) {
|
|
$merchant_info = $merchant_info->whereHas('anchorV2', function ($sql) use ($keyword) {
|
|
$sql->where('mobile', 'like', '%' . $keyword . '%')
|
|
->orWhere('name', 'like', '%' . $keyword . '%')
|
|
->orWhere('id', $keyword);
|
|
});
|
|
}
|
|
//$m_ids= implode(",", $merchant_ids);
|
|
$merchant_ids = trim($merchant_ids, '[');
|
|
$merchant_ids = trim($merchant_ids, ']');
|
|
$merchant_info = $merchant_info->orderByRaw("FIELD(id, " . $merchant_ids . ")")
|
|
->paginate();
|
|
foreach ($merchant_info as $merchant) {
|
|
$ids = ConsultAccount::where('merchant_id', $merchant->id)
|
|
->pluck('id')
|
|
->toArray();
|
|
$merchant->consult_count = Consultation::whereIn('consult_account_id', $ids)
|
|
->count();
|
|
$live_anchor_ids = Anchor::where('m_id', $merchant->id)
|
|
->pluck('id')
|
|
->toArray();
|
|
$merchant->live_count = Live::whereIn('anchor_id', $live_anchor_ids)
|
|
->count();
|
|
}
|
|
return $this->success('ok', $merchant_info);
|
|
} catch (\Exception $e) {
|
|
Log::error($e->getMessage() . $e->getLine());
|
|
return $this->failure('数据错误');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 用户的商户是否加入企业联盟和是否有服务商账号
|
|
* @param Request $request
|
|
* @return JsonResponse|string
|
|
*/
|
|
public function isJoinAlliance(Request $request)
|
|
{
|
|
$alliance_id = $request->alliance_id;
|
|
if (!$alliance_id) {
|
|
return $this->failure('联盟ID必传');
|
|
}
|
|
$alliance = EnterpriseAlliance::find($alliance_id);
|
|
$mobile = MerchantAccount::where('id', $alliance->mch_id)
|
|
->value('mobile');
|
|
$wechatUser = session('wechat.oauth_user.new');
|
|
if (empty($wechatUser)) {
|
|
$openId = $request->openid;
|
|
} else {
|
|
$openId = $wechatUser->getId();
|
|
}
|
|
$merchant_user = MerchantUsers::where('openid', $openId)
|
|
->select('id', 'openid', 'mobile', 'nickname', 'pic', 'authorize_at')
|
|
->first();
|
|
$openid_merchant = MerchantAccount::with('anchorV2')
|
|
->where('openid', $openId)
|
|
->first();
|
|
if (isset($request->mobile) && $request->mobile) {
|
|
$mobile_merchant = MerchantAccount::with('anchorV2')
|
|
->where('mobile', $request->mobile)
|
|
->first();
|
|
} else {
|
|
$mobile_merchant = null;
|
|
}
|
|
$merchant = $openid_merchant ?: ($mobile_merchant ?: false);
|
|
if (!$merchant) {
|
|
$is_join = 0;
|
|
$merchant = [];
|
|
$merchant_audit_status = null;
|
|
$mobile = MerchantAccount::where('id', $alliance->mch_id)
|
|
->value('mobile');
|
|
return $this->success('ok', compact('is_join', 'merchant', 'merchant_user', 'alliance', 'merchant_audit_status', 'mobile'));
|
|
}
|
|
$allianceMerchant = EnterpriseAllianceMerchant::where('enterprise_alliance_id', $alliance_id)
|
|
->where('mch_id', $merchant->id)
|
|
->first();
|
|
$is_join = (!empty($allianceMerchant['audit_type']) && $allianceMerchant['audit_type'] == 2) ? 1 : 0;
|
|
$merchant_audit_status = !empty($allianceMerchant['audit_type']) ? $allianceMerchant['audit_type'] : 0;
|
|
|
|
//分享码
|
|
$url = env('APP_URL') . '/api/official/merchat/auth/login';
|
|
$qr_codes = Redis::get('MerchantLogin_S' . $merchant->id);
|
|
if (!$qr_codes) {
|
|
$qr_code = $this->getPreviewQrcode($url);
|
|
Redis::setex('MerchantLogin_S' . $merchant->id, 60 * 60 * 24 * 30, $qr_code);
|
|
$qr_codes = Redis::get('MerchantLogin_S->id' . $merchant->id);
|
|
}
|
|
$merchant->qr_code = $qr_codes;
|
|
$merchant->url = $url;
|
|
|
|
//分享码
|
|
$jump_url = urlencode(env('APP_URL') . '/al_m/#/joinUnion');
|
|
$url = env('APP_URL') . '/api/official/live/wechat/silenceAuth?alliance_id=' . $alliance_id . '&url=' . $jump_url . '&from_openid=' . $openId;
|
|
$qr_codes = Redis::get('EnterpriseAllianceJoin_S' . $alliance_id);
|
|
if (!$qr_codes) {
|
|
$qr_code = $this->getPreviewQrcode($url);
|
|
Redis::setex('EnterpriseAllianceJoin_S' . $alliance_id, 60 * 60 * 24 * 30, $qr_code);
|
|
$qr_codes = Redis::get('EnterpriseAllianceJoin_S' . $alliance_id);
|
|
}
|
|
$alliance->share_qrcode = $qr_codes;
|
|
return $this->success('ok', compact('is_join', 'merchant', 'merchant_user', 'alliance', 'merchant_audit_status', 'mobile'));
|
|
}
|
|
|
|
/**
|
|
* 加入企业联盟
|
|
* @param Request $request
|
|
* @return JsonResponse|string
|
|
*/
|
|
public function joinAlliance(Request $request)
|
|
{
|
|
$alliance_id = $request->alliance_id;
|
|
if (!$alliance_id) {
|
|
return $this->failure('联盟ID必传');
|
|
}
|
|
$alliance = EnterpriseAlliance::find($alliance_id);
|
|
// return $alliance;
|
|
if (!$alliance) return $this->failure('企业联盟不存在');
|
|
$wechatUser = session('wechat.oauth_user.new');
|
|
if (empty($wechatUser)) {
|
|
$openId = $request->openid;
|
|
} else {
|
|
$openId = $wechatUser->getId();
|
|
}
|
|
$openid_merchant = MerchantAccount::where('openid', $openId)->first();
|
|
if (isset($request->mobile) && $request->mobile) {
|
|
$mobile_merchant = MerchantAccount::with('anchorV2')
|
|
->where('mobile', $request->mobile)
|
|
->first();
|
|
} else {
|
|
$mobile_merchant = null;
|
|
}
|
|
$merchant = $openid_merchant ?$openid_merchant: ($mobile_merchant ?: false);
|
|
if (!$merchant) {
|
|
$is_join = 0;
|
|
$merchant = [];
|
|
return $this->success('ok', compact('is_join', 'merchant'));
|
|
}
|
|
$enterpriseAllianceMerchant = EnterpriseAllianceMerchant::where('enterprise_alliance_id', $alliance_id)
|
|
->where('mch_id', $merchant->id)
|
|
->first();
|
|
if ($enterpriseAllianceMerchant) {
|
|
if ($enterpriseAllianceMerchant->audit_type == 1) {
|
|
return $this->success('已经提交加盟申请加盟');
|
|
} else if ($enterpriseAllianceMerchant->audit_type == 2) {
|
|
return $this->success('已成功加盟');
|
|
} else {
|
|
return $this->success('已被拒绝加入该联盟');
|
|
}
|
|
}
|
|
$enterprise_alliance_merchant = new EnterpriseAllianceMerchant();
|
|
$enterprise_alliance_merchant->enterprise_alliance_id = $alliance_id;
|
|
$enterprise_alliance_merchant->mch_id = $merchant->id;
|
|
if ($alliance->audit_type == 1) {
|
|
$enterprise_alliance_merchant->audit_type = 1;
|
|
} else {
|
|
$enterprise_alliance_merchant->audit_type = 2;
|
|
}
|
|
$enterprise_alliance_merchant->save();
|
|
if ($enterprise_alliance_merchant) {
|
|
$anchor = Anchor::where('m_id', $merchant->id)
|
|
->first();
|
|
if ($alliance['audit_type'] == 1) {
|
|
$message = $anchor->name . '申请加入' . $alliance->name;
|
|
} else {
|
|
$message = $anchor->name . '已加入' . $alliance->name;
|
|
}
|
|
$join_mch_ids = EnterpriseAllianceMerchant::where('enterprise_alliance_id', $alliance_id)
|
|
->where('mch_id', '<>', $merchant->id)
|
|
->pluck('mch_id');
|
|
if (count($join_mch_ids) > 0) {
|
|
$add = [];
|
|
for ($i = 0; $i < count($join_mch_ids); $i++) {
|
|
$add[$i]['notice_mch_id'] = $join_mch_ids[$i];
|
|
$add[$i]['join_mch_id'] = $merchant->id;
|
|
$add[$i]['alliance_id'] = $alliance_id;
|
|
$add[$i]['content'] = $message;
|
|
$add[$i]['type'] = 'join_alliance';
|
|
$add[$i]['created_at'] = date('Y-m-d H:i:s');
|
|
}
|
|
AllianceNotices::insert($add);
|
|
}
|
|
return $this->success('ok', $merchant);
|
|
} else {
|
|
return $this->failure('加盟失败');
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 获取通知
|
|
* *
|
|
* @param Request $request
|
|
* @return JsonResponse|string
|
|
*/
|
|
public function getJoinAllianceNotice(Request $request)
|
|
{
|
|
$alliance_id = $request->alliance_id;
|
|
if (!$alliance_id) {
|
|
return $this->failure('联盟ID必传');
|
|
}
|
|
$wechatUser = session('wechat.oauth_user.new');
|
|
if (empty($wechatUser)) {
|
|
$openId = $request->openid;
|
|
} else {
|
|
$openId = $wechatUser->getId();
|
|
}
|
|
$openid_merchant = MerchantAccount::with('anchorV2')
|
|
->where('openid', $openId)
|
|
->first();
|
|
if (isset($request->mobile) && $request->mobile) {
|
|
$mobile_merchant = MerchantAccount::with('anchorV2')
|
|
->where('mobile', $request->mobile)
|
|
->first();
|
|
} else {
|
|
$mobile_merchant = null;
|
|
}
|
|
$merchant = $openid_merchant ?: ($mobile_merchant ?: false);
|
|
if (!$merchant) {
|
|
return $this->success('ok', []);
|
|
}
|
|
$notices = AllianceNotices::where('alliance_id', $alliance_id)
|
|
->where('notice_mch_id', $merchant->id)
|
|
->orderBy('id', 'desc')
|
|
->paginate();
|
|
foreach ($notices as $key => $notice) {
|
|
$jump_url = urlencode(env('APP_URL') . '/pu/#/');
|
|
$url = env('APP_URL') . '/api/official/live/wechat/FamilyAuth?merchant_id=' . $notice->join_mch_id . '&url=' . $jump_url . '&from_openid=' . $openId;
|
|
$notice->url = $url;
|
|
//$join_merchant = MerchantAccount::with('anchorV2')->where('id',$notice->join_mch_id)->first();
|
|
$merchant = Anchor::where('m_id', $notice->join_mch_id)
|
|
->select('id', 'name', 'pic')
|
|
->first();
|
|
if (!$merchant) {
|
|
unset($notices[$key]);
|
|
continue;
|
|
}
|
|
$notice->merchant = $merchant;
|
|
}
|
|
return $this->success('ok', $notices);
|
|
}
|
|
|
|
/**
|
|
* 读取通知
|
|
* *
|
|
* @param $notice_id
|
|
* @return JsonResponse|string
|
|
*/
|
|
public function readAllianceNotice($notice_id)
|
|
{
|
|
$notice = AllianceNotices::find($notice_id);
|
|
if($notice){
|
|
if ($notice->is_read != 1) {
|
|
$notice->is_read = 1;
|
|
$notice->save();
|
|
}
|
|
}else{
|
|
return $this->failure('通知信息不存在');
|
|
}
|
|
|
|
return $this->success('ok');
|
|
}
|
|
|
|
/**
|
|
* 获取企业联盟详情
|
|
* *
|
|
* @param Request $request
|
|
* @return JsonResponse|string
|
|
*/
|
|
public function alliance(Request $request)
|
|
{
|
|
try {
|
|
$alliance_id = $request->alliance_id;
|
|
if (!$alliance_id) {
|
|
return $this->failure('联盟ID必传');
|
|
}
|
|
$merchant_audit_status = 0;
|
|
$wechatUser = session('wechat.oauth_user.new');
|
|
if (empty($wechatUser)) {
|
|
$openId = $request->openid;
|
|
} else {
|
|
$openId = $wechatUser->getId();
|
|
}
|
|
|
|
if (!empty($openId)) {
|
|
$merchant_id = MerchantAccount::where('openid', $openId)->value('id');
|
|
if ($merchant_id) {
|
|
$merchant_audit_status = EnterpriseAllianceMerchant::where('enterprise_alliance_id', $alliance_id)
|
|
->where('mch_id', $merchant_id)
|
|
->value('audit_type');
|
|
}
|
|
}
|
|
|
|
$alliance = EnterpriseAlliance::find($alliance_id);
|
|
$mobile = MerchantAccount::where('id', $alliance->mch_id)
|
|
->value('mobile');
|
|
$merchants = EnterpriseAllianceMerchant::where('enterprise_alliance_id', $alliance_id)
|
|
->pluck('mch_id');
|
|
$settle_count = Anchor::whereIn('m_id', $merchants)
|
|
->count();
|
|
$alliance->merchant_count = $settle_count;
|
|
$alliance['mobile'] = $mobile;
|
|
$alliance['merchant_audit_status'] = $merchant_audit_status;
|
|
return $this->success('ok', $alliance);
|
|
} catch (\Exception $e) {
|
|
Log::error($e->getMessage());
|
|
return $this->failure('数据错误');
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* public function join($merchant_id,$alliance_id) {
|
|
* $merchant = MerchantAccount::with('anchorV2')
|
|
* ->where('id',$merchant_id)
|
|
* ->first();
|
|
* $is_join = EnterpriseAllianceMerchant::where('enterprise_alliance_id',$alliance_id)
|
|
* ->where('mch_id',$merchant_id)
|
|
* ->where('audit_type',2)
|
|
* ->first() ? 1:0;
|
|
* if($is_join) return true;
|
|
* $enterprise_alliance_merchant = new EnterpriseAllianceMerchant();
|
|
* $enterprise_alliance_merchant->enterprise_alliance_id = $alliance_id;
|
|
* $enterprise_alliance_merchant->mch_id = $merchant_id;
|
|
* $enterprise_alliance_merchant->save();
|
|
* if($enterprise_alliance_merchant) {
|
|
* $alliance = EnterpriseAlliance::find($alliance_id);
|
|
* $anchor = Anchor::where('m_id',$merchant_id)
|
|
* ->first();
|
|
* $message = $anchor->name.'加入了'.$alliance->name;
|
|
* $join_mch_ids = EnterpriseAllianceMerchant::where('enterprise_alliance_id',$alliance_id)
|
|
* ->where('mch_id','<>',$merchant->id)
|
|
* >pluck('mch_id');
|
|
* if(count($join_mch_ids) > 0) {
|
|
* $add = [];
|
|
* for ($i=0; $i< count($join_mch_ids); $i++) {
|
|
* $add[$i]['notice_mch_id'] = $join_mch_ids[$i];
|
|
* $add[$i]['join_mch_id'] = $merchant->id;
|
|
* $add[$i]['alliance_id'] = $alliance_id;
|
|
* $add[$i]['content'] = $message;
|
|
* $add[$i]['type'] = 'join_alliance';
|
|
* $add[$i]['created_at'] = date('Y-m-d H:i:s');
|
|
* }
|
|
* AllianceNotices::insert($add);
|
|
* }
|
|
* return true;
|
|
* } else {
|
|
* return false;
|
|
* }
|
|
* }
|
|
* **/
|
|
|
|
/**
|
|
* 企业入驻
|
|
* *
|
|
* @param Request $request
|
|
* @return JsonResponse|string
|
|
*/
|
|
public function enterpriseSettlement(Request $request)
|
|
{
|
|
$alliance_id = $request->alliance_id;
|
|
$wechatUser = session('wechat.oauth_user.new');
|
|
if (empty($wechatUser)) {
|
|
$openId = $request->openid;
|
|
} else {
|
|
$openId = $wechatUser->getId();
|
|
}
|
|
$operate = $request->operate;
|
|
$mobile = $request->mobile;
|
|
$code = $request->code;
|
|
//$password = $request->password??$mobile;
|
|
$sms = new Sms(new App);
|
|
$result = $sms->check($mobile, $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;
|
|
}
|
|
}
|
|
$MerchantAccount = MerchantAccount::where('mobile', $mobile)
|
|
->first();
|
|
if ($MerchantAccount) {
|
|
if ($operate == 'join') {
|
|
$is_join = EnterpriseAllianceMerchant::where('enterprise_alliance_id', $alliance_id)
|
|
->where('mch_id', $MerchantAccount->id)
|
|
->where('audit_type', 2)
|
|
->first() ? 1 : 0;
|
|
if ($is_join) {
|
|
return $this->success('ok', compact('MerchantAccount', 'anchor', 'operate'));
|
|
}
|
|
$result = $this->join($MerchantAccount->id, $alliance_id);
|
|
$mch_info = MerchantInfo::where('m_id', $MerchantAccount->id)
|
|
->first();
|
|
if ($mch_info) {
|
|
$mch_info->address = $request->address;
|
|
$mch_info->location_longitude = $request->location_longitude;
|
|
$mch_info->location_latitude = $request->location_latitude;
|
|
$mch_info->save();
|
|
} else {
|
|
$info = new MerchantInfo();
|
|
$info->m_id = $MerchantAccount->id;
|
|
$info->address = $request->address;
|
|
$info->province = $request->province ?? '';
|
|
$info->city = $request->city ?? '';
|
|
$info->district = $request->district ?? '';
|
|
$info->location_longitude = $request->location_longitude;
|
|
$info->location_latitude = $request->location_latitude;
|
|
$info->save();
|
|
}
|
|
}
|
|
$Account = MerchantAccount::where('openid', $openId)
|
|
->first();
|
|
if ($Account) $openId = '';
|
|
$MerchantAccount->openid = $openId;
|
|
$MerchantAccount->save();
|
|
$anchor = Anchor::where('m_id', $MerchantAccount->id)
|
|
->select('id', 'name', 'pic')
|
|
->first();
|
|
return $this->success('ok', compact('MerchantAccount', 'anchor', 'operate'));
|
|
}
|
|
$MerchantAccount = new MerchantAccount();
|
|
$MerchantAccount->mobile = $mobile;
|
|
if (isset($request->password) && $request->password) {
|
|
$MerchantAccount->password = encrypt($request->password);
|
|
} else {
|
|
$url = \CommonUtilsService::shortUrl(env('APP_URL') . '/pu_m/#/login');
|
|
$str = $this->getTradeNO();
|
|
$password = substr($str, 4, 8);
|
|
$message = '你的账号已注册成功,请访问' . $url['url'] . ' 完善信息 用户名 :' . $mobile . ' 密码 :' . $password . '【福恋】';
|
|
Message::create([
|
|
'phone' => $mobile,
|
|
'message' => $message,
|
|
'confirmed' => 1,
|
|
'ip' => request() ? request()->ip() : '127.0.0.1',
|
|
]);
|
|
}
|
|
|
|
// $MerchantAccount->type = 'business';
|
|
$Account = MerchantAccount::where('openid', $openId)
|
|
->first();
|
|
if ($Account) $openId = '';
|
|
$MerchantAccount->openid = $openId;
|
|
$MerchantAccount->save();
|
|
$rand_str = $this->randString(6);
|
|
$anchor = new Anchor();
|
|
$anchor->viewer_id = 0;
|
|
$anchor->pic = User::DefaultAvatar;
|
|
$anchor->name = $request->merchant_name ?: '用户' . $rand_str;
|
|
$anchor->status = 0;
|
|
$anchor->channel = 6;
|
|
$anchor->service_nature = 'person';
|
|
$anchor->mobile = $request->mobile;
|
|
$anchor->m_id = $MerchantAccount->id;
|
|
$anchor->save();
|
|
$info = new MerchantInfo();
|
|
$info->m_id = $MerchantAccount->id;
|
|
$info->address = $request->address ?? '';
|
|
$info->province = $request->province ?? '';
|
|
$info->city = $request->city ?? '';
|
|
$info->district = $request->district ?? '';
|
|
$info->location_longitude = $request->location_longitude ?? '';
|
|
$info->location_latitude = $request->location_latitude ?? '';
|
|
$info->save();
|
|
if (!(isset($request->password) && $request->password)) {
|
|
Messengers::sendSMS($mobile, $message);
|
|
}
|
|
// \CommonUtilsService::addNewAnchorToUser($anchor);
|
|
$MerchantAccount->token = $this->api_token($MerchantAccount);
|
|
$MerchantAccount->name = $rand_str;
|
|
$MerchantAccount->pic = User::DefaultAvatar;
|
|
$type = ['service', 'activity', 'consult', 'course', 'shop'];
|
|
foreach ($type as $key => $value) {
|
|
$rules = new MEarningRules();
|
|
$rules->m_id = $MerchantAccount->id;
|
|
$rules->name = $value;
|
|
$rules->ratio = 0;
|
|
$rules->first_sharer = 0;
|
|
$rules->last_sharer = 0;
|
|
$rules->other_sharer = 0;
|
|
$rules->forzen_time = 1;
|
|
$rules->save();
|
|
}
|
|
$data = ['merchant_id' => $MerchantAccount->id, 'anchor_id' => $anchor->id];
|
|
NewMerchantDefaultService::dispatch($data)->onQueue('love');
|
|
$url = env('APP_URL') . '/pu_m/#/';
|
|
$MerchantAccount->url = $url;
|
|
if ($operate == 'create_alliance') {
|
|
Redis::zadd('ApplyCreateAlliance_S', 1, $MerchantAccount->id);
|
|
$url = \CommonUtilsService::shortUrl(env('APP_URL') . '/pu_admin/#/');
|
|
$message = '通过您的商家账号登录电脑端' . $url['url'] . ' 创建企业联盟';
|
|
Message::create([
|
|
'phone' => $MerchantAccount->mobile,
|
|
'message' => $message,
|
|
'confirmed' => 1,
|
|
'ip' => request() ? request()->ip() : '127.0.0.1',
|
|
]);
|
|
Messengers::sendSMS($request->mobile, $message);
|
|
} elseif ($operate == 'join') {
|
|
$is_join = EnterpriseAllianceMerchant::where('enterprise_alliance_id', $alliance_id)
|
|
->where('mch_id', $MerchantAccount->id)
|
|
->where('audit_type', 2)
|
|
->first() ? 1 : 0;
|
|
if ($is_join) {
|
|
return $this->success('ok', compact('MerchantAccount', 'anchor', 'operate'));
|
|
}
|
|
$result = $this->join($MerchantAccount->id, $alliance_id);
|
|
if ($result) {
|
|
return $this->success('ok', compact('MerchantAccount', 'anchor', 'operate'));
|
|
} else {
|
|
return $this->failure('创建商家成功,加入联盟失败');
|
|
}
|
|
}
|
|
return $this->success('ok', compact('MerchantAccount', 'anchor', 'operate'));
|
|
}
|
|
|
|
/**
|
|
* 创建企业联盟
|
|
* *
|
|
* @param Request $request
|
|
* @return JsonResponse|string
|
|
*/
|
|
public function createAlliance(Request $request)
|
|
{
|
|
$wechatUser = session('wechat.oauth_user.new');
|
|
if (empty($wechatUser)) {
|
|
$openId = $request->openid;
|
|
} else {
|
|
$openId = $wechatUser->getId();
|
|
}
|
|
$merchant = MerchantAccount::where('openid', $openId)
|
|
->first();
|
|
if (!$merchant) {
|
|
return $this->failure('请先到个人中心注册成为商家');
|
|
}
|
|
$count = Redis::zscore('ApplyCreateAlliance_S', $merchant->id . '_' . $request->alliance_id);
|
|
if (!$count) {
|
|
$mobile = $merchant->mobile;
|
|
if (!$mobile) {
|
|
$mobile = Anchor::where('m_id', $merchant->id)
|
|
->value('mobile');
|
|
}
|
|
Redis::zadd('ApplyCreateAlliance_S', 1, $merchant->id . '_' . $request->alliance_id);
|
|
$url = \CommonUtilsService::shortUrl(env('APP_URL') . '/al_admin/#/');
|
|
$message = '通过您的商家账号登录电脑端' . $url['url'] . ' 创建企业联盟';
|
|
Message::create([
|
|
'phone' => $merchant->mobile,
|
|
'message' => $message,
|
|
'confirmed' => 1,
|
|
'ip' => request() ? request()->ip() : '127.0.0.1',
|
|
]);
|
|
Messengers::sendSMS($mobile, $message);
|
|
$is_subscribe = $this->getSubscribeStatus($openId);
|
|
$is_send_sms = 1;
|
|
} else {
|
|
Redis::zincrby('ApplyCreateAlliance_S', 1, $merchant->id);
|
|
$is_subscribe = $this->getSubscribeStatus($openId);
|
|
$is_send_sms = 0;
|
|
}
|
|
return $this->success('ok', compact('is_subscribe', 'is_send_sms'));
|
|
}
|
|
|
|
}
|