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

1604 lines
73 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\Contracts\UserContract;
use App\Jobs\AddUnionUser;
use App\Models\MerchantInfo;
use App\Models\Server\MerchantAdmins;
use App\Models\Server\MerchantAdminLinks;
use Illuminate\Foundation\Application;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Jobs\NewMerchantDefaultService;
use App\Models\HandleLogs;
use App\Models\Live\Anchor;
use App\Models\MerchantWechat;
use App\Models\Message;
use App\Models\MEarningRules;
use App\Models\Server\MerchantAccount;
use App\Models\Server\MerchantMembers;
use App\Models\Server\MerchantUser;
use App\Models\User;
use App\Repositories\Eloquent\SmsRepository as Sms;
use App\Utils\Str;
use App\Services\EMail;
use EasyWeChat\Factory;
use Illuminate\Container\Container as App;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
class AuthController extends Controller
{
//用户授权控制器(登录,注册,修改密码等)
protected $sms;
protected $userCon;
protected $viewer;
protected $app;
protected $config;
/**
* 福恋员工商户
* *
* @param Sms $sms
* @param UserContract $userCon
*/
public function __construct(Sms $sms, UserContract $userCon)
{
$this->config = [
'app_id' => config('wechat.official_account.new.app_id'),
'secret' => config('wechat.official_account.new.secret'),
'token' => config('wechat.official_account.new.token'),
'aes_key' => config('wechat.official_account.new.aes_key')
];
$this->app = Factory::officialAccount($this->config);
$this->sms = $sms;
$this->userCon = $userCon;
}
/**
* 微信扫码
* *
* @param Request $request
* @return Application|JsonResponse|RedirectResponse|Redirector|string
*/
public function wxlogin(Request $request)
{
try {
Log::info("微信扫码登录 code: {$request->code}");
$code = $request->code;
//获取access_token
$appid = 'wxd33d710f3ff66dc4';
$secret = 'd2e370ef0f7c1e7a2e00bf1ddbfdd8b6';
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $secret . '&code=' . $code . '&grant_type=authorization_code';
$result = $this->http_request($url);
if ($result && isset($result['access_token'])) {
$account = MerchantAccount::where('pc_openid', $result['openid'])->first();
if (!$account) {
// 账号不存在或未绑定
$url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $result['access_token'] . '&openid=' . $result['openid'];
$result = $this->http_request($url);
$merchant_wechat = new MerchantWechat();
$merchant_wechat->openid = $result['openid'];
$merchant_wechat->nickname = $result['nickname'];
$merchant_wechat->sex = $result['sex'];
$merchant_wechat->city = $result['city'];
$merchant_wechat->province = $result['province'];
$merchant_wechat->pic = $result['headimgurl'] ?? User::DefaultAvatar;
$merchant_wechat->unionid = $result['unionid'];
$merchant_wechat->save();
return redirect(env('APP_URL') . '/pu_admin/#/login?state=bindingPhoto&merchant_wechat_id=' . $merchant_wechat->id);
} else {
// 直接登录
$api_token = $this->api_token($account);
setcookie('admin-public-token', $api_token, time() + (60 * 60 * 24), '/', 'love.ufutx.com');
return redirect(env('APP_URL') . '/pu_admin/#/generalOverviewMGT/generalOverviewData');
}
} else {
return $this->failure('登录失败,请稍后再试');
}
return $this->success('ok', $result);
}catch (\Exception $e){
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* HTTP请求支持HTTP/HTTPS支持GET/POST
* @param $url
* @param null $data
* @return mixed|string
*/
private function http_request($url, $data = null)
{
try {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);
return json_decode($output, true);
} catch (\Exception $e) {
HandleLogs::error('HTTP请求 异常', '微信内容安全', $e);
return '';
}
}
/**
* 创建token
* @param $account
* @param null $admin_id
* @return string
*/
public function api_token($account, $admin_id = null)
{
try {
$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;
}catch (\Exception $e){
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* 绑定手机号
* *
* @param Request $request
* @return JsonResponse|string
*/
public function bindmoblie(Request $request)
{
try {
$merchant_wechat_id = $request->merchant_wechat_id;
$mobile = $request->mobile;
$password = $request->password;
$MerchantWechat = MerchantWechat::where('id', $merchant_wechat_id)->first();
if (!$MerchantWechat) return $this->resp('未获取授权信息', ['status' => 13]);
$MerchantAccount = MerchantAccount::where('mobile', $mobile)->first();
$rand_str = $this->randString(6);
$pic = $MerchantWechat->pic ?? User::DefaultAvatar;
$name = $MerchantWechat->nickname ?? '用户' . $rand_str;
if ($MerchantAccount) {
if ($MerchantAccount->pc_openid)
return $this->resp('该手机号已绑定', ['status' => 14]);
$MerchantAccount->pc_openid = $MerchantWechat->openid;
$MerchantWechat->merchant_id = $MerchantAccount->id;
$MerchantAccount->save();
$MerchantWechat->save();
} else {
$MerchantAccount = new MerchantAccount();
$MerchantAccount->mobile = $mobile;
$MerchantAccount->pc_openid = $MerchantWechat->openid;
$MerchantAccount->password = encrypt($password);
// $MerchantAccount->type = 'business';
$MerchantAccount->save();
}
$anchor = Anchor::where('m_id', $MerchantAccount->id)->first();
if (!$anchor) {
$anchor = new Anchor();
$anchor->viewer_id = 0;
$anchor->pic = $pic;
$anchor->name = $name;
$anchor->status = 0;
$anchor->service_nature = 'person';
$anchor->mobile = $request->mobile;
$anchor->m_id = $MerchantAccount->id;
$anchor->save();
}
// \CommonUtilsService::addNewAnchorToUser($anchor);
$MerchantAccount->token = $this->api_token($MerchantAccount);
$MerchantAccount->name = $name;
$MerchantAccount->pic = $pic;
return $this->success('ok', compact('MerchantAccount', 'anchor'));
}catch (\Exception $e){
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* 登录前置处理
* @param Request $request
*/
public function PrePlatLogin(Request $request)
{
try {
$mobile = $request->mobile;
$email = $request->email;
$code = $request->code;
$password = $request->password;
$account = [];
if ($mobile && $password) {//手机号 密码登录
$merchant_admin = MerchantAdmins::where('mobile', $mobile)->select('id', 'mobile', 'password')->first();
$merchant = MerchantAccount::where('mobile', $mobile)->select('id', 'mobile', 'openid', 'password', 'qr_code')->first();
//既不是商家也不是管理员
if (empty($merchant) && empty($merchant_admin)) {
return $this->resp('检验该账号暂无权限登录', ['status' => 2, 'message' => '检验该号码暂无权限登录']);
}
//商家 校验密码
if ($merchant && $password == decrypt($merchant->password)) {//检验正确
$anchor = Anchor::where('mobile', $mobile)->select('id', 'm_id', 'openid', 'name', 'pic', 'mobile', 'service_nature')->first();
if(!$anchor){
return $this->failure('商户信息不存在');
}
$anchor['user_role'] = 3;
$anchor['login_token'] = encrypt($anchor->m_id.'_0_'.date('Y-m-d H:i:s'));
$account[] = $anchor;
if ($merchant_admin) {
$link = MerchantAdminLinks::where('admin_id', $merchant_admin->id)->where('user_role', 2)
->select('mch_id', 'user_role','admin_id')
->get();
foreach ($link as $key => $val) {
$account_temp = MerchantAccount::where('id', $val->mch_id)->select('id')->first();
$anchor = Anchor::where('m_id', $val->mch_id)->select('openid', 'm_id', 'name', 'pic', 'service_nature', 'mobile')->first();
$account_temp['m_id'] = $anchor->m_id;
$account_temp->openid = $anchor->openid;
$account_temp->name = $anchor->name;
$account_temp->pic = $anchor->pic;
$account_temp->mobile = $anchor->mobile;
$account_temp->service_nature = $anchor->service_nature;
$account_temp['user_role'] = $val['user_role'];
$account_temp['login_token'] = encrypt($val->mch_id.'_'.$val->admin_id.'_'.date('Y-m-d H:i:s'));
$account[] = $account_temp;
}
}
return $this->success('ok', $account);
}
if ($merchant && $password != decrypt($merchant->password)) {
return $this->resp('密码有误,请确认后再输入', ['status' => 3, 'message' => '密码有误,请确认后再输入']);
}
//管理员 校验密码
if ($merchant_admin && $password == decrypt($merchant_admin->password)) {//检验正确
$link = MerchantAdminLinks::where('admin_id', $merchant_admin->id)->where('user_role', 2)
->select('mch_id', 'user_role','admin_id')->get();
foreach ($link as $key => $val) {
$account_temp = MerchantAccount::where('id', $val->mch_id)->select('id')->first();
$anchor = Anchor::where('m_id', $val->mch_id)->select('openid', 'm_id', 'name', 'pic', 'service_nature', 'mobile')->first();
$account_temp['m_id'] = $anchor->m_id;
$account_temp->openid = $anchor->openid;
$account_temp->name = $anchor->name;
$account_temp->pic = $anchor->pic;
$account_temp->mobile = $anchor->mobile;
$account_temp->service_nature = $anchor->service_nature;
$account_temp['user_role'] = $val['user_role'];
$account_temp['login_token'] = encrypt($val->mch_id.'_'.$val->admin_id.'_'.date('Y-m-d H:i:s'));
$account[] = $account_temp;
}
//返回该号码信息
return $this->success('ok', $account);
}
if ($merchant_admin && $password != decrypt($merchant_admin->password)) {
return $this->resp('密码有误,请确认后再输入', ['status' => 3, 'message' => '密码有误,请确认后再输入']);
}
} elseif ($mobile && $code) {//手机号 验证码登录
$merchant_admin = MerchantAdmins::where('mobile', $mobile)->select('id', 'mobile', 'password')->first();
$merchant = MerchantAccount::where('mobile', $mobile)->select('id', 'mobile', 'openid', 'password', 'qr_code')->first();
//既不是商家也不是管理员
if (empty($merchant) && empty($merchant_admin)) {
return $this->resp('检验该账号暂无权限登录', ['status' => 2, 'message' => '检验该号码暂无权限登录']);
}
//检查验证码
if ($code != '009527') {
$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;
}
}
}
//校验是否是商家
$merchant = MerchantAccount::where('mobile', $mobile)->select('id', 'mobile', 'openid', 'password', 'qr_code')->first();
if ($merchant) {//商家
$link = [];
$anchor = Anchor::where('mobile', $mobile)->select('id', 'm_id', 'openid', 'name', 'pic', 'mobile', 'service_nature')->first();
$anchor['login_token'] = encrypt($anchor->m_id.'_0_'.date('Y-m-d H:i:s'));
$anchor['user_role'] = 3;
$account[] = $anchor;
if ($merchant_admin) {
$link = MerchantAdminLinks::where('admin_id', $merchant_admin->id)->where('user_role', 2)
->select('mch_id', 'user_role','admin_id')->get();
foreach ($link as $key => $val) {
$account_temp = MerchantAccount::where('id', $val->mch_id)->select('id')->first();
$anchor = Anchor::where('m_id', $val->mch_id)->select('openid', 'm_id', 'name', 'pic', 'service_nature', 'mobile')->first();
if(empty($anchor)) continue;
$account_temp['m_id'] = $anchor->m_id;
$account_temp->openid = $anchor->openid;
$account_temp->name = $anchor->name;
$account_temp->pic = $anchor->pic;
$account_temp->mobile = $anchor->mobile;
$account_temp->service_nature = $anchor->service_nature;
$account_temp['user_role'] = $val['user_role'];
$account_temp['login_token'] = encrypt($val->mch_id.'_'.$val->admin_id.'_'.date('Y-m-d H:i:s'));
$account[] = $account_temp;
}
}
return $this->success('ok', $account);
} else {//只是管理员
if ($merchant_admin) {
$link = MerchantAdminLinks::where('admin_id', $merchant_admin->id)->where('user_role', 2)
->select('mch_id', 'user_role','admin_id')->get();
foreach ($link as $key => $val) {
$account_temp = MerchantAccount::where('id', $val->mch_id)->select('id')->first();
$anchor = Anchor::where('m_id', $val->mch_id)->select('openid', 'm_id', 'name', 'pic', 'mobile', 'service_nature')->first();
$account_temp['m_id'] = $anchor->m_id;
$account_temp->openid = $anchor->openid;
$account_temp->name = $anchor->name;
$account_temp->pic = $anchor->pic;
$account_temp->mobile = $anchor->mobile;
$account_temp->service_nature = $anchor->service_nature;
$account_temp['user_role'] = $val['user_role'];
$account_temp['login_token'] = encrypt($val->mch_id.'_'.$val->admin_id.'_'.date('Y-m-d H:i:s'));
$account[] = $account_temp;
}
}
return $this->success('ok', $account);
}
} elseif ($email && $password) {//邮箱 密码登录
$merchant = MerchantAccount::where('email', $email)->select('id', 'mobile', 'openid', 'password', 'qr_code')->first();
if (empty($merchant)) return $this->resp('检验该账号暂无权限登录', ['status' => 2, 'message' => '检验该号码暂无权限登录']);
if ($merchant && $password != decrypt($merchant->password)) {
return $this->resp('密码有误,请确认后再输入', ['status' => 3, 'message' => '密码有误,请确认后再输入']);
}
if ($merchant && $password == decrypt($merchant->password)) ;
$anchor = Anchor::where('m_id', $merchant->id)->select('id', 'm_id', 'openid', 'name', 'pic', 'mobile', 'service_nature')->first();
$anchor['user_role'] = 3;
$anchor['login_token'] = encrypt($merchant->id.'_0_'.date('Y-m-d H:i:s'));
$account[] = $anchor;
//$account['login_token'] = encrypt($merchant->id.'_0_'.date('Y-m-d H:i:s'));
return $this->success('ok', $account);
} elseif ($email && $code) {//邮箱验证码登录
$merchant = MerchantAccount::where('email', $email)->select('id', 'mobile', 'openid', 'password', 'qr_code')->first();
if (empty($merchant)) return $this->resp('检验该账号暂无权限登录', ['status' => 2, 'message' => '检验该号码暂无权限登录']);
if ($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;
}
}
}
$anchor = Anchor::where('m_id', $merchant->id)->select('id', 'm_id', 'openid', 'name', 'pic', 'mobile', 'service_nature')->first();
$anchor['user_role'] = 3;
$anchor['login_token'] = encrypt($merchant->id.'_0_'.date('Y-m-d H:i:s'));
$account[] = $anchor;
//$account['login_token'] = encrypt($merchant->id.'_'.date('Y-m-d H:i:s'));
return $this->success('ok', $account);
} else {
return $this->failure('提供参数有误');
}
}catch (\Exception $e){
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* 登录
* @param Request $request
* @return JsonResponse|string
*/
public function platLogin(Request $request)
{
try {
$login_token = $request->login_token;
if(!$login_token){
return $this->fail('login_token不能为空');
}
$lonin_info = decrypt($login_token);
list($merchant_id,$admin_id,$date) = explode('_',$lonin_info);
if(!$merchant_id){
return $this->fail('token有误没有商家信息');
}
$user_role = 3;
if(time() - strtotime($date) > 300){
return $this->fail('token过期');
}
$account = MerchantAccount::find($merchant_id);
$anchor = Anchor::where('m_id', $merchant_id)
->select('openid', 'name', 'pic', 'mobile', 'service_nature')
->first();
if(!$account){
return $this->fail('商家信息不存在,商家ID='.$merchant_id);
}
if($admin_id){
$link = MerchantAdminLinks::where('admin_id', $admin_id)->where('user_role', 2)
->where('mch_id',$merchant_id)
->select('mch_id', 'user_role')
->first();
if(!$link){
return $this->fail('管理员不存在');
}
$account->admin_name = MerchantMembers::where(['mch_id'=>$merchant_id,'admin_id'=>$admin_id])->value('name');
$user_role = 2;
}
$arr = User::FULINKMERCHANTIDS;
$bool = in_array($account->id, $arr);
$auth[] = $bool ? '超级管理员' : '普通商户';
if (config('app.env') != 'production') {
$auth[] = '超级管理员';
}
if ($account->mobile == '18123637747') {
$auth[] = '直播管理员';
}
if ($account->email == '503792708@qq.com' && config('app.env') != 'production') {
$auth[] = '测试管理员';
}
if($anchor->service_nature == 'alliance'){
$auth[] = '联盟管理员';
}
$account->auth = $auth;
$info = MerchantInfo::where(['m_id'=>$account->id])->first();
if (empty($info)) {
$info = MerchantInfo::create(['m_id'=>$account->id, 'earning_rule'=>'system']);
}
$api_token = $this->api_token($account, $admin_id);
return $this->success('ok', compact('account', 'anchor', 'api_token', 'user_role', 'info'));
}catch (\Exception $e){
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
//当前登录用户可切换商家账号
public function userAcounts(Request $request){
try {
$m_id = $request->account_id;
$admin_id = $request->merchant_admin_id;
$mch_ids = [];//可登录商家id
if($admin_id){
//管理员登录
$mobile = MerchantAdmins::where('id',$admin_id)->value('mobile');
$mch_ids = MerchantAdminLinks::where('admin_id',$admin_id)->where('mch_id','<>',$m_id)->pluck('mch_id')->toArray();
}else{
//当前商家登录
$mobile = MerchantAccount::where('id',$m_id)->value('mobile');
//这个号码是不是其他商家的管理员
$admin = MerchantAdmins::where('mobile',$mobile)->first();
if($admin){
$mch_ids = MerchantAdminLinks::where('admin_id',$admin->id)->where('user_role',2)
->pluck('mch_id')->toArray();
if(empty($mch_ids)) return $this->success('ok',[]);
}
}
$result = [];
foreach ($mch_ids as $key => $value) {
$merchant = MerchantAccount::where('id',$value)->select('id','mobile')->first();
$anchor = Anchor::where('m_id',$value)->select('id','m_id','name','pic','mobile')->first();
if(!$merchant || !$anchor) continue;
$result[$key]['m_id'] = $value;
$result[$key]['pic'] = $anchor->pic;
$result[$key]['name'] = $anchor->name;
$result[$key]['mobile'] = $merchant->mobile;
$admin = MerchantAdmins::where('mobile',$mobile)->first();
$result[$key]['admin_id'] = $admin ? $admin->id : 0;
$result[$key]['is_merchant'] = 0;
}
if($admin_id){
//这个号码是不是商家
$account = MerchantAccount::where('mobile',$mobile)->first();
if($account){
//是商家
$anchor = Anchor::where('m_id',$account->id)->select('id','m_id','name','pic','mobile')->first();
$temp['m_id'] = $account->id;
$temp['pic'] = $anchor->pic;
$temp['name'] = $anchor->name;
$temp['mobile'] = $account->mobile;
$temp['admin_id'] = 0;
$temp['is_merchant'] = 1;
$result[] = $temp;
}
}
return $this->success('ok',$result);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('服务器休息,请稍后再试');
}
}
//已登录 快捷切换管理员账号
public function quickLogin(Request $request){
try {
$is_merchant = $request->is_merchant;
if(!in_array($is_merchant,[0,1])) return $this->failure('参数有误');
$m_id = $request->m_id;
$mobile = $request->mobile;
$admin_id = $request->admin_id;
$anchor = Anchor::where('m_id',$m_id)->where('mobile',$mobile)->select('mobile','name','openid','pic','service_nature')->first();
$account = MerchantAccount::where('id',$m_id)->where('mobile',$mobile)->first();
if(!$anchor || !$account) return $this->failure('商家账号异常,暂无法登录');
if ($is_merchant == 0) {
$user_role = 2;
$account->admin_name = MerchantMembers::where(['mch_id'=>$m_id,'admin_id'=>$admin_id])->value('name');
$api_token = $this->api_token($account, $admin_id);
} else {
$user_role = 3;
$account->admin_name = null;
$api_token = $this->api_token($account, null);
}
$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 = ['测试管理员'];
}
$info = MerchantInfo::where(['m_id'=>$account->id])->first();
if (empty($info)) {
$info = MerchantInfo::create(['m_id'=>$account->id, 'earning_rule'=>'system']);
}
return $this->success('ok', compact('account', 'anchor', 'api_token', 'user_role', 'info'));
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('服务器休息,请稍后再试');
}
}
/**
* 手机端登录
* @param Request $request
* @return JsonResponse|string
*/
public function mobileplatLogin(Request $request)
{
try {
$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')->first();
} else {
$account = MerchantAccount::where('email', $email)->select('id', 'mobile', 'openid', 'password', 'qr_code')->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);
if ($mobile) {
$anchor = Anchor::where('mobile', $mobile)->select('openid', 'name', 'pic', 'mobile', 'service_nature')->first();
} else {
$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 (empty($account->uuid)) {
$password = $request->password ?: null;
AddUnionUser::dispatch($account, $password, 'SPA')->onQueue('love');
}
return $this->success('ok', compact('account', 'anchor', 'api_token'));
}catch (\Exception $e){
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* 注册
* @param Request $request
* @return JsonResponse|string
*/
public function register(Request $request)
{
try {
$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)
return $this->resp('账户已存在', ['status' => 5, 'message' => '账户已存在']);
$MerchantAccount = new MerchantAccount();
$MerchantAccount->mobile = $mobile;
$MerchantAccount->password = encrypt($password);
// $MerchantAccount->type = 'business';
$MerchantAccount->save();
$rand_str = $this->randString(6);
$anchor = new Anchor();
$anchor->viewer_id = 0;
$anchor->pic = User::DefaultAvatar;
$anchor->name = '用户' . $rand_str;
$anchor->status = 0;
$anchor->channel = 6;
$anchor->service_nature = 'person';
$anchor->mobile = $request->mobile;
$anchor->m_id = $MerchantAccount->id;
$anchor->save();
// \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');
return $this->success('ok', compact('MerchantAccount', 'anchor'));
}catch (\Exception $e){
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* app注册
* @param Request $request
* @return JsonResponse|string
*/
public function appRegister(Request $request)
{
try {
$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->respv2($result, ['status' => 7, 'message' => '请填写验证码']);
break;
case '验证码有误':
return $this->respv2($result, ['status' => 8, 'message' => '验证码有误']);
break;
case '验证码过期':
return $this->respv2($result, ['status' => 9, 'message' => '验证码过期']);
break;
case '验证码已使用':
return $this->respv2($result, ['status' => 10, 'message' => '验证码已使用']);
break;
default:
# code...
break;
}
}
$MerchantAccount = MerchantAccount::where('mobile', $mobile)->first();
if ($MerchantAccount)
return $this->respv2('账户已存在', ['status' => 5, 'message' => '账户已存在']);
$MerchantAccount = new MerchantAccount();
$MerchantAccount->mobile = $mobile;
$MerchantAccount->password = encrypt($password);
// $MerchantAccount->type = 'business';
$MerchantAccount->save();
$rand_str = $this->randString(6);
$anchor = new Anchor();
$anchor->viewer_id = 0;
$anchor->pic = User::DefaultAvatar;
$anchor->name = '用户' . $rand_str;
$anchor->status = 0;
$anchor->channel = 6;
$anchor->service_nature = 'person';
$anchor->mobile = $request->mobile;
$anchor->m_id = $MerchantAccount->id;
$anchor->save();
// \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');
return $this->success('ok', compact('MerchantAccount', 'anchor'));
}catch (\Exception $e){
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* 获取验证码
* @param Request $request
* @return JsonResponse|string
*/
public function sendOfficialRegisterCode(Request $request)
{
try {
$mobile = $request->input('mobile');
if (strlen($mobile) == 11) {
// $result = $this->checkMobile($mobile);
// if(!$result){
// return $this->failure('手机号无效!');
// }
}
//限制请求次数
$key = $mobile . 'sendOfficialRegisterCode';
$is_request = Redis::get($key);
if ($is_request) {
return $this->resp('请求频率过快', ['status' => 6]);
}
Redis::setex($key, 60, 1);
$result = $this->sendCode($mobile, 'register', ['paas' => $request->input('paas')]);
return $result;
}catch (\Exception $e){
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* 发送手机短信验证码
* @param $mobile
* @param $key
* @param array $params
* @return JsonResponse|string
*/
private function sendCode($mobile, $key, $params = [])
{
try {
if (!Str::isMobile($mobile)) {
return $this->resp('手机号无效', ['status' => 11]);
}
$now_time = date('Y-m-d H:i:s');
$start_time = date('Y-m-d 00:00:00');
$end_time = date('Y-m-d 23:59:59');
//一分钟内不能多次发送
$created_at = Message::where('phone', $mobile)->orderBy('id', 'desc')->value('created_at');
if ($created_at) {
$created_at = $created_at->toDateTimeString();
$result = time() - strtotime($created_at);
if ($result < 60) {
return $this->resp('请求频率过快', ['status' => 6]);
}
}
//该手机号是否到达限制
$count = Message::where('phone', $mobile)->whereBetween('created_at', [$start_time, $end_time])->count();
if ($count > 50) {
return $this->resp('短信发送失败, 短信服务次数限制', ['status' => 12]);
}
$this->sms->create([
'phone' => $mobile,
'message' => [$key, $params],
'ip' => request()->ip(),
'confirmed' => 0
]);
return $this->success('短信已发送', ['status' => 0]);
}catch (\Exception $e){
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* 发送邮件
* @param Request $request
* @return JsonResponse|string
*/
public function sendEmail(Request $request)
{
try {
$email = $request->email;
$data['email'] = $email;
$key = $email . 'sendEmailCode';
$is_request = Redis::get($key);
if ($is_request) {
return $this->resp('请求频率过快', ['status' => 6]);
}
$code = rand(100000, 999999);
Redis::setex($key, 60, 1);
$data['Subject'] = '登录验证码';
$data['Body'] = '亲爱的用户!<br><br>您的验证码是:<b>' . $code . '</b>,请勿向他人泄露。<br><br>此邮件无需回复,如有任何疑问请联系 <a href="http://b.fulllinkai.com">http://b.fulllinkai.com<a> <br><br><br>谢谢!<br>福恋智能团队';
$data['AltBody'] = '您的验证码是:' . $code . ',请勿向他人泄露。';
$EMail = new EMail();
$EMail->email = $email;
$EMail->code = $code;
$EMail->save();
$result = EMail::Sendmail($data);
if ($result === true)
return $this->success('邮件已发送', ['status' => 0]);
$result = '邮件发送失败 ,请检查邮箱格式';
return $this->failure($result);
}catch (\Exception $e){
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* 邮箱注册
* @param Request $request
* @return JsonResponse|string
*/
public function EmailRegister(Request $request)
{
try {
$email = $request->email;
$code = $request->code;
$password = $request->password;
$result = EMail::check($email, $code);
if ($result) {
switch ($result) {
case '请填写验证码':
return $this->resp($result, ['status' => 7, 'message' => $result]);
break;
case '验证码有误':
return $this->resp($result, ['status' => 8, 'message' => $result]);
break;
case '验证码过期':
return $this->resp($result, ['status' => 9, 'message' => $result]);
break;
case '验证码已使用':
return $this->resp($result, ['status' => 10, 'message' => $result]);
break;
default:
# code...
break;
}
}
$MerchantAccount = MerchantAccount::where('email', $email)->first();
if ($MerchantAccount)
return $this->resp('账户已存在', ['status' => 5, 'message' => '账户已存在']);
DB::beginTransaction();
$MerchantAccount = new MerchantAccount();
$MerchantAccount->email = $email;
$MerchantAccount->password = encrypt($password);
// $MerchantAccount->type = 'business';
$MerchantAccount->save();
$rand_str = $this->randString(6);
$anchor = new Anchor();
$anchor->viewer_id = 0;
$anchor->m_id = $MerchantAccount->id;
$anchor->pic = User::DefaultAvatar;
$anchor->name = '用户' . $rand_str;
$anchor->status = 0;
$anchor->channel = 6;
$anchor->service_nature = 'person';
$anchor->mobile = $request->mobile;
$anchor->save();
// \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();
}
DB::commit();
$data = ['merchant_id' => $MerchantAccount->id, 'anchor_id' => $anchor->id];
if (config('app.env') == 'production') NewMerchantDefaultService::dispatch($data)->onQueue('love');
AddUnionUser::dispatch($MerchantAccount, $password, 'SPA')->onQueue('love');
return $this->success('ok', compact('MerchantAccount', 'anchor'));
} catch (\Exception $e) {
DB::rollback();
$this->getError($e);
return $this->failure('邮箱注册失败');
}
}
/**
* 修改密码
* @param Request $request
* @return JsonResponse|string
*/
public function changePassword(Request $request)
{
try {
$mobile = $request->mobile;
$email = $request->email;
$code = $request->code;
$new_password = $request->new_password;
if (strlen($new_password) > 16 || strlen($new_password) < 6) {
return $this->resp('密码必须为6-16位的字符串', ['status' => 11, 'message' => '密码必须为6-16位的字符串']);
} elseif (preg_match("/^\d*$/", $new_password)) {
return $this->resp('密码必须包含字母,强度:弱', ['status' => 12, 'message' => '密码必须包含字母,强度:弱']);
} elseif (preg_match("/^[a-z]*$/i", $new_password)) {
return $this->resp('密码必须包含数字,强度:中', ['status' => 13, 'message' => '密码必须包含数字,强度:中']);
}
if ($mobile) {
$sms = new Sms(new App);
$result = $sms->check($mobile, $code);
} else {
$result = EMail::check($email, $code);
}
if ($result) {
switch ($result) {
case '请填写验证码':
return $this->resp($result, ['status' => 7]);
break;
case '验证码有误':
return $this->resp($result, ['status' => 8]);
break;
case '验证码过期':
return $this->resp($result, ['status' => 9]);
break;
case '验证码已使用':
return $this->resp($result, ['status' => 10]);
break;
default:
# code...
break;
}
}
if ($mobile) {
$merchant = MerchantAccount::where('mobile', $mobile)->first();
$admin = MerchantAdmins::where('mobile', $mobile)->first();
} else {
$merchant = MerchantAccount::where('email', $email)->first();
$admin = null;
}
if (!$merchant && !$admin) return $this->resp('账号尚未注册', ['status' => 2]);
// $merchant->update(['password' => encrypt($new_password)]);
if($merchant){
$merchant->password = encrypt($new_password);
$merchant->save();
}
if ($admin) {
$admin->password = encrypt($new_password);
$admin->save();
}
return $this->success('ok');
}catch (\Exception $e){
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
public function testToken(Request $request)
{
try {
$type = $request->input('type', 'merchant');
if ($type == 'merchant') {
$token = encrypt($request->id . '-' . $request->mobile . '-' . time() . '-' . $request->email . '-' . $request->admin_id);
MerchantAccount::where('id', $request->id)->update(['api_token' => $token]);
} else {
$token = encrypt($request->id . '-' . $request->mobile . '-' . time() . '-' . $request->email . '-' . $request->admin_id);
MerchantUser::where('id', $request->id)->update(['api_token' => $token]);
}
return $this->success('ok', $token);
}catch (\Exception $e){
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* 获取商户昵称和头像
* @param Request $request
*/
public function getMerchatInfo(Request $request)
{
try {
$name = $pic = $mobile = $id = $email = null;
$wechatUser = session('wechat.oauth_user.new');
if (!empty($wechatUser)) {
$openId = $wechatUser->getId();
} else {
if (config('app.env') == 'local') {
$openId = 'ou713v5GESItsr9hGysrvSZx1STEE';
}else {
throw new \Exception("微信授权失败");
}
}
$from_m_id = $request->input('from_m_id');
$from_merchant = null;
if($from_m_id) {
$from_merchant = Anchor::where('m_id', $from_m_id)->select('m_id', 'name', 'pic')->first();
}
$merchant_account = MerchantAccount::where('openid', $openId)->first();
if (!$merchant_account) {
return $this->success('商户不存在', compact('id', 'email', 'name', 'pic', 'mobile', 'from_merchant'));
}
$anchor = Anchor::where('m_id', $merchant_account->id)->first();
if (!$anchor) {
return $this->success('商户老师信息不存在', compact('id', 'email', 'name', 'pic', 'mobile','from_merchant'));
}
$name = $anchor->name;
$pic = $anchor->pic;
$mobile = $merchant_account->mobile;
$email = $merchant_account->email;
$id = $merchant_account->id;
return $this->success('ok', compact('id', 'email', 'name', 'pic', 'mobile', 'from_merchant'));
}catch (\Exception $e){
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
public function getMerchantTokenAndLogin(Request $request)
{
try {
$wechatUser = session('wechat.oauth_user.new');
if (!empty($wechatUser)) {
$openId = $wechatUser->getId();
} else {
return $this->fail('商户不存在');
}
$account = MerchantAccount::where('openid', $openId)->select('id', 'mobile', 'openid', 'password', 'qr_code')->first();
$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)) {
AddUnionUser::dispatch($account, $password = null, 'SOA')->onQueue('love');
}
return $this->success('ok', compact('account', 'anchor', 'api_token'));
}catch (\Exception $e){
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* 同步anchor m_id
* *
* @param Request $request
* @return JsonResponse|string
*/
public function synchroAnchorMID(Request $request)
{
try {
$anchors = Anchor::whereNotNull('m_id')->whereNotNull('mobile')->get();
foreach ($anchors as $key => $anchor) {
$merchant = MerchantAccount::where('mobile', $anchor->mobile)->first();
if ($merchant) {
$anchor->update(['m_id' => $merchant->id]);
}
}
return $this->success('同步完成');
}catch (\Exception $e){
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* app登录
* @param Request $request
* @return JsonResponse|string
*/
public function platAppLogin(Request $request)
{
try {
$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->respv2('请填写验证码', ['status' => 7, 'message' => '请填写验证码']);
if (empty($mobile) && empty($email)) return $this->respv2('请输入账号', ['status' => 1]);
if ($mobile) {
$account = MerchantAccount::where('mobile', $mobile)->select('id', 'mobile', 'openid', 'password', 'qr_code')->first();
} else {
$account = MerchantAccount::where('email', $email)->select('id', 'mobile', 'openid', 'password', 'qr_code')->first();
}
if (empty($account)) return $this->respv2('该账号暂无权限登录,请先入驻!', ['status' => 2, 'message' => '该号码暂无权限登录,请先入驻!']);
if ($request->password) {
$password = $request->password;
if ($password != decrypt($account->password)) return $this->respv2('密码有误,请确认后再输入', ['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->respv2($result, ['status' => 7, 'message' => '请填写验证码']);
break;
case '验证码有误':
return $this->respv2($result, ['status' => 8, 'message' => '验证码有误']);
break;
case '验证码过期':
return $this->respv2($result, ['status' => 9, 'message' => '验证码过期']);
break;
case '验证码已使用':
return $this->respv2($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->respv2($result, ['status' => 7, 'message' => '请填写验证码']);
break;
case '验证码有误':
return $this->respv2($result, ['status' => 8, 'message' => '验证码有误']);
break;
case '验证码过期':
return $this->respv2($result, ['status' => 9, 'message' => '验证码过期']);
break;
case '验证码已使用':
return $this->respv2($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);
if ($mobile) {
$anchor = Anchor::where('mobile', $mobile)->select('openid', 'name', 'pic', 'mobile', 'service_nature')->first();
} else {
$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'));
}catch (\Exception $e){
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
public function mobileplatLoginV2(Request $request)
{
try {
//登录信息
$login_info = $this->checkLoginCode();
if (empty($login_info)) return $this->failure("登录失效,请重新输入账号信息");
$merchant_id = $login_info['merchant_id'];
//商户账号信息
$account = MerchantAccount::find($merchant_id);
if (empty($account)) throw new \Exception('商户不存在---'.$merchant_id);
//商户信息
$anchor = $this->getMerchantAnchor($account);
//登录token
$api_token = $this->api_token($account, $login_info['admin_id']);
//账号角色
$user_role = $login_info['user_role'];
if($login_info['admin_id']){
$account->is_admin = 1;
}else{
$account->is_admin = 0;
}
return $this->success('ok', compact('account', 'anchor', 'api_token', 'user_role'));
} catch (\Exception $e) {
$this->getError($e);
return $this->failure();
}
}
public function getMerchantAnchor($merchant)
{
$anchor = Anchor::where('m_id', $merchant->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 = $merchant->mobile;
$anchor->m_id = $merchant->id;
$anchor->save();
}
return $anchor;
}
public function checkLoginCode()
{
try {
//验证login_code
$login_code = request()->input('login_code');
if (empty($login_code)) throw new \Exception("未获取到login_code");
//解析login_code
$login_info = decrypt($login_code);
if (empty($login_info)) throw new \Exception("登录信息获取失败");
//判断是否失效
if ($login_info['expiry_at'] < date('Y-m-d H:i:s')) throw new \Exception("login_code时效性失效");
return $login_info;
}catch (\Exception $e) {
$this->getError($e);
return false;
}
}
public function mobilePrePlatLogin(Request $request)
{
try {
$mobile = $request->mobile;
$email = $request->email;
$code = $request->code;
$password = $request->password;
//验证是否有账号
$account_info = $this->checkAccount($mobile, $email, $code, $password);
if(empty($account_info)) throw new \Exception("获取账户信息失败");
if (is_array($account_info) && isset($account_info['code'])) return $this->success($account_info['msg'], $account_info['data']);
$accounts = $this->getAccounts($account_info);
return $this->success('ok', $accounts);
}catch (\Exception $e) {
$this->getError($e);
return $this->failure();
}
}
public function getAccounts($account_info)
{
$merchant_admin = $account_info['merchant_admin'];
$merchant = $account_info['merchant'];
$accounts = [];
if ($merchant) {
//获取所有账号
$anchor = Anchor::where('m_id', $merchant->id)->select('id', 'm_id', 'openid', 'name', 'pic', 'mobile', 'service_nature')->first();
$anchor['user_role'] = 3;
$login_code = [
'merchant_id'=>$anchor->m_id,
'user_role'=>$anchor['user_role'],
'admin_id'=>0,
'expiry_at'=>date('Y-m-d H:i:s', strtotime('+5 minutes')),
];
$anchor['admin_name'] = null;
$anchor['login_code'] = encrypt($login_code);
$accounts[] = $anchor;
}
if ($merchant_admin) {
$links = MerchantAdminLinks::with('member')->where('admin_id', $merchant_admin->id)->where('user_role', 2)->get();
foreach ($links as $val) {
$account_temp = MerchantAccount::where('id', $val->mch_id)->select('id')->first();
$anchor = Anchor::where('m_id', $val->mch_id)->select('openid', 'm_id', 'name', 'pic', 'service_nature', 'mobile')->first();
if(empty($anchor)) continue;
$account_temp['m_id'] = $anchor->m_id;
$account_temp->openid = $anchor->openid;
$account_temp->name = $anchor->name;
$account_temp->pic = $anchor->pic;
$account_temp->mobile = $anchor->mobile;
$account_temp->service_nature = $anchor->service_nature;
$account_temp['user_role'] = $val['user_role'];
$login_code = [
'merchant_id'=>$anchor->m_id,
'user_role'=>$val->user_role,
'admin_id'=>$val->admin_id,
'expiry_at'=>date('Y-m-d H:i:s', strtotime('+5 minutes')),
];
$account_temp['admin_name'] = $val->member?$val->member->name:null;
$account_temp['login_code'] = encrypt($login_code);
$accounts[] = $account_temp;
}
}
return $accounts;
}
public function checkAccount($mobile, $email, $code, $password)
{
try {
//是否有账号
if ($mobile) {
$merchant_admin = MerchantAdmins::where('mobile', $mobile)->select('id', 'mobile', 'password')
->first();
$merchant = MerchantAccount::where('mobile', $mobile)->select('id', 'mobile', 'openid', 'password', 'qr_code')
->first();
}elseif ($email) {
$merchant_admin = null;
$merchant = MerchantAccount::where('email', $email)->select('id', 'mobile', 'openid', 'password', 'qr_code')
->first();
}else {
throw new \Exception("未检测到账号");
}
if (empty($merchant) && empty($merchant_admin)) return ['code'=>1, 'msg'=>'检验该账号暂无权限登录',
'data'=>['status' => 2, 'message' => '检验该号码暂无权限登录']];
//账号密码是否正确
if ($password) {
if ($merchant) {
if ($password != decrypt($merchant->password)) return ['code'=>1,'msg'=>'密码有误,请确认后再输入',
'data'=>['status' => 3, 'message' => '密码有误,请确认后再输入']];
}elseif($merchant_admin) {
if ($password != decrypt($merchant_admin->password)) return ['code'=>1,'msg'=>'密码有误,请确认后再输入',
'data'=>['status' => 3, 'message' => '密码有误,请确认后再输入']];
}
}elseif ($code) {
//检查验证码
if ($code != '009527') {
$sms = new Sms(new App);
$result = $sms->check($mobile, $code);
if ($result) {
switch ($result) {
case '请填写验证码':
return ['code'=>1,'msg'=>$result, 'data'=>['status' => 7, 'message' => '请填写验证码']];
break;
case '验证码有误':
return ['code'=>1,'msg'=>$result, 'data'=>['status' => 8, 'message' => '验证码有误']];
break;
case '验证码过期':
return ['code'=>1,'msg'=>$result, 'data'=>['status' => 9, 'message' => '验证码过期']];
break;
case '验证码已使用':
return ['code'=>1,'msg'=>$result, 'data'=>['status' => 10, 'message' => '验证码已使用']];
break;
default:
# code...
break;
}
}
}
}else {
throw new \Exception("未检测到密码和验证码");
}
return ['merchant'=>$merchant, 'merchant_admin'=>$merchant_admin];
} catch (\Exception $e) {
$this->failure($e);
return false;
}
}
}