serve_tab; $account = MerchantAccount::select( 'id', 'qr_code', 'share_icon', 'share_subtitle', 'share_title', 'openid', 'choose_tarbar', 'template_position', 'member_info', 'mobile', 'email', 'menu_config' ) ->where('id', $request->account_id) ->first(); $type = $request->type; if ($account && $account->menu_config) { $account->menu_config = json_decode($account->menu_config, true); } if ($type == 'simple') { $anchor = Anchor::where('m_id', $account->id)->select('id', 'm_id', 'pic', 'name', 'mobile', 'service_nature')->first(); } else { $anchor = Anchor::where('m_id', $account->id)->select( 'id', 'm_id', 'service_nature', 'pic', 'name', 'designation', 'introduction', 'service_nature' )->first(); } if ($anchor->service_nature == 'team' && $account->member_info) { $account->member_info = json_decode($account->member_info, true); } $jump_url = urlencode(config("app.url") . '/pu/#/'); $share_url = config("app.url") . '/api/official/live/wechat/FamilyAuth?merchant_id=' . $request->account_id . '&anchor_openid=' . $anchor->openid . '&serve_tab=' . $serve_tab . '&url=' . $jump_url; $qr_codes = Redis::get('HomePage_S' . $request->account_id); if (!$qr_codes) { $qr_codes = $this->getPreviewQrcode($share_url); Redis::setex('HomePage_S' . $request->account_id, 60 * 60 * 24 * 30, $qr_codes); $qr_codes = Redis::get('HomePage_S' . $request->account_id); } if ($account->choose_tarbar) { $account->choose_tarbar = json_decode($account->choose_tarbar, true); } else { $account->choose_tarbar = null; } if ($account->template_position) { $account->template_position = json_decode($account->template_position, true); } else { $account->template_position = null; } $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; $location_latitude = $account->info ? $account->info->location_latitude : null; $location_longitude = $account->info ? $account->info->location_longitude : null; $address = $account->info ? $account->info->address : null; $account['sign_code'] = $this->getSignInQrCode($request->account_id, null, null); $account->admin_name = null; if ($request->merchant_admin_id) $account->admin_name = MerchantMembers::where(['mch_id' => $request->account_id, 'admin_id' => $request->merchant_admin_id])->value('name'); $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('anchor', 'account', 'qr_codes', 'share_url', 'location_longitude', 'location_latitude', 'address', 'info')); } catch (\Exception $e) { DB::rollBack(); $this->getError($e); return $this->failure('获取个人资料失败'); } } /** * 编辑个人信息 * * * @param Request $request * @return JsonResponse|string */ public function updateBaseInfo(Request $request) { try { $await_checkoutTextArray = []; $account = MerchantAccount::where('id', $request->account_id)->first(); $pic = $request->pic; $introduction = $request->introduction; $share_icon = $request->share_icon;//分享图标 $share_title = $request->share_title;//分享标题 $await_checkoutTextArray[] = $share_title; $share_subtitle = $request->share_subtitle;//分享副标题 $await_checkoutTextArray[] = $share_subtitle; //敏感词汇过滤 if (empty($await_checkoutTextArray)) { $result = \CommonUtilsService::checkoutTextArrayV3($await_checkoutTextArray); if ($result['code'] == 1) { return $this->failure($result['cause']); } } $result = \CommonUtilsService::imageContentCecurity([$pic, $share_icon]); if ($result && isset($result['result']) && $result['result']) { return $this->failure('图片' . $result['result'] . ',请换一张照片'); } $anchor = Anchor::where('m_id', $account->id)->first(); if ($anchor) { if ($request->has('name')) { $anchor->name = $request->name; $serviceRoleAdmins = ServiceRoleAdmin::with([ 'operations', 'operations.remarks' => function ($query) use ($request) { $query->where('merchant_id', $request->account_id); } ]) ->where('merchant_id', $request->account_id)->where('admin_id', 0) ->get(); foreach ($serviceRoleAdmins as $serviceRoleAdmin) { if (!empty($serviceRoleAdmin->operations)) { foreach ($serviceRoleAdmin->operations as $sub_val) { if (!empty($sub_val->remarks)) { foreach ($sub_val->remarks as $sub_sub_val) { if ($sub_sub_val->admin_id == 0) { $sub_sub_val->admin_name = $request->name; $sub_sub_val->save(); } } } } } $serviceRoleAdmin->admin_name = $request->name; $serviceRoleAdmin->save(); } } if ($request->has('pic')) { $anchor->pic = $request->pic; } if ($request->has('introduction')) { $anchor->introduction = $request->introduction; } if ($request->has('service_nature')) { if ($anchor->service_nature == 'alliance' && $request->service_nature != 'alliance') { return $this->failure('暂不提供联盟转个人或团体功能'); } $anchor->service_nature = $request->service_nature; } if ($request->has('designation')) { $anchor->designation = $request->designation; } } if ($request->has('qr_code')) { $account->qr_code = $request->qr_code; } if ($request->has('share_icon')) { $account->share_icon = $request->share_icon; } if ($request->has('share_title')) { $account->share_title = $request->share_title; } if ($request->has('share_subtitle')) { $account->share_subtitle = $request->share_subtitle; } if ($request->has('registered_income')) { $account->registered_income = $request->registered_income; } if ($request->has('pay_income')) { $account->pay_income = $request->pay_income; } $info = MerchantInfo::where('m_id', $request->account_id)->first(); // $info = MerchantInfo::updateOrCreate(['m_id'=>$request->account_id]); if (!$info) { $info = new MerchantInfo(); $info->m_id = $request->account_id; $info->save(); } if ($request->has('location_latitude')) { $info->location_latitude = $request->location_latitude; } if ($request->has('location_longitude')) { $info->location_longitude = $request->location_longitude; } if ($request->has('address')) { $info->address = $request->address; } if ($request->has('make_friends')) { $info->make_friends = $request->make_friends; } if ($request->has('menu_config')) { if ($request->merchant_admin_id) { return $this->failure('管理员不能设置菜单'); } $account->menu_config = json_encode($request->menu_config); } DB::beginTransaction(); $anchor->save(); $info->save(); $account->save(); DB::commit(); $array = [ 'type' => 'baseinfo', 'content' => $introduction,//富文本内容 'id' => $anchor->id, ]; // UploadUrl::dispatch($array)->onQueue('love'); return $this->success('ok'); } catch (\Exception $e) { DB::rollBack(); $this->getError($e); return $this->failure('编辑个人资料失败'); } } /** * 创建团队成员 * @param Request $request * @return JsonResponse|string */ public function createTeamMember(Request $request) { try { $await_checkoutTextArray = []; $account = MerchantAccount::where('id', $request->account_id) ->first(); $anchor = Anchor::where('m_id', $account->id) ->first(); if (empty($anchor) || empty($account)) { return $this->failure('编辑失败,请联系开发人员'); } $input_data = json_decode($request->getContent(), true); $await_checkoutTextArray[] = $input_data['name']; $await_checkoutTextArray[] = $input_data['designation']; $await_checkoutTextArray[] = $input_data['introduction']; $result = CommonUtilsService::checkoutTextArrayV3($await_checkoutTextArray);//敏感词汇过滤 if ($result['code'] == 1) { return $this->failure($result['cause']); } DB::beginTransaction(); //查询成员是否存在 $merchantMembers = MerchantMembers::where('mch_id', $request->account_id) ->where('name', $input_data['name']) ->where('designation', $input_data['designation']) ->first(); if (!empty($input_data['name'])) { if (strlen($input_data['name']) > 20) { return $this->failure('姓名长度不能大于20个英文字符或8个中文汉字'); } } else { return $this->failure('姓名为必传字段'); } if ($merchantMembers) { return $this->failure('成员已经存在'); } $input_data['m_id'] = $request->account_id; if (!empty($input_data['user_role'])) { if ($input_data['user_role'] == 2 && $input_data['mobile']) { if ($request->merchant_admin_id) { return $this->failure('只能商家创建者才能添加管理员'); } $merchantMembers = new MerchantMembers(); $merchantMembers->name = $input_data['name']; $merchantMembers->status = $input_data['status']; $merchantMembers->designation = $input_data['designation']; $merchantMembers->introduction = $input_data['introduction']; $merchantMembers->avatar = $input_data['avatar']; $merchantMembers->mch_id = $request->account_id; $merchantMembers->save(); $merchant_admins = MerchantAdmins::where('mobile', $input_data['mobile']) ->withTrashed() ->first(); if ($merchant_admins) { if ($merchant_admins->deleted_at) { $merchant_admins->restore(); } $merchant_admin_link = MerchantAdminLinks::where('mch_id', $request->account_id) ->withTrashed() ->where('admin_id', $merchant_admins->id) ->first(); if ($merchant_admin_link) { if ($merchant_admin_link->deleted_at) { $merchant_admin_link->restore(); } else { return $this->failure('此管理员已经存在'); } } else { $merchantAdminLinks = new MerchantAdminLinks(); $merchantAdminLinks->mch_id = $request->account_id; $merchantAdminLinks->admin_id = $merchant_admins->id; $merchantAdminLinks->user_role = 2; $merchantAdminLinks->save(); //发短信 $password = decrypt($merchant_admins['password']); $anchor_name = Anchor::where('m_id', $request->account_id) ->value('name') ?? null; $message = $anchor_name . "平台已设置您为管理员,账号:" . $input_data['mobile'] . "密码:" . $password; Message::create([ 'phone' => $input_data['mobile'], 'message' => $message, 'confirmed' => 1, 'code' => 'saas,update_admin', 'ip' => request() ? request()->ip() : '127.0.0.1', ]); Messenger::sendSMS($input_data['mobile'], $message); } } else { $merchant_admins = new MerchantAdmins(); $merchant_admins->mobile = $input_data['mobile']; //查询是否是商家 $is_merchant = MerchantAccount::where('mobile', $input_data['mobile']) ->first(); if (!$is_merchant) { $password = $this->randString(6); $merchant_admins->password = encrypt($password); ; $merchant_admins->save(); } else { $merchant_admins->password = $is_merchant['password']; ; $merchant_admins->save(); $password = decrypt($is_merchant['password']); } $anchor_name = Anchor::where('m_id', $request->account_id) ->value('name') ?? null; $message = $anchor_name . "平台已设置您为管理员,账号:" . $input_data['mobile'] . "密码:" . $password; Message::create([ 'phone' => $input_data['mobile'], 'message' => $message, 'confirmed' => 1, 'code' => 'saas,update_admin', 'ip' => request() ? request()->ip() : '127.0.0.1', ]); Messenger::sendSMS($input_data['mobile'], $message); $merchant_admins_id = $merchant_admins->id; $merchant_admin_link = MerchantAdminLinks::where('mch_id', $request->account_id) ->where('admin_id', $merchant_admins_id) ->first(); if (!$merchant_admin_link) { $merchantAdminLinks = new MerchantAdminLinks(); $merchantAdminLinks->mch_id = $request->account_id; $merchantAdminLinks->admin_id = $merchant_admins_id; $merchantAdminLinks->user_role = 2; $merchantAdminLinks->save(); } } $merchantMembers->admin_id = $merchant_admins->id; $merchantMembers->save(); } elseif ($input_data['user_role'] == 1) { $merchantMembers = new MerchantMembers(); $merchantMembers->name = $input_data['name']; $merchantMembers->status = $input_data['status']; $merchantMembers->designation = $input_data['designation']; $merchantMembers->introduction = $input_data['introduction']; $merchantMembers->avatar = $input_data['avatar']; $merchantMembers->mch_id = $request->account_id; $merchantMembers->save(); } } if ($anchor->service_nature == 'person') { $anchor->service_nature = 'team'; } $anchor->save(); DB::commit(); return $this->success('ok'); } catch (\Exception $e) { DB::rollBack(); $this->getError($e); return $this->failure('编辑团队资料失败'); } } /** * 更新成员显示状态 */ public function updateMemberStatus(Request $request) { $member_id = $request->member_id; $status = $request->status; if (config('app.debug')) { if (!$member_id || !isset($status)) { return $this->failure('参数不全'); } } $member = MerchantMembers::where('mch_id', $request->account_id)->where('id', $member_id) ->first(); if (!$member) { return $this->failure('成员不存在'); } if (isset($status)) { $member->status = $status; $member->save(); } return $this->success('ok'); } /** * 更新成员信息 * @param Request $request * @return JsonResponse */ public function updateTeamMember(Request $request): JsonResponse { try { $input_data = json_decode($request->getContent(), true); if (empty($input_data['member_id'])) { return $this->failure('member_id为必填参数'); } $await_checkoutTextArray[] = $input_data['name']; $await_checkoutTextArray[] = $input_data['designation']; $await_checkoutTextArray[] = $input_data['introduction']; $result = CommonUtilsService::checkoutTextArrayV3($await_checkoutTextArray);//敏感词汇过滤 if ($result['code'] == 1) { return $this->failure($result['cause']); } if (!empty($input_data['name'])) { if (strlen($input_data['name']) > 20) { return $this->failure('姓名长度不能大于20个英文字符或8个中文汉字'); } } else { return $this->failure('姓名为必传字段'); } $member = MerchantMembers::where('id', $input_data['member_id']) ->where('mch_id', $request->account_id) ->first(); if ($member) { if ((!empty($input_data['user_role']) && $input_data['user_role'] == 1) || empty($input_data['user_role'])) { DB::beginTransaction(); $member->name = $input_data['name']; $member->status = $input_data['status']; $member->designation = $input_data['designation']; $member->introduction = $input_data['introduction']; $member->save(); if (!empty($member->admin_id)) { if ($request->merchant_admin_id) { return $this->failure('只能商家创建者才能更新管理员信息'); } $admin = MerchantAdmins::where('id', $member['admin_id']) ->first(); if ($admin) { $link = MerchantAdminLinks::where('admin_id', $admin->id) ->get(); if (sizeof($link) == 1) { MerchantAdminLinks::where('admin_id', $admin->id) ->delete(); MerchantAdmins::where('id', $admin->id) ->delete(); } else { MerchantAdminLinks::where('admin_id', $admin->id) ->where('mch_id', $request->account_id) ->delete(); } $member->admin_id = null; $member->save(); } } DB::commit(); } if (!empty($input_data['user_role']) && $input_data['user_role'] == 2 && $input_data['mobile']) { if ($request->merchant_admin_id) { return $this->failure('只能商家创建者才能更新管理员信息'); } $merchant_admins = MerchantAdmins::where('mobile', $input_data['mobile']) ->withTrashed() ->first(); if ($merchant_admins) { if ($merchant_admins->deleted_at) { $merchant_admins->restore(); } $merchant_admin_link = MerchantAdminLinks::where('mch_id', $request->account_id) ->withTrashed() ->where('admin_id', $merchant_admins->id) ->first(); if ($merchant_admin_link) { if ($merchant_admin_link->deleted_at) { $merchant_admin_link->restore(); } else { $member->name = $input_data['name']; $member->status = $input_data['status']; $member->designation = $input_data['designation']; $member->introduction = $input_data['introduction']; $member->save(); } } } else { DB::beginTransaction(); $merchantAdmins = new MerchantAdmins(); $merchantAdmins->mobile = $input_data['mobile']; $password = $this->randString(6); $merchantAdmins->password = encrypt($password); $merchantAdmins->save(); $merchant_admins_id = $merchantAdmins->id; //查询是否是商家 $is_merchant = MerchantAccount::where('mobile', $input_data['mobile']) ->first(); if (!$is_merchant) { $password = $this->randString(6); $merchantAdmins->password = encrypt($password); ; $merchantAdmins->save(); } else { $merchantAdmins->password = $is_merchant['password']; ; $merchantAdmins->save(); $password = decrypt($is_merchant['password']); } $anchor_name = Anchor::where('m_id', $request->account_id) ->value('name') ?? null; $message = $anchor_name . "平台已设置您为管理员,账号:" . $input_data['mobile'] . "密码:" . $password; Message::create([ 'phone' => $input_data['mobile'], 'message' => $message, 'confirmed' => 1, 'code' => 'saas,update_admin', 'ip' => request() ? request()->ip() : '127.0.0.1', ]); Messenger::sendSMS($input_data['mobile'], $message); $merchant_admin_link = MerchantAdminLinks::where('mch_id', $request->account_id) ->where('admin_id', $merchant_admins_id) ->withTrashed() ->first(); if ($merchant_admin_link) { if ($merchant_admin_link->deleted_at) { $merchant_admin_link->restore(); } return $this->success('ok'); } else { $merchant_admin_link = new MerchantAdminLinks(); $merchant_admin_link->mch_id = $request->account_id; $merchant_admin_link->admin_id = $merchant_admins_id; $merchant_admin_link->user_role = 2; $merchant_admin_link->save(); } $member->admin_id = $merchant_admins_id; $member->save(); DB::commit(); } } DB::commit(); return $this->success('更新成员成功'); } else { $this->failure('成员信息不存在'); } } catch (\Exception $e) { DB::rollBack(); $this->getError($e); return $this->failure('更新成员信息失败'); } } /** * 删除成员 * @param Request $request * @return JsonResponse */ public function deleteTeamMember(Request $request): JsonResponse { try { DB::beginTransaction(); $input_data = $request->all(); if (!$input_data['member_id']) { $this->failure('成员ID为必须参数'); } $merchantMembers = MerchantMembers::where('mch_id', $request->account_id) ->where('id', $input_data['member_id']) ->first(); if (!$merchantMembers) { $this->failure('团队成员信息不存在'); } if ($merchantMembers && !empty($merchantMembers['admin_id'])) { if ($request->merchant_admin_id) { return $this->failure('只能商家创建者才能删除管理员'); } $admin = MerchantAdmins::where('id', $merchantMembers['admin_id']) ->first(); if ($admin) { $link = MerchantAdminLinks::where('admin_id', $admin->id) ->get(); if (sizeof($link) == 1) { MerchantAdmins::where('id', $admin->id) ->delete(); } MerchantAdminLinks::where('admin_id', $admin->id) ->where('mch_id', $request->account_id) ->delete(); $merchantMembers->delete(); } } else { $merchantMembers->delete(); } DB::commit(); return $this->success('处理成功'); } catch (\Exception $e) { DB::rollBack(); $this->getError($e); return $this->failure('处理失败'); } } /** * 获取成员详情 * @param Request $request */ public function getMemberDetail(Request $request) { try { $input_data = $request->all(); if (!$input_data['member_id']) { $this->failure('成员ID为必须参数'); } $data = MerchantMembers::where('id', $input_data['member_id']) ->where('mch_id', $request->account_id) ->select('id', 'mch_id', 'name', 'designation', 'avatar', 'introduction', 'created_at', 'admin_id', 'status') ->first(); if (!empty($data) && !empty($data['admin_id'])) { $data['mobile'] = MerchantAdmins::where('id', $data['admin_id']) ->value('mobile'); $data['user_role'] = MerchantAdminLinks::where('mch_id', $request->account_id) ->where('admin_id', $data['admin_id']) ->value('user_role'); } else { $data['user_role'] = 1; $data['mobile'] = null; } return $this->success('处理成功', $data); } catch (\Exception $e) { $this->getError($e); return $this->failure('处理失败'); } } /** * 用户数据统计 * * * @param Request $request * @return JsonResponse|string */ public function usersStatv2(Request $request) { try { $openid = MerchantAccount::where('id', $request->account_id)->value('openid') ?? 'bussiness'; $start_time = date('Y-m-d 00:00:00:'); $end_time = date('Y-m-d 23:59:59:'); $data = []; //今日活跃用户 $today_active_users = AccessRecord::where('account_id', $request->account_id)->whereBetween('last_time', [$start_time, $end_time])->count(); $data['today_active_users'] = $today_active_users; //今日新增用户 // $today_regist_users = AccessRecord::where('account_id',$request->account_id)->whereBetween('created_at',[$start_time,$end_time])->count(); // $data['today_regist_users'] = $today_regist_users; //累计用户 $total_users = AccessRecord::where(function ($query) use ($request, $openid) { if ($openid && $openid != 'null') { $query->where('from_openid', $openid) ->orWhere('account_id', $request->account_id); } else { $query->where('account_id', $request->account_id); } })->groupBy('open_id')->get()->count(); $data['total_users'] = $total_users; // 今日访问用户 $data['today_visit_user'] = $today_active_users; //今日访问次数 $data['today_visit_count'] = $today_active_users; //今日付费用户 $data['today_consume_users'] = TouristOrder::where('merchant_id', $request->account_id)->whereIn('pay_status', [1, 4])->where('price', '>', 0)->whereBetween('created_at', [$start_time, $end_time])->select('account_id')->distinct()->count(); // 累计付费用户 $data['consume_users'] = MEarning::where('m_id', $request->account_id)->where('m_user_id', '<>', 0)->groupBy('m_user_id')->get()->count() ?? 0; // 今日流水 $data['today_consume_money'] = TouristOrder::where('merchant_id', $request->account_id)->whereIn('pay_status', [1, 4])->whereBetween('created_at', [$start_time, $end_time])->sum('price'); $account = MEarningAccount::where('m_id', $request->account_id)->where('m_user_id', 0)->first(); //总收益 $data['can_cashout_total'] = 0; if ($account) { //累计收益 $data['can_cashout_total'] = floatval($account->total_value); } //今日订单数 $data['today_orders'] = TouristOrder::where('merchant_id', $request->account_id)->whereIn('pay_status', [1, 4])->whereBetween('created_at', [$start_time, $end_time])->get()->count(); $data['total_orders'] = Order::where('merchant_id', $request->account_id)->whereIn('pay_status', [1, 4])->count(); return $this->success('ok', $data); } catch (\Exception $e) { Log::error($e->getMessage()); return $this->failure('数据错误,请稍后重试'); } } //月报柱形图 public function monthStat(Request $request) { $m_id = $request->account_id; $start_month = '2022-01-01'; $end_month = $request->end_month ?? '2022-05-01'; $month_arr = []; $visit_count = []; $add_count = []; $order_count = []; $profit = []; $start = new \DateTime($start_month); $end = new \DateTime($end_month); $interval = \DateInterval::createFromDateString('1 month'); $period = new \DatePeriod($start, $interval, $end); foreach ($period as $dt) { $month_arr[] = $dt->format("Y-m"); } $max_count = count($month_arr); foreach ($month_arr as $key => $month) { if ($key == $max_count - 1) { $lastMonthEnd = date('Y-m', strtotime('+1 month', strtotime($month))); } else { $lastMonthEnd = $month_arr[$key + 1]; } $order_count[] = \App\Models\Server\TouristOrder::where('merchant_id', $m_id)->where('created_at', '>=', $month)->where('created_at', '<=', $lastMonthEnd)->count(); $visit_count[] = AccessRecord::where('account_id', $month)->where('updated_at', '>=', $month)->where('updated_at', '<=', $lastMonthEnd)->count(); $profit[] = MEarning::where('m_id', $m_id)->where('status', 'finished')->where('created_at', '>=', $month)->where('created_at', '<=', $lastMonthEnd)->sum('value'); $add_count[] = AccessRecord::where('account_id', $m_id)->where('created_at', '>=', $month)->where('created_at', '<=', $lastMonthEnd)->count(); } return $this->success('ok', compact('month_arr', 'order_count', 'visit_count', 'add_count', 'profit')); } /** * 最近一周新增用户数据 * @param Request $request * @return JsonResponse|string */ public function weekUserStat(Request $request) { try { $openid = MerchantAccount::where('id', $request->account_id)->value('openid') ?? 'bussiness'; if ($request->start_time && $request->end_time) { $start_time = $request->start_time; $end_time = $request->end_time; //间隔天数 $between_time = ceil((strtotime($end_time) - strtotime($start_time)) / 3600 / 24) + 1; for ($i = 0; $i < $between_time; $i++) { $date[] = date("Y-m-d", strtotime("+$i day", strtotime($start_time))); } } foreach ($date as $start_time) { $date_arr[] = $start_time; $start_time = $start_time . ' 00:00:00'; $end_time = date("Y-m-d 23:59:59", strtotime($start_time)); $incre_count = AccessRecord::where('account_id', $request->account_id)->whereBetween('created_at', [$start_time, $end_time])->count(); $arr[] = $incre_count; $active_count = AccessRecord::where('account_id', $request->account_id)->whereBetWeen('last_time', [$start_time, $end_time])->count(); $active_arr[] = $active_count; } //三条平台通知 $notices = SaasNotice::where('m_id', $request->account_id)->where('type', 'analyse')->orderBy('id', 'desc')->limit(3)->get(); foreach ($notices as $notice) { $data = json_decode($notice->content, true); $notice->title = $data['title']; $notice->content = $data['content']; //unset($notice->content); } $is_read = SaasNotice::where('m_id', $request->account_id)->where('type', 'analyse')->where('is_read', 0)->count() ? 0 : 1; //四条产品动态 $infos = ProductInformations::orderBy('id', 'desc')->limit(4)->get(); foreach ($infos as $info) { $info->pv = Redis::zscore('product_information', $info->id) ?: 0; } return $this->success('ok', compact('date_arr', 'arr', 'active_arr', 'notices', 'infos', 'is_read')); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * 用户数据按时间统计 * @param Request $request * @return JsonResponse|string */ public function userStatByTime(Request $request) { try { if ($request->start_time && $request->end_time) { $start_time = $request->start_time; $end_time = $request->end_time; //间隔天数 $between_time = ceil((strtotime($end_time) - strtotime($start_time)) / 3600 / 24) + 1; for ($i = 0; $i < $between_time; $i++) { $date[] = date("Y-m-d", strtotime("+$i day", strtotime($start_time))); } } else { return $this->failure('请选择开始和结束时间'); } // $consult_ids = ConsultAccount::where('merchant_id',$request->account_id)->pluck('id'); // $community_ids = CommunityActivityResource::where('merchant_id',$request->account_id)->withTrashed()->pluck('id'); // $consulation_ids = Consultation::whereIn('consult_account_id',$consult_ids)->withTrashed()->pluck('id'); // $course_ids = Course::where('merchant_id',$request->account_id)->withTrashed()->pluck('id'); // $shop_ids = MerchantShop::where('merchant_id',$request->account_id)->withTrashed()->pluck('id'); // $information_ids = MerchantInformation::where('merchant_id',$request->account_id)->withTrashed()->pluck('id'); // $test_ids = QATest::where('merchant_id',$request->account_id)->withTrashed()->pluck('id'); foreach ($date as $start_time) { $date_arr[] = $start_time; $start_time = $start_time . ' 00:00:00'; $end_time = date("Y-m-d 23:59:59", strtotime($start_time)); //访问用户数 // $visiti_count[] = \App\Models\Log::where(function ($query) use ($shop_ids,$community_ids,$consulation_ids,$course_ids,$information_ids,$test_ids,$start_time,$end_time) { // // $query->where(function ($query) use ($community_ids,$start_time,$end_time) { // $query->where('log_type','App\Models\CommunityActivityResource') // ->whereIn('log_id',$community_ids)->whereBetween('created_at',[$start_time,$end_time])->pluck('logable_id'); // })->orWhere(function ($query) use ($consulation_ids,$start_time,$end_time) { // $query->where('log_type','App\Models\Consultation') // ->whereIn('log_id',$consulation_ids) // ->whereBetween('created_at',[$start_time,$end_time]); // })->orWhere(function ($query) use ($course_ids,$start_time,$end_time) { // $query->where('log_type','App\Models\Course\Course') // ->whereIn('log_id',$course_ids) // ->whereBetween('created_at',[$start_time,$end_time]); // })->orWhere(function ($query) use ($information_ids,$start_time,$end_time) { // $query->where('log_type','App\Models\MerchantInformation') // ->whereIn('log_id',$information_ids) // ->whereBetween('created_at',[$start_time,$end_time]); // })->orWhere(function ($query) use ($shop_ids,$start_time,$end_time) { // $query->where('log_type','App\Models\MerchantShop') // ->whereIn('log_id',$shop_ids) // ->whereBetween('created_at',[$start_time,$end_time]); // })->orWhere(function ($query) use ($test_ids,$start_time,$end_time) { // $query->where('log_type','App\Models\QATest') // ->whereIn('log_id',$test_ids) // ->whereBetween('created_at',[$start_time,$end_time]); // }); // })->select('logable_id')->distinct()->count(); //订单数 $order_count[] = TouristOrder::where('merchant_id', $request->account_id) ->where('pay_status', '<>', 0)->whereBetween('created_at', [$start_time, $end_time]) ->count(); // 成交金额 $deal_money[] = floatval(TouristOrder::where('merchant_id', $request->account_id) ->where('pay_status', '<>', 0)->whereBetween('created_at', [$start_time, $end_time]) ->sum('price')); } $start_time = $request->start_time . ' 00:00:00'; $end_time = $request->end_time . ' 23:59:59'; $total_money = TouristOrder::where('merchant_id', $request->account_id)->where('pay_status', '<>', 0) ->whereBetween('created_at', [$start_time, $end_time]) ->sum('price'); return $this->success('ok', compact('date_arr', 'order_count', 'deal_money', 'total_money')); } catch (\Exception $e) { Log::error($e->getMessage()); return $this->failure('数据错误,请稍后重试'); } } /** * 实时数据 今日 累计 * @param Request $request * @return JsonResponse|string */ public function userData(Request $request) { try { $openid = MerchantAccount::where('id', $request->account_id)->value('openid') ?? 'bussiness'; $start_time = date('Y-m-d 00:00:00:'); $end_time = date('Y-m-d 23:59:59:'); $data = []; //今日活跃用户 $today_active_users = AccessRecord::where('account_id', $request->account_id)->whereBetween('last_time', [$start_time, $end_time])->count(); //今日新增用户 $today_regist_users = AccessRecord::where('account_id', $request->account_id)->whereBetween('created_at', [$start_time, $end_time])->count(); //累计用户 $total_users = AccessRecord::where(function ($query) use ($request, $openid) { if ($openid && $openid != 'null') { $query->where('from_openid', $openid) ->orWhere('account_id', $request->account_id); } else { $query->where('account_id', $request->account_id); } })->groupBy('open_id')->get()->count(); //今日订单数 $today_orders = TouristOrder::where('merchant_id', $request->account_id)->whereIn('pay_status', [1, 4])->whereNotIn('type', ['system', 'reward_info', 'reward_activity'])->whereBetween('created_at', [$start_time, $end_time])->get()->count(); $data['today_orders'] = $today_orders; $account = MEarningAccount::where('m_id', $request->account_id)->where('m_user_id', 0)->first(); //可提现金额 $can_cash_out_amount = 0; $can_cashout_total = 0; if ($account) { $can_cash_out_amount = $account->balance; $data['can_cash_out_amount'] = floatval($can_cash_out_amount); //累计收益 $can_cashout_total = floatval($account->total_value); } // 累计付费用户 $consume_users = MEarning::where('m_id', $request->account_id)->where('m_user_id', '<>', 0)->groupBy('m_user_id')->get()->count() ?? 0; // 今日收益 $today_can_cashout_total = MEarning::where('m_id', $request->account_id)->where('m_user_id', 0)->whereBetween('created_at', [$start_time, $end_time])->sum('value'); $account_id = $request->account_id; $account = MerchantAccount::where('id', $account_id)->first(); $anchor = Anchor::where('m_id', $account->id)->first(); // if(empty($anchor)) return $this->failure('获取列表失败,请联系开发人员'); if ($anchor) { //我的社群服务id $community_ids = CommunityActivity::where('anchor_id', $anchor->id)->where('type', 'business')->where('class', 'many')->pluck('id')->toArray(); //我的社群活动id $activity_ids = CommunityActivity::where('anchor_id', $anchor->id)->where('type', 'business')->where('class', 'one')->pluck('id')->toArray(); //我的课程id if (!$anchor->openid) $anchor->openid = 'business';//未完善信息商户无数据 // $course_ids = Course::where('open_id',$anchor->openid)->where('type','business')->pluck('id')->toArray(); } else { $community_ids = CommunityActivity::where('merchant_id', $account_id)->where('type', 'business')->where('class', 'many')->pluck('id')->toArray(); //我的社群活动id $activity_ids = CommunityActivity::where('merchant_id', $account_id)->where('type', 'business')->where('class', 'one')->pluck('id')->toArray(); // $course_ids = Course::where('merchant_id',$account_id)->where('type','business')->pluck('id')->toArray(); } $course_ids = Course::where('merchant_id', $account_id)->where('type', 'business')->pluck('id')->toArray(); //我的咨询服务id $consult_ids = ConsultAccount::where('merchant_id', $request->account_id)->pluck('id')->toArray(); $consult_ids = Consultation::whereIn('consult_account_id', $consult_ids)->pluck('id')->toArray(); //我的商品id $shop_ids = MerchantShop::where('merchant_id', $request->account_id)->pluck('id')->toArray(); //vip $member_ids = SaasMemberLevel::where('merchant_id', $request->account_id)->pluck('id')->toArray(); $service_order_count = TouristOrder::whereIn('pay_status', [1, 4])->where('type', 'community')->whereIn('type_id', $community_ids)->count(); $activity_order_count = TouristOrder::whereIn('pay_status', [1, 4])->where('type', 'community')->whereIn('type_id', $activity_ids)->count(); $course_order_count = TouristOrder::whereIn('pay_status', [1, 4])->where('type', 'course')->whereIn('type_id', $course_ids)->count(); $shop_order_count = TouristOrder::whereIn('pay_status', [1, 4])->where('type', 'shop')->whereIn('type_id', $shop_ids)->count(); $consult_order_count = TouristOrder::whereIn('pay_status', [1, 4])->where('type', 'consult')->whereIn('type_id', $consult_ids)->count(); $vip_order_count = TouristOrder::whereIn('pay_status', [1, 4])->where('type', 'member')->whereIn('type_id', $member_ids)->count(); $total_count = $service_order_count + $activity_order_count + $course_order_count + $shop_order_count + $consult_order_count + $vip_order_count; $label = []; $label[0] = ['name' => '全部', 'num' => $total_count, 'type' => '', 'state' => true]; $label[1] = ['name' => '服务', 'num' => $service_order_count, 'type' => 'community', 'state' => false]; $label[2] = ['name' => '活动', 'num' => $activity_order_count, 'type' => 'activity', 'state' => false]; $label[3] = ['name' => '课程', 'num' => $course_order_count, 'type' => 'course', 'state' => false]; $label[4] = ['name' => '咨询', 'num' => $consult_order_count, 'type' => 'consult', 'state' => false]; $label[5] = ['name' => '商城', 'num' => $shop_order_count, 'type' => 'shop', 'state' => false]; $label[6] = ['name' => 'VIP', 'num' => $vip_order_count, 'type' => 'member', 'state' => false]; // $result->label = $label; return $this->success('ok', compact('today_active_users', 'today_can_cashout_total', 'consume_users', 'can_cash_out_amount', 'today_orders', 'today_regist_users', 'total_users', 'can_cashout_total', 'label')); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } public function personalInfo(Request $request) { try { $data = []; $serve_tab = $request->serve_tab; $avatar = User::DefaultAvatar; $nickname = '匿名商户'; $admin_id = $request->merchant_admin_id; $merchant_id = $request->account_id; $merchant = MerchantAccount::find($merchant_id); $anchor = Anchor::where('m_id', $merchant->id)->first(); if ($anchor) { $avatar = $anchor->pic; $nickname = $anchor->name; } $admin_name = null; if ($admin_id) { $admin_name = MerchantMembers::where('mch_id', $merchant_id)->where('admin_id', $admin_id)->value('name'); } $data['admin_name'] = $admin_name; $data['avatar'] = $avatar; $data['nickname'] = $nickname; $account = MEarningAccount::where('m_id', $merchant_id)->where('m_user_id', 0)->first(); $can_cash_out_amount = 0; $can_cashout_total = 0; if ($account) { $can_cash_out_amount = floatval($account->balance); //累计收益 $can_cashout_total = floatval($account->total_value); } $data['can_cashout_total'] = $can_cashout_total; $data['can_cash_out_amount'] = $can_cash_out_amount; //总订单数 $order_count = TouristOrder::whereIn('pay_status', [1, 4])->where('merchant_id', $merchant_id)->whereNotin('type', ['reward_activity', 'reward_info'])->whereNotNull('account_id')->count(); $data['order_count'] = $order_count; $jump_url = urlencode(env('APP_URL') . '/pu/#/'); $url = env('APP_URL') . '/api/official/live/wechat/FamilyAuth?merchant_id=' . $request->account_id . '&anchor_openid=' . $anchor->openid . '&serve_tab=' . $serve_tab . '&url=' . $jump_url; $qr_codes = Redis::get('HomePage_S' . $request->account_id); if (!$qr_codes) { $qr_codes = $this->getPreviewQrcode($url); Redis::setex('HomePage_S' . $request->account_id, 60 * 60 * 24 * 30, $qr_codes); $qr_codes = Redis::get('HomePage_S' . $request->account_id); } $data['qr_code'] = $qr_codes; //判断是否有未读通知 $system_sum = SaasNotice::where('m_id', $request->account_id)->whereNotIn('type', ['order', 'analyse'])->where('is_read', 0)->count(); $order_sum = SaasNotice::where('m_id', $request->account_id)->where('type', 'order')->where('is_read', 0)->count(); $data['system_sum'] = $system_sum; $data['order_sum'] = $order_sum; $data['is_hot'] = $system_sum + $order_sum; $data['id'] = $request->account_id; $data['share_url'] = $url; //未处理预约数 $data['appoint_count'] = SaasUserAppointment::where('merchant_id', $merchant_id)->where('appointment_date', '>=', date('Y-m-d'))->where('start_time', '>', date('H:i:s'))->count(); return $this->success('ok', $data); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * 移动端后台账号信息 * @param Request $request * @return JsonResponse|string */ public function baseInfoV2(Request $request) { try { $merchant_id = $request->merchant_id; $merchant = MerchantAccount::findOrFail($merchant_id); $anchor = Anchor::where('m_id', $merchant->id)->select('id', 'service_nature', 'introduction', 'designation', 'pic', 'name')->first(); if (empty($anchor)) return $this->failure('获取个人信息失败'); $anchor->qr_code = $merchant->qr_code; $member_info = []; if ($merchant->member_info && $merchant->member_info != 'null') { $member_info = json_decode($merchant->member_info, true); } $anchor->member_info = $member_info; $anchor->pic = $anchor->pic ? $anchor->pic : User::DefaultAvatar; $anchor->location_latitude = $merchant->info ? $merchant->info->location_latitude : null; $anchor->location_longitude = $merchant->info ? $merchant->info->location_longitude : null; $anchor->address = $merchant->info ? $merchant->info->address : null; return $this->success('ok', $anchor); } catch (\Exception $e) { $this->getError($e); return $this->failure('获取个人资料失败'); } } /** * 移动端后台编辑账号信息 * @param Request $request * @return JsonResponse|string */ public function updateBaseInfoV2(Request $request) { try { $merchant_id = $request->account_id; $merchant = MerchantAccount::findOrFail($merchant_id); $anchor = Anchor::where('m_id', $merchant->id)->select('id', 'service_nature', 'introduction', 'designation', 'pic', 'name')->first(); if (empty($anchor)) return $this->failure('编辑个人信息失败'); DB::beginTransaction(); if ($request->has('pic') && $request->pic != $anchor->pic) { $anchor->pic = $request->pic; } if ($request->name && $request->name != $anchor->name) { $anchor->name = $request->name; } if ($request->has('introduction') && $request->introduction != $anchor->introduction) { $anchor->introduction = $request->introduction; } if ($request->has('service_nature') && $request->service_nature != $anchor->service_nature) { $anchor->service_nature = $request->service_nature; } if ($request->qr_code && $request->qr_code != $merchant->qr_code) { $merchant->qr_code = $request->qr_code; } if ($request->has('designation') && $request->designation != $anchor->designation) { $anchor->designation = $request->designation; } if ($request->member_info && json_encode($request->member_info) != $merchant->member_info) { $merchant->member_info = json_encode($request->member_info); } if ($request->has('location_latitude') && $request->has('location_longitude') && $request->has('address')) { MerchantInfo::updateOrCreate(['m_id' => $merchant_id], ['location_latitude' => $request->location_latitude, 'location_longitude' => $request->location_longitude, 'address' => $request->address]); } $anchor->save(); $merchant->save(); DB::commit(); return $this->success('ok'); } catch (\Exception $e) { DB::rollBack(); $this->getError($e); return $this->failure('编辑资料失败,请联系开发人员'); } } /** * 绑定极光推送账户 * * * @param Request $request * @return JsonResponse|string */ public function BindJpush(Request $request) { try { $id = $request->account_id; $info = MerchantInfo::where('m_id', $id)->where('android_id', $request->android_id)->exists(); if (!$info) MerchantInfo::updateOrCreate(['m_id' => $id], ['android_id' => $request->android_id]); return $this->success('ok'); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * 获取成员列表 * @param Request $request * @return JsonResponse|string */ public function getMerchantMemberList(Request $request) { try { $keyword = $request->keyword; $merchant_id = $request->account_id; $admin = $request->admin; $data = MerchantMembers::with([ 'admin' => function ($query) { $query->select('id', 'mobile'); } ]) ->with([ 'adminLink' => function ($query) { $query->select('mch_id', 'admin_id', 'user_role'); } ]) ->where('mch_id', $merchant_id) ->when($keyword, function ($query) use ($merchant_id, $keyword) { $query->where(function ($sub_query_1) use ($merchant_id, $keyword) { $sub_query_1->where('mch_id', $merchant_id) ->Where('name', 'like', "%$keyword%"); }) ->orWhere(function ($sub_query_2) use ($merchant_id, $keyword) { $sub_query_2->where('mch_id', $merchant_id) ->Where('designation', 'like', "%$keyword%"); }) ->orWhere(function ($sub_query_3) use ($merchant_id, $keyword) { $sub_query_3->where('mch_id', $merchant_id) ->Where('introduction', 'like', "%$keyword%"); }); }) ->when($admin, function ($query) { $query->whereNotNull('admin_id'); }) ->select('id', 'mch_id', 'name', 'admin_id', 'designation', 'avatar', 'introduction', 'created_at', 'status') ->orderBy('id', 'desc'); if ($request->input('nopage') == 1) { $data = $data->get()->toArray(); } else { $data = $data->paginate()->toArray(); foreach ($data['data'] as $key => $val) { if (!empty($val['admin'])) { $data['data'][$key]['mobile'] = $val['admin']['mobile']; } else { $data['data'][$key]['mobile'] = null; } if (!empty($val['admin_link']) && !empty($val['admin'])) { if ($val['admin']['id'] == $val['admin_link']['admin_id'] && !empty($val['admin_link']['user_role'])) { $data['data'][$key]['user_role'] = $val['admin_link']['user_role']; } } else { $data['data'][$key]['user_role'] = 1; } unset($data['data'][$key]['admin']); unset($data['data'][$key]['admin_link']); unset($data['data'][$key]['mch_id']); } } return $this->success('操作成功', $data); } catch (\Exception $e) { $this->getError($e); return $this->failure('编辑资料失败,请联系开发人员'); } } /** * 更改密码 * @param Request $request * @return JsonResponse|string */ public function changeAdminPassword(Request $request) { try { $input_data = $request->all(); $member_id = $request->merchant_admin_id; if (empty($input_data['password'])) { return $this->failure('密码为必须参数'); } if ($member_id) { $admin = MerchantAdmins::find($member_id); if ($admin) { $admin->password = encrypt($input_data['password']); $admin->save(); if (!empty($admin->mobile)) { $merchant = MerchantAccount::where('mobile', $admin->mobile) ->first(); if ($merchant) { $merchant->password = encrypt($input_data['password']); $merchant->save(); } } } else { return $this->failure('用户信息不存在'); } } else { return $this->failure('只能管理员本人能更改密码'); } return $this->success('操作成功'); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * 临时转换用户用脚本 * @param Request $request */ public function itemMemberConvert(Request $request) { try { $merchant_member = MerchantAccount::whereNotNull('member_info') ->select('id', 'member_info') ->get(); foreach ($merchant_member as $key => $val) { $val = json_decode($val, true); if (!empty($val['member_info'])) { $val['member_info'] = json_decode($val['member_info'], true); try { DB::beginTransaction(); foreach ($val['member_info'] as $m_key => $m_val) { $member = MerchantMembers::where('name', $m_val['name']) ->where('avatar', $m_val['avatar']) ->where('mch_id', $val['id']) ->first(); if ($member) { continue; } $merchantMembersAdd = new MerchantMembers(); $merchantMembersAdd->name = $m_val['name']; $merchantMembersAdd->avatar = $m_val['avatar']; $merchantMembersAdd->designation = $m_val['designation']; $merchantMembersAdd->introduction = $m_val['introduction']; $merchantMembersAdd->mch_id = $val['id']; $merchantMembersAdd->save(); } //$merchant_member[$key]->member_info = null; //$merchant_member[$key]->save(); DB::commit(); } catch (\Exception $e) { DB::rollBack(); } } } return $this->success('ok'); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * 生成签到二维码 * @param $merchant_id * @param $type * @param $type_id * @return JsonResponse|string */ private function getSignInQrCode($merchant_id, $type, $type_id) { try { $qr_code = QrCode::when($type, function ($query) use ($type) { $query->where('type', $type); }) ->when($type_id, function ($query) use ($type_id) { $query->where('type_id', $type_id); }) ->where('merchant_id', $merchant_id) ->first(); if (!$qr_code) { $qr_code = new QrCode(); $qr_code->type = $type; $qr_code->merchant_id = $merchant_id; $qr_code->type_id = $type_id; $qr_code->save(); $jump_url = urlencode(env('APP_URL') . '/pu/#/signIn/' . $qr_code->id); $url = env('APP_URL') . '/api/official/live/wechat/FamilyAuth?merchant_id=' . $merchant_id . '&qr_code_id=' . $qr_code->id . '&url=' . $jump_url; $qr_code_url = $this->getPreviewQrcode($url); $qr_code->jump_url = $url; $qr_code->url = $qr_code_url; $qr_code->save(); } return $qr_code; } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } }