491 lines
21 KiB
PHP
491 lines
21 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Server\Admin;
|
||
|
||
|
||
use App\Jobs\NewMerchantDefaultService;
|
||
use App\Jobs\SendEasySms;
|
||
use App\Jobs\SendMail;
|
||
use App\Models\EmailMessage;
|
||
use App\Models\Live\Anchor;
|
||
use App\Models\MEarningRules;
|
||
use App\Models\MerchantAccount;
|
||
use App\Models\SaasAppointment;
|
||
use App\Models\SaasUserAppointment;
|
||
use App\Models\Server\SaasAppointmentMember;
|
||
use App\Models\User;
|
||
use App\Repositories\Eloquent\SmsRepository as Sms;
|
||
use EasyWeChat\Kernel\Exceptions\Exception;
|
||
use Illuminate\Container\Container as App;
|
||
use Illuminate\Support\Facades\Cache;
|
||
use Illuminate\Http\JsonResponse;
|
||
use Illuminate\Http\Request;
|
||
use App\Http\Controllers\Controller;
|
||
use Illuminate\Support\Carbon;
|
||
use Illuminate\Support\Facades\DB;
|
||
use function Clue\StreamFilter\fun;
|
||
|
||
class AppointmentController extends Controller
|
||
{
|
||
|
||
/**
|
||
* 创建更新预约
|
||
* @param Request $request
|
||
* @return JsonResponse|string
|
||
*/
|
||
public function createAppointment(Request $request)
|
||
{
|
||
try {
|
||
DB::beginTransaction();
|
||
$content = $request->all();
|
||
if ($content['appointment_type'] == 'merchant') {
|
||
$appointment_type= MerchantAccount::class;
|
||
$appointment_target_id = $request->account_id;
|
||
} else {
|
||
$content['appointment_type'] = 'merchant';
|
||
$appointment_type = MerchantAccount::class;
|
||
}
|
||
//设置时间段类型
|
||
$time_type = $request->input('type', 'half_hour');
|
||
if (is_array($content['times'])) {
|
||
foreach ($content['times'] as $key => $val) {
|
||
if ($val['status']) {
|
||
if (Carbon::parse($val['end_time']) < Carbon::parse($val['start_time'])) {
|
||
return $this->failure('结束时间不能小于开始时间');
|
||
}
|
||
}
|
||
$appointment = SaasAppointment::where('merchant_id', $request->account_id)->where('appointment_type', $appointment_type)
|
||
->where('segment', $val['segment'])
|
||
->first();
|
||
if (!$appointment) {
|
||
$appointment = new SaasAppointment();
|
||
$appointment->appointment_type = $appointment_type;
|
||
$appointment->segment = $val['segment'];
|
||
$appointment->merchant_id = $request->account_id;
|
||
$appointment->appointment_target_id = $appointment_target_id;
|
||
$appointment->start_time = $val['start_time'];
|
||
$appointment->end_time = $val['end_time'];
|
||
$appointment->status = $val['status'];
|
||
$appointment->time_type= $time_type;
|
||
$appointment->save();
|
||
} else {
|
||
$appointment->appointment_type = $appointment_type;
|
||
$appointment->segment = $val['segment'];
|
||
$appointment->merchant_id = $request->account_id;
|
||
$appointment->appointment_target_id = $appointment_target_id;
|
||
$appointment->start_time = $val['start_time'];
|
||
$appointment->end_time = $val['end_time'];
|
||
$appointment->status = $val['status'];
|
||
$appointment->time_type = $time_type;
|
||
$appointment->save();
|
||
}
|
||
}
|
||
}
|
||
DB::commit();
|
||
return $this->success('ok');
|
||
} catch (\Exception $e) {
|
||
DB::rollBack();
|
||
$this->getError($e);
|
||
return $this->failure('服务器休息中,请稍后再试');
|
||
}
|
||
}
|
||
|
||
public function getMerchantAppointmentList(Request $request)
|
||
{
|
||
$type = $request->appointment_type ?? 'merchant';
|
||
if ($type == 'merchant') {
|
||
$type = MerchantAccount::class;
|
||
} else {
|
||
return $this->failure('无效的预约类型');
|
||
}
|
||
$list = SaasAppointment::where('merchant_id', $request->account_id)->where('appointment_type', $type)
|
||
->get();
|
||
return $this->success('ok', $list);
|
||
}
|
||
|
||
public function appointmentQrcode(Request $request)
|
||
{
|
||
try {
|
||
$merchant_id = $request->account_id;
|
||
$list = SaasAppointment::where('merchant_id', $merchant_id)->whereNotNull('start_time')->whereNotNull('end_time')->count();
|
||
if (empty($list)) return $this->failure('请先使设置可预约时间段');
|
||
$base_url = urlencode(env('APP_URL').'/pu/#/merchantsMembers?merchant_id='.$merchant_id);
|
||
$url = env('APP_URL').'/api/official/live/wechat/FamilyAuth?merchant_id='.$merchant_id.'&url='.$base_url;
|
||
$key = 'merchant:'.$request->account_id.':appointment:qrcode';
|
||
$qrcode = Cache::get($key);
|
||
if (!$qrcode){
|
||
$qrcode = $this->getUrlqrcode($url);
|
||
Cache::forever($key, $qrcode);
|
||
}
|
||
return $this->success('ok', compact('qrcode', 'url'));
|
||
|
||
}catch (Exception $e) {
|
||
$this->getError($e);
|
||
return $this->failure();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取预约用户列表
|
||
* @param Request $request
|
||
* @return JsonResponse|string
|
||
*/
|
||
public function getUserAppointmentList(Request $request)
|
||
{
|
||
$date = $request->appointment_date;
|
||
$start_time = $request->start_time;
|
||
$end_time = $request->end_time;
|
||
$keyword = $request->keyword;
|
||
$merchant_id = $request->account_id;
|
||
$type = MerchantAccount::class;
|
||
$list = SaasUserAppointment::with('merchant_user:id,nickname,pic','anchor:id,m_id,name')->where(function ($sql) use($merchant_id){
|
||
$sql->where('merchant_id', $merchant_id)->orWhere('owner_m_id', $merchant_id);
|
||
})->where('appointment_type', $type)
|
||
->when($date, function ($query) use ($date) {
|
||
$query->where('appointment_date', $date);
|
||
})
|
||
->when($start_time, function ($query) use ($start_time) {
|
||
$query->where('start_time', '>=', $start_time);
|
||
})
|
||
->when($end_time, function ($query) use ($end_time) {
|
||
$query->where('end_time', '<=', $end_time);
|
||
});
|
||
if($keyword){
|
||
$list = $list ->where(function ($query) use ($keyword){
|
||
$query->where('name','like',"%$keyword%")
|
||
->orWhere('mobile','like',"%$keyword%");
|
||
});
|
||
}
|
||
// $list = $list->orderBy('appointment_date', 'asc')
|
||
// ->orderBy('start_time', 'asc')
|
||
$list = $list->orderBy('id', 'desc')
|
||
->paginate();
|
||
|
||
return $this->success('ok', $list);
|
||
}
|
||
|
||
public function appointmentUsers(Request $request)
|
||
{
|
||
$date_type = $request->input('date_type');
|
||
$date = null;
|
||
$time = null;
|
||
switch ($date_type){
|
||
case 'today':
|
||
$date = date('Y-m-d');
|
||
break;
|
||
case 'yesterday':
|
||
$date = date('Y-m-d', strtotime('-1 day'));
|
||
break;
|
||
case 'week':
|
||
//周一
|
||
$week_num = (date('w') != 0)?date('w') :7;
|
||
$beginweek = date('Y-m-d', strtotime('-'.($week_num - 1).' days'));
|
||
$endweek = date('Y-m-d', strtotime('+'.(7 - $week_num).' days'));
|
||
$date = [$beginweek, $endweek];
|
||
break;
|
||
case 'month':
|
||
$beginmonth = date('Y-m-01');
|
||
$endmonth = date('Y-m-'.date('t') );
|
||
$date = [$beginmonth, $endmonth];
|
||
break;
|
||
default:
|
||
$start_date = $request->input('start_date','');
|
||
$end_date = $request->input('end_date', '');
|
||
if ($start_date && $end_date) {
|
||
$date = [$start_date, $end_date];
|
||
}
|
||
$start_time = $request->input('start_time');
|
||
$end_time = $request->input('start_time');
|
||
if ($start_time && $end_time) {
|
||
$time = [$start_time, $end_time];
|
||
}
|
||
break;
|
||
}
|
||
|
||
$keyword = $request->keyword;
|
||
$merchant_id = $request->account_id;
|
||
$type = MerchantAccount::class;
|
||
$list = SaasUserAppointment::with('merchant_user:id,nickname,pic')->where('merchant_id', $merchant_id)
|
||
->where('appointment_type', $type);
|
||
if ($date) {
|
||
$list = $list->when($date, function ($query) use ($date, $time) {
|
||
if (is_array($date)) {
|
||
$query->whereBetween('appointment_date', $date)->whereBetween('start_time', $time);
|
||
}else{
|
||
$query->where('appointment_date', $date);
|
||
}
|
||
});
|
||
}
|
||
|
||
if($keyword){
|
||
$list = $list ->where(function ($query) use ($keyword){
|
||
$query->where('name','like',"%$keyword%")
|
||
->orWhere('mobile','like',"%$keyword%");
|
||
});
|
||
}
|
||
$list = $list->orderBy('appointment_date', 'asc')
|
||
->orderBy('start_time', 'asc')
|
||
->paginate();
|
||
|
||
return $this->success('ok', $list);
|
||
}
|
||
|
||
/**
|
||
* 邀请预约成员二维码和链接
|
||
* @param Request $request
|
||
* @return JsonResponse|string
|
||
*/
|
||
public function memberQrcode(Request $request)
|
||
{
|
||
try {
|
||
$merchant_id = $request->account_id;
|
||
$base_url = urlencode(env('APP_URL').'/pu_m/#/customersBooking?merchant_id='.$merchant_id);
|
||
$url = env('APP_URL').'/api/official/live/wechat/FamilyAuth?merchant_id='.$merchant_id.'&url='.$base_url;
|
||
$key = 'merchant:'.$request->account_id.':appointment:member:qrcode';
|
||
$qrcode = Cache::get($key);
|
||
if (!$qrcode){
|
||
$qrcode = $this->getUrlqrcode($url);
|
||
Cache::forever($key, $qrcode);
|
||
}
|
||
return $this->success('ok', compact('qrcode', 'url'));
|
||
}catch (\Exception $e) {
|
||
$this->getError($e);
|
||
return $this->failure();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 预约商户成员列表
|
||
* @param Request $request
|
||
* @return JsonResponse|string
|
||
*/
|
||
public function members(Request $request)
|
||
{
|
||
try {
|
||
$merchant_id = $request->account_id;
|
||
$members = SaasAppointmentMember::with('liveAnchor:m_id,name,mobile,pic')->where('owner_m_id', $merchant_id);
|
||
$keyword = trim($request->input('keyword'));
|
||
if ($keyword) {
|
||
$members->whereHas('liveAnchor', function ($sql) use($keyword){
|
||
$sql->where(function ($sql) use($keyword){
|
||
$sql->where('name', 'like', '%'.$keyword.'%')
|
||
->orWhere('mobile', 'like', '%'.$keyword.'%');
|
||
});
|
||
});
|
||
}
|
||
$members = $members->orderBy('id', 'desc')->paginate();
|
||
return $this->success("ok", $members);
|
||
}catch (\Exception $e) {
|
||
$this->getError($e);
|
||
return $this->failure();
|
||
}
|
||
}
|
||
|
||
public function member(Request $request, $m_id)
|
||
{
|
||
try {
|
||
$merchant_id = $request->account_id;
|
||
$member = SaasAppointmentMember::with('liveAnchor:m_id,name,mobile,pic,designation,introduction')->where('m_id', $m_id)->where('owner_m_id', $merchant_id)->first();
|
||
if (empty($member)) return $this->failure("成员信息不存在");
|
||
return $this->success("ok", $member);
|
||
}catch (\Exception $e) {
|
||
$this->getError($e);
|
||
return $this->failure();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 修改预约成员信息
|
||
* @param Request $request
|
||
* @param $id
|
||
* @return JsonResponse|string
|
||
*/
|
||
public function updateMember(Request $request, $id)
|
||
{
|
||
try {
|
||
$merchant_id = $request->account_id;
|
||
$member = SaasAppointmentMember::where('m_id', $id)->where('owner_m_id', $merchant_id)->first();
|
||
if (empty($member)) throw new \Exception("成员信息不存在");
|
||
$status = $request->input('status');
|
||
$member->status = $status;
|
||
$member->save();
|
||
return $this->success('ok');
|
||
}catch (\Exception $e) {
|
||
$this->getError($e);
|
||
return $this->failure();
|
||
}
|
||
}
|
||
|
||
public function addMember(Request $request)
|
||
{
|
||
DB::beginTransaction();
|
||
try {
|
||
$merchant_id = $request->input('merchant_id');
|
||
if (empty($merchant_id)) throw new \Exception("缺少参数.merchant_id");
|
||
$owner_merchant = MerchantAccount::find($merchant_id);
|
||
if (config('app.env') == 'local') {
|
||
$open_id = "ou713v5GESItsr9hGysrvSZx1STEE";
|
||
}else {
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if (empty($wechatUser)) throw new \Exception("绑定预约成员授权失败");
|
||
$open_id = $wechatUser->getId();
|
||
}
|
||
$merchant_account = MerchantAccount::where('openid', $open_id)->first();
|
||
$password_email = 0;
|
||
if (empty($merchant_account)){
|
||
$mobile = $request->input("mobile");
|
||
$email = $request->input('email');
|
||
if (empty($mobile) && empty($email)) return $this->failure("请输入手机号或邮箱");
|
||
$code = $request->input('code');
|
||
if ($mobile && ($mobile == $owner_merchant->mobile || $owner_merchant->openid == $open_id)) return $this->failure('加入失败,暂不支持加入与本账号同样的商户');
|
||
if ($email && ($email == $owner_merchant->email || $owner_merchant->openid == $open_id)) return $this->failure('加入失败,暂不支持加入与本账号同样的商户');
|
||
if ($mobile) {
|
||
//检测验证码
|
||
$sms = new Sms(new App);
|
||
$result = $sms->check($mobile, $code);
|
||
//注册商户信息
|
||
$merchant_account = MerchantAccount::with('anchorV2')->where('mobile', $mobile)->first();
|
||
}else {
|
||
$result = EmailMessage::checkCode($email, $code);
|
||
//注册商户信息
|
||
$merchant_account = MerchantAccount::with('anchorV2')->where('email', $email)->first();
|
||
}
|
||
if ($result) return $this->failure($result);
|
||
|
||
if ($merchant_account) {
|
||
//存在账号并且微信号不一致
|
||
if ($merchant_account->openid && $merchant_account->openid != $open_id) return $this->failure('该手机号已存在');
|
||
|
||
//存在账号并且没有绑定微信(没有openid)
|
||
if (empty($merchant_account->openid)) {
|
||
//绑定微信
|
||
MerchantAccount::where('id', $merchant_account->id)->update(['openid'=>$open_id]);
|
||
Anchor::where('m_id', $merchant_account->id)->update(['openid'=>$open_id]);
|
||
}
|
||
$anchor = $merchant_account->anchorV2;
|
||
if (empty($anchor)) {
|
||
$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 = $merchant_account->mobile;
|
||
$anchor->openid = $open_id;
|
||
$anchor->m_id = $merchant_account->id;
|
||
$anchor->save();
|
||
}
|
||
$merchant_account->anchor = $anchor;
|
||
}else {
|
||
$password_email = 1;
|
||
//没有账号
|
||
$merchant_account = new MerchantAccount();
|
||
$merchant_account->mobile = $mobile;
|
||
$merchant_account->email = $email;
|
||
$str = $this->getTradeNO();
|
||
$password = substr($str,4,8);
|
||
$merchant_account->password = encrypt($password);
|
||
$merchant_account->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->openid = $open_id;
|
||
$anchor->m_id = $merchant_account->id;
|
||
$anchor->save();
|
||
$merchant_account->anchor = $anchor;
|
||
$type = ['service', 'activity', 'consult', 'course', 'shop'];
|
||
foreach ($type as $key => $value) {
|
||
$rules = new MEarningRules();
|
||
$rules->m_id = $merchant_account->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' => $merchant_account->id, 'anchor_id' => $anchor->id];
|
||
NewMerchantDefaultService::dispatch($data)->onQueue('love');
|
||
}
|
||
}else {
|
||
if ($merchant_account->id == $merchant_id) return $this->failure("加入失败,暂不支持加入与本账号同样的商户");
|
||
$anchor = $merchant_account->anchorV2;
|
||
if (empty($anchor)) {
|
||
$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 = $merchant_account->mobile;
|
||
$anchor->openid = $open_id;
|
||
$anchor->m_id = $merchant_account->id;
|
||
$anchor->save();
|
||
}
|
||
$merchant_account->anchor = $anchor;
|
||
|
||
}
|
||
$merchant_account->token = $this->apiToken($merchant_account);
|
||
$merchant_account->name = $anchor->name;
|
||
$merchant_account->pic = User::DefaultAvatar;
|
||
$merchant_account->is_admin = 1;
|
||
//增加成员
|
||
SaasAppointmentMember::firstOrCreate(['m_id'=>$merchant_account->id, 'owner_m_id'=>$merchant_id]);
|
||
if ($password_email && $email) {
|
||
$message = '恭喜您注册saas商家,您的邮箱账号:'.$email.'对应的密码:'.$password.',详情请进入后台管理:https://love.ufutx.com/pu_admin/#/login';
|
||
SendMail::dispatch($message, $email, 'message')->onQueue('security_code');
|
||
}
|
||
DB::commit();
|
||
return $this->success('ok', $merchant_account);
|
||
}catch (\Exception $e) {
|
||
DB::rollBack();
|
||
$this->getError($e);
|
||
return $this->failure();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 创建token
|
||
* @param $account
|
||
* @param null $admin_id
|
||
* @return string
|
||
*/
|
||
public function apiToken($account, $admin_id = null)
|
||
{
|
||
try {
|
||
$token = MerchantAccount::where('id', $account->id)->value('api_token');
|
||
if ($token) {
|
||
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('服务器休息中,请稍后再试');
|
||
}
|
||
}
|
||
}
|