4709 lines
190 KiB
PHP
4709 lines
190 KiB
PHP
<?php
|
||
namespace App\Http\Controllers\Mobile;
|
||
use App\Models\BlessingUsers;
|
||
use App\Models\CommunityStar;
|
||
use App\Models\Course\UserCourses;
|
||
use App\Models\RankHistory;
|
||
use App\Jobs\CheckMobile;
|
||
use App\Http\Controllers\SmsController;
|
||
use App\Models\ApproveHistory;
|
||
use App\Models\Community;
|
||
use App\Models\CommunityMoment;
|
||
use App\Models\InteractLive;
|
||
use App\Models\Live\ActivityUser;
|
||
use App\Models\WangYiYunUser;
|
||
use App\Models\Message;
|
||
use App\Models\Live\ActivityUserRemark;
|
||
use App\Models\Live\Article;
|
||
use App\Models\Live\Ballot;
|
||
use App\Models\Live\Lottery;
|
||
use App\Models\Live\LotteryList;
|
||
use App\Models\Live\Opus;
|
||
use App\Models\MEarningRules;
|
||
use App\Models\Live\UserArticle;
|
||
use App\Models\UserReservation;
|
||
use App\Jobs\SendTemplateMsg;
|
||
use App\Models\SmsCountry;
|
||
use App\Services\UserService;
|
||
use App\Models\Live\LiveOrder;
|
||
use App\Models\Live\Anchor;
|
||
use App\Models\Live\AnchorViewer;
|
||
use App\Models\Live\Asset;
|
||
use App\Models\Live\AssetLog;
|
||
use App\Models\Live\Feedback;
|
||
use App\Models\Live\GiftOrder;
|
||
use App\Models\Live\LiveClass;
|
||
use App\Models\Live\Messenger;
|
||
use App\Models\Live\PayOrder;
|
||
use App\Models\Live\Question;
|
||
use App\Models\Live\Record;
|
||
use App\Models\Live\ViewerShare;
|
||
use App\Utils\IP;
|
||
use SimpleSoftwareIO\QrCode\Facades\QrCode;
|
||
use App\Models\Live\LiveViewer;
|
||
use App\Models\Live\Viewer;
|
||
use App\Models\Live\Live;
|
||
use App\Models\Live\ShareRole;
|
||
use App\Models\MerchantAccount;
|
||
use App\Models\ConsultationRecords;
|
||
use App\Models\Live\ShareIncome;
|
||
use App\Utils\Messenger as Messengers;
|
||
use App\Repositories\Eloquent\SmsRepository as Sms;
|
||
use App\Http\Controllers\Controller;
|
||
use App\Contracts\UserContract;
|
||
use Illuminate\Http\Request;
|
||
use App\Models\User;
|
||
use Illuminate\Support\Facades\Redis;
|
||
use Illuminate\View\View;
|
||
use App\Models\Wechat;
|
||
use EasyWeChat\Factory;
|
||
use App\Utils\Http;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\Cache;
|
||
use App\Services\LiveAlipayService;
|
||
use App\Jobs\TransferToAlipayAccount;
|
||
use App\Http\Controllers\Admin\Aliyun;
|
||
use App\Models\Live\Video;
|
||
use App\Jobs\SendSpreaderNote;
|
||
use App\Facades\QrcodeRectService;
|
||
use Illuminate\Pagination\LengthAwarePaginator;
|
||
use Propaganistas\LaravelPhone\PhoneServiceProvider;
|
||
use App\Jobs\addActivityUser;
|
||
use App\Jobs\NewMerchantDefaultService;
|
||
use App\Models\AccessRecord;
|
||
use App\Models\CoinLog;
|
||
use App\Models\CoinWithdrawLog;
|
||
use App\Models\ConsultAccount;
|
||
use App\Models\Consultation;
|
||
use App\Models\Course\Course;
|
||
use App\Models\MarriageMessenger;
|
||
use App\Models\MerchantUsers;
|
||
use App\Models\Order;
|
||
use App\Models\PayLog;
|
||
use App\Models\Server\MEarningAccount;
|
||
use App\Models\Server\MerchantUser;
|
||
use App\Models\Server\SaasNotice;
|
||
use App\Models\TouristOrder;
|
||
use App\Services\IMService;
|
||
use Illuminate\Container\Container as App;
|
||
|
||
class LiveController extends Controller
|
||
{
|
||
|
||
protected $sms;
|
||
protected $userCon;
|
||
protected $viewer;
|
||
protected $app;
|
||
public function __construct(Request $request, Sms $sms, UserContract $userCon){
|
||
|
||
|
||
|
||
$this->sms = $sms;
|
||
$this->userCon = $userCon;
|
||
|
||
$config = [
|
||
'app_id' => config('wechat.official_account.new.app_id'),
|
||
'secret' => config('wechat.official_account.new.secret'),
|
||
];
|
||
$this->app = Factory::officialAccount($config);
|
||
|
||
}
|
||
|
||
public function lives(Request $request)
|
||
{
|
||
try {
|
||
$keyword = $request->keyword;
|
||
|
||
//如果是首页搜索就给关键词增加热度
|
||
Redis::zincrby('live_search', 1 ,$keyword);
|
||
|
||
|
||
$lives = Live::where('is_show', 1)->select('*');
|
||
$start = $request->start;
|
||
|
||
if ($start == 1) {
|
||
$lives = $lives->whereIn('status', [1,3]);
|
||
} elseif ($start == 2) {
|
||
$lives = $lives->where('status', 0);
|
||
} elseif ($start == 3){
|
||
$lives = $lives->where('status', 2);
|
||
}
|
||
|
||
if ($request->has('class_id') && $request->class_id) {
|
||
$lives = $lives->where('class_id', $request->class_id);
|
||
}
|
||
|
||
if ($request->has('channel_id') && $request->channel_id) {
|
||
$lives = $lives->where('channel_id', $request->channel_id );
|
||
}
|
||
|
||
|
||
if($request->has('recommend')){
|
||
$lives = $lives->where('recommend', $request->recommend);
|
||
}
|
||
|
||
if($request->has('anchor_id') && $request->anchor_id){
|
||
$live_arr = [262,54,163,122,123];
|
||
$lives = $lives->where('anchor_id', $request->anchor_id )->whereNotIn('id',$live_arr);
|
||
}
|
||
|
||
if ($request->has('keyword') && $request->keyword) {
|
||
if (is_numeric($request->keyword)) {
|
||
$lives = $lives->where('id', $request->keyword);
|
||
}else{
|
||
$lives = $lives->where('title', 'like', '%' . $request->keyword . '%');
|
||
}
|
||
}
|
||
$lives = $lives->where('id', '!=', 108)->selectRaw('ABS(TIMESTAMPDIFF(HOUR,end_time,now())) as time')->orderByRaw(DB::raw('FIELD(status, 1,3,0,2) asc'))->orderByRaw('time asc')->paginate();
|
||
foreach ($lives as &$live){
|
||
$live->pv = Redis::zscore('page_pv', $live->id);
|
||
$live->start_time = date('Y-m-d H:i', strtotime($live->start_time));
|
||
|
||
if($live->times == 0){
|
||
$live->times = Redis::zscore('page_pv', $live->id);
|
||
}
|
||
// $command_arr = [];
|
||
// $new_arr = [];
|
||
// if(!empty($live->lottery)){
|
||
// foreach ($live->lottery as $item) {
|
||
// $new_arr['command'] = $item->command;
|
||
// $new_arr['num'] = $item->lottery->num;
|
||
// $command_arr[] = $new_arr;
|
||
// }
|
||
// }
|
||
// $live->command = $command_arr;
|
||
}
|
||
return $this->success('ok', $lives);
|
||
} catch (\Exception $e) {
|
||
$this->getError($e);
|
||
return $this->failure('获取直播首页数据失败,请稍后再试');
|
||
}
|
||
|
||
}
|
||
|
||
public function live(Request $request, $live_id)
|
||
{
|
||
$userId = 0;
|
||
$user = null;
|
||
$debug = $request->input('debug',0);
|
||
try {
|
||
if (\Auth::guard('api')->check()){
|
||
$user = \Auth::guard('api')->user();
|
||
$userId = $user->id;
|
||
$viewData = Viewer::where('user_id', $userId)->first();
|
||
if($viewData){
|
||
if($user->nickname != $viewData->nickname){
|
||
if($user->nickname){
|
||
$viewData->nickname = $user->nickname;
|
||
$viewData->save();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(empty( $wechatUser)){
|
||
$openId = $request->openid;
|
||
}else{
|
||
$openId = $wechatUser->getId();
|
||
}
|
||
$viewer = Viewer::where('openid', $openId)->first();
|
||
$live = Live::with(['class:id,name'])->withCount('register')->find($live_id);
|
||
if(empty($live)){
|
||
return $this->failure('直播不存在');
|
||
}
|
||
|
||
$live->viewer_id = $viewer->id??0;
|
||
// 查询用户是否预约和收藏
|
||
$live->reservations = 0;
|
||
$live->favorite = 0;
|
||
$live->is_subscribe = $this->getSubscribeStatus($request->openid);
|
||
|
||
$UserReservation = UserReservation::where('user_id',$userId)->where('type_id',$live_id)->where('status','!=',2)->get();
|
||
foreach ($UserReservation as $key => $value) {
|
||
if($value->type=='favorite')
|
||
$live->favorite = 1;
|
||
if($value->type=='reservations')
|
||
$live->reservations = 1;
|
||
}
|
||
if(!$live->user_id)
|
||
$live->user_id = Viewer::where('id', $live->teacher->viewer_id)->value('user_id');
|
||
$live->viewer = Viewer::where('live_id', $live_id)->select('id','nickname','avatar','user_id')->withCount('share')->paginate();
|
||
$live->pv = Redis::zscore('page_pv', $live_id);
|
||
//是否可以播放
|
||
$live->is_play = 1;
|
||
if($live->is_free == 1 && !empty($viewer)){
|
||
if($live->status == 2){
|
||
$key = $viewer->id.'_playlist';
|
||
$live->is_play = Redis::sismember($key, $live_id);
|
||
}
|
||
}
|
||
|
||
//如果是回放的话
|
||
if($live->status == 2){
|
||
$videos = Video::where('live_id', $live_id)->orderBy('created_at', 'desc')->get();
|
||
$new_videos = [];
|
||
$videoids =[];
|
||
foreach ($videos as &$video){
|
||
|
||
if(substr($video->uri, 0,4) != 'http') {
|
||
$new_videos[] = 'https://ufutx-image.oss-cn-shenzhen.aliyuncs.com/' . $video->uri;
|
||
}else{
|
||
$new_videos[] = $video->uri;
|
||
}
|
||
$videoids[] = $video->videoid;
|
||
}
|
||
$live->video = $new_videos;
|
||
$live->videoid = $videoids;
|
||
|
||
|
||
}
|
||
$mp_qrcode = Redis::get($live_id.'_mp_qrcode');
|
||
|
||
|
||
//如果已过期,重新生成
|
||
if(!$mp_qrcode){
|
||
if(empty($live->mp_qrcode)){
|
||
if(config('app.env') == 'local'){
|
||
$url = 'http://alpha.ufutx.net/api/official/live/wechat/oauth?source=live&live_id='.$live_id.'&type=live';
|
||
}
|
||
if (config('app.env') == 'alpha') {
|
||
$url = 'http://alpha.ufutx.net/api/official/live/wechat/oauth?source=live&live_id='.$live_id.'&type=live';
|
||
}
|
||
if(config('app.env') == 'production'){
|
||
$url = env('APP_URL').'/api/official/live/wechat/oauth?source=live&live_id='.$live_id.'&type=live';
|
||
}
|
||
|
||
$mp_qrcode = $this->qrcodeWithScene($url);
|
||
Redis::setex($live_id.'_mp_qrcode', 30 * 24 * 3600, $mp_qrcode);
|
||
}
|
||
}
|
||
|
||
$live->mp_qrcode = $mp_qrcode;
|
||
$live->start_time = str_replace('-', '/', $live->start_time);
|
||
if (empty($live)) {
|
||
return $this->failure('直播数据不存在');
|
||
}
|
||
|
||
$aliyun = New Aliyun();
|
||
$live->playUrl = $aliyun->getPlayUrl($live->class_id, $live->id);
|
||
$live->mobilePlayUrl = $aliyun->getHlsPlayUrl($live->class_id, $live->id);
|
||
$InteractLive = InteractLive::where('user_id',$live->user_id)->first();
|
||
$live->channels = $InteractLive->channels??[];
|
||
if( $live->channels){
|
||
foreach ($live->channels as $key => $value) {
|
||
$value->hls_pull_url = str_replace('http','https',$value->hls_pull_url);
|
||
$value->http_pull_url = str_replace('http','https',$value->hls_pull_url);
|
||
}
|
||
}
|
||
|
||
// $live->mobilePlayUrl = str_replace('https','http',$live->mobilePlayUrl);
|
||
//统计页面pv
|
||
$this->addPv($live, $viewer);
|
||
$live->live_viewer = LiveViewer::with('viewer:id,nickname,avatar,user_id')->where('live_id', $live_id)->limit(15)->orderBy('created_at', 'desc')->get();
|
||
$live_viewer = [];
|
||
foreach ($live->live_viewer as $key => $value) {
|
||
if(!$value->viewer){
|
||
unset($live->live_viewer[$key]);
|
||
continue;
|
||
}
|
||
array_push($live_viewer,$value);
|
||
|
||
}
|
||
$live->live_viewer = $live_viewer;
|
||
$live->viewer_count = count($live->live_viewer);
|
||
|
||
$live->wyy_user = null;
|
||
if(!empty($user)){
|
||
$live->wyy_user = $this->userCon->createWYYUser($user);
|
||
}
|
||
|
||
if($request->type=='business'){
|
||
$openid = $request->openid;
|
||
$token = WangYiYunUser::where('accid',$openid)->value('token');
|
||
|
||
if(!$token&&$openid){
|
||
// 创建token
|
||
$rand_str = $this->randString(10);
|
||
$data['openid'] = $openid;
|
||
$data['nickname'] = $name??'用户'.$rand_str;
|
||
$data['sex'] = 0;
|
||
$this->createWyyUser($data);
|
||
$token = WangYiYunUser::where('accid',$request->openid)->value('token');
|
||
|
||
}
|
||
$live->accid = $openid;
|
||
$live->token = $token;
|
||
}
|
||
// unset($live->wyy_user->mobile);
|
||
|
||
if($live->chat_room_id == 0){
|
||
$anchors = Anchor::with('viewer')->where('id', $live->anchor_id)->first();
|
||
|
||
if(empty($anchors)){
|
||
throw new \Exception("创建IM聊天室失败", 1);
|
||
}
|
||
if(empty($anchors->viewer)){
|
||
throw new \Exception("创建IM聊天室失败", 1);
|
||
}
|
||
|
||
$chat_room = \CommonUtilsService::createChatRoom($anchors->viewer->user_id,$live->title);
|
||
if (empty($chat_room)) throw new \Exception("创建IM聊天室失败", 1);
|
||
|
||
$liveData = Live::find($live_id);
|
||
$liveData->chat_room_id = $chat_room['roomid'];
|
||
$live->chat_room_id= $chat_room['roomid'];
|
||
$liveData->save();
|
||
}
|
||
return $this->success('ok', $live);
|
||
} catch (\Exception $e) {
|
||
$this->getError($e);
|
||
return $this->failure('获取直播详情失败');
|
||
}
|
||
|
||
}
|
||
|
||
public function liveRecommendCourses(Request $request, $live_id)
|
||
{
|
||
$user = auth()->user();
|
||
$user_id = 0;
|
||
if(isset($user))
|
||
$user_id = $user->id;
|
||
$live = Live::find($live_id);
|
||
if($live->recommend_course_id) {
|
||
$course_ids = json_decode($live->recommend_course_id,true);
|
||
$courses = Course::where('is_show',1)->whereIn('id',$course_ids)->withCount('videos')->orderBy('sort','desc')->orderBy('user_count','desc')->paginate();
|
||
foreach ($courses as $key => $value) {
|
||
//原价
|
||
$value->original_price = $value->charge;
|
||
$value->discount_status = 0;
|
||
if($value->end_time_discount>now()&&$value->start_time_discount<now()&&$value->discount_price){
|
||
$value->charge = $value->discount_price;
|
||
$value->discount_status = 1;
|
||
}
|
||
$value['videos_count'] = $value->videos_count;
|
||
$value['paymentStatus'] = false;
|
||
if(isset($user->id))
|
||
$value['paymentStatus'] = UserCourses::where('course_id',$value->id)->where('user_id',$user->id)->where('status',1)->count()?true:false;
|
||
}
|
||
} else {
|
||
$courses = [];
|
||
}
|
||
return $this->success('ok', $courses);
|
||
}
|
||
|
||
// 推广数据
|
||
public function InviteInfo(Request $request)
|
||
{
|
||
$user = auth()->user();
|
||
$coin = $user->coinInfo();
|
||
$data['alipay_account'] = $coin['alipay_account']??0;
|
||
$data['alipay_real_name'] = $coin['alipay_real_name']??0;
|
||
//推广人数
|
||
$data['user_num'] = CoinLog::where('user_id', $user->id)->where('type','INVITE')->groupBy('type_id')->count()??0;
|
||
//总金额
|
||
$data['total_value'] = intval(CoinLog::where('type', 'INVITE')->where('user_id', $user->id)->sum('coin')??0);
|
||
//已提现金额
|
||
$data['withdrawl'] =intval(CoinWithdrawLog::where('user_id', $user->id)->wherein('status', [0, 1])->wherein('is_hooked', [0, 1])->where('remark', '商家入驻')->sum('value'));
|
||
//可提现金额
|
||
$data['balance'] = $data['total_value']-$data['withdrawl']??0;
|
||
$data['poundage'] = 2; //提现手续费百分比
|
||
return $this->success('ok',$data);
|
||
}
|
||
// 推广记录
|
||
public function InviteList(Request $request)
|
||
{
|
||
$user_id = auth()->user()->id;
|
||
$sql = CoinLog::orderBy('id','DESC')->limit(1000);
|
||
$result = CoinLog::select('type_id')->from(DB::raw('('.$sql->toSql().') as a'))->where('user_id', $user_id)->where('type','INVITE')->groupBy('type_id')->paginate();
|
||
foreach ($result as $key => $value) {
|
||
$value->name = $value->anchor->name??'匿名用户';
|
||
$value->pic = $value->anchor->pic??User::DefaultAvatar;
|
||
$account = MerchantAccount::where('openid',$value->anchor->openid)->first();
|
||
if($account){
|
||
$qr_codes = Redis::get('PreviewQrcode-s'.$account->id);
|
||
if(!$qr_codes){
|
||
$jump_url = urlencode(env('APP_URL').'/pu/#/');
|
||
// $u = urlencode('anchor_openid='.$openid.'&url=http://love.ufutx.cn/pu/#/');
|
||
$url = env('APP_URL').'/api/official/live/wechat/FamilyAuth?merchant_id='.$account->id.'&anchor_openid='.$account->openid.'&url='.$jump_url;
|
||
$qr_codes = $this->getPreviewQrcode($url);
|
||
Redis::setex('PreviewQrcode-s'.$account->id,60*60*24*30,$qr_codes);
|
||
$qr_codes = Redis::get('PreviewQrcode-s'.$account->id);
|
||
}
|
||
$value->qr_code = $qr_codes;
|
||
$value->user_num = MerchantAccount::PromotionIncome($account->id);
|
||
}else{
|
||
$value->qr_code = null;
|
||
$value->user_num = 0;
|
||
}
|
||
$list = CoinLog::select('coin','remark','created_at')->where('user_id', $user_id)->where('type_id',$value->type_id)->groupBy('remark')->get();
|
||
$amount = $list->sum('coin');
|
||
$value->total_coin = $amount;
|
||
$value->list = $list;
|
||
unset($value->anchor);
|
||
|
||
}
|
||
return $this->success('ok',$result);
|
||
}
|
||
// 提现记录
|
||
public function withdrawLogV2(Request $request)
|
||
{
|
||
$user = auth()->user();
|
||
$result = CoinWithdrawLog::select('value','is_hooked','created_at','message')->where('user_id',$user->id)->where('remark','商家入驻')->paginate();
|
||
return $this->success('ok',$result);
|
||
}
|
||
|
||
|
||
/**
|
||
* 绑定支付宝账号
|
||
* @param Request $request [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function BindAlipayAccountV2(Request $request)
|
||
{
|
||
try {
|
||
$alipay_account = $request->input('alipay_account');
|
||
$alipay_real_name = $request->input('alipay_real_name');
|
||
if (empty($alipay_account) || empty($alipay_real_name)) return $this->failure('绑定账号失败');
|
||
$user = auth()->user();
|
||
$coin = $user->coinInfo();
|
||
$coin->alipay_account = $alipay_account;
|
||
$coin->alipay_real_name = $alipay_real_name;
|
||
$coin->save();
|
||
return $this->success('ok');
|
||
} catch (\Exception $e) {
|
||
$this->getError($e);
|
||
return $this->failure('绑定账号失败,请稍后再试');
|
||
}
|
||
}
|
||
|
||
// 提现
|
||
public function UserWithdrawal(Request $request)
|
||
{
|
||
$user = auth()->user();
|
||
$coin = $user->coinInfo();
|
||
$alipay_account = $coin->alipay_account;
|
||
$alipay_real_name = $coin->alipay_real_name;
|
||
$amount = $request->amount;
|
||
$data = [];
|
||
//总金额
|
||
$total_value = CoinLog::where('type', 'INVITE')->where('user_id', $user->id)->sum('coin');
|
||
//已提现金额
|
||
$withdrawl = CoinWithdrawLog::where('user_id', $user->id)->wherein('status', [0, 1])->wherein('is_hooked', [0, 1])->where('remark', '商家入驻')->sum('value');
|
||
//可提现金额
|
||
$balance = $total_value-$withdrawl??0;
|
||
if($balance<=0) return $this->failure('您暂无提现额度');
|
||
if($amount<1) return $this->failure('提现额度最低为1元,请重新输入');
|
||
if($balance<$amount) return $this->failure('提现额度不足,请重新输入');
|
||
$poundage = 2; //提现手续费百分比
|
||
$poundage = $amount * ($poundage /100) >= 0.01 ? $amount * ($poundage /100): 0.01;//收取手续费费用 不足1分 按一分处理
|
||
$actual_received = $amount - $poundage;//实际到账金额
|
||
$data['payee_account'] = $alipay_account;//支付宝账号
|
||
$data['payee_real_name'] = $alipay_real_name;//支付宝绑定姓名
|
||
$data['remark'] = '提现已到账'; //提现备注
|
||
$data['out_biz_no'] = \CommonUtilsService::getTradeNO();
|
||
$data['amount'] = $actual_received;
|
||
$ali = new LiveAlipayService();
|
||
$result = $ali->platTransferAccount($data);
|
||
if(is_array($result)){
|
||
return $this->failure($result['msg']);
|
||
}else{//提现成功
|
||
$CoinWithdrawLog = new CoinWithdrawLog();
|
||
$CoinWithdrawLog->user_id = $user->id;
|
||
$CoinWithdrawLog->value = $amount;
|
||
$CoinWithdrawLog->status = 1;
|
||
$CoinWithdrawLog->transfer_type = 'alipay';
|
||
$CoinWithdrawLog->trade_no = $data['out_biz_no'];
|
||
$CoinWithdrawLog->is_hooked = 1;
|
||
$CoinWithdrawLog->remark = '商家入驻';
|
||
$CoinWithdrawLog->save();
|
||
}
|
||
return $this->success('ok');
|
||
}
|
||
|
||
// 老师入驻
|
||
public function RecruitingTeachers(Request $request)
|
||
{
|
||
try {
|
||
$mobile = $request->mobile;
|
||
$exists = MerchantAccount::where('mobile',$request->mobile)->first();
|
||
if($exists) {
|
||
$password = decrypt($exists->password);
|
||
return $this->success('该号码已经使用,无法再次申请',['status'=>1,'mobile'=>$request->mobile,'password'=>$password]);
|
||
}
|
||
$code = $request->code;
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(empty( $wechatUser)){
|
||
$openId = $request->openid;
|
||
}else{
|
||
$openId = $wechatUser->getId();
|
||
}
|
||
// 是否关注公众号
|
||
$is_subscribe = \WechatService::officialIsSubscribed($openId);
|
||
//检查验证码
|
||
$sms = new Sms(new App);
|
||
$result = $sms->check($request->mobile, $code);
|
||
if ($result){
|
||
switch ($result) {
|
||
case '请填写验证码':
|
||
return $this->resp($result,['status'=>7,'message'=>'请填写验证码','is_subscribe'=>$is_subscribe]);
|
||
break;
|
||
case '验证码有误':
|
||
return $this->resp($result,['status'=>8,'message'=>'验证码有误','is_subscribe'=>$is_subscribe]);
|
||
break;
|
||
case '验证码过期':
|
||
return $this->resp($result,['status'=>9,'message'=>'验证码过期','is_subscribe'=>$is_subscribe]);
|
||
break;
|
||
case '验证码已使用':
|
||
return $this->resp($result,['status'=>10,'message'=>'验证码已使用','is_subscribe'=>$is_subscribe]);
|
||
break;
|
||
default:
|
||
# code...
|
||
break;
|
||
}
|
||
}
|
||
$anchor = Anchor::where('mobile',$mobile)->first();
|
||
$viewer = Viewer::where('mobile',$mobile)->first();
|
||
$Teacher = false;
|
||
if($viewer)
|
||
$Teacher = Anchor::where('viewer_id',$viewer->id)->first();
|
||
if(($anchor)||($Teacher&&$Teacher->cooperation_type)) return $this->success('fail',['status'=>1]);
|
||
if($Teacher){
|
||
$anchor = $Teacher;
|
||
}else{
|
||
$anchor = new anchor;
|
||
$anchor->viewer_id = $viewer->id??0;
|
||
$anchor->pic = $viewer->avatar??'';
|
||
$anchor->name = $request->name;
|
||
$anchor->status = 0;
|
||
}
|
||
$anchor->branch = $request->branch;
|
||
$anchor->service_nature = $request->service_nature??'person';
|
||
$anchor->wechat = $request->wechat;
|
||
$anchor->from_openid = $request->from_openid;
|
||
$anchor->withdrawal_radio = 100;
|
||
$anchor->mobile = $request->mobile;
|
||
$anchor->openid = $request->openid;
|
||
$anchor->teaching_user = json_encode($request->teaching_user);
|
||
$anchor->cooperation_type = json_encode($request->cooperation_type);
|
||
$anchor->save();
|
||
\CommonUtilsService::addNewAnchorToUser($anchor);
|
||
$url = \CommonUtilsService::shortUrl(env('APP_URL').'/pu_m/#/');
|
||
$str = $this->getTradeNO();
|
||
$password = substr($str,4,8);
|
||
$message = '你的账号已注册成功,请访问'.$url['url'].' 完善信息 用户名 :'.$mobile.' 密码 :'.$password.'【福恋】';
|
||
Message::create([
|
||
'phone' => $mobile,
|
||
'message' => $message,
|
||
'confirmed' => 1,
|
||
'ip' => request() ? request()->ip() : '127.0.0.1',
|
||
]);
|
||
$bool = MerchantAccount::where('openid',$anchor->openid)->first();
|
||
if($bool){
|
||
$arr = ['password'=>encrypt($password)];
|
||
}else{
|
||
$arr = ['password'=>encrypt($password),'openid'=>$anchor->openid];
|
||
}
|
||
MerchantAccount::updateOrCreate(['mobile'=>$mobile],$arr);
|
||
Messengers::sendSMS($mobile, $message);
|
||
$type = ['service','activity','consult','course','shop'];
|
||
$MerchantAccount = MerchantAccount::where('mobile',$mobile)->first();
|
||
$anchor->m_id = $MerchantAccount->id;
|
||
$anchor->save();
|
||
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();
|
||
}
|
||
// 发送模板通知
|
||
if($request->openid){
|
||
// 是否关注公众号
|
||
$is_subscribe = \WechatService::officialIsSubscribed($openId);
|
||
if($is_subscribe==1){
|
||
$data['touser'] = $request->openid;
|
||
$data['template_id'] = 'KCSYnW4IKslhTOmKQ8cFzWcvRVvrDSV-I_wfEm_ZW-U';
|
||
$data['url'] = env('APP_URL').'/pu_m/#/';
|
||
$data['data'] = [
|
||
'first' => $request->name.'您好,你已入驻成功!',
|
||
'keyword1' =>$request->name,
|
||
'keyword2' => now(),
|
||
'reamrk' => '点击登录完善您的信息',
|
||
];
|
||
SendTemplateMsg::dispatch($data)->onQueue('template_message');
|
||
}
|
||
}
|
||
if($request->from_openid&&$request->from_openid!=$request->openid){
|
||
$Viewer = Viewer::where('openid',$request->from_openid)->first();
|
||
if($Viewer&&$Viewer->user_id) {
|
||
$CoinLog = new CoinLog();
|
||
$CoinLog->user_id = $Viewer->user_id;
|
||
$CoinLog->coin = 1;
|
||
$CoinLog->is_hooked = 1;
|
||
$CoinLog->type_id = $anchor->id;
|
||
$CoinLog->type = 'INVITE';
|
||
$CoinLog->remark = '商家入驻';
|
||
$CoinLog->save();
|
||
$message = $Viewer->nickname.'您好,服务商-'.$request->name.'已通过您的邀请入驻成功!';
|
||
// 是否关注公众号
|
||
$is_subscribe = \WechatService::officialIsSubscribed($request->from_openid);
|
||
if($is_subscribe==1){
|
||
$data['touser'] = $request->from_openid;
|
||
$data['template_id'] = 'KCSYnW4IKslhTOmKQ8cFzWcvRVvrDSV-I_wfEm_ZW-U';
|
||
$data['url'] = env('APP_URL').'/h5/#/my';
|
||
$data['data'] = [
|
||
'first' => $message,
|
||
'keyword1' =>$request->name,
|
||
'keyword2' => now(),
|
||
'reamrk' => '',
|
||
];
|
||
SendTemplateMsg::dispatch($data)->onQueue('template_message');
|
||
//增加邀请入驻记录
|
||
$s_merchant = 0;
|
||
$s_anchor = Anchor::where('openid',$request->from_openid)->first();
|
||
if($s_anchor) $s_merchant = MerchantAccount::where('mobile',$s_anchor->mobile)->first();
|
||
if($s_merchant){
|
||
$content = '服务商'.$request->name.'已通过您的邀请入驻成功';
|
||
SaasNotice::addRecord($s_merchant->id,0,'m_invite',$MerchantAccount->id,$content,1);
|
||
}
|
||
}else{
|
||
// 短信推送
|
||
if($Viewer&&$Viewer->mobile){
|
||
$this->sentMessage($Viewer->mobile,$message);
|
||
}
|
||
//增加邀请入驻记录
|
||
$s_merchant = 0;
|
||
$s_anchor = Anchor::where('openid',$request->from_openid)->first();
|
||
if($s_anchor) $s_merchant = MerchantAccount::where('mobile',$s_anchor->mobile)->first();
|
||
if($s_merchant){
|
||
$content = '服务商'.$request->name.'已通过您的邀请入驻成功';
|
||
SaasNotice::addRecord($s_merchant->id,0,'m_invite',$MerchantAccount->id,$content,1);
|
||
}
|
||
}
|
||
|
||
}
|
||
//增加入驻记录通知
|
||
$content = '恭喜您,成功入驻福恋智能商家版,欢迎使用!';
|
||
SaasNotice::addRecord($MerchantAccount->id,0,'recruiting',$MerchantAccount->id,$content,1);
|
||
$data = ['merchant_id'=>$MerchantAccount->id,'anchor_id'=>$anchor->id];
|
||
NewMerchantDefaultService::dispatch($data)->onQueue('love');
|
||
}
|
||
|
||
return $this->success('ok',['status'=>0,'is_subscribe'=>$is_subscribe,'mobile'=>$request->mobile,'password'=>$password]);
|
||
} catch (\Exception $e) {
|
||
$this->getError($e);
|
||
return $this->failure('入驻失败,请稍后再试');
|
||
}
|
||
}
|
||
//咨询列表
|
||
public function consultingService(Request $request)
|
||
{
|
||
$teaching_user = $request->teaching_user;
|
||
$keyword = $request->keyword;
|
||
$openid = $request->openid;
|
||
$anchor = Anchor::select('id','introduction','name','designation','pic','teaching_user','price','label')->where('status',2)->where('is_show_consulation',1);
|
||
if($keyword){
|
||
$anchor = $anchor->where('name','like', '%'.$keyword.'%');
|
||
}
|
||
if($request->anchor_id){
|
||
$anchor = $anchor->where('id','<>', $request->anchor_id)
|
||
->orderBy(DB::raw('RAND()'))
|
||
->take(8)
|
||
->get();
|
||
$viewer_id = Viewer::where('openid',$openid)->value('id');
|
||
}else{
|
||
$anchor = $anchor->orderBy('sort','desc')->get();
|
||
}
|
||
$data = [];
|
||
foreach ($anchor as $key => $value) {
|
||
$value->cooperation_type = json_decode($value->cooperation_type,true);
|
||
if($request->anchor_id){
|
||
//咨询人数
|
||
$value->pay_num = ConsultationRecords::where('anchor_id', $value->id)->where('status',2)->groupBY('phone')->get()->count();
|
||
//是否有未完成的预约
|
||
$value->state = 0;
|
||
$value->order_id = '';
|
||
if($viewer_id){
|
||
$result = ConsultationRecords::where('viewer_id', $viewer_id)->where('anchor_id',$value->id)->where('pay_status',1)->orderBy('id','desc')->first();
|
||
if($result){
|
||
if($result->status==0){
|
||
$value->state = 1;
|
||
}elseif($result->status==2&&!$result->Praise){
|
||
$value->state = 2;//待评分
|
||
}
|
||
}else{
|
||
$value->state = 0;
|
||
}
|
||
|
||
$value->order_id = $result->id??'';
|
||
}
|
||
}
|
||
$result = ConsultationRecords::where('anchor_id',$value->id)->where('status',2)->get();
|
||
$array = json_decode( $result,true);
|
||
if(!empty($array)){
|
||
$Praise = $result->sum('Praise');
|
||
$Praise_num = $result->count('Praise');
|
||
if($Praise&& $Praise_num){
|
||
$value->Praise_num = number_format($Praise/$Praise_num,1);
|
||
}else{
|
||
$value->Praise_num = number_format (5.0,1);
|
||
}
|
||
}else{
|
||
$value->Praise_num = number_format (5.0,1);
|
||
}
|
||
$value->Praise_num = intval($value->Praise_num/5*100);
|
||
$value->price = floatval($value->price);
|
||
$value->label = json_decode( $value->label,true);
|
||
if($teaching_user){
|
||
if(in_array($teaching_user,$value->teaching_user))
|
||
array_push($data,$value);
|
||
}else{
|
||
array_push($data,$value);
|
||
}
|
||
}
|
||
if(!$request->anchor_id){
|
||
$page = $request->page ?? 1;
|
||
$perPage = 15;
|
||
$offset = ($page * $perPage) - $perPage;
|
||
$result = new LengthAwarePaginator(
|
||
array_slice($data, $offset, $perPage),
|
||
count($data),
|
||
$perPage,
|
||
$page,
|
||
['path' => $request->url(), 'query' => $request->query()]
|
||
);
|
||
}else{
|
||
$result = $anchor;
|
||
}
|
||
|
||
|
||
return $this->success('ok',$result);
|
||
}
|
||
|
||
|
||
//咨询列表
|
||
public function BusinessconsultingService(Request $request)
|
||
{
|
||
$merchant_id =$request->merchant_id;
|
||
$merchant_user_id = $request->merchant_user_id;
|
||
$ids = ConsultAccount::where('merchant_id',$merchant_id)->pluck('id');
|
||
$result = Consultation::wherein('consult_account_id',$ids)->where('status',1)->paginate();
|
||
foreach ($result as $key => $value) {
|
||
//咨询人数
|
||
$value->pay_num = ConsultationRecords::where('consulation_id', $value->id)->where('status',2)->groupBY('phone')->get()->count();
|
||
//是否有未完成的预约
|
||
$value->state = 0;
|
||
$value->order_id = '';
|
||
$data = ConsultationRecords::where('merchant_user_id', $merchant_user_id)->where('consulation_id',$value->id)->where('pay_status',1)->orderBy('id','desc')->first();
|
||
if($data){
|
||
if($data->status==0){
|
||
$value->state = 1;
|
||
}elseif($data->status==2&&!$data->Praise){
|
||
$value->state = 2;//待评分
|
||
}
|
||
}else{
|
||
$value->state = 0;
|
||
}
|
||
$teacher = ConsultAccount::where('id',$value->consult_account_id)->first();
|
||
$value->introduction = $teacher->introduction;
|
||
$value->name = $teacher->name;
|
||
$data = ConsultationRecords::where('consulation_id',$value->id)->where('status',2)->get();
|
||
$array = json_decode( $data,true);
|
||
if(!empty($array)){
|
||
$Praise = $data->sum('Praise');
|
||
$Praise_num = $data->count('Praise');
|
||
if($Praise&& $Praise_num){
|
||
$value->Praise_num = number_format($Praise/$Praise_num,1);
|
||
}else{
|
||
$value->Praise_num = number_format (5.0,1);
|
||
}
|
||
}else{
|
||
$value->Praise_num = number_format (5.0,1);
|
||
}
|
||
$value->Praise_num = intval($value->Praise_num/5*100);
|
||
$value->label = json_decode($teacher->label,true);
|
||
// $value->pic = $teacher->pic;
|
||
$value->price = floatval($value->price);
|
||
$value->experience = $teacher->experience;
|
||
$value->seniority = $teacher->seniority;
|
||
$value->designation = $teacher->designation;
|
||
|
||
}
|
||
|
||
return $this->success('ok',$result);
|
||
}
|
||
|
||
public function BusinessConsultationDetails(Request $request)
|
||
{
|
||
$consultation_id = $request->consultation_id;
|
||
$order_id = $request->order_id;
|
||
$result = Consultation::where('id',$consultation_id)->first();
|
||
if(!$result) return $this->failure('咨询信息有误');
|
||
$teacher = ConsultAccount::where('id',$result->consult_account_id)->first();
|
||
$result->intro = $teacher->introduction;
|
||
$result->name = $teacher->name;
|
||
$result->mobile = $teacher->mobile;
|
||
$data = ConsultationRecords::where('consulation_id',$consultation_id)->where('status',2)->get();
|
||
$Praise = $data->sum('Praise');
|
||
$Praise_num = $data->count('Praise');
|
||
if($Praise&&$Praise_num){
|
||
$data->Praise_num = number_format($Praise/$Praise_num,1);
|
||
}else{
|
||
$data->Praise_num = number_format (5.0,1);
|
||
}
|
||
$result->Praise_num = intval($data->Praise_num/5*100);
|
||
$result->label = json_decode($teacher->label,true);
|
||
$result->t_pic = $teacher->pic;
|
||
$result->price = floatval($result->price);
|
||
$result->experience = $teacher->experience;
|
||
$result->seniority = $teacher->seniority;
|
||
$result->designation = $teacher->designation;
|
||
if($teacher->credentials && $teacher->credentials != 'null'){
|
||
$result->credentials = json_decode($teacher->credentials,true);
|
||
}else{
|
||
$result->credentials = [];
|
||
}
|
||
$state = 0;
|
||
$record = ConsultationRecords::where('consulation_id',$consultation_id)->where('merchant_user_id',$request->merchant_user_id)->where('pay_status',1)->orderBy('status','asc')->first();
|
||
if(!$record){
|
||
$state = 0;
|
||
}elseif($record &&$record->status == 0){
|
||
$state = 1;
|
||
}elseif($record->status ==2 && !$record->Praise){
|
||
$state = 2;
|
||
}else{
|
||
$state = 3;
|
||
}
|
||
$result->state = $state;
|
||
$result->pay_num = 0;
|
||
$result->Praise_num = 100;
|
||
$result->mobile = $teacher->mobile;
|
||
if($order_id){
|
||
$result->order_id =$order_id;
|
||
}else{
|
||
$result->order_id =$record->id??0;
|
||
}
|
||
Redis::zincrby('consulation', 1 ,$consultation_id);
|
||
return $this->success('ok',$result);
|
||
}
|
||
// 咨询详情
|
||
public function ConsultationDetails(Request $request)
|
||
{
|
||
$openid = $request->openid;
|
||
$id = $request->anchor_id;
|
||
$anchor = Anchor::select('id','introduction','name','designation','pic','mobile','price','Years','label','resume','duration','experience','service_content','openid')->where('id', $id)->where('status',2)->first();
|
||
if(!$anchor) return $this->failure('该老师已经停止服务,请选择其他的老师吧~');
|
||
//咨询人数
|
||
$anchor->pay_num = ConsultationRecords::where('anchor_id', $id)->where('status',2)->groupBY('phone')->get()->count();
|
||
$result = ConsultationRecords::where('anchor_id',$id)->where('status',2)->get();
|
||
$Praise = $result->sum('Praise');
|
||
$Praise_num = $result->count('Praise');
|
||
if($Praise&&$Praise_num){
|
||
$anchor->Praise_num = number_format($Praise/$Praise_num,1);
|
||
}else{
|
||
$anchor->Praise_num = number_format (5.0,1);
|
||
}
|
||
$is_teacher = 0;//是否是老师本人
|
||
if($openid == $anchor->openid) $is_teacher = 1;
|
||
|
||
$anchor->Praise_num = intval($anchor->Praise_num/5*100);
|
||
$anchor->label = json_decode($anchor->label,true);
|
||
$anchor->price = floatval($anchor->price);
|
||
$anchor->is_teacher = $is_teacher;
|
||
$viewer_id = Viewer::where('openid',$openid)->value('id');
|
||
//是否有未完成的预约
|
||
$anchor->state = 0;
|
||
$anchor->order_id = '';
|
||
if($viewer_id){
|
||
$result = ConsultationRecords::where('viewer_id', $viewer_id)->where('anchor_id',$id)->where('pay_status',1)->orderBy('id','desc')->first();
|
||
if($result){
|
||
if($result->status==0){
|
||
$anchor->state = 1;
|
||
}elseif($result->status==2&&!$result->Praise){
|
||
$anchor->state = 2;//待评分
|
||
}
|
||
}else{
|
||
$anchor->state = 0;
|
||
}
|
||
|
||
$anchor->order_id = $result->id??'';
|
||
}
|
||
return $this->success('ok',$anchor);
|
||
}
|
||
//更改咨询订单状态
|
||
public function changeOrderStatus(Request $request)
|
||
{
|
||
$id = $request->id;
|
||
$result = ConsultationRecords::where('id',$id)->where('pay_status',1)->first();
|
||
if(!$result) return $this->failure('未查到该订单信息~');
|
||
if($request->status)
|
||
$result->status = $request->status;
|
||
if($request->record)
|
||
$result->record = $request->record;
|
||
$result->save();
|
||
return $this->success('操作成功');
|
||
|
||
}
|
||
|
||
//咨询-用户评价列表
|
||
|
||
public function ConsultationComment(Request $request)
|
||
{
|
||
$result = ConsultationRecords::select('id','anchor_id','name','price','type','comment','Praise','created_at','viewer_id')
|
||
->where('anchor_id',$request->anchor_id)
|
||
->where('status',2)
|
||
->whereNotNull('Praise')
|
||
->orderBy('id','desc')
|
||
->paginate();
|
||
foreach ($result as $key => $value) {
|
||
# code...
|
||
$time = date('Y-m-d H:i',strtotime($value->created_at));
|
||
$value->time = $time;
|
||
$value->avatar = $value->viewer->avatar??'https://images.ufutx.com/202108/06/e1b03db630b24fd818d1aa9967ce3fb1.png';
|
||
$value->duration = $value->anchor->duration??60;
|
||
unset($value->viewer);
|
||
unset($value->anchor);
|
||
}
|
||
return $this->success('ok',$result);
|
||
}
|
||
|
||
//商户咨询-用户评价列表
|
||
|
||
public function BusinessConsultationComment(Request $request)
|
||
{
|
||
$consulation_id = $request->consultation_id;
|
||
$merchant_user_id = $request->merchant_user_id;
|
||
$result = ConsultationRecords::select('id','anchor_id','name','price','type','comment','Praise','created_at','consulation_id','merchant_id','merchant_user_id')
|
||
->where('consulation_id',$consulation_id)
|
||
->where('status',2)
|
||
->whereNotNull('Praise')
|
||
->orderBy('id','desc')
|
||
->paginate();
|
||
foreach ($result as $key => $value) {
|
||
# code...
|
||
$time = date('Y-m-d H:i',strtotime($value->created_at));
|
||
$value->time = $time;
|
||
$teacher = Consultation::where('id',$consulation_id)->first();
|
||
$pic = MerchantUsers::where('id',$value->merchant_user_id)->value('pic')??User::DefaultAvatar;
|
||
$value->avatar = $pic;
|
||
$value->duration = $teacher->duration??60;
|
||
$value->is_mine = $value->merchant_user_id == $merchant_user_id ? 1 : 0;
|
||
unset($value->viewer);
|
||
unset($value->anchor);
|
||
}
|
||
return $this->success('ok',$result);
|
||
}
|
||
|
||
//商户咨询-删除用户评价列表
|
||
public function deleteConsultationComment(Request $request,$id){
|
||
|
||
$record = ConsultationRecords::where('id',$id)->first();
|
||
if(!$record) return $this->failure('该评论不存在');
|
||
$record->update(['Praise'=>Null,'comment'=>Null]);
|
||
return $this->success('ok');
|
||
}
|
||
|
||
//咨询-支付订单
|
||
public function PayConsultation(Request $request)
|
||
{
|
||
$anchor_id = $request->anchor_id;
|
||
$num = $request->num??1;
|
||
$trade_no = \CommonUtilsService::getTradeNO();
|
||
$openid = $request->openid;
|
||
$from_open_id = $request->from_open_id??0;
|
||
|
||
$viewer = viewer::where('openid',$openid)->first();
|
||
if(!$viewer)
|
||
return response()->json(['code'=>2, 'message' => '账户信息有误,请重新登录.'], 200);
|
||
if($viewer->openid!= $openid)
|
||
return response()->json(['code'=>2, 'message' => '账户信息有误,请重新登录.'], 200);
|
||
|
||
$Anchor = Anchor::where('id',$anchor_id)->first();
|
||
if(!$Anchor||$Anchor->price<0)
|
||
return $this->failure('咨询信息有误。');
|
||
|
||
$Consultation = new ConsultationRecords();
|
||
|
||
$Consultation->anchor_id = $anchor_id;
|
||
$Consultation->name = $request->name;
|
||
$Consultation->phone = $request->phone;
|
||
$Consultation->price = $Anchor->price??0;
|
||
$Consultation->num = $num;
|
||
$Consultation->merchant_id =$request->merchant_id??0;
|
||
$Consultation->consulation_id =$request->consulation_id??0;
|
||
$Consultation->type = $request->type??'voice';
|
||
$Consultation->class = $request->class;
|
||
$Consultation->expect = $request->expect;
|
||
$Consultation->desc = $request->desc;
|
||
$Consultation->pay_status = 0;
|
||
$Consultation->duration = $Anchor->duration;
|
||
$Consultation->Remaining_duration = $Anchor->duration*60;
|
||
$Consultation->viewer_id = $viewer->id;
|
||
$Consultation->trade_no = $trade_no;
|
||
$Consultation->from_open_id = $from_open_id;
|
||
$Consultation->save();
|
||
$config = Consultation::where('id', $request->consulation_id)->first();
|
||
if($Anchor->price != 0){
|
||
$callback = config('app.url').'/api/app/callback/consultation/'.$trade_no;
|
||
|
||
$attributes = array(
|
||
'id'=> $Consultation->id,
|
||
'trade_type' => 'JSAPI', // JSAPI,NATIVE,APP...
|
||
'body' => $config->title,
|
||
'detail' => '咨询服务',
|
||
'out_trade_no' => $trade_no,
|
||
'total_fee' => round($Anchor->price*100),
|
||
'notify_url' => $callback, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
|
||
'openid' => $openid,
|
||
);
|
||
$result = \WechatService::officialPay($attributes);
|
||
return $this->success('ok',$result);
|
||
}else{
|
||
$Consultation->pay_status = 1;
|
||
$Consultation->save();
|
||
$data['status'] = 1;
|
||
$data['id'] = $Consultation->id;
|
||
return $this->success('ok',$data);
|
||
}
|
||
}
|
||
|
||
|
||
//商户咨询-支付订单
|
||
public function BusinessPayConsultation(Request $request)
|
||
{
|
||
$consultation_id = $request->consultation_id;
|
||
$num = $request->num??1;
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
$openId = null;
|
||
if($wechatUser)
|
||
$openId = $wechatUser->getId();
|
||
$trade_no = $this->getTradeNO();
|
||
$from_open_id = $request->from_open_id??0;
|
||
$user = MerchantUsers::where('id',$request->merchant_user_id)->first();
|
||
if(!$user) return $this->fail('信息有误',2);
|
||
$config = Consultation::where('id', $consultation_id)->first();
|
||
$Consultation = new ConsultationRecords();
|
||
$Consultation->anchor_id = 0;
|
||
$Consultation->merchant_id =$request->merchant_id;
|
||
$Consultation->merchant_user_id = $request->merchant_user_id;
|
||
$Consultation->consulation_id = $consultation_id;
|
||
$Consultation->name = $request->name;
|
||
$Consultation->phone = $request->phone;
|
||
$Consultation->price = $config->price??0;
|
||
$Consultation->num = $num;
|
||
$Consultation->type = $request->type??'voice';
|
||
$Consultation->class = $request->class;
|
||
$Consultation->expect = $request->expect;
|
||
$Consultation->desc = $request->desc;
|
||
$Consultation->pay_status = 0;
|
||
$Consultation->duration = $config->duration;
|
||
$Consultation->Remaining_duration = $config->duration*60;
|
||
$Consultation->viewer_id = 0;
|
||
$Consultation->trade_no = $trade_no;
|
||
$Consultation->from_open_id = $from_open_id;
|
||
$Consultation->save();
|
||
|
||
$TouristOrder = new TouristOrder();
|
||
$TouristOrder->open_id = $openId;
|
||
$TouristOrder->price = $config->price;
|
||
$TouristOrder->type_id = $request->consultation_id;
|
||
$TouristOrder->pay_type = 'wechat';
|
||
$TouristOrder->type = 'consult';
|
||
$TouristOrder->withdrawal_radio = 100;
|
||
$TouristOrder->pay_status = 0;
|
||
$TouristOrder->trade_no = $trade_no;
|
||
$TouristOrder->from_openid = $from_open_id;
|
||
$TouristOrder->share_channel_id = $request->share_channel_id;
|
||
$TouristOrder->name = $request->name;
|
||
$TouristOrder->desc = $config->title;
|
||
$TouristOrder->mobile = $request->phone;
|
||
$TouristOrder->merchant_id = $request->merchant_id;
|
||
$TouristOrder->account_id = $request->merchant_user_id;
|
||
$TouristOrder->save();
|
||
if($config->price != 0){
|
||
$callback = config('app.url').'/api/app/callback/consultation/'.$trade_no;
|
||
|
||
$attributes = array(
|
||
'id'=> $Consultation->id,
|
||
'trade_type' => 'JSAPI', // JSAPI,NATIVE,APP...
|
||
'body' => $config->title,
|
||
'detail' => '咨询服务',
|
||
'out_trade_no' => $trade_no,
|
||
'total_fee' => round($config->price*100),
|
||
'notify_url' => $callback, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
|
||
'openid' => $openId??$user->openid,
|
||
);
|
||
$result = \WechatService::officialPay($attributes);
|
||
return $this->success('ok',$result);
|
||
}else{
|
||
$Consultation->pay_status = 1;
|
||
$TouristOrder->pay_status = 1;
|
||
$Consultation->save();
|
||
$TouristOrder->save();
|
||
$data['status'] = 1;
|
||
$data['id'] = $Consultation->id;
|
||
return $this->success('ok',$data);
|
||
}
|
||
}
|
||
|
||
// 咨询-用户提交评价
|
||
public function SubmitComment(Request $request)
|
||
{
|
||
//咨询订单id
|
||
$id = $request->id;
|
||
$comment = $request->comment;
|
||
$Praise = $request->Praise;
|
||
$pic = $request->pic;
|
||
$pay_log = new PayLog();
|
||
$pay_log->type = 'SubmitComment';
|
||
$pay_log->remark = $id ;
|
||
$pay_log->save();
|
||
$result = ConsultationRecords::where('id', $id)->where('pay_status',1)->where('status','<>',0)->first();
|
||
if(!$result)
|
||
return $this->failure('未查询到该订单。');
|
||
if($result->comment)
|
||
return $this->failure('您已经提交过评价啦');
|
||
$record = ConsultationRecords::where('id',$id)->first();
|
||
$con = Consultation::where('id',$record->consulation_id)->first();
|
||
$value = TouristOrder::where('type','consult')->where('type_id',$con->id)->first();
|
||
$result->comment = $comment;
|
||
$result->Praise = $Praise;
|
||
$result->save();
|
||
$value->comment = $comment;
|
||
$value->Praise = $Praise;
|
||
$value->pic = json_encode($pic);
|
||
$value->save();
|
||
return $this->success('ok');
|
||
}
|
||
|
||
//课程 咨询 服务订单 --提交评价
|
||
public function orderComments(Request $request){
|
||
$id = $request->id;
|
||
$trade_no = $request->trade_no;
|
||
$comment = $request->comment;
|
||
$Praise = $request->Praise;
|
||
$pic = $request->pic;
|
||
$value = TouristOrder::where('id',$id)->whereIn('pay_status',[1,4])->first();
|
||
if($trade_no)
|
||
$value = TouristOrder::where('trade_no',$trade_no)->whereIn('pay_status',[1,4])->first();
|
||
if(!$value)
|
||
return $this->failure('未查询到该订单。');
|
||
if($value->comment || $value->Praise)
|
||
return $this->failure('您已经提交过评价啦');
|
||
$value->comment = $comment;
|
||
$value->Praise = $Praise;
|
||
$value->pic = json_encode($pic);
|
||
$record = ConsultationRecords::where('trade_no',$value->trade_no)->first();
|
||
if($record){
|
||
$record->comment = $comment;
|
||
$record->Praise = $Praise;
|
||
$record->save();
|
||
}
|
||
$value->save();
|
||
return $this->success('ok');
|
||
}
|
||
|
||
//课程 咨询 服务订单 --修改提交评价
|
||
public function changeorderComments(Request $request){
|
||
$id = $request->id;
|
||
$comment = $request->comment;
|
||
$Praise = $request->Praise;
|
||
$pic = $request->pic;
|
||
$value = TouristOrder::where('id',$id)->whereIn('pay_status',[1,4])->first();
|
||
|
||
if($request->comment) $value->comment = $comment;
|
||
if($request->Praise) $value->Praise = $Praise;
|
||
if($request->pic) $value->comment = json_encode($pic);
|
||
|
||
$record = ConsultationRecords::where('trade_no',$value->trade_no)->first();
|
||
if($record){
|
||
$record->comment = $comment;
|
||
$record->Praise = $Praise;
|
||
$record->save();
|
||
}
|
||
$value->save();
|
||
return $this->success('ok');
|
||
}
|
||
|
||
|
||
//商户 查看订单评价
|
||
public function orderComment(Request $request){
|
||
$id = $request->id;
|
||
$order = TouristOrder::where('id',$id)->whereIn('pay_status',[1,4])->first();
|
||
if($request->trade_no) $order = TouristOrder::where('trade_no',$request->trade_no)->whereIn('pay_status',[1,4])->first();
|
||
if(!$order) return $this->failure('未查询到该订单。');
|
||
$order->pic = json_decode($order->pic,true) ??[];
|
||
$order->Praise = floatval($order->Praise);
|
||
$order->price = floatval($order->price);
|
||
$order->avatar = MerchantUsers::where('openid',$request->openid)->value('pic')??User::DefaultAvatar;
|
||
if($order->type == 'consult'){
|
||
$record = ConsultationRecords::where('trade_no',$order->trade_no)->first();
|
||
$order->phone = $record->phone;
|
||
$order->duration = $record->duration;
|
||
$order->expect = $record->expect;
|
||
$order->duration = $record->duration;
|
||
$order->desc = $record->desc;
|
||
}
|
||
return $this->success('ok',$order);
|
||
}
|
||
// 我的咨询列表
|
||
public function MyConsultation(Request $request)
|
||
{
|
||
$status = $request->status;
|
||
$openid = $request->input('openid');
|
||
$viewer_id = viewer::where('openid',$openid)->value('id');
|
||
if(!$viewer_id)
|
||
return response()->json(['code'=>2, 'message' => '账户信息有误,请重新登录.'], 200);
|
||
$anchor_id = Anchor::where('viewer_id',$viewer_id)->value('id');
|
||
if($request->type=='user'){
|
||
$result = ConsultationRecords::where('viewer_id',$viewer_id)->where('pay_status',1);
|
||
}else{
|
||
$result = ConsultationRecords::where('anchor_id', $anchor_id)->where('pay_status',1);
|
||
}
|
||
if($status) $result = $result->where('status',$status);
|
||
$result = $result->orderBy('id','desc')->paginate();
|
||
foreach ($result as $key => $value) {
|
||
$value->avatar = Anchor::where('id',$value->anchor_id)->value('pic')??'https://images.ufutx.com/202108/06/e1b03db630b24fd818d1aa9967ce3fb1.png';
|
||
$value->name = Anchor::where('id',$value->anchor_id)->value('name');
|
||
// $value->avatar = $value->viewer->avatar??'https://images.ufutx.com/202108/06/e1b03db630b24fd818d1aa9967ce3fb1.png';
|
||
$value->designation = $value->anchor->designation??null;
|
||
if(isset($value->anchor->label)){
|
||
$value->label = json_decode($value->anchor->label,true);
|
||
}else{
|
||
$value->label = null;
|
||
}
|
||
$value->price = floatval($value->price);
|
||
// 订单状态
|
||
$value->state = 0;
|
||
if($value->status==0){
|
||
$value->state = 1;
|
||
}elseif($value->status==2&&!$value->Praise){
|
||
$value->state = 2;//待评分
|
||
}else{
|
||
$value->state = 3; //已完成
|
||
}
|
||
unset($value->anchor);
|
||
unset($value->viewer);
|
||
|
||
}
|
||
return $this->success('ok',$result);
|
||
}
|
||
// 商户-我的咨询
|
||
public function BusinessMyConsultation(Request $request)
|
||
{
|
||
$status = $request->status;
|
||
$merchant_id =$request->merchant_id;
|
||
$merchant_user_id = $request->merchant_user_id;
|
||
if($request->type=='user'){
|
||
|
||
$result = ConsultationRecords::where('merchant_id',$merchant_id)->where('merchant_user_id', $merchant_user_id)->where('pay_status',1);
|
||
}else{
|
||
$result = ConsultationRecords::where('merchant_id',$merchant_id)->where('pay_status',1);
|
||
|
||
}
|
||
if($status) $result = $result->where('status',$status);
|
||
$result = $result->orderBy('id','desc')->paginate();
|
||
foreach ($result as $key => $value) {
|
||
$config = Consultation::withTrashed()->where('id',$value->consulation_id)->first();
|
||
$teacher = ConsultAccount::where('id',$config->consult_account_id)->first();
|
||
$value->avatar = $teacher->pic;
|
||
$value->mobile = $teacher->mobile;
|
||
$value->duration = $config->duration;
|
||
$value->name = $teacher->name;
|
||
$value->designation = $teacher->designation;
|
||
if(isset($teacher->label)){
|
||
$value->label = json_decode($teacher->label,true);
|
||
}else{
|
||
$value->label = null;
|
||
}
|
||
$value->price = floatval($value->price);
|
||
// 订单状态
|
||
$value->state = 0;
|
||
if($value->status==0){
|
||
$value->state = 1;
|
||
}elseif($value->status==2&&!$value->Praise){
|
||
$value->state = 2;//待评分
|
||
}else{
|
||
$value->state = 3; //已完成
|
||
}
|
||
unset($value->anchor);
|
||
unset($value->viewer);
|
||
|
||
}
|
||
return $this->success('ok',$result);
|
||
}
|
||
|
||
|
||
// 获取虚拟号
|
||
|
||
public function virtual_number(Request $request)
|
||
{
|
||
$id = $request->id;
|
||
$result = ConsultationRecords::where('id',$id)->first();
|
||
$pay_log = new PayLog();
|
||
$pay_log->type = 'get_number';
|
||
$pay_log->trade_no = $id ;
|
||
if(!$result){
|
||
$pay_log->remark = '订单不存在。' ;
|
||
$pay_log->save();
|
||
return $this->failure('订单不存在。');
|
||
}
|
||
if( $result->pay_status!=1)
|
||
return $this->failure('您尚未支付。');
|
||
if($result->virtual_number)
|
||
return $this->success('ok', $result->virtual_number);
|
||
|
||
$tel_a = $result->phone;
|
||
$tel_b = Anchor::where('id',$result->anchor_id)->value('mobile');
|
||
$pay_log->remark = '手机a:'. $tel_a.'手机b'.$tel_b ;
|
||
if(!$tel_a||!$tel_b){
|
||
$pay_log->save();
|
||
return $this->failure('手机号获取失败。');
|
||
|
||
}
|
||
$tel_xs = ['13078094376','13078094415','13078094638'];
|
||
$rand_key = array_rand($tel_xs);
|
||
$tel_x = $tel_xs[$rand_key];
|
||
$data = [
|
||
// 以下代码以ax为例,选填参数以实际需求,对应填写
|
||
"corp_key" => "7574596477198833",//企业账户key 必填
|
||
"request_id" => $result->trade_no,// 企业每个请求Id唯一,如果是同一个请求重复提交,则Id保持相同 必填
|
||
"tel_a" => $tel_a,// 呼叫方
|
||
"tel_b" => $tel_b,//接受方
|
||
// "area_code" => "010",//虚拟号地区
|
||
"tel_x" => $tel_x,//接受方
|
||
"expiration" => "300",// 有效持续时间 必填
|
||
];
|
||
$data['ts'] = time();
|
||
$data['sign'] = $this->sign($data, '8uM2Mxe7EaCU8XXW4FHjaq6m8N3794Xv');//密钥信息
|
||
// print_r(json_encode($data));
|
||
$url = 'http://api.1ketong.com:81/ykt-pool/number/axb/binding';//请求地址
|
||
$curl = curl_init($url);
|
||
|
||
|
||
curl_setopt_array($curl, array(
|
||
CURLOPT_RETURNTRANSFER => true,
|
||
CURLOPT_ENCODING => "",
|
||
CURLOPT_MAXREDIRS => 10,
|
||
CURLOPT_TIMEOUT => 30,
|
||
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
|
||
CURLOPT_CUSTOMREQUEST => "POST",
|
||
CURLOPT_POSTFIELDS => json_encode($data),
|
||
CURLOPT_HTTPHEADER => array(
|
||
"Accept: */*",
|
||
"Accept-Encoding: gzip, deflate",
|
||
"Cache-Control: no-cache",
|
||
"Connection: keep-alive",
|
||
"Content-Type: application/json;charset=utf-8",
|
||
"cache-control: no-cache"
|
||
),
|
||
));
|
||
|
||
$response = curl_exec($curl);
|
||
$err = curl_error($curl);
|
||
curl_close($curl);
|
||
|
||
if ($err) {
|
||
return $this->failure('手机号获取失败。'.$err);
|
||
} else {
|
||
$response = json_decode($response,true);
|
||
// return $response;
|
||
$result->virtual_number = $response['data']['tel_x'];
|
||
$result->bind_id = $response['data']['bind_id'];
|
||
$result->save();
|
||
$pay_log->remark = $pay_log->remark.'tel_x'.$result->virtual_number;
|
||
$pay_log->save();
|
||
// return $this->success('ok',$response);
|
||
return $this->success('ok',$response['data']['tel_x']);
|
||
}
|
||
}
|
||
|
||
// 隐私号回调
|
||
|
||
public function virtual_Callback(Request $request)
|
||
{
|
||
$bind_id = $request->bind_id;
|
||
$pay = new PayLog();
|
||
$pay->type = 'virtual';
|
||
$result = ConsultationRecords::where('bind_id',$bind_id)->first();
|
||
if(!$result){
|
||
$pay->remark = $request->event.'bind_id'.$bind_id;
|
||
}
|
||
|
||
$pay->trade_no = $result->trade_no??null;
|
||
$pay->remark = $request->event;
|
||
//接听
|
||
if($request->event =='PICKUP'){
|
||
$result->recorder_id = $request->recorder_id;
|
||
$result->status = 1;
|
||
$result->begin_time = now();
|
||
$result->save();
|
||
|
||
}
|
||
// 挂机
|
||
if($request->event =='HANGUP'){
|
||
$result->end_time = now();
|
||
$start = strtotime($result->begin_time);
|
||
$result->Remaining_duration = $result->Remaining_duration-(time()-$start)??0;
|
||
$result->status = 2;
|
||
$result->save();
|
||
|
||
}
|
||
$pay->save();
|
||
return $this->success('ok');
|
||
|
||
}
|
||
|
||
//查看订单状态
|
||
public function get_order_status(Request $request)
|
||
{
|
||
$id = $request->id;
|
||
$status = $request->status??0;
|
||
if($status){
|
||
ConsultationRecords::where('id',$id)->update(['status'=>$status]);
|
||
}
|
||
|
||
$result = ConsultationRecords::select('id','anchor_id','status','Remaining_duration')->where('id',$id)->first();
|
||
return $this->success('ok',$result);
|
||
|
||
}
|
||
|
||
|
||
//主动挂断通话
|
||
|
||
public function InterruptCall(Request $request)
|
||
{
|
||
$id = $request->id;
|
||
$result = ConsultationRecords::where('id',$id)->first();
|
||
if(!$result)
|
||
return $this->failure('未找到该订单');
|
||
if($result->status=='0')
|
||
return $this->failure('咨询未开始');
|
||
if($result->status=='2')
|
||
return $this->failure('咨询已结束');
|
||
|
||
$data = [
|
||
// 以下代码以ax为例,选填参数以实际需求,对应填写
|
||
"corp_key" => "7574596477198833",//企业账户key 必填
|
||
"request_id" => $result->trade_no,// 企业每个请求Id唯一,如果是同一个请求重复提交,则Id保持相同 必填
|
||
"recorder_id" => $result->recorder_id,//接受方
|
||
];
|
||
$data['ts'] = time();
|
||
$data['sign'] = $this->sign($data, '8uM2Mxe7EaCU8XXW4FHjaq6m8N3794Xv');//密钥信息
|
||
// print_r(json_encode($data));
|
||
$url = 'http://124.160.62.210:9092/ykt-open/hold/hangup2';//请求地址
|
||
$curl = curl_init($url);
|
||
|
||
curl_setopt_array($curl, array(
|
||
CURLOPT_RETURNTRANSFER => true,
|
||
CURLOPT_ENCODING => "",
|
||
CURLOPT_MAXREDIRS => 10,
|
||
CURLOPT_TIMEOUT => 30,
|
||
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
|
||
CURLOPT_CUSTOMREQUEST => "POST",
|
||
CURLOPT_POSTFIELDS => json_encode($data),
|
||
CURLOPT_HTTPHEADER => array(
|
||
"Accept: */*",
|
||
"Accept-Encoding: gzip, deflate",
|
||
"Cache-Control: no-cache",
|
||
"Connection: keep-alive",
|
||
"Content-Type: application/json;charset=utf-8",
|
||
"cache-control: no-cache"
|
||
),
|
||
));
|
||
|
||
$response = curl_exec($curl);
|
||
$err = curl_error($curl);
|
||
curl_close($curl);
|
||
|
||
if ($err) {
|
||
return $this->failure('挂断失败'.$err);
|
||
} else {
|
||
$response = json_decode($response,true);
|
||
return $this->success('ok',$response);
|
||
}
|
||
|
||
}
|
||
|
||
function sign($data, $key){
|
||
if (isset($data['sign'])) {
|
||
unset($data['sign']);
|
||
}
|
||
$data_str = $this->signStr($data) . '&corp_secret=' . $key;
|
||
return md5($data_str);
|
||
}
|
||
|
||
function signStr($data){
|
||
|
||
$array_keys = array_keys($data);
|
||
asort($array_keys);
|
||
$newData = [];
|
||
foreach ($array_keys as $v) {
|
||
if ($v && !empty($data[$v])) {
|
||
$newData[] = "{$v}={$data[$v]}";
|
||
}
|
||
}
|
||
return implode('&', $newData);
|
||
}
|
||
|
||
|
||
// 咨询订单详情
|
||
public function MyConsultationDetails(Request $request)
|
||
{
|
||
$id = $request->id;
|
||
$result = ConsultationRecords::select('id','name','phone','price','desc','expect','Praise','comment','status','duration','created_at','record','viewer_id')->where('id', $id)->where('pay_status',1)->first();
|
||
$result->Praise = floatval($result->Praise);
|
||
$result->price = floatval($result->price);
|
||
$result->avatar = $result->viewer->avatar??'https://images.ufutx.com/202108/06/e1b03db630b24fd818d1aa9967ce3fb1.png';
|
||
unset($result->viewer);
|
||
return $this->success('ok',$result);
|
||
|
||
}
|
||
|
||
// 商户咨询订单详情
|
||
public function BusinessMyConsultationDetails(Request $request)
|
||
{
|
||
$id = $request->id;
|
||
$result = ConsultationRecords::select('id','name','phone','price','desc','expect','Praise','comment','status','duration','created_at','record','viewer_id')->where('id', $id)->where('pay_status',1)->first();
|
||
$result->Praise = floatval($result->Praise);
|
||
$result->price = floatval($result->price);
|
||
$pic = TouristOrder::where('trade_no',$result->trade_no)->value('pic');
|
||
$result->pic = json_decode($pic,true);
|
||
$result->avatar = MerchantUsers::where('id',$request->merchant_user_id)->value('pic')??User::DefaultAvatar;
|
||
unset($result->viewer);
|
||
return $this->success('ok',$result);
|
||
|
||
}
|
||
|
||
/**获取聊天室的地址 */
|
||
public function getChatroomAddress(Request $request){
|
||
try{
|
||
$im = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
|
||
$ip = $request->getClientIp();
|
||
if(!$request->roomid||!$request->anchor_id||!$request->clienttype){
|
||
return $this->failure('参数错误');
|
||
}
|
||
$anchors = Anchor::with('viewer')->where('id', $request->anchor_id)->first();
|
||
|
||
if($request->type=='business'){
|
||
$accid = 'business'.$request->anchor_id;
|
||
}else{
|
||
$accid = $anchors->viewer->user_id;
|
||
}
|
||
$url = $im->getChatroomAddress($request->roomid,$accid,$request->clienttype);
|
||
return $this->success('ok', ['chatroomAddress'=>$url]);
|
||
} catch (\Exception $e) {
|
||
return $this->failure('失败');
|
||
}
|
||
|
||
|
||
}
|
||
|
||
//增加观看历史、直播pv、主播热度
|
||
public function addPv($live, $viewer){
|
||
if(!empty($viewer)){
|
||
$key = $viewer->id.'_watch';
|
||
$count = LiveViewer::where('viewer_id', $viewer->id)->where('live_id', $live->id)->count();
|
||
if(!$count){
|
||
$new_live_viewer = new LiveViewer();
|
||
$new_live_viewer->viewer_id = $viewer->id;
|
||
$new_live_viewer->live_id = $live->id;
|
||
$new_live_viewer->times = 1;
|
||
$new_live_viewer->save();
|
||
}
|
||
$len = Redis::llen($key);
|
||
if($len){
|
||
Redis::lrem($key, 1, $live->id);
|
||
Redis::lpush($key, $live->id);
|
||
redis::ltrim($key,0,149);
|
||
}else{
|
||
Redis::lpush($key, $live->id);
|
||
}
|
||
|
||
$count = LiveViewer::where('viewer_id', $viewer->id)->where('live_id', $live->id)->count();
|
||
if(!$count){
|
||
$new_live_viewer = new LiveViewer();
|
||
$new_live_viewer->viewer_id = $viewer->id;
|
||
$new_live_viewer->live_id = $live->id;
|
||
$new_live_viewer->times = 1;
|
||
$new_live_viewer->save();
|
||
}
|
||
if($count){
|
||
LiveViewer::where('viewer_id', $viewer->id)->where('live_id', $live->id)->increment('times');
|
||
}
|
||
}
|
||
|
||
|
||
|
||
//增加页面跟主播的pv
|
||
Redis::zincrby('page_pv', 1 ,$live->id);
|
||
|
||
$anchor_id = $live->anchor_id;
|
||
if($anchor_id){
|
||
Redis::zincrby('anchor_red', 1 ,$anchor_id);
|
||
}
|
||
return true;
|
||
}
|
||
|
||
//绑定手机号
|
||
public function bindMobile(Request $request){
|
||
try{
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(!empty($wechatUser)){
|
||
$openId = $wechatUser->getId();
|
||
}else{
|
||
$openId = $request->oprnid;
|
||
}
|
||
$viewer = Viewer::where('openid', $openId)->first();
|
||
|
||
$mobile = $request->mobile;
|
||
if(!$mobile){
|
||
return $this->failure('请输入手机号');
|
||
}
|
||
|
||
$code = $request->input('code');
|
||
if (empty($code)) {
|
||
return $this->failure('请输入验证码');
|
||
}
|
||
//验证手机号
|
||
$result = $this->sms->check($mobile, $code);
|
||
if ($result) {
|
||
return $this->failure($result);
|
||
}
|
||
$viewer = Viewer::where('mobile', $mobile)->first();
|
||
|
||
if(empty($viewer)){
|
||
$viewer = Viewer::where('openid', $viewer->openid)->first();
|
||
$viewer->mobile = $mobile;
|
||
|
||
$user_id = User::where('mobile', $mobile)->value('id');
|
||
if(!empty($user_id)){
|
||
$viewer->user_id = $user_id;
|
||
}else{
|
||
$request->merge([
|
||
'mobile'=>$mobile,
|
||
'nickname'=>$viewer->nickname,
|
||
'name'=>$viewer->nickname,
|
||
'from_official_openid'=>$viewer->referrer ? $viewer->referrer->openid : '',
|
||
'sex'=>$viewer->sex,
|
||
'avatar'=>$viewer->avatar,
|
||
'type'=>'single',
|
||
'passworde'=>bcrypt($mobile),
|
||
]);
|
||
$userCon = New UserService();
|
||
$user = $userCon->loginByMobile($request);
|
||
$viewer->user_id = $user->id;
|
||
}
|
||
$user = $this->guard()->loginUsingId($viewer->user_id, true);
|
||
$token = '';
|
||
if (!empty($user)) {
|
||
$token = $user->createToken($viewer->mobile)->accessToken;
|
||
}
|
||
$viewer->save();
|
||
} elseif ($viewer->openid != $viewer->openid){
|
||
return $this->failure('手机号已被‘'.$viewer->nickname.'’绑定');
|
||
}
|
||
|
||
DB::commit();
|
||
return $this->success('login_success', compact('viewer', 'token'));
|
||
} catch (\Exception $e) {
|
||
DB::rollback();
|
||
\Log::info($e->getMessage());
|
||
return $this->success('绑定失败');
|
||
}
|
||
}
|
||
//我的分享
|
||
public function shareLists(Request $request){
|
||
$user = auth()->user();
|
||
$viewer = Viewer::where('user_id', $user->id)->orWhere('mobile', $user->mobile)->first();
|
||
if(empty($viewer)){
|
||
return $this->failure('直播用户不存在');
|
||
}
|
||
|
||
if($request->has('keyword') && $request->keyword){
|
||
$keyword = $request->keyword;
|
||
$lists = ViewerShare::whereHas('share', function ($query) use ($keyword) {
|
||
$query->where('nickname', 'like', "%".$keyword."%");
|
||
});
|
||
}else{
|
||
$lists = ViewerShare::with('share:id,nickname,avatar');
|
||
}
|
||
|
||
if($request->type){
|
||
$lists = $lists->where('type', $request->type);
|
||
}
|
||
$lists =$lists->where('sharer_id', $viewer->id)->where('is_show', 1)->groupby('viewer_id')->orderBy('created_at', 'desc')->paginate();
|
||
foreach ($lists as $k => &$list){
|
||
if($list->share){
|
||
$list->nickname = $list->share->nickname;
|
||
$list->avatar = $list->share->avatar;
|
||
}
|
||
|
||
}
|
||
|
||
return $this->success('ok', $lists);
|
||
}
|
||
|
||
public function shareCount(Request $request){
|
||
$user = auth()->user();
|
||
$viewer = Viewer::where('user_id', $user->id)->orWhere('mobile', $user->mobile)->first();
|
||
if(empty($viewer)){
|
||
return $this->failure('直播用户不存在');
|
||
}
|
||
|
||
$data['all_count'] = ViewerShare::where('sharer_id', $viewer->id)->where('is_show', 1)->count();
|
||
$data['new_count'] = ViewerShare::where('sharer_id', $viewer->id)->where('type', 1)->count();
|
||
$data['old_count'] = ViewerShare::where('sharer_id', $viewer->id)->where('is_show', 1)->where('type', 2)->count();
|
||
return $this->success('ok', $data);
|
||
}
|
||
|
||
public function viewer(Request $request){
|
||
try {
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(!empty($wechatUser)){
|
||
$openId = $wechatUser->getId();
|
||
}else{
|
||
$openId = $request->openid;
|
||
}
|
||
$viewer = Viewer::where('openid', $openId)->first();
|
||
if(empty($viewer)) throw new \Exception('获取viewer失败 openid='.$openId);
|
||
if(empty($viewer->ip)){
|
||
$viewer->ip = $request->getClientIp();
|
||
}
|
||
$viewer->save();
|
||
$viewer->share_count = ViewerShare::where('sharer_id', $viewer->id)->where('is_show',1)->groupBy('viewer_id')->get()->count();
|
||
$viewer->focus_count = AnchorViewer::where('viewer_id', $viewer->id)->where('focusing', 1)->groupBy('anchor_id')->get()->count();
|
||
// $viewer->new_share_count = Viewer::where('viewer_id', $viewer->id)->count();
|
||
//$role_id = ShareRoleViewer::where('viewer_id', $viewer->id)->value('share_role_id');
|
||
//$viewer->share_role_name = ShareRole::where('id', $role_id)->value('name');
|
||
// $viewer->is_messenger = Messenger::where('viewer_id', $viewer->id)->where('is_audit', 1)->count();
|
||
|
||
// //公众号信息
|
||
// $data = $this->mpData($viewer->openid);
|
||
// $viewer->mp_fans = $data['fans'];
|
||
// $viewer->type = $data['type'];
|
||
$anchor = Anchor::where('viewer_id', $viewer->id)->first();
|
||
$viewer->is_teacher = empty($anchor) ? 0 : 1;
|
||
if($viewer->is_teacher){
|
||
$viewer->fans = AnchorViewer::where('anchor_id', $anchor->id)->where('focusing', 1)->count();
|
||
}
|
||
|
||
$viewer->photo = $viewer->avatar;
|
||
$viewer->rank_deadline = null;
|
||
$user = User::where('mobile', $viewer->mobile)->orWhere('id', $viewer->user_id)->first();
|
||
if(!empty($user)){
|
||
$viewer->photo = !empty($user->photo) ? $user->photo : $viewer->avatar;
|
||
$deadline = RankHistory::where('user_id', $user->id)->where('rank_id', $user->rank_id)->orderBy('deadline', 'desc')->value('deadline');
|
||
$viewer->rank_deadline = $deadline;
|
||
}
|
||
$user = User::where('id', $viewer->user_id)->orWhere('mobile', $viewer->mobile)->first();
|
||
$viewer->rank_id = $user->rank_id??0;
|
||
$viewer->is_educate_approved = $user->is_educate_approved??0;
|
||
$viewer->moment_count = !empty($user) ? CommunityMoment::where('user_id', $viewer->user_id)->count() : 0;
|
||
$viewer->is_real_approved = !empty($user) ? $user->is_real_approved : 0;
|
||
$viewer->is_approved = !empty($user) ? ($user->is_approved == 1?1:0) : 0;
|
||
$viewer->approve_time = !empty($user) ? $user->approve_time : 0;
|
||
$viewer->name = !empty($user) ? $user->name : '';
|
||
$viewer->card_num = !empty($user) ? $user->card_num : '';
|
||
$viewer->identification_photos = !empty($user) ? json_decode($user->identification_photos) : '';
|
||
|
||
//统一h5跟user头像
|
||
if(!empty($user)){
|
||
$viewer->avatar = $user->avatar;
|
||
}
|
||
//是否可以提现
|
||
//$is_withdrawer = $viewer->isWithdrawer();
|
||
//$viewer->is_withdrawer = $is_withdrawer;
|
||
return $this->success('ok', $viewer);
|
||
} catch (\Exception $e) {
|
||
$this->getError($e);
|
||
return $this->failure();
|
||
}
|
||
}
|
||
|
||
|
||
public function getUserInfo(Request $request)
|
||
{
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(!empty($wechatUser)){
|
||
$openId = $wechatUser->getId();
|
||
}else{
|
||
$openId = $request->openid;
|
||
}
|
||
$user = MerchantUser::where('openid',$openId)->select('pic','nickname')->first();
|
||
if(!$user) {
|
||
$user = Viewer::where('openid', $openId)->select('avatar as pic','nickname')->first();
|
||
}
|
||
if(!$user) {
|
||
$rand_str = $this->randString(8);
|
||
$user = [
|
||
'pic' => User::DefaultAvatar,
|
||
'nickname' => '用户'.$rand_str
|
||
];
|
||
}
|
||
return $this->success('ok',$user);
|
||
}
|
||
|
||
public function getBlessUserInfo(Request $request)
|
||
{
|
||
|
||
$openId = $request->openid;
|
||
$user = BlessingUsers::where('openid',$openId)->first();
|
||
$user->share_url = env('APP_URL').'/h5/#/blessingHome';
|
||
$qr_code = Redis::get('Drifting_bottle_url');
|
||
if(!$qr_code){
|
||
$qr_code = $this->getPreviewQrcode($user->share_url);
|
||
Redis::setex('Drifting_bottle_url',60*60*24*30,$qr_code);
|
||
$qr_code = Redis::get('Drifting_bottle_url');
|
||
}
|
||
$user->qr_code = $qr_code;
|
||
if(!$user) {
|
||
return $this->failure('没有获取到用户信息');
|
||
} else {
|
||
return $this->success('ok',$user);
|
||
}
|
||
}
|
||
|
||
|
||
//我的分享海报
|
||
public function sharePoter(Request $request){
|
||
$user = auth()->user();
|
||
$viewer = Viewer::where('user_id', $user->id)->orWhere('mobile', $user->mobile)->first();
|
||
if(empty($viewer)){
|
||
return $this->failure('直播用户不存在');
|
||
}
|
||
|
||
|
||
$live = Live::where('end_time', '>', date('Y-m-d H:i:s'))->orderBy('end_time','asc')->first();
|
||
$qrcode = $this->liveQrcode($live->id, $viewer->id);
|
||
|
||
$time = strtotime($live->start_time);
|
||
$weekarray=array("日","一","二","三","四","五","六");
|
||
$start_time = date('Y-m-d', $time)." 星期".$weekarray[date("w", $time)].' '.date('H:i', $time);
|
||
$arr = [
|
||
'qrcode' => $qrcode,
|
||
'name' => $viewer->nickname,
|
||
'avatar' => $viewer->avatar,
|
||
'live_id' => $live->id,
|
||
'live_poster' => $live->poster,
|
||
'title' => $live->title,
|
||
'start_time' => $start_time
|
||
];
|
||
return $this->success('ok', $arr);
|
||
}
|
||
|
||
public function getUrlqrcode($url, $size = 100){
|
||
$name = substr($url, stripos($url, '='));
|
||
$path = "../storage/qrcode/$name.png";
|
||
QrCode::format('png')->margin(1)->size($size)->generate($url, $path);
|
||
$pic = $this->uploadFile($path);
|
||
unlink($path);
|
||
return $pic;
|
||
|
||
}
|
||
|
||
public function mpDataV2(Request $request){
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(empty( $wechatUser)){
|
||
$openid = $request->openid;
|
||
}else{
|
||
$openid = $wechatUser->getId();
|
||
}
|
||
$data['type'] = 1;
|
||
$openid = 'ou713vx7f8dkEO3gOXRI2JeEcsf8';
|
||
if(!empty($openid)){
|
||
$config = [
|
||
'app_id' => config('wechat.official_account.new.app_id'),
|
||
'secret' => config('wechat.official_account.new.secret'),
|
||
];
|
||
$app = Factory::officialAccount($config);
|
||
$wechatUser = $app->user->get($openid);
|
||
print_r($wechatUser);die;
|
||
$result = !empty($wechatUser)&&isset($wechatUser['subscribe']);
|
||
$data['type'] = $result?$wechatUser['subscribe']:0;
|
||
}
|
||
$count = Wechat::count();
|
||
$data['fans'] = round($count/10000, 2).'万';
|
||
return $this->success('ok', $data);
|
||
}
|
||
|
||
public function mpData($openid){
|
||
$data = [];
|
||
|
||
if (config('app.env') == 'local'){
|
||
$data['type'] = 1;
|
||
$data['fans'] = 1010101;
|
||
}else{
|
||
|
||
$config = [
|
||
'app_id' => config('wechat.official_account.new.app_id'),
|
||
'secret' => config('wechat.official_account.new.secret'),
|
||
];
|
||
$app = Factory::officialAccount($config);
|
||
$wechatUser = $app->user->get($openid);
|
||
$result = !empty($wechatUser)&&isset($wechatUser['subscribe']);
|
||
$data['type'] = $result?$wechatUser['subscribe']:0;
|
||
|
||
$count = Wechat::count();
|
||
$data['fans'] = round($count/10000, 2).'万';
|
||
}
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* showdoc
|
||
* @catalog H5/直播/V2
|
||
* @title 搜索
|
||
* @description 搜索的接口
|
||
* @method POST
|
||
* @url api/official/live/search
|
||
* @return { "code": 0, "message": "ok", "data": []}
|
||
* @remark 这里是备注信息
|
||
* @number 99
|
||
*/
|
||
//搜索
|
||
public function search(Request $request){
|
||
$user = auth()->user();
|
||
$viewer = Viewer::where('user_id', $user->id)->orWhere('mobile', $user->mobile)->first();
|
||
if(empty($viewer)){
|
||
return $this->failure('直播用户不存在');
|
||
}
|
||
//搜索记录放入redis
|
||
$keyword = $request->keyword;
|
||
$this->setSearch($request->keyword);
|
||
|
||
|
||
$data['lives'] = Live::select('url', 'title')->where('title', 'like', '%'.$keyword.'%')->orderBy('end_time', 'desc')->paginate();
|
||
$data['classes'] = LiveClass::select('id', 'name')->where('name', 'like', '%'.$keyword.'%')->orderBy('created_at', 'desc')->get();
|
||
$data['viewers'] = Viewer::whereHas('anchor')->select('id', 'nickname')->where('nickname', 'like', '%'.$keyword.'%')->orderBy('created_at', 'desc')->get();
|
||
return $this->success('ok', $data);
|
||
|
||
}
|
||
|
||
/**
|
||
* showdoc
|
||
* @catalog H5/直播/V2
|
||
* @title 清除搜索记录
|
||
* @description 清除搜索记录的接口
|
||
* @method DELETE
|
||
* @url api/official/live/search/del
|
||
* @return { "code": 0, "message": "ok", "data": []}
|
||
* @remark 这里是备注信息
|
||
* @number 99
|
||
*/
|
||
public function setSearch($keyword){
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(empty( $wechatUser)){
|
||
$openId = $wechatUser->getId();
|
||
}else{
|
||
$openId = $wechatUser->getId();
|
||
}
|
||
$viewer = Viewer::where('openid', $openId)->first();
|
||
|
||
//搜索词汇增加热度
|
||
Redis::zincrby('live_search', 1 ,$keyword);
|
||
//搜索词放入队列
|
||
$key = 'viewer_'.$viewer->id;
|
||
$len = Redis::llen($key);
|
||
// $index = Redis::lrange($key, 0, -1);
|
||
// print_r($index);die;
|
||
if($len){
|
||
Redis::lrem($key, 1, $keyword);
|
||
Redis::lpush($key, $keyword);
|
||
redis::ltrim($key,0,9);
|
||
}else{
|
||
Redis::lpush($key, $keyword);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* showdoc
|
||
* @catalog H5/直播/V2
|
||
* @title 历史搜索以及热门记录
|
||
* @description 历史搜索以及热门的接口
|
||
* @method GET
|
||
* @url api/official/live/search/red
|
||
* @remark 这里是备注信息
|
||
* @number 99
|
||
*/
|
||
//搜索历史以及热门搜索
|
||
public function searchRed(Request $request){
|
||
$user = auth()->user();
|
||
$viewer = Viewer::where('user_id', $user->id)->orWhere('mobile', $user->mobile)->first();
|
||
if(empty($viewer)){
|
||
return $this->failure('直播用户不存在');
|
||
}
|
||
|
||
$key = 'viewer_'.$viewer->id;
|
||
$data['red'] = Redis::zRevRangeByScore('live_search', 100, 0, array('limit' => array(0, 10)));
|
||
return $this->success('ok', $data);
|
||
}
|
||
|
||
/**
|
||
* showdoc
|
||
* @catalog H5/直播/V2
|
||
* @title 清除搜索记录
|
||
* @description 清除搜索记录的接口
|
||
* @method DELETE
|
||
* @url api/official/live/search/del
|
||
* @return { "code": 0, "message": "ok", "data": []}
|
||
* @remark 这里是备注信息
|
||
* @number 99
|
||
*/
|
||
//清除搜索历史
|
||
public function searchDel(Request $request){
|
||
$user = auth()->user();
|
||
$viewer = Viewer::where('user_id', $user->id)->orWhere('mobile', $user->mobile)->first();
|
||
if(empty($viewer)){
|
||
return $this->failure('直播用户不存在');
|
||
}
|
||
|
||
Redis::ltrim('viewer_'.$viewer->id, 1, 0);
|
||
return $this->success('ok');
|
||
}
|
||
|
||
/**
|
||
* showdoc
|
||
* @catalog H5/直播/V2
|
||
* @title 观看历史
|
||
* @description 观看历史的接口
|
||
* @method POST
|
||
* @url /api/official/live/watch
|
||
* @return { "code": 0, "message": "ok", "data": []}
|
||
* @remark 这里是备注信息
|
||
* @number 99
|
||
*/
|
||
//redis存入观看历史
|
||
public function addWatchLog(Request $request){
|
||
$user = auth()->user();
|
||
$viewer = Viewer::where('user_id', $user->id)->orWhere('mobile', $user->mobile)->first();
|
||
if(empty($viewer)){
|
||
return $this->failure('直播用户不存在');
|
||
}
|
||
|
||
if(!$request->has('live_id') || !$request->live_id){
|
||
return $this->failure('请输入直播id');
|
||
}
|
||
|
||
$live_id = $request->live_id;
|
||
$live = Live::where('id', $live_id)->first();
|
||
|
||
$key = $viewer->id.'_watch';
|
||
$len = Redis::llen($key);
|
||
if($len){
|
||
Redis::lrem($key, 1, $live_id);
|
||
Redis::lpush($key, $live_id);
|
||
redis::ltrim($key,0,149);
|
||
}else{
|
||
Redis::lpush($key, $live_id);
|
||
}
|
||
|
||
//增加页面跟主播的pv
|
||
Redis::zincrby('page_pv', 1 ,$live_id);
|
||
|
||
$anchor_id = $live->anchor_id;
|
||
if($anchor_id){
|
||
Redis::zincrby('anchor_red', 1 ,$anchor_id);
|
||
}
|
||
return $this->success('ok');
|
||
}
|
||
|
||
|
||
/**
|
||
* showdoc
|
||
* @catalog H5/直播/V2
|
||
* @title 观看历史
|
||
* @description 观看历史的接口
|
||
* @method GET
|
||
* @url api/official/live/watch/logs
|
||
* @return 返回同直播列表
|
||
* @remark 这里是备注信息
|
||
* @number 99
|
||
*/
|
||
//观看历史
|
||
public function watchLog(Request $request){
|
||
$user = auth()->user();
|
||
$viewer = Viewer::where('user_id', $user->id)->orWhere('mobile', $user->mobile)->first();
|
||
if(empty($viewer)){
|
||
return $this->failure('直播用户不存在');
|
||
}
|
||
|
||
$page = $request->page ?: 0;
|
||
$limit = $request->limit ?: 14;
|
||
$start = (($page-1)*$limit) == 0 ? ($page-1)*$limit : (($page-1)*$limit)+1;
|
||
$stop = $start+$limit;
|
||
$key = $viewer->id.'_watch';
|
||
$total = Redis::llen($key);
|
||
$last_page = ceil($total/$limit);
|
||
$next_page = ($page+1) > $last_page ? $page : $page+1;
|
||
$ids = Redis::lrange($key, $start, $stop);
|
||
$lives = [];
|
||
if($ids){
|
||
$sort = trim(implode(',' ,$ids), ',');
|
||
$lives = Live::whereIn('id', $ids)->orderByRaw(DB::raw('FIELD(id, '.$sort.') asc'))->get();
|
||
}
|
||
|
||
|
||
$data = [
|
||
'data'=>$lives,
|
||
'from' => $page,
|
||
'per_page' => $limit+1,
|
||
'last_page' => $last_page,
|
||
'next_page' => $next_page,
|
||
'total' => $total
|
||
];
|
||
return $this->success('ok', $data);
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* showdoc
|
||
* @catalog H5/直播/V2
|
||
* @title 关注老师
|
||
* @description 关注老师的接口
|
||
* @method POST
|
||
* @url /api/official/viewer/focu/{anchor_id}
|
||
* @return { "code": 0, "message": "ok", "data": []}
|
||
* @remark 这里是备注信息
|
||
* @number 99
|
||
*/
|
||
//关注主播
|
||
public function focusAnchor(Request $request, $anchor_id){
|
||
$user = auth()->user();
|
||
$viewer = Viewer::where('user_id', $user->id)->orWhere('mobile', $user->mobile)->first();
|
||
if(empty($viewer)){
|
||
return $this->failure('直播用户不存在');
|
||
}
|
||
$isset = AnchorViewer::where('viewer_id', $viewer->id)->where('anchor_id', $anchor_id)->where('focusing', 1)->count();
|
||
if($isset){
|
||
return $this->failure('已关注');
|
||
}
|
||
$hasFocu = AnchorViewer::where('viewer_id', $viewer->id)->where('anchor_id', $anchor_id)->first();
|
||
if($hasFocu){
|
||
AnchorViewer::where('viewer_id', $viewer->id)->where('anchor_id', $anchor_id)->update(['focusing'=>1]);
|
||
}else{
|
||
$insert = [
|
||
'viewer_id' => $viewer->id,
|
||
'anchor_id' => $anchor_id,
|
||
'created_at' => date('Y-m-d H:i:s'),
|
||
'updated_at' => date('Y-m-d H:i:s')
|
||
];
|
||
$anchorViewer = AnchorViewer::insert($insert);
|
||
}
|
||
$openId = $request->openid;
|
||
$key = 'h5-recommend'.$openId;
|
||
$name = Anchor::where('id',$anchor_id)->value('name');
|
||
// 发送公众号通知
|
||
$data['touser'] = $request->openid;
|
||
$data['template_id'] = 'xr_peoT5zsuQ9lQUH92UCqfB5QdnQ8aPG2jKuXlqDgs';
|
||
$data['url'] = env('APP_URL').'/h5/#/teacherDetail/'.$anchor_id;
|
||
$data['data'] = [
|
||
'first' => '你已成功关注'.$name.'老师,您可以通过关注列表查看老师详情。',
|
||
'keyword1' => '通过关注列表查看老师详情。',
|
||
'keyword2' => '400 040 1707',
|
||
'reamrk' => '您关注的老师开播前,将会提前通知您,不要错过学习的机会喔~',
|
||
];
|
||
SendTemplateMsg::dispatch($data)->onQueue('template_message');
|
||
Cache::forget($key);
|
||
return $this->success('ok');
|
||
}
|
||
|
||
|
||
//主播
|
||
public function anchor(Request $request, $anchor_id)
|
||
{
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(empty( $wechatUser)){
|
||
$openId = $request->openid;
|
||
}else{
|
||
$openId = $wechatUser->getId();
|
||
}
|
||
$viewer = Viewer::where('openid', $openId)->first();
|
||
$anchor = Anchor::withCount(['focu'=>function($query){
|
||
$query->where('focusing', 1);
|
||
}])->with('viewer:id,nickname,avatar,sex,user_id')->find($anchor_id);
|
||
if(empty($anchor)){
|
||
return $this->failure('老师不存在。');
|
||
}
|
||
$user_id = $anchor->viewer->user_id??0;
|
||
$anchor->user_id = $user_id??0;
|
||
if($anchor->focu_count){
|
||
$anchor->focu_count = $anchor->focu_count<10000 ? $anchor->focu_count : round($anchor->focu_count/10000, 2).'万';
|
||
}else{
|
||
$anchor->focu_count = 0;
|
||
}
|
||
if(!empty($viewer)){
|
||
$is_focu = AnchorViewer::where('viewer_id', $viewer->id)->where('anchor_id', $anchor_id)->where('focusing', 1)->count();
|
||
}
|
||
|
||
$anchor->is_focu = !empty($is_focu) ? 1 : 0;
|
||
$anchor->pv = Redis::zscore('anchor_red', $anchor_id);
|
||
|
||
return $this->success('ok', $anchor);
|
||
}
|
||
|
||
/**
|
||
* showdoc
|
||
* @catalog H5/直播/V2
|
||
* @title 取消关注
|
||
* @description 取消关注的接口
|
||
* @method PUT
|
||
* @url /api/official/viewer/focu/cancel/{anchor_id}
|
||
* @return { "code": 0, "message": "ok", "data": []}
|
||
* @remark 这里是备注信息
|
||
* @number 99
|
||
*/
|
||
//取消关注
|
||
public function focusCancel(Request $request, $anchor_id){
|
||
$user = auth()->user();
|
||
$viewer = Viewer::where('user_id', $user->id)->orWhere('mobile', $user->mobile)->first();
|
||
if(empty($viewer)){
|
||
return $this->failure('直播用户不存在');
|
||
}
|
||
|
||
$anchorViewer = AnchorViewer::where('viewer_id', $viewer->id)->where('anchor_id', $anchor_id)->first();
|
||
if(empty($anchorViewer) || $anchorViewer->focusing == 2){
|
||
return $this->failure('还未关注');
|
||
}
|
||
AnchorViewer::where('viewer_id', $viewer->id)->where('anchor_id', $anchor_id)->update(['focusing'=>2]);
|
||
return $this->success('ok');
|
||
}
|
||
|
||
|
||
/**
|
||
* showdoc
|
||
* @catalog H5/直播/V2
|
||
* @title 我的关注
|
||
* @description 我关注的人接口
|
||
* @method GET
|
||
* @url api/official/viewer/focus
|
||
* @return {"code": 0,"message": "ok","data": { "focus": { "current_page": 1, "data": [ {"viewer_id": 8,"anchor_id": 2,"focusing": 1,"created_at": "2020-04-14 14:20:50","updated_at": "2020-04-14 14:20:54","pv": null,"anchor": {"id": 2,"name": "","pic": "","viewer_id": 9,"viewer": { "id": 9, "nickname": "Zeng_斌", "avatar": ""}} }, {"viewer_id": 8,"anchor_id": 1,"focusing": 1,"created_at": "2020-04-14 14:20:15","updated_at": "2020-04-14 14:20:18","pv": "2","anchor": {"id": 1,"name": "","pic": "","viewer_id": 8,"viewer": { "id": 8, "nickname": "为什么来", "avatar": ""}} } ], "first_page_url": "http://wlj.test/api/official/viewer/focus?page=1", "from": 1, "last_page": 1, "last_page_url": "http://wlj.test/api/official/viewer/focus?page=1", "next_page_url": null, "path": "http://wlj.test/api/official/viewer/focus", "per_page": 15, "prev_page_url": null, "to": 2, "total": 2 }, "recommed": [ { "id": 1, "viewer_id": 8, "name": "", "pic": "", "pv": "2", "viewer": {"id": 8,"avatar": "","nickname": "为什么来" } } ]}}
|
||
* @return_param '' int ''
|
||
* @remark 这里是备注信息
|
||
* @number 99
|
||
*/
|
||
//关注的主播列表
|
||
public function myfocus(Request $request){
|
||
$user = auth()->user();
|
||
$viewer = Viewer::where('user_id', $user->id)->orWhere('mobile', $user->mobile)->first();
|
||
if(empty($viewer)){
|
||
return $this->failure('直播用户不存在');
|
||
}
|
||
|
||
if(isset($request->keyword) && $request->keyword) {
|
||
$keyword = $request->keyword;
|
||
$focusAnchors = AnchorViewer::with(['anchor:id,name,pic,viewer_id,introduction', 'anchor.viewer:id,nickname,avatar'])
|
||
->where('focusing', 1)->where('viewer_id', $viewer->id);
|
||
$focusAnchors = $focusAnchors->whereHas('anchor', function($sql) use($keyword){
|
||
$sql->where('name', 'like', '%'.$keyword.'%');
|
||
})->groupby('anchor_id')->orderBy('created_at', 'desc')->paginate();
|
||
} else {
|
||
$focusAnchors = AnchorViewer::with(['anchor:id,name,pic,viewer_id,introduction', 'anchor.viewer:id,nickname,avatar'])
|
||
->where('focusing', 1)->where('viewer_id', $viewer->id)
|
||
->groupby('anchor_id')->orderBy('created_at', 'desc')->paginate();
|
||
}
|
||
//$focusAnchors = AnchorViewer::with(['anchor:id,name,pic,viewer_id,introduction', 'anchor.viewer:id,nickname,avatar'])->where('focusing', 1)->where('viewer_id', $viewer->id)->groupby('anchor_id')->orderBy('created_at', 'desc')->paginate();
|
||
$focusId = [];
|
||
$info = [];
|
||
foreach ($focusAnchors as $anchor) {
|
||
if($anchor->anchor) {
|
||
$anchor->pv = Redis::zscore('anchor_red', $anchor->anchor->id);
|
||
$focusId[] = $anchor->anchor->id;
|
||
array_push($info, $anchor);
|
||
}
|
||
}
|
||
//手动分页
|
||
$page = $request->page??1;
|
||
$perPage = 15;
|
||
$offset = ($page * $perPage) -$perPage;
|
||
$result = new LengthAwarePaginator(array_slice($info, $offset, $perPage, true), count($info), $perPage, $page, ['path' => $request->url(), 'query' => $request->query()]);
|
||
$data['focus'] = $result;
|
||
//按热度推荐
|
||
// $anchor_ids = Redis::zRevRangeByScore('anchor_red', "+inf", "-inf" , array('limit' => array(0, 10)));
|
||
|
||
$anchor_ids = Redis::zRevRangeByScore('anchor_red', "+inf", "-inf")??[];
|
||
$anchor_ids = array_diff($anchor_ids, $focusId);
|
||
// $anchor_ids1 = array_slice($anchor_ids1,0,5);
|
||
// $anchor_ids2 = Anchor::with('viewer:id,nickname,avatar,user_id')->select('id','m_id','viewer_id','pic','introduction','name','pic','designation')
|
||
// ->with('viewer:id,nickname,avatar,user_id')->where('is_rec','1')
|
||
// ->orderby('sort','desc')->pluck('id');
|
||
// $anchor_ids = array_merge($anchor_ids1,$anchor_ids2);
|
||
// $anchor_ids = array_unique($anchor_ids);
|
||
// $anchor_ids = array_unique($anchor_ids);
|
||
$commentAnchors = Anchor::with('viewer:id,avatar,nickname')->select('id', 'viewer_id', 'name', 'pic', 'introduction')->whereIn('id', $anchor_ids)->get();
|
||
foreach ($commentAnchors as $key => $anchor) {
|
||
if($anchor->viewer) {
|
||
$anchor->pv = Redis::zscore('anchor_red', $anchor->id);
|
||
} else {
|
||
unset($commentAnchors[$key]);
|
||
}
|
||
|
||
}
|
||
$data['recommed'] = $commentAnchors;
|
||
return $this->success('ok', $data);
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* showdoc
|
||
* @catalog H5/直播/V2
|
||
* @title 编辑个人头像等信息
|
||
* @description 编辑个人头像的接口
|
||
* @method PUT
|
||
* @url /api/official/viewer/{viewer_id}
|
||
* @param avatar 可选 string 简介
|
||
* @return { "code": 0, "message": "ok", "data": []}
|
||
* @return_param '' int ''
|
||
* @remark 这里是备注信息
|
||
* @number 99
|
||
*/
|
||
public function updateInfo(Request $request){
|
||
$user = auth()->user();
|
||
$viewer = Viewer::where('user_id', $user->id)->orWhere('mobile', $user->mobile)->first();
|
||
if(empty($viewer)){
|
||
return $this->failure('直播用户不存在');
|
||
}
|
||
|
||
if($request->has('avatar') && $request->avatar){
|
||
$viewer->avatar = $request->avatar;
|
||
}
|
||
|
||
if($request->has('nickname') && $request->nickname){
|
||
$viewer->nickname = $request->nickname;
|
||
$user->nickname = $request->nickname;
|
||
$user->save();
|
||
}
|
||
$viewer->save();
|
||
// $anchor = Anchor::where('viewer_id', $viweer->id)->first();
|
||
// if(!empty($anchor)){
|
||
// $anchor->pic = $request->avatar;
|
||
// $anchor->save();
|
||
// }
|
||
return $this->success('ok');
|
||
}
|
||
|
||
/**
|
||
* showdoc
|
||
* @catalog H5/直播/V2
|
||
* @title 关注主播列表
|
||
* @description 关注主播列表的接口
|
||
* @method GET
|
||
* @url /api/official/live/anchor/followers/{anchor_id}
|
||
* @param keyword 可选 string 关键词
|
||
* @return 返回同直播用户列表接口
|
||
* @return_param '' int ''
|
||
* @remark 这里是备注信息
|
||
* @number 99
|
||
*/
|
||
public function followers(Request $request, $anchor_id){
|
||
$list = AnchorViewer::with('viewer:id,nickname,avatar')->select('anchor_id', 'viewer_id', 'created_at')->where('anchor_id', $anchor_id)->orderBy('created_at', 'desc');
|
||
if($request->nopage){
|
||
$list = $list->limit(20)->get();
|
||
}else{
|
||
$list = $list->paginate();
|
||
}
|
||
return $this->success('ok', $list);
|
||
}
|
||
|
||
|
||
|
||
// 发送消息
|
||
public function snedRecord(Request $request, $live_id){
|
||
$user = auth()->user();
|
||
$viewer = Viewer::Where('mobile', $user->mobile)->first();
|
||
if(empty($viewer)){
|
||
return $this->failure('直播用户不存在');
|
||
}
|
||
if($request->openid!=$viewer->openid){
|
||
$pay_log = new PayLog;
|
||
$pay_log->type = 'open_id';
|
||
$pay_log->user_id = $user->id;
|
||
$pay_log->remark = 'open_id: '.$request->openid;
|
||
$pay_log->save();
|
||
return response()->json(['code'=>2, 'message' => '账户信息有误,请重新登录.'], 200);
|
||
}
|
||
if($viewer->free_time && $viewer->free_time > date('Y-m-d H:i:s')){
|
||
$time = $viewer->free_time;
|
||
return $this->failure('账号禁言中,'.$viewer->free_time.'解封');
|
||
}
|
||
$content = $request->input('content');
|
||
//敏感词汇过滤
|
||
$result = \CommonUtilsService::checkoutTextArrayV2([$content]);
|
||
if($result['code'] == 1){
|
||
return ['code'=>1, 'message'=>$result['cause']];
|
||
}
|
||
|
||
$pic = $request->pic;
|
||
if(!$content && !$pic){
|
||
return $this->failure('聊天记录不能为空');
|
||
}
|
||
|
||
$record = New Record();
|
||
$record->viewer_id = $viewer->id;
|
||
$record->live_id = $live_id;
|
||
$record->content = $content;
|
||
$record->related_msg = $request->related_msg;
|
||
$record->pic = $pic;
|
||
$record->ip = IP::getip();
|
||
$record->save();
|
||
|
||
$records = Record::with('viewer:id,avatar,nickname')->select('id', 'content', 'viewer_id', 'pic', 'created_at', 'type', 'related_msg')->where('live_id', $live_id)->where('id', $record->id)->first();
|
||
|
||
|
||
$new_id = ($record->id - 1);
|
||
|
||
$newRecords = Record::with('viewer:id,avatar,nickname')->select('id', 'content', 'viewer_id', 'pic', 'created_at', 'type', 'related_msg')->where('id', '>', $new_id)->where('live_id', $live_id)->orderBy('created_at', 'desc')->get();
|
||
Cache::put($live_id.$new_id.'_records', $newRecords, 0.1);
|
||
|
||
//参加抽奖活动?
|
||
$lottery = Lottery::where('live_id', $live_id)->where('command', $request->input('content'))->first();
|
||
if(!empty($lottery)){
|
||
$is_lotteryer = LotteryList::where('viewer_id', $viewer->id)->where('lottery_id', $lottery->id)->count();
|
||
if(empty($is_lotteryer)){
|
||
$lottery_list = New LotteryList();
|
||
$lottery_list->viewer_id = $viewer->id;
|
||
$lottery_list->lottery_id = $lottery->id;
|
||
$lottery_list->save();
|
||
}
|
||
}
|
||
//统一h5跟user头像
|
||
if(!empty($user)){
|
||
$records->viewer->avatar = $user->avatar;
|
||
}
|
||
return $this->success('ok', $records);
|
||
}
|
||
|
||
//消息记录
|
||
public function records(Request $request, $live_id){
|
||
|
||
$records = Record::with(['viewer'=>function($q){
|
||
$q->leftJoin('users',function($q){
|
||
$q->on('viewers.user_id','=','users.id');
|
||
});
|
||
$q->select('viewers.id',
|
||
'viewers.nickname',
|
||
'viewers.avatar',
|
||
'viewers.user_id',
|
||
'users.photo',
|
||
'users.circle_avatar',
|
||
'users.app_avatar',
|
||
'users.sex');
|
||
}])
|
||
->whereHas('viewer')
|
||
->select('id', 'content', 'viewer_id', 'pic', 'created_at', 'type', 'related_msg')
|
||
->where('class','h5')
|
||
->where('live_id', $live_id);
|
||
|
||
if($request->has('keyword') && $request->keyword){
|
||
$records = $records->where('content', 'like', '%'.$request->keyword.'%');
|
||
}
|
||
$records = $records->orderBy('created_at', 'desc')->paginate();
|
||
|
||
|
||
//拼接打赏类型的消息
|
||
foreach ($records as &$record){
|
||
if($record->type == 1){
|
||
$order = GiftOrder::where('trade_no', $record->content)->first();
|
||
if(!empty($order)){
|
||
$viewer = Viewer::select('nickname', 'avatar')->where('id', $order->viewer_id)->first();
|
||
$teacher = Anchor::select('name', 'pic')->where('id', $order->teacher_id)->first();
|
||
if(!empty($viewer)){
|
||
$record->content = [
|
||
'viewer_name' => $viewer->nickname,
|
||
'viewer_avatar' => $viewer->avatar,
|
||
'teacher_name' => $teacher->name,
|
||
'teacher_avatar' => $teacher->pic,
|
||
'amount' => $order->price
|
||
];
|
||
}
|
||
}
|
||
}
|
||
$record->viewer->avatar = $this->userCon->getUserValidPhoto($record->viewer);
|
||
}
|
||
return $this->success('ok', $records);
|
||
}
|
||
|
||
//最新消息记录
|
||
public function newRecords(Request $request, $live_id){
|
||
|
||
if(!$request->has('new_id') || !$request->new_id){
|
||
return $this->success('ok');
|
||
}
|
||
$new_id = $request->new_id;
|
||
|
||
|
||
$records = Cache::remember($live_id.$new_id.'_records', 0.1, function () use ($new_id, $live_id) {
|
||
|
||
return Record::with('viewer:id,avatar,nickname')->select('id', 'content', 'viewer_id', 'pic', 'created_at', 'type', 'related_msg')->where('id', '>', $new_id)->where('live_id', $live_id)->orderBy('created_at', 'desc')->get();
|
||
});
|
||
|
||
|
||
//拼接打赏类型的消息
|
||
foreach ($records as &$record){
|
||
if($record->type == 1){
|
||
$order = GiftOrder::where('trade_no', $record->content)->first();
|
||
if(!empty($order)){
|
||
$viewer = Viewer::select('viewers.nickname', 'users.photo','users.circle_avatar','users.app_avatar','users.sex')->where('viewers.id', $order->viewer_id)
|
||
->leftJoin('users',function($q){
|
||
$q->where('viewers.user_id','=','users.id');
|
||
})->first();
|
||
$teacher = Anchor::select('name', 'pic')->where('id', $order->teacher_id)
|
||
->first();
|
||
$teacher = Anchor::select('name', 'pic')->where('id', $order->teacher_id)->first();
|
||
$record->content = [
|
||
'viewer_name' => $viewer->nickname,
|
||
'viewer_avatar' => $this->userCon->getUserValidPhoto($viewer),
|
||
'teacher_name' => $teacher->name,
|
||
'teacher_avatar' => $teacher->pic,
|
||
'amount' => $order->price
|
||
];
|
||
}
|
||
}
|
||
}
|
||
|
||
$pv = Redis::zscore('page_pv', $live_id);
|
||
$anchor_id = Live::where('id', $live_id)->value('anchor_id');
|
||
$teacher_pv = Redis::zscore('anchor_red', $anchor_id);
|
||
$created_at = $request->created_at ?: date('Y-m-d H:i:s');
|
||
$live_viewer = LiveViewer::with('viewer')->where('live_id', $live_id)->limit(15)->where('created_at', '>', $created_at)->orderBy('created_at', 'desc')->get();
|
||
$viewer_count = LiveViewer::where('live_id', $live_id)->count();
|
||
|
||
//点赞量
|
||
if($request->like_count){
|
||
Live::where('id', $live_id)->increment('like_count', $request->like_count);
|
||
}
|
||
$like_count = Live::where('id', $live_id)->value('like_count');
|
||
return $this->success('ok', compact('records', 'pv', 'teacher_pv', 'live_viewer', 'viewer_count', 'like_count'));
|
||
}
|
||
|
||
|
||
/**点赞数量 */
|
||
public function newRecordsLikeCount(Request $request, $live_id){
|
||
if($request->like_count >= 1000){
|
||
$request->like_count = 1000;
|
||
}
|
||
//点赞量
|
||
if($request->like_count){
|
||
Live::where('id', $live_id)->increment('like_count', $request->like_count);
|
||
}
|
||
$like_count = Live::where('id', $live_id)->value('like_count');
|
||
return $this->success('ok', compact('like_count'));
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* showdoc
|
||
* @catalog H5/直播/V2
|
||
* @title 排行榜
|
||
* @description 排行榜接口
|
||
* @method GET
|
||
* @url /api/official/live/rank/{live_id}
|
||
* @return { "code": 0, "message": "ok", "data": { "share_rank": [{ "id": 9, "avatar": "http://thirdwx.qlogo.cn/mmopen/vi_32/dPUP1rTicEnBchsJ9pPiaHML3WibMHvKkm5jN1lhkvMCILzowAticvVQwolgcTxdUegpt3Fwk1o0FHCIcWqiaQGtQmg/132", "nickname": "Zeng_斌", "share_count": "10"},{ "id": 8, "avatar": "http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIQzYLGNqZWEyYS0qLgibNh3RCkJf5fAwicPNxKyqY2RzXLaxHYXs1PBhNcoYEln2EkRQfWCVUOtGUA/132", "nickname": "为什么来", "share_count": "2"} ], "reward_rank": "" }}
|
||
* @return_param share_rank arr 分享排行榜
|
||
* @return_param reward_rank arr 分享排行
|
||
* @remark 这里是备注信息
|
||
* @number 99
|
||
*/
|
||
public function rank(Request $request, $live_id){
|
||
|
||
//分页
|
||
$share_rank = ViewerShare::select(DB::raw('count(*) as share_count, sharer_id'))
|
||
->with(['sharer'=>function($q){
|
||
$q->leftJoin('users',function($q){
|
||
$q->on('viewers.user_id','=','users.id');
|
||
});
|
||
$q->select('viewers.id',
|
||
'viewers.nickname',
|
||
'viewers.avatar',
|
||
'viewers.user_id',
|
||
'users.photo',
|
||
'users.circle_avatar',
|
||
'users.app_avatar',
|
||
'users.sex');
|
||
}])
|
||
->where('live_id', $live_id)->groupBy('sharer_id')->orderBy('share_count', 'desc')->get();
|
||
//return $this->success('ok', $share_rank);
|
||
//$share_rank = Redis::zRevRangeByScore($live_id.'_share_rank', "+inf", "-inf" , ['withscores' => TRUE, 'limit' => [0, 30]]);
|
||
//$shares = Viewer::select('id', 'avatar', 'nickname')->withCount(['messenger'=>function($query){
|
||
//$query->where('is_audit', 1);
|
||
//}])->whereIn('id', array_keys($share_rank))->get()->toArray();
|
||
$rank_arr = [];
|
||
$shares = [];
|
||
foreach ($share_rank as $key => &$item){
|
||
if(!empty($item->sharer)) {
|
||
$rank_arr['share_count'] = $item->share_count;
|
||
$rank_arr['id'] = $item->sharer->id;
|
||
$rank_arr['nickname'] = $item->sharer->nickname;
|
||
$rank_arr['avatar'] = $this->userCon->getUserValidPhoto($item->sharer);
|
||
$rank_arr['messenger_count'] = Messenger::where('viewer_id', $item->sharer->id)->count();
|
||
$shares[] = $rank_arr;
|
||
}
|
||
}
|
||
$reward = GiftOrder::select(DB::raw('id, viewer_id, sum(price) as cash'))
|
||
->with(['viewer'=>function($q){
|
||
$q->leftJoin('users',function($q){
|
||
$q->on('viewers.user_id','=','users.id');
|
||
});
|
||
$q->select('viewers.id',
|
||
'viewers.nickname',
|
||
'viewers.avatar',
|
||
'viewers.user_id',
|
||
'users.photo',
|
||
'users.circle_avatar',
|
||
'users.app_avatar',
|
||
'users.sex');
|
||
}])
|
||
->whereHas('viewer')
|
||
->where('live_id', $live_id)->where('pay_status', 1)->groupBy('viewer_id')->orderBy('cash', 'desc')->get();
|
||
foreach ($reward as &$item){
|
||
$item->viewer->avatar = $this->userCon->getUserValidPhoto($item->viewer);
|
||
if(!empty($item->viewer)){
|
||
$item->id = $item->viewer->id;
|
||
$item->nickname = $item->viewer->nickname;
|
||
$item->avatar = $item->viewer->avatar;
|
||
}
|
||
unset($item->viewer);
|
||
}
|
||
|
||
$rank['share_rank'] = $shares;
|
||
$rank['reward_rank'] = $reward;
|
||
$data = [
|
||
'data'=>$rank,
|
||
];
|
||
return $this->success('ok', $data);
|
||
}
|
||
|
||
|
||
|
||
// //打赏排行榜
|
||
// public function rewardRank(Request $request, $live_id){
|
||
// if(empty($this->viewer)){
|
||
// $wechatUser = session('wechat.oauth_user.new');
|
||
// $openId = $wechatUser->getId();
|
||
// $this->viewer = Viewer::where('openid', $openId)->first();
|
||
// }
|
||
//
|
||
// //分页
|
||
// $reward_rank = Redis::zRevRangeByScore($live_id.'_reward_rank', "+inf", "-inf" , ['withscores' => TRUE, 'limit' => [0, 30]]);
|
||
// $viewers = Viewer::select('id', 'avatar', 'nickname')->whereIn('id', array_keys($reward_rank))->get()->toArray();
|
||
// foreach ($viewers as $key => &$item){
|
||
// $item['share_count'] = $reward_rank[$item['id']];
|
||
// }
|
||
// array_multisort(array_column($viewers,'share_count'), SORT_DESC, $viewers);
|
||
//// $reward = GiftOrder::where('live_id', $live_id)->where('viweer_id', $this->viewer->id)->where('pay_status', 1)->groupBy('viewer_id')->sum()->toArray();
|
||
// $rank['share_rank'] = $viewers;
|
||
// $rank['reward_rank'] = '';
|
||
// $data = [
|
||
// 'data'=>$rank,
|
||
// ];
|
||
// return $this->success('ok', $data);
|
||
// }
|
||
|
||
/**
|
||
* showdoc
|
||
* @catalog H5/直播/V2
|
||
* @title 分类列表
|
||
* @description 分类列表接口
|
||
* @method GET
|
||
* @url api/official/live/classes
|
||
* @return { "code": 0, "message": "ok", "data": [{"id": 1,"name": "美年T LB","sort": 99},{"id": 2,"name": "嘉宾互动\t","sort": 0},{"id": 3,"name": "活动","sort": 0},{"id": 4,"name": "福恋社群","sort": 0},{"id": 5,"name": "福恋介绍","sort": 0},{"id": 7,"name": "婚恋辅导","sort": 0} ]}
|
||
* @remark 这里是备注信息
|
||
* @number 99
|
||
*/
|
||
public function classes(Request $request){
|
||
$classes = LiveClass::select('id', 'name', 'sort')->where('is_show', 1)->orderBy('sort', 'desc')->get();
|
||
return $this->success('ok', $classes);
|
||
}
|
||
|
||
/**
|
||
* showdoc
|
||
* @catalog H5/直播/V2
|
||
* @title 推荐直播
|
||
* @description 推荐直播接口
|
||
* @method GET
|
||
* @url api/official/lives/hot
|
||
* @return {"code":0,"message":"ok","data":[{"id":1,"poster":"111","title":"test"},{"id":2,"poster":"11","title":"市市"}]}
|
||
* @remark 这里是备注信息
|
||
* @number 99
|
||
*/
|
||
public function hotLives(Request $request){
|
||
// $wechatUser = session('wechat.oauth_user.new');
|
||
// if(empty( $wechatUser)){
|
||
// $openId = $request->openid;
|
||
// }else{
|
||
// $openId = $wechatUser->getId();
|
||
// }
|
||
// $viewer = Viewer::where('openid', $openId)->first();
|
||
|
||
// $ids = Redis::zRevRangeByScore('page_pv', "+inf", "-inf", array('limit' => array(0, 6)));
|
||
//
|
||
// $lives = Live::whereIn('id', $ids)->get()->toArray();
|
||
$lives = Live::select('id', 'url', 'title', 'poster')->where('recommend', '1')->where('is_show', 1)->orderByRaw(DB::raw('FIELD(status, 1,3,0,2) asc'))->orderBy('created_at', 'desc')->get();
|
||
return $this->success('ok', $lives);
|
||
}
|
||
|
||
//直播间分享
|
||
public function liveShare(Request $request, $live_id){
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(empty( $wechatUser)){
|
||
$openId = $request->openid;
|
||
}else{
|
||
$openId = $wechatUser->getId();
|
||
}
|
||
$viewer = Viewer::where('openid', $openId)->first();
|
||
|
||
$qrcode = $this->liveQrcode($live_id, $viewer->id);
|
||
|
||
$live = Live::find($live_id);
|
||
if(!empty($qrcode)){
|
||
$viewer->qrcode = $qrcode;
|
||
}
|
||
|
||
return $this->success('ok', ['live'=>$live, 'viewer'=>$viewer]);
|
||
}
|
||
|
||
|
||
//生成直播间分享二维码
|
||
public function liveQrcode($live_id, $viewer_id){
|
||
//判断是否存在
|
||
$key = sprintf('l_%d_v_%d', $live_id, $viewer_id);
|
||
$qrcode = Redis::get($key);
|
||
if(empty($qrcode)){
|
||
if(config('app.env') == 'local'){
|
||
$url = 'http://alpha.ufutx.net/h5/#/watchLive/'.$live_id.'?from_id='.$viewer_id.'&live_id='.$live_id.'&type=live';
|
||
}
|
||
if (config('app.env') == 'alpha') {
|
||
$url = 'http://alpha.ufutx.net/h5/#/watchLive/'.$live_id.'?from_id='.$viewer_id.'&live_id='.$live_id.'&type=live';
|
||
}
|
||
if(config('app.env') == 'production'){
|
||
$url = env('APP_URL').'/h5/#/watchLive/'.$live_id.'?from_id='.$viewer_id.'&live_id='.$live_id.'&type=live';
|
||
}
|
||
$qrcode = $this->qrcodeWithScene($url);
|
||
Redis::setex($key, 30 * 24 * 3600, $qrcode);
|
||
}
|
||
return $qrcode;
|
||
}
|
||
public function qrcode(Request $request){
|
||
//判断是否存在
|
||
$url = '1010article&type=live';
|
||
$qrcode = $this->qrcodeWithScene($url);
|
||
return $qrcode;
|
||
}
|
||
|
||
//生成带参二维码
|
||
public function qrcodeWithScene($scene_str){
|
||
$app = $this->app;
|
||
$result = $app->qrcode->temporary($scene_str, 30 * 24 * 3600);
|
||
|
||
if($result){
|
||
$url = $app->qrcode->url($result['ticket']);
|
||
$content = file_get_contents($url); // 得到二进制图片内容
|
||
$ticket = $result['ticket'];
|
||
$path = "../storage/qrcode/".$ticket.".png";
|
||
// if(!file_exists("../storage/qrcode/")){
|
||
// mkdir("../storage/qrcode/", 0777, true );
|
||
// }
|
||
file_put_contents($path, $content);
|
||
$pic = $this->uploadFile($path);
|
||
unlink($path);
|
||
}
|
||
return $pic;
|
||
}
|
||
//余额变动记录(提现记录)
|
||
public function assetLog(Request $request){
|
||
$user = auth()->user();
|
||
$viewer = Viewer::where('user_id', $user->id)->orWhere('mobile', $user->mobile)->first();
|
||
if(empty($viewer)){
|
||
return $this->failure('直播用户不存在');
|
||
}
|
||
|
||
$asset = Asset::where('viewer_id', $viewer->id)->first();
|
||
$score = $asset?bcmul(bcadd($asset->share_cash,$asset->cash,2), 100, 0):0;
|
||
$viewer_id = $viewer->id;
|
||
$logs = AssetLog::select('id', 'num', 'account', 'score', 'created_at', 'reason', 'cash_status', 'is_audit')->where('viewer_id', $viewer_id)->where('type', $request->type)->orderBy('created_at', 'desc')->paginate();
|
||
if(empty($logs)){
|
||
return $this->success('ok', $logs);
|
||
}
|
||
|
||
foreach ($logs as &$log){
|
||
$date = substr($log->created_at->toDateTimeString(), 0, 16);
|
||
|
||
$log->num = bcmul($log->num, 100, 0);
|
||
$log->time = $date;
|
||
if(!empty($log->reason)) {
|
||
switch ($log->reason) {
|
||
case 'PAYEE_NOT_EXIST':
|
||
$log->reason = '支付宝账户或者真实姓名有误,请重新绑定';
|
||
break;
|
||
case 'INVALID_PARAMETER':
|
||
$log->reason = '缺少支付宝或者真实姓名';
|
||
break;
|
||
default :
|
||
$log->reason = $log->reason?:'';
|
||
}
|
||
}
|
||
}
|
||
$has_alipay = empty($viewer->alipay_account) ? 0 : 1;
|
||
$alipay_account = substr_replace($viewer->alipay_account, '****', 3, 4);
|
||
|
||
return $this->success('ok', [['score'=>$score, 'has_alipay_account'=>$has_alipay, 'alipay_account'=>$alipay_account], $logs]);
|
||
}
|
||
|
||
//收益记录
|
||
public function gainLogs(Request $request){
|
||
$user = auth()->user();
|
||
$viewer = Viewer::where('user_id', $user->id)->orWhere('mobile', $user->mobile)->first();
|
||
if(empty($viewer)){
|
||
return $this->failure('直播用户不存在');
|
||
}
|
||
|
||
$logs = AssetLog::select('num', 'type', 'score', 'created_at', 'log_id')->where('viewer_id', $viewer->id)->where('is_hooked', 1)->whereIn('type', [1,2,4,5])->orderBy('created_at','desc')->paginate();
|
||
foreach ($logs as &$log){
|
||
if($log->type != 3){
|
||
$log->from = Viewer::find($log->log_id);
|
||
}
|
||
$log->num = bcmul($log->num, 100, 0);
|
||
}
|
||
return $this->success('ok', $logs);
|
||
|
||
}
|
||
|
||
//打赏记录
|
||
public function rewardLog(Request $request){
|
||
$user = auth()->user();
|
||
$viewer = Viewer::where('user_id', $user->id)->orWhere('mobile', $user->mobile)->first();
|
||
if(empty($viewer)){
|
||
return $this->failure('直播用户不存在');
|
||
}
|
||
|
||
$viewer_id = $viewer->id;
|
||
|
||
$logs = GiftOrder::with(['teacher:id,name','gift:id,price', 'live:id,title'])->select('id', 'teacher_id', 'price', 'gift_id', 'live_id','created_at')->where('pay_status', 1)->where('viewer_id', $viewer_id)->orderBy('created_at', 'desc')->orderBy('created_at', 'desc')->paginate();
|
||
if(empty($logs)){
|
||
return $this->success('ok', $logs);
|
||
}
|
||
|
||
foreach ($logs as $log){
|
||
$date = substr($log->created_at->toDateTimeString(), 0, 16);
|
||
|
||
$log->time = $date;
|
||
}
|
||
|
||
return $this->success('ok', $logs);
|
||
}
|
||
|
||
//直播反馈
|
||
public function addFeedback(Request $request){
|
||
$user = auth()->user();
|
||
$viewer = Viewer::where('user_id', $user)->orWhere('mobile', $user->mobile)->first();
|
||
if(empty($viewer)){
|
||
return $this->failure('直播用户不存在');
|
||
}
|
||
|
||
if(!$request->has('content') || !$request->input('content')){
|
||
return $this->failure('请输入内容');
|
||
}
|
||
// if(!$request->has('') || !$request->){
|
||
// return $this->failure('');
|
||
// }
|
||
// if(!$request->has('') || !$request->){
|
||
// return $this->failure('');
|
||
// }
|
||
$viewer_id = $viewer->id;
|
||
$feedback = New Feedback();
|
||
$feedback->viewer_id = $viewer->id;
|
||
$feedback->content = $request->input('content');
|
||
if($request->has('pic') && $request->pic){
|
||
|
||
$feedback->pic = json_encode($request->pic);
|
||
}
|
||
if($request->has('contact') && $request->contact){
|
||
$feedback->contact = $request->contact;
|
||
}
|
||
$feedback->type = $request->type;
|
||
$feedback->save();
|
||
return $this->success('ok');
|
||
}
|
||
|
||
//赠送礼物
|
||
public function secdgift(Request $request, $live_id){
|
||
$user = auth()->user();
|
||
$from_user_id = $request->from_user_id??0;
|
||
$viewer = Viewer::where('user_id', $user->id)->orWhere('mobile', $user->mobile)->first();
|
||
if(empty($viewer)){
|
||
return $this->failure('直播用户不存在');
|
||
}
|
||
|
||
$trade_no = \CommonUtilsService::getTradeNO();
|
||
$live = Live::find($live_id);
|
||
if(empty($live)){
|
||
return $this->failure('直播间不存在');
|
||
}
|
||
|
||
$gift_order = New GiftOrder();
|
||
$gift_order->viewer_id = $viewer->id;
|
||
$gift_order->live_id = $request->live_id;
|
||
$gift_order->teacher_id = $live->anchor_id;
|
||
$gift_order->gift_id = $request->gift_id ?: 0;
|
||
$gift_order->num = $request->num ?: 0;
|
||
$gift_order->price = $request->price;
|
||
$gift_order->trade_no = $trade_no;
|
||
$gift_order->pay_status = 0;
|
||
$gift_order->save();
|
||
|
||
$pay_order = New PayOrder();
|
||
$pay_order->viewer_id = $viewer->id;
|
||
$pay_order->trade_no = $trade_no;
|
||
$pay_order->cash = $request->price;
|
||
$pay_order->from_user_id = $from_user_id;
|
||
|
||
$pay_order->save();
|
||
|
||
$config = [
|
||
'app_id' => 'wx36c72a00f1b2be64',
|
||
'mch_id' => '1581903621',
|
||
'key' => 'BuEjXg7ikKuV6tWKrsQq904ui13WpevT',
|
||
|
||
// 'cert_path' => env('WECHAT_PAYMENT_CERT_PATH'),
|
||
// 'key_path' => env('WECHAT_PAYMENT_KEY_PATH'),
|
||
|
||
'notify_url' => env('APP_URL').'/api/admin/live/pay/callback/'.$gift_order->trade_no,
|
||
|
||
|
||
//沙箱模式
|
||
// 'sandbox' => true,
|
||
];
|
||
|
||
|
||
$app = Factory::payment($config);
|
||
|
||
|
||
|
||
$attributes = array(
|
||
'trade_type' => 'JSAPI', // JSAPI,NATIVE,APP...
|
||
'body' => '福恋直播打赏',
|
||
'detail' => '打赏老师',
|
||
'out_trade_no' => $trade_no,
|
||
'total_fee' => ($request->price*100),
|
||
'notify_url' => env('APP_URL').'/api/admin/live/pay/callbak/'.$trade_no, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
|
||
'openid' => $viewer->openid,
|
||
);
|
||
//生成订单
|
||
$result = $app->order->unify($attributes);
|
||
$jssdk = $app->jssdk;
|
||
|
||
$prepayId = '';
|
||
$code_url = null;
|
||
if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){
|
||
$prepayId = $result['prepay_id'];
|
||
\Log::info('pay_status: '.json_encode($result));
|
||
} else {
|
||
\Log::info('pay_status: '.json_encode($result));
|
||
// Messenger::sendSMS('15872844805', $attributes['out_trade_no'].', 统一下单失败'.$result['return_msg']);
|
||
}
|
||
$config = $jssdk->sdkConfig($prepayId);
|
||
|
||
|
||
|
||
$data = [
|
||
'pay_order' => $pay_order,
|
||
'wx_pay' => [
|
||
'config' => $config,
|
||
'attributes' => $attributes,
|
||
'mweb_url' => isset($result['mweb_url'])?$result['mweb_url']:'',
|
||
]
|
||
];
|
||
return $this->success('ok', $data);
|
||
}
|
||
|
||
//购买回放
|
||
public function buyReplay(Request $request, $live_id){
|
||
$user = auth()->user();
|
||
$from_user_id = $request->from_user_id??0;
|
||
$viewer = Viewer::where('user_id', $user->id)->orWhere('mobile', $user->mobile)->first();
|
||
if(empty($viewer)){
|
||
return $this->failure('直播用户不存在');
|
||
}
|
||
|
||
$trade_no = \CommonUtilsService::getTradeNO();
|
||
$live = Live::find($live_id);
|
||
if(empty($live)){
|
||
return $this->failure('直播间不存在');
|
||
}
|
||
if($live->status != 2){
|
||
return $this->failure('还未生成回放');
|
||
}
|
||
|
||
$live_order = New LiveOrder();
|
||
$live_order->viewer_id = $viewer->id;
|
||
$live_order->live_id = $live_id;
|
||
$live_order->price = $live->price;
|
||
$live_order->trade_no = $trade_no;
|
||
$live_order->pay_status = 0;
|
||
$live_order->save();
|
||
|
||
$pay_order = New PayOrder();
|
||
$pay_order->viewer_id = $viewer->id;
|
||
$pay_order->trade_no = $trade_no;
|
||
$pay_order->cash = $live->price;
|
||
$pay_order->from_user_id = $from_user_id;
|
||
|
||
$pay_order->save();
|
||
|
||
$config = [
|
||
'app_id' => 'wx36c72a00f1b2be64',
|
||
'mch_id' => '1581903621',
|
||
'key' => 'BuEjXg7ikKuV6tWKrsQq904ui13WpevT',
|
||
|
||
// 'cert_path' => env('WECHAT_PAYMENT_CERT_PATH'),
|
||
// 'key_path' => env('WECHAT_PAYMENT_KEY_PATH'),
|
||
|
||
'notify_url' => env('APP_URL').'/api/admin/live/pay/callback/'.$live_order->trade_no,
|
||
|
||
|
||
//沙箱模式
|
||
// 'sandbox' => true,
|
||
];
|
||
|
||
|
||
$app = Factory::payment($config);
|
||
|
||
|
||
|
||
$attributes = array(
|
||
'trade_type' => 'JSAPI', // JSAPI,NATIVE,APP...
|
||
'body' => '福恋直播',
|
||
'detail' => '购买回放视频',
|
||
'out_trade_no' => $trade_no,
|
||
'total_fee' => ($request->price*100),
|
||
'notify_url' => env('APP_URL').'/api/admin/live/buyreplay/callbak/'.$trade_no, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
|
||
'openid' => $viewer->openid,
|
||
);
|
||
//生成订单
|
||
$result = $app->order->unify($attributes);
|
||
$jssdk = $app->jssdk;
|
||
|
||
$prepayId = '';
|
||
$code_url = null;
|
||
if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){
|
||
$prepayId = $result['prepay_id'];
|
||
\Log::info('pay_status: '.json_encode($result));
|
||
} else {
|
||
\Log::info('pay_status: '.json_encode($result));
|
||
// Messenger::sendSMS('15872844805', $attributes['out_trade_no'].', 统一下单失败'.$result['return_msg']);
|
||
}
|
||
$config = $jssdk->sdkConfig($prepayId);
|
||
|
||
|
||
|
||
$data = [
|
||
'pay_order' => $pay_order,
|
||
'wx_pay' => [
|
||
'config' => $config,
|
||
'attributes' => $attributes,
|
||
'mweb_url' => isset($result['mweb_url'])?$result['mweb_url']:'',
|
||
]
|
||
];
|
||
return $this->success('ok', $data);
|
||
}
|
||
|
||
//微信支付回调
|
||
public function payCallback(Request $request, $trade_no){
|
||
$order = GiftOrder::where('trade_no', $trade_no)->first();
|
||
$order->pay_status = 1;
|
||
$order->save();
|
||
|
||
$pay_order = PayOrder::where('trade_no', $trade_no)->first();
|
||
$pay_order->is_hookd = 1;
|
||
$pay_order->save();
|
||
|
||
$viewer_id = Anchor::where('id', $order->teacher_id)->value('viewer_id');
|
||
|
||
$asset = Asset::where('viewer_id', $viewer_id)->first();
|
||
if(empty($asset)){
|
||
$asset = New Asset();
|
||
$asset->viewer_id = $viewer_id;
|
||
$asset->cash = $order->price;
|
||
$asset->save();
|
||
}else{
|
||
$asset->increment('cash', $order->price);
|
||
$asset->save();
|
||
}
|
||
|
||
$asset_log = New AssetLog();
|
||
$asset_log->viewer_id = $viewer_id;
|
||
$asset_log->num = $order->price;
|
||
$asset_log->type = 1;
|
||
$asset_log->log_id = $trade_no;
|
||
$asset_log->is_hooked = 1;
|
||
$asset_log->account = 0;
|
||
$asset_log->score = $asset->cash;
|
||
$asset_log->save();
|
||
|
||
//生成聊天记录通知
|
||
$viewer_name = Viewer::where('id', $order->viewer_id)->value('nickname');
|
||
$teacher_name = Anchor::where('id', $order->teacher_id)->value('name');
|
||
$record = new Record();
|
||
$record->viewer_id = $order->viewer_id;
|
||
$record->live_id = $order->live_id;
|
||
$record->content = $trade_no;
|
||
$record->type = 1;//打赏类型消息
|
||
$record->save();
|
||
|
||
$replyxml = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
|
||
echo $replyxml;
|
||
}
|
||
|
||
//购买回放微信支付回调
|
||
public function replayPayCallback(Request $request, $trade_no){
|
||
$order = LiveOrder::where('trade_no', $trade_no)->first();
|
||
if($order->pay_status == 1){
|
||
$replyxml = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
|
||
echo $replyxml;
|
||
}
|
||
$order->pay_status = 1;
|
||
$order->save();
|
||
|
||
$pay_order = PayOrder::where('trade_no', $trade_no)->first();
|
||
$pay_order->is_hookd = 1;
|
||
$pay_order->save();
|
||
|
||
//增加播放权限
|
||
$key = $order->viewer_id.'_playlist';
|
||
Redis::sadd($key,$order->live_id);
|
||
|
||
$replyxml = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
|
||
echo $replyxml;
|
||
}
|
||
|
||
public function getClientIp() {
|
||
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
|
||
$ip = getenv('HTTP_CLIENT_IP');
|
||
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
|
||
$ip = getenv('HTTP_X_FORWARDED_FOR');
|
||
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
|
||
$ip = getenv('REMOTE_ADDR');
|
||
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
|
||
$ip = $_SERVER['REMOTE_ADDR'];
|
||
} else {
|
||
$ip = '0.0.0.0';
|
||
}
|
||
return preg_match('/[\d\.]{7,15}/', $ip, $matches) ? $matches[0] : '';
|
||
|
||
}
|
||
|
||
//提现
|
||
public function withdraw(Request $request){
|
||
$user = auth()->user();
|
||
$viewer = Viewer::where('user_id', $user->id)->orWhere('mobile', $user->mobile)->first();
|
||
if(empty($viewer)){
|
||
return $this->failure('直播用户不存在');
|
||
}
|
||
|
||
//限制请求次数
|
||
$key = $user->id.'_withdraw_count';
|
||
$is_request = Redis::get($key);
|
||
if($is_request){
|
||
return $this->failure('请求频率过快');
|
||
}
|
||
Redis::setex($key, 2, 1);
|
||
|
||
// if($this->viewer->id != 2024){
|
||
// return $this->failure('未开放');
|
||
// }
|
||
try {
|
||
\DB::beginTransaction();
|
||
if (empty($request->input('price'))) {
|
||
throw new \Exception("请输入提现金额", 1);
|
||
|
||
}
|
||
if(empty($viewer->alipay_account)){
|
||
throw new \Exception("没有绑定支付宝账号", 2);
|
||
}
|
||
$asset = Asset::where('viewer_id', $viewer->id)->first();
|
||
$score = bcadd($viewer->asset->share_cash,$viewer->asset->cash,2);
|
||
$price = $request->price;
|
||
if(empty($asset) || !$price || $price > $score){
|
||
|
||
throw new \Exception("余额不足", 3);
|
||
}
|
||
//先扣除分享金额
|
||
if ($price >= $asset->share_cash) {
|
||
$sub_score = bcsub($price, $asset->share_cash, 2);
|
||
$asset->decrement('share_cash', $asset->share_cash);
|
||
$asset->decrement('cash', $sub_score);
|
||
if ($asset->cash < 0 || $asset->share_cash < 0) {
|
||
throw new \Exception("提现失败,请稍后再试", 4);
|
||
}
|
||
}else{
|
||
$asset->decrement('share_cash', $price);
|
||
}
|
||
$out_biz_no = date('YmdHis') . mt_rand(1000, 9999);
|
||
$asset_log = New AssetLog();
|
||
$asset_log->viewer_id = $viewer->id;
|
||
$asset_log->num = $price;
|
||
$asset_log->type = 3;
|
||
$asset_log->cash_status = 1;
|
||
$asset_log->out_biz_no = $out_biz_no;
|
||
$asset_log->is_hooked = 0;
|
||
$asset_log->account = 2;
|
||
$asset_log->score = bcadd($asset->cash, $asset->share_cash, 2);
|
||
$asset_log->save();
|
||
|
||
// $parames = [
|
||
// 'out_biz_no' => $out_biz_no,
|
||
// 'payee_type' => 'ALIPAY_LOGONID',
|
||
// 'payee_account' => $this->viewer->alipay_account,
|
||
// 'amount' => $request->price,
|
||
// 'payer_show_name' => '福恋智能',
|
||
// 'payee_real_name' => $this->viewer->actual_name,
|
||
// 'reamrk' => '直播账户余额提现'
|
||
// ];
|
||
// TransferToAlipayAccount::dispatch($parames)->onQueue('love');
|
||
\DB::commit();
|
||
return $this->success('ok', '提交成功,等待后台审核');
|
||
} catch (\Exception $e) {
|
||
\DB::rollback();
|
||
if ($e->getCode() <= 4) {
|
||
return $this->failure($e->getMessage());
|
||
}
|
||
return $this->failure('提现失败,请稍后再试');
|
||
}
|
||
|
||
}
|
||
|
||
public function info(Request $request){
|
||
$data = [
|
||
'order_id' => '20200518110070000006090054815529',
|
||
'pay_fund_order_id' => '20200518110070001506090056083615',
|
||
'out_biz_no' => '0.1'
|
||
];
|
||
$alipay = new LiveAlipayService();
|
||
$request = $alipay->info($data);
|
||
return $this->success('ok', $request);
|
||
}
|
||
|
||
//绑定支付宝账号
|
||
public function bindAlipayAccount(Request $request){
|
||
$user = auth()->user();
|
||
$viewer = Viewer::where('user_id', $user->id)->orWhere('mobile', $user->mobile)->first();
|
||
if(empty($viewer)){
|
||
return $this->failure('直播用户不存在');
|
||
}
|
||
|
||
if(!$request->has('alipay_account') || !$request->alipay_account){
|
||
return $this->failure('请输入支付宝账号');
|
||
}
|
||
|
||
if (preg_match("/[\x7f-\xff]/", $request->alipay_account)) {
|
||
return $this->failure('支付宝账号中不能包含中文');
|
||
}
|
||
$viewer->alipay_account = $request->alipay_account;
|
||
$viewer->actual_name = $request->name;
|
||
$viewer->save();
|
||
return $this->success('ok');
|
||
}
|
||
|
||
/**
|
||
* 分享角色列表
|
||
*/
|
||
public function shareRoles(Request $request, ShareRole $share_role)
|
||
{
|
||
try {
|
||
return $this->success("角色列表", $share_role->all());
|
||
} catch (\Exception $e) {
|
||
return $this->failure('角色列表获取失败');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 分配角色
|
||
*/
|
||
public function distributeShareRole(Request $request, Viewer $viewer)
|
||
{
|
||
return $this->failure('婚姻使者级别将自动升级,请积极分享!');
|
||
try {
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(empty( $wechatUser)){
|
||
$openId = $request->openid;
|
||
}else{
|
||
$openId = $wechatUser->getId();
|
||
}
|
||
$viewer = Viewer::where('openid', $openId)->first();
|
||
if ($viewer->id == $viewer->id) {
|
||
return $this->failure('暂不支持给自己分配角色');
|
||
}
|
||
//判断自己是否是普通介绍人以上级别
|
||
//用户色角色
|
||
$share_role = $viewer->shareRole();
|
||
$other_share_role = $viewer->shareRole();
|
||
$share_role_id = $request->input('share_role_id');
|
||
if (empty($share_role_id)) {
|
||
return $this->failure('请选择分配角色');
|
||
}
|
||
$re_share_role = ShareRole::where(['id'=>$share_role_id])->first();
|
||
if (empty($re_share_role)) {
|
||
return $this->failure('分享角色不存在,请重新选择');
|
||
}
|
||
|
||
if ($share_role) {
|
||
//判断自己的角色大于分配的角色
|
||
$role_name_arr = $this->checkShareRole($share_role->name);
|
||
if (empty(count($role_name_arr)) || !in_array($re_share_role->name, $role_name_arr)) {
|
||
return $this->failure('您的角色等级不够');
|
||
}
|
||
|
||
$viewer->shareRoles()->detach();
|
||
$viewer->shareRoles()->attach($share_role_id, ['created_at'=>date('Y-m-d H:i:s'), 'updated_at'=>date('Y-m-d H:i:s')]);
|
||
//短信通知
|
||
$mobile = $viewer->mobile;
|
||
if ($mobile) {
|
||
$role_name = $this->getRoleName($share_role_id);
|
||
$param['mobile'] = $mobile;
|
||
$param['message'] = ($viewer->nickname).",您好!经福恋平台审核,恭喜您正式成为【".$role_name."婚姻使者】,现在您可以开始祝福他人进入蒙福家庭的旅程了!";
|
||
SendSpreaderNote::dispatch($param)->onQueue('love');
|
||
}
|
||
return $this->success("角色分配成功");
|
||
}else{
|
||
return $this->failure('分配角色失败,您还没有被分配角色');
|
||
}
|
||
} catch (\Exception $e) {
|
||
\Log::error($e->getMessage());
|
||
return $this->failure('角色分配失败');
|
||
}
|
||
}
|
||
|
||
public function getRoleName($share_role_id)
|
||
{
|
||
$role = ShareRole::find($share_role_id);
|
||
$name = mb_substr($role->name,0,2);
|
||
if ($name == '普通') {
|
||
$name = '';
|
||
}
|
||
return $name;
|
||
}
|
||
|
||
/**
|
||
* 检查分配角色权限
|
||
*/
|
||
public function checkShareRole($role_name)
|
||
{
|
||
$role_name_arr = [];
|
||
switch ($role_name) {
|
||
case '普通推广员':
|
||
$role_name_arr = [];
|
||
break;
|
||
case '铜牌推广员':
|
||
$role_name_arr = ['普通推广员'];
|
||
break;
|
||
case '银牌推广员':
|
||
$role_name_arr = ['普通推广员', '铜牌推广员'];
|
||
break;
|
||
case '金牌推广员':
|
||
$role_name_arr = ['普通推广员', '铜牌推广员', '银牌推广员'];
|
||
break;
|
||
default:
|
||
$role_name_arr = [];
|
||
break;
|
||
}
|
||
return $role_name_arr;
|
||
}
|
||
|
||
/**
|
||
* 用户分享人列表(可分配分享角色)
|
||
*/
|
||
public function shareViewers(Request $request, Viewer $viewer, ViewerShare $viewer_share)
|
||
{
|
||
try {
|
||
$user = auth()->user();
|
||
$viewer = Viewer::where('user_id', $user->id)->orWhere('mobile', $user->mobile)->first();
|
||
if(empty($viewer)){
|
||
return $this->failure('直播用户不存在');
|
||
}
|
||
|
||
$viewer->shareRoles;
|
||
$keyword = $request->input('keyword');
|
||
$lists = $this->getShareViewers($request, $viewer, $viewer_share);
|
||
//我的收益
|
||
$income = $viewer->asset? bcmul(bcadd($viewer->asset->share_cash,$viewer->asset->cash,2), 100, 0):0;
|
||
unset($viewer->asset);
|
||
//我的分享数
|
||
$share_num = $viewer_share->where('sharer_id',$viewer->id)->where('type', 1)->count();
|
||
return $this->success('ok', compact('lists', 'viewer', 'income', 'share_num'));
|
||
} catch (\Exception $e) {
|
||
\Log::error($e->getMessage());
|
||
return $this->failure("列表获取失败");
|
||
}
|
||
|
||
}
|
||
|
||
public function getShareViewers($request, $viewer, $viewer_share)
|
||
{
|
||
$keyword = $request->input('keyword');
|
||
$viewer_ids = $viewer_share->where('sharer_id',$viewer->id)->where('type', 1)->pluck('viewer_id');
|
||
$lists = \DB::table('viewers')->leftJoin('live_share_role_viewers', 'viewers.id', '=', 'live_share_role_viewers.viewer_id')->select('viewers.id', 'viewers.nickname', 'viewers.avatar', 'live_share_role_viewers.share_role_id', 'viewers.created_at')->whereIn('viewers.id', $viewer_ids);
|
||
if (trim($keyword)) {
|
||
$lists = $lists->where('viewers.nickname', 'like', '%'.$keyword.'%');
|
||
}
|
||
$lists = $lists->orderBy('share_role_id', 'desc')->paginate();
|
||
foreach ($lists as $list) {
|
||
$list->role_name = ShareRole::where('id', $list->share_role_id)->value('name');
|
||
$list->share_num = $viewer_share->where('sharer_id', $list->id)->where('type', 1)->count();
|
||
}
|
||
return $lists;
|
||
|
||
}
|
||
|
||
// 福恋使者人数
|
||
|
||
public function MessengerNumber(Request $request)
|
||
{
|
||
$num = MarriageMessenger::get()->count();
|
||
$data['num'] = $num;
|
||
return $this->success('ok', $data);
|
||
|
||
}
|
||
/**
|
||
* 用户分享列表
|
||
*/
|
||
public function viewerShareViewers(Request $request, Viewer $viewer, ViewerShare $viewer_share)
|
||
{
|
||
try {
|
||
$lists = $this->getShareViewers($request, $viewer, $viewer_share);
|
||
return $this->success('ok', $lists);
|
||
} catch (\Exception $e) {
|
||
\Log::error($e->getMessage());
|
||
return $this->failure("列表获取失败");
|
||
}
|
||
}
|
||
|
||
//申请成为福恋使者
|
||
public function becomeMessenger(Request $request){
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(!empty($wechatUser)){
|
||
$openId = $wechatUser->getId();
|
||
}else{
|
||
$openId = $request->openid;
|
||
}
|
||
$viewer = Viewer::where('openid', $openId)->first();
|
||
|
||
$is_set = Messenger::where('viewer_id', $viewer->id)->where('is_audit', '!=', 2)->count();
|
||
if($is_set){
|
||
return $this->failure('您已申请福恋使者');
|
||
}
|
||
if(!$request->has('name') || !$request->name){
|
||
return $this->failure('请输入名字');
|
||
}
|
||
if(!$request->has('sex') || !is_numeric($request->sex)){
|
||
return $this->failure('请选择性别');
|
||
}
|
||
if(!$request->has('mobile') || !$request->mobile){
|
||
return $this->failure('请输入电话');
|
||
}
|
||
if ($request->has('code')) {
|
||
if(!$request->code){
|
||
return $this->failure('请输入验证码');
|
||
}else{
|
||
//验证手机号
|
||
$result = $this->sms->check($request->mobile, $request->code);
|
||
if ($result) {
|
||
return $this->failure($result);
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
if(!$request->has('age') || !is_numeric($request->age)){
|
||
return $this->failure('请选择年龄');
|
||
}
|
||
if(!$request->has('marital_status') || !is_numeric($request->marital_status)){
|
||
return $this->failure('请选择婚姻状态');
|
||
}
|
||
if(!$request->has('belief') || !is_numeric($request->belief)){
|
||
return $this->failure('请选择信仰');
|
||
}
|
||
|
||
|
||
try {
|
||
DB::beginTransaction();
|
||
$messenger = Messenger::where('viewer_id', $viewer->id)->where('is_audit', 2)->first();
|
||
if (empty($messenger)) {
|
||
$messenger = New Messenger();
|
||
}
|
||
$from_id = is_array($request->from_id) ? 0 : $request->from_id;
|
||
$messenger->viewer_id = $viewer->id;
|
||
$messenger->user_id = $viewer->user_id;
|
||
$messenger->share_viewer_id = $from_id;
|
||
$messenger->name = $request->name;
|
||
$messenger->sex = $request->sex;
|
||
$messenger->mobile = $request->mobile;
|
||
$messenger->age = $request->age;
|
||
$messenger->marital_status = $request->marital_status;
|
||
$messenger->belief = $request->belief;
|
||
$messenger->is_audit = 0;
|
||
$messenger->degree = $request->degree;
|
||
$messenger->recruit_type = $request->recruit_type;
|
||
|
||
$resident_place = $request->resident_place;
|
||
$messenger->resident_province = $resident_place[1];
|
||
$messenger->resident_city = $resident_place[2];
|
||
|
||
$work_place = $request->work_place;
|
||
$messenger->work_province = $work_place[1];
|
||
$messenger->work_city = $work_place[2];
|
||
if($request->has('platform') && $request->platform){
|
||
$messenger->source = $request->platform;
|
||
}
|
||
|
||
if($request->has('live_id') && is_numeric($request->live_id)){
|
||
$messenger->live_id = $request->live_id ?:0;
|
||
}
|
||
|
||
// if($request->has('task_id') && $request->task_id) {
|
||
// $task = Task::find($request->task_id);
|
||
// if (!empty($task)){
|
||
// if ($task->the_rest > 0 && $task->num && $task->type == '婚姻使者') {
|
||
// $messenger->task_id = $request->task_id;
|
||
// $task->decrement('the_rest');
|
||
// }
|
||
// }
|
||
// }
|
||
|
||
$messenger->save();
|
||
|
||
$mobile_has_viewer = Viewer::where('mobile', $request->mobile)->count();
|
||
$mobile_user = User::where('mobile', $request->mobile)->first();
|
||
if(empty($viewer->mobile) && empty($mobile_has_viewer) && !empty($mobile_user)){
|
||
$viewer->mobile = $request->mobile;
|
||
$viewer->user_id = $mobile_user->id;
|
||
$viewer->save();
|
||
}
|
||
DB::commit();
|
||
return $this->success('ok');
|
||
} catch (\Exception $e) {
|
||
DB::rollBack();
|
||
$this->getError($e);
|
||
return $this->failure("申请失败");
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 直播用户小程序码
|
||
*/
|
||
public function viewerMiniQrcide(Request $request, Viewer $viewer)
|
||
{
|
||
// if(empty($this->viewer)){
|
||
// $wechatUser = session('wechat.oauth_user.new');
|
||
// $openId = $wechatUser->getId();
|
||
// $this->viewer = Viewer::where('openid', $openId)->first();
|
||
// }
|
||
$user_id = 0;
|
||
$is_super_rank = 0;
|
||
$type = '';
|
||
if($viewer->mobile){
|
||
$user_id = User::where('mobile', $viewer->mobile)->value('id');
|
||
//用户小程序会员名称
|
||
$user = User::find($user_id);
|
||
$is_super_rank = $user->isSuperRank();
|
||
$type = $user->type;
|
||
}
|
||
$avatar = $viewer->avatar;
|
||
$mobile = $viewer->mobile;
|
||
$name = $viewer->nickname;
|
||
$openid = $viewer->openid;
|
||
$sex = $viewer->sex;
|
||
$type = !empty($user) ? $user->type : '';
|
||
if ($viewer->mini_qrcode) {
|
||
$mini_qrcode = $viewer->mini_qrcode;
|
||
return $this->success('ok', compact('mini_qrcode', 'user_id', 'is_super_rank','avatar', 'mobile', 'name', 'openid', 'sex', 'type'));
|
||
}
|
||
//生成二维码
|
||
$mini_qrcode = QrcodeRectService::getViewerMiniQrcode($user_id);
|
||
if (empty($mini_qrcode)) {
|
||
return $this->failure('获取二维码失败,请稍后再试');
|
||
}
|
||
|
||
$viewer->mini_qrcode = $mini_qrcode;
|
||
$viewer->save();
|
||
return $this->success('ok', compact('mini_qrcode', 'user_id', 'is_super_rank','avatar', 'mobile', 'name', 'openid', 'sex', 'type'));
|
||
}
|
||
|
||
//提现轮播
|
||
public function newWithdrawLogs(Request $request){
|
||
try {
|
||
$logs = AssetLog::with('viewer:id,nickname,avatar')->select('id', 'viewer_id', 'num', 'created_at')->where('type', 3)->where('cash_status', 2)->orderBy('created_at', 'desc')->limit(15)->get();
|
||
return $this->success('ok', $logs);
|
||
} catch (\Exception $e) {
|
||
\Log::error($e->getMessage());
|
||
return $this->failure("获取失败");
|
||
}
|
||
}
|
||
|
||
//问卷调查
|
||
public function question(Request $request, $live_id){
|
||
$user = auth()->user();
|
||
$viewer = Viewer::where('user_id', $user->id)->orWhere('mobile', $user->mobile)->first();
|
||
if(empty($viewer)){
|
||
return $this->failure('直播用户不存在');
|
||
}
|
||
|
||
if(!$request->star || !is_numeric($request->star)){
|
||
return $this->failure('请打分');
|
||
}
|
||
|
||
|
||
|
||
$question = New Question();
|
||
$question->viewer_id = $viewer->id;
|
||
$question->live_id = $live_id;
|
||
$question->star = $request->star;
|
||
$question->harvest = $request->harvest ?: '无';
|
||
$question->defect = $request->defect ?: '无';
|
||
$question->hope = $request->hope ?: '无';
|
||
$question->save();
|
||
|
||
return $this->success('ok');
|
||
}
|
||
|
||
//福恋使者页面配置
|
||
public function messengerSetting(Request $request){
|
||
$config = Redis::get('messenger_config');
|
||
$config = json_decode($config);
|
||
$log = Messenger::select('id','viewer_id', 'created_at')->with('viewer:id,nickname,avatar')->has('viewer')->where('is_audit', 1)->limit(30)->orderBy('created_at', 'desc')->get()->toArray();
|
||
$log = array_chunk($log, 3);
|
||
foreach ($log as $k => &$v){
|
||
$v = [
|
||
'class' => $v
|
||
];
|
||
}
|
||
$count = Messenger::count();
|
||
return $this->success('ok', compact('config', 'log', 'count'));
|
||
}
|
||
|
||
//点击用户头像,获取跳转小程序的二维码
|
||
public function miniQrcode(Request $request, $viewer_id){
|
||
$viewer = Viewer::find($viewer_id);
|
||
if(empty($viewer)){
|
||
return $this->failure('直播用户不存在');
|
||
}
|
||
if(empty($viewer->mobile)){
|
||
return $this->success('ok', '');
|
||
}
|
||
|
||
$user_id = User::where('mobile', $viewer->mobile)->value('id');
|
||
|
||
$key = sprintf('v_%d_u_%d', $viewer_id, $user_id);
|
||
$qrcode = Redis::get($key);
|
||
if(empty($qrcode)){
|
||
$url = 'pages/home/information?id='.$user_id.'&type=mini_userinfo';
|
||
$qrcode = $this->qrcodeWithScene($url);
|
||
Redis::setex($key, 3600*24*30, $qrcode);
|
||
}
|
||
return $this->success('ok', $qrcode);
|
||
|
||
}
|
||
|
||
public function remindRegist(Request $request, $viewer_id){
|
||
$viewer = Viewer::find($viewer_id);
|
||
if(empty($viewer)){
|
||
return $this->failure('直播用户不存在');
|
||
}
|
||
if(empty($viewer->mobile)){
|
||
return $this->failure('直播用户没有手机号');
|
||
}
|
||
|
||
|
||
$user_id = User::where('mobile', $viewer->mobile)->value('id');
|
||
if(empty($user_id)){
|
||
$this->sms->sentMessage($viewer->mobile, '有人提醒你注册福恋小程序,添加为好友');
|
||
return $this->failure('小程序用户不存在,已提醒该用户进行注册');
|
||
}
|
||
return $this->success('ok');
|
||
}
|
||
|
||
public function smsCountries(Request $request){
|
||
$sms_countries = SmsCountry::select('id', 'name', 'name_zh', 'phonecode')->get();
|
||
return $this->success('ok', $sms_countries);
|
||
}
|
||
|
||
public function approve(Request $request){
|
||
try {
|
||
DB::beginTransaction();
|
||
$user = auth()->user();
|
||
$has_audited = ApproveHistory::whereIn('status', [0, 1])->where('type', 'name')->count();
|
||
if ($has_audited) {
|
||
return $this->failure('您已申请过认证');
|
||
}
|
||
|
||
$front = $request->front;
|
||
if (empty($front)) {
|
||
return $this->failure('请上传身份证正面照');
|
||
}
|
||
$back = $request->back;
|
||
if (empty($back)) {
|
||
return $this->failure('请上传身份证反面照');
|
||
}
|
||
$identification_photos = json_encode([$front, $back]);
|
||
if ($user->type == 'single') {
|
||
$profile = ProfileCourtship::with('salary')->where('user_id', $user->id)->first();
|
||
$profile->identification_photos = $identification_photos;
|
||
$profile->save();
|
||
} else {
|
||
$profile = ProfileMarriage::where('user_id', $user->id)->first();
|
||
$profile->identification_photos = $identification_photos;
|
||
$profile->save();
|
||
}
|
||
$approve_history = New ApproveHistory();
|
||
$approve_history->user_id = $user->id;
|
||
$approve_history->save();
|
||
DB::commit();
|
||
return $this->success('ok');
|
||
} catch (\Exception $e) {
|
||
DB::rollback();
|
||
$this->getError($e);
|
||
return $this->failure('认证失败');
|
||
}
|
||
}
|
||
|
||
public function addRemark(Request $request){
|
||
try{
|
||
DB::beginTransaction();
|
||
$user = auth()->user();
|
||
|
||
$activity_user = ActivityUser::where('user_id', $request->user_id)->where('follow_user_id', $user->id)->where('status', 2)->first();
|
||
if(empty($activity_user)){
|
||
return $this->failure('您不是该用户的跟进人');
|
||
}
|
||
|
||
$remark = New ActivityUserRemark();
|
||
$remark->user_id = $request->user_id;
|
||
$remark->remark_user_id = $user->id;
|
||
$remark->content = $request->input('content');
|
||
$remark->save();
|
||
|
||
// $activity_user->status = $request->status;
|
||
// switch ($request->status){
|
||
// case 1:
|
||
// $activity_user->status = 1;
|
||
// break;
|
||
// case 2:
|
||
// $activity_user->follow_user_id = 0;
|
||
// $activity_user->status = 0;
|
||
// default:
|
||
// break;
|
||
// }
|
||
// $activity_user->save();
|
||
DB::commit();
|
||
return $this->success('ok');
|
||
} catch (\Exception $e) {
|
||
DB::rollback();
|
||
$this->getError($e);
|
||
return $this->failure('备注失败');
|
||
}
|
||
|
||
}
|
||
|
||
//标记跟进用户
|
||
public function remarkActivityUser(Request $request){
|
||
try{
|
||
DB::beginTransaction();
|
||
$user = auth()->user();
|
||
if($request->status == 1){
|
||
ActivityUser::where('follow_user_id', $user->id)->where('type', 7)->whereIn('user_id', $request->id)->whereIn('status', [0,2])->update(['status'=>1,'updated_at'=>date('Y-m-d H:i:s')]);
|
||
DB::table('activity_user_log')->where('follow_user_id', $user->id)->whereIn('user_id', $request->id)->where('status', 2)->update(['status'=>1,'updated_at'=>date('Y-m-d H:i:s')]);
|
||
}
|
||
|
||
DB::commit();
|
||
return $this->success('ok');
|
||
} catch (\Exception $e) {
|
||
DB::rollback();
|
||
$this->getError($e);
|
||
return $this->failure('标记失败');
|
||
}
|
||
|
||
}
|
||
|
||
// //获取自己的活跃用户
|
||
// public function getActivityUser(Request $request){
|
||
// $user = auth()->user();
|
||
// $viewer = Viewer::where('user_id', $user->id)->orWhere('mobile', $user->mobile)->first();
|
||
// if(!$viewer){
|
||
// return $this->failure('不是电访专员');
|
||
// }
|
||
// if($viewer->is_follower != 1){
|
||
// return $this->failure('不是电访专员');
|
||
// }
|
||
// //查询未处理的活跃用户
|
||
// $activity_user_id = ActivityUser::where('follow_user_id', $user->id)->where('status', 2)->where('type', 7)->limit(15)->pluck('user_id')->toArray();
|
||
// $add_activity_user_id = [];
|
||
// if(empty($activity_user_id)||count($activity_user_id)<15){
|
||
// //未满15条数据则填充
|
||
// $num = 15-count($activity_user_id);
|
||
// //查询上月沟通过的用户
|
||
// // $start = date('Y-m-01', strtotime('-1 month'));
|
||
// // $f_user = DB::table('activity_user_log')->where('status',1)->whereBetween('created_at',[$start,now()])->pluck('user_id')->toArray();
|
||
// //已经分配的用户
|
||
// // $a_user = ActivityUser::where('type', 7)->pluck('user_id')->toArray();
|
||
// $a_user = DB::table('activity_user_log')->orderBy('user_id','asc')->pluck('user_id')->toArray();
|
||
// //屏蔽id;
|
||
// $black_user = User::FULLLINKIDS;
|
||
// $blacklist = array_merge($a_user,$black_user);
|
||
// // $blacklist = array_merge($f_user,$a_user,$black_user);
|
||
// //查询活跃会员
|
||
// // $add_activity_user_id = User::where('belief','基督教')
|
||
// // ->where('is_real_approved',0)
|
||
// // ->wherenotin('id',$blacklist)
|
||
// // ->where('type','single')
|
||
// // ->where('hidden_profile','NONE')
|
||
// // ->orderBy('last_visit','desc')->limit($num)->pluck('id')->toArray();
|
||
// $min_birth = '1980-01-01 00:00:00';
|
||
// $max_birth = '1999-12-31 23:59:59';//挑选80 90后
|
||
// $add_activity_user_id = User::where('belief','like','%基督%')
|
||
// ->wherenotin('id',$blacklist)
|
||
// // ->where('type','single')
|
||
// // ->where('hidden_profile','NONE')
|
||
// ->whereHas('profileCourtship',function($sql) use($min_birth,$max_birth){
|
||
// $sql->where('city','like','%深圳%')->whereBetWeen('birthday',[$min_birth,$max_birth]);
|
||
// })
|
||
// ->orderBy('last_visit','desc')->limit($num)->pluck('id')->toArray();
|
||
// foreach ( $add_activity_user_id as $val){
|
||
// $arr = [
|
||
// 'user_id' => $val,
|
||
// 'follow_user_id' => $user->id,
|
||
// 'created_at' => date('Y-m-d H:i:s'),
|
||
// 'updated_at' => date('Y-m-d H:i:s'),
|
||
// ];
|
||
// $activity = [
|
||
// 'user_id' => $val,
|
||
// 'follow_user_id' => $user->id,
|
||
// 'status'=>2,
|
||
// 'type'=>7,
|
||
// 'created_at' => date('Y-m-d H:i:s'),
|
||
// 'updated_at' => date('Y-m-d H:i:s'),
|
||
// ];
|
||
// $insert[] = $arr;
|
||
// $insert_activity[] = $activity;
|
||
|
||
// }
|
||
// DB::table('activity_user')->insert($insert_activity);
|
||
// DB::table('activity_user_log')->insert($insert);
|
||
// }
|
||
// $activity_user_id = array_merge($activity_user_id,$add_activity_user_id);
|
||
// $users = User::with('profileCourtship')->withCount('userRemark as remark_count')->whereDate('created_at','<=','2021-05-01 00:00:00')->whereIn('id', $activity_user_id)->get();
|
||
// foreach ($users as $user){
|
||
// $user->city = !empty($user->profileCourtship) ? $user->profileCourtship->city : '未填写';
|
||
// }
|
||
// return $this->success('ok', $users);
|
||
// }
|
||
//获取自己的活跃用户
|
||
public function getActivityUser(Request $request){
|
||
$user = auth()->user();
|
||
$viewer = Viewer::where('user_id', $user->id)->orWhere('mobile', $user->mobile)->first();
|
||
if(!$viewer){
|
||
return $this->failure('不是电访专员');
|
||
}
|
||
if($viewer->is_follower != 1){
|
||
return $this->failure('不是电访专员');
|
||
}
|
||
//查询未处理的活跃用户
|
||
if(Cache::has('activity_user_id'.$user->id)){
|
||
$activity_user_id = Cache::get('activity_user_id'.$user->id);
|
||
}else{
|
||
$activity_user_id = ActivityUser::where('follow_user_id', $user->id)->where('status', 2)->where('type', 7)->limit(5)->pluck('user_id')->toArray();
|
||
Cache::put('activity_user_id'.$user->id,$activity_user_id,60*60*24*7);
|
||
}
|
||
$users = User::with('profileCourtship')->withCount('userRemark as remark_count')->whereDate('created_at','<=','2021-05-01 00:00:00')->whereIn('id', $activity_user_id)->get();
|
||
foreach ($users as $user){
|
||
$user->city = !empty($user->profileCourtship) ? $user->profileCourtship->city : '未填写';
|
||
}
|
||
return $this->success('ok', $users);
|
||
$add_activity_user_id = [];
|
||
if(empty($activity_user_id)||count($activity_user_id)<15){
|
||
//未满15条数据则填充
|
||
$num = 15-count($activity_user_id);
|
||
//查询上月沟通过的用户
|
||
// $start = date('Y-m-01', strtotime('-1 month'));
|
||
// $f_user = DB::table('activity_user_log')->where('status',1)->whereBetween('created_at',[$start,now()])->pluck('user_id')->toArray();
|
||
//已经分配的用户
|
||
// $a_user = ActivityUser::where('type', 7)->pluck('user_id')->toArray();
|
||
$a_user = DB::table('activity_user_log')->orderBy('user_id','asc')->pluck('user_id')->toArray();
|
||
//屏蔽id;
|
||
$black_user = User::FULLLINKIDS;
|
||
$blacklist = array_merge($a_user,$black_user);
|
||
// $blacklist = array_merge($f_user,$a_user,$black_user);
|
||
//查询活跃会员
|
||
// $add_activity_user_id = User::where('belief','基督教')
|
||
// ->where('is_real_approved',0)
|
||
// ->wherenotin('id',$blacklist)
|
||
// ->where('type','single')
|
||
// ->where('hidden_profile','NONE')
|
||
// ->orderBy('last_visit','desc')->limit($num)->pluck('id')->toArray();
|
||
$min_birth = '1980-01-01 00:00:00';
|
||
$max_birth = '1999-12-31 23:59:59';//挑选80 90后
|
||
$add_activity_user_id = User::where('belief','like','%基督%')
|
||
->wherenotin('id',$blacklist)
|
||
// ->where('type','single')
|
||
// ->where('hidden_profile','NONE')
|
||
->whereHas('profileCourtship',function($sql) use($min_birth,$max_birth){
|
||
$sql->where('city','like','%深圳%')->whereBetWeen('birthday',[$min_birth,$max_birth]);
|
||
})
|
||
->orderBy('last_visit','desc')->limit($num)->pluck('id')->toArray();
|
||
foreach ( $add_activity_user_id as $val){
|
||
$arr = [
|
||
'user_id' => $val,
|
||
'follow_user_id' => $user->id,
|
||
'created_at' => date('Y-m-d H:i:s'),
|
||
'updated_at' => date('Y-m-d H:i:s'),
|
||
];
|
||
$activity = [
|
||
'user_id' => $val,
|
||
'follow_user_id' => $user->id,
|
||
'status'=>2,
|
||
'type'=>7,
|
||
'created_at' => date('Y-m-d H:i:s'),
|
||
'updated_at' => date('Y-m-d H:i:s'),
|
||
];
|
||
$insert[] = $arr;
|
||
$insert_activity[] = $activity;
|
||
|
||
}
|
||
DB::table('activity_user')->insert($insert_activity);
|
||
DB::table('activity_user_log')->insert($insert);
|
||
}
|
||
$activity_user_id = array_merge($activity_user_id,$add_activity_user_id);
|
||
$users = User::with('profileCourtship')->withCount('userRemark as remark_count')->whereDate('created_at','<=','2021-05-01 00:00:00')->whereIn('id', $activity_user_id)->get();
|
||
foreach ($users as $user){
|
||
$user->city = !empty($user->profileCourtship) ? $user->profileCourtship->city : '未填写';
|
||
}
|
||
return $this->success('ok', $users);
|
||
}
|
||
|
||
//清除我的未完成活跃用户
|
||
public function deleteActivityUsers(Request $request){
|
||
$follow_user_ids = ActivityUser::pluck('follow_user_id')->toArray();
|
||
$follow_user_ids = array_unique($follow_user_ids);
|
||
$follow_log_user_ids = DB::table('activity_user_log')->pluck('follow_user_id')->toArray();
|
||
$follow_log_user_ids = array_unique($follow_log_user_ids);
|
||
// dd($follow_user_ids,$follow_user_ids);
|
||
foreach ($follow_user_ids as $key => $follow_user_id) {
|
||
//查询未处理的活跃用户
|
||
ActivityUser::where('follow_user_id', $follow_user_id)->where('status', 2)->where('type', 7)->delete();
|
||
DB::table('activity_user_log')->where('follow_user_id',$follow_user_id)->where('status',2)->delete();
|
||
}
|
||
return $this->success('ok');
|
||
}
|
||
|
||
//活跃用户备注列表
|
||
public function activityUserRemark(Request $request){
|
||
$remark = ActivityUserRemark::where('user_id', $request->user_id)->get();
|
||
return $this->success('ok', $remark);
|
||
}
|
||
|
||
public function addActivityUser(Request $request){
|
||
// addActivityUser::dispatch()->onQueue('love');
|
||
$activity_user_ids = ActivityUser::pluck('user_id')->toArray();
|
||
$users = User::where('is_approved', 1)->whereNotIn('id', $activity_user_ids)->get();
|
||
$insert_arr = [];
|
||
$new_arr = [];
|
||
|
||
foreach ($users as $user){
|
||
$new_arr['user_id'] = $user->id;
|
||
$new_arr['type'] = 4;
|
||
$new_arr['created_at'] = date('Y-m-d H:i:s');
|
||
$new_arr['updated_at'] = date('Y-m-d H:i:s');
|
||
$insert_arr[] = $new_arr;
|
||
}
|
||
ActivityUser::insert($insert_arr);
|
||
}
|
||
|
||
//标语征集首页
|
||
public function opusHome(Request $request){
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(empty( $wechatUser)){
|
||
$openId = $request->openid;
|
||
}else{
|
||
$openId = $wechatUser->getId();
|
||
}
|
||
$viewer_id = Viewer::where('openid', $openId)->value('id');
|
||
|
||
$beginToday=date('Y-m-d');
|
||
$endToday=date("Y-m-d",strtotime("+1 day"));
|
||
$today_ballot_count = Ballot::whereBetween('created_at', [$beginToday, $endToday])->where('viewer_id', $viewer_id)->count();
|
||
|
||
$today_ballot_count = 3-$today_ballot_count;
|
||
$opus_count = Opus::where('is_audit', 1)->count();
|
||
$ballot_count = Ballot::count();
|
||
$pv = Opus::where('is_audit', 1)->sum('pv');
|
||
$is_subscribe = $this->is_subscribe($openId);
|
||
return $this->success('ok', compact('opus_count', 'ballot_count', 'pv', 'today_ballot_count', 'is_subscribe'));
|
||
}
|
||
|
||
//作品列表
|
||
public function opusList(Request $request){
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(empty( $wechatUser)){
|
||
$openId = $request->openid;
|
||
}else{
|
||
$openId = $wechatUser->getId();
|
||
}
|
||
$viewer_id = Viewer::where('openid', $openId)->value('id');
|
||
|
||
$list = Opus::with('viewer')->where('is_audit', 1);
|
||
if($request->keyword) {
|
||
$keyword = $request->keyword;
|
||
$list = $list->where(function ($query) use ($keyword) {
|
||
$query->where('name', 'like','%'.$keyword.'%')->orWhere('number', $keyword);
|
||
});
|
||
// $list = $list->whereHas('viewer', function($query) use ($keyword){
|
||
// $query->whereLike('nickname', $keyword);
|
||
// });
|
||
}
|
||
|
||
$list = $list->orderBy('id', 'desc')->paginate();
|
||
|
||
foreach ($list as $val){
|
||
$rank = Opus::where('ballot_count', '>', $val->ballot_count)->count();
|
||
$val->rank = $rank+1;
|
||
$val->can_ballot = $this->can_ballot($val->id, $viewer_id);
|
||
}
|
||
return $this->success('ok', $list);
|
||
}
|
||
|
||
//排行榜
|
||
public function opusRank(Request $request){
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(empty( $wechatUser)){
|
||
$openId = $request->openid;
|
||
}else{
|
||
$openId = $wechatUser->getId();
|
||
}
|
||
$viewer_id = Viewer::where('openid', $openId)->value('id');
|
||
|
||
|
||
$list = Opus::with('viewer')->where('ballot_count', '>', 0)->where('is_audit', 1);
|
||
|
||
if($request->keyword) {
|
||
$keyword = $request->keyword;
|
||
$list = $list->where(function ($query) use ($keyword) {
|
||
$query->where('name', 'like','%'.$keyword.'%')->orWhere('number', $keyword);
|
||
});
|
||
// $list = $list->whereHas('viewer', function($query) use ($keyword){
|
||
// $query->whereLike('nickname', $keyword);
|
||
// });
|
||
}
|
||
$list = $list->orderBy('ballot_count', 'desc')->orderBy('id', 'desc')->limit(15)->get();
|
||
foreach ($list as $val){
|
||
$val->can_ballot = $this->can_ballot($val->id, $viewer_id);
|
||
}
|
||
return $this->success('ok', $list);
|
||
}
|
||
|
||
|
||
//报名参加标语征集
|
||
public function joinOpus(Request $request){
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(empty( $wechatUser)){
|
||
$openId = $request->openid;
|
||
}else{
|
||
$openId = $wechatUser->getId();
|
||
}
|
||
$viewer_id = Viewer::where('openid', $openId)->value('id');
|
||
|
||
$opus_count = Opus::where('viewer_id', $viewer_id)->where('is_audit', '!=', 2)->count();
|
||
if($opus_count >= 3){
|
||
return $this->failure('作品数不能超过3个');
|
||
}
|
||
|
||
if(!$request->name){
|
||
return $this->failure('姓名不能为空');
|
||
}
|
||
if(!$request->mobile){
|
||
return $this->failure('手机号不能为空');
|
||
}
|
||
if(!$request->code){
|
||
return $this->failure('验证码不能为空');
|
||
}
|
||
if(!$request->input('content') || mb_strlen($request->input('content')) > 50){
|
||
return $this->failure('内容不能为空或者长度不能超过50');
|
||
}
|
||
|
||
//验证手机号
|
||
$result = $this->sms->check($request->mobile, $request->code);
|
||
if ($result) {
|
||
return $this->failure($result);
|
||
}
|
||
|
||
$opus = New Opus();
|
||
$opus->viewer_id = $viewer_id;
|
||
$opus->name = $request->name;
|
||
$opus->mobile = $request->mobile;
|
||
$opus->content = $request->input('content');
|
||
$opus->explan = $request->explan;
|
||
$opus->save();
|
||
|
||
return $this->success('ok');
|
||
}
|
||
|
||
//征集详情
|
||
public function opus(Request $request, $opus_id){
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(empty( $wechatUser)){
|
||
$openId = $request->openid;
|
||
}else{
|
||
$openId = $wechatUser->getId();
|
||
}
|
||
$viewer_id = Viewer::where('openid', $openId)->value('id');
|
||
|
||
$opus = Opus::with('viewer')->withCount(('ballot'))->where('id', $opus_id)->first();
|
||
if(empty($opus)){
|
||
return $this->success('作品不存在');
|
||
}
|
||
$rank = Opus::where('ballot_count', '>', $opus->ballot_count)->count();
|
||
$opus->rank = $rank+1;
|
||
$opus->can_ballot = $this->can_ballot($opus_id, $viewer_id);
|
||
$opus->increment('pv', 1);
|
||
|
||
//关注公众号二维码
|
||
$qrcode = Redis::get($opus_id.'_opus_qrcode');
|
||
//如果已过期,重新生成
|
||
if(!$qrcode){
|
||
if(config('app.env') == 'local'){
|
||
$url = 'http://alpha.ufutx.net/h5/#/voteDetails/'.$opus_id;
|
||
}
|
||
if (config('app.env') == 'alpha') {
|
||
$url = 'http://alpha.ufutx.net/h5/#/voteDetails/'.$opus_id;
|
||
}
|
||
if(config('app.env') == 'production'){
|
||
$url = env('APP_URL').'/h5/#/voteDetails/'.$opus_id;
|
||
}
|
||
$qrcode = $this->qrcodeWithScene($url);
|
||
Redis::setex($opus_id.'_opus_qrcode', 30 * 24 * 3600, $qrcode);
|
||
}
|
||
$opus->qrcode = $qrcode;
|
||
|
||
return $this->success('ok', $opus);
|
||
}
|
||
|
||
//投票
|
||
public function toBallot(Request $request, $opus_id){
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(empty( $wechatUser)){
|
||
$openId = $request->openid;
|
||
}else{
|
||
$openId = $wechatUser->getId();
|
||
}
|
||
return $this->failure('投票已经终止,敬请期待下次活动');
|
||
$viewer_id = Viewer::where('openid', $openId)->value('id');
|
||
try{
|
||
DB::beginTransaction();
|
||
//限制请求次数
|
||
$key = $viewer_id.'_to_ballot';
|
||
$is_request = Redis::get($key);
|
||
if($is_request){
|
||
return $this->failure('请求频率过快');
|
||
}
|
||
Redis::setex($key, 2, 1);
|
||
|
||
$beginToday=date('Y-m-d');
|
||
$endToday=date("Y-m-d",strtotime("+1 day"));
|
||
$ballot_count = Ballot::whereBetween('created_at', [$beginToday, $endToday])->where('viewer_id', $viewer_id)->count();
|
||
if($ballot_count >= 3){
|
||
return $this->failure('您今天已经投过3票了');
|
||
}
|
||
$has_baoolt = Ballot::whereBetween('created_at', [$beginToday, $endToday])->where('viewer_id', $viewer_id)->where('opus_id', $opus_id)->count();
|
||
if($has_baoolt){
|
||
return $this->failure('您今天已经给作品投过票了');
|
||
}
|
||
|
||
$opus = Opus::find($opus_id);
|
||
if($opus->is_audit != 1){
|
||
return $this->failure('只能给审核通过的作品投票');
|
||
}
|
||
$opus->increment('ballot_count', 1);
|
||
|
||
$ballot = New Ballot();
|
||
$ballot->viewer_id = $viewer_id;
|
||
$ballot->opus_id = $opus_id;
|
||
$ballot->save();
|
||
DB::commit();
|
||
return $this->success('ok');
|
||
} catch (\Exception $e) {
|
||
DB::rollback();
|
||
$this->getError($e);
|
||
}
|
||
}
|
||
|
||
//我发布的征集作品
|
||
public function myOpus(Request $request){
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(empty( $wechatUser)){
|
||
$openId = $request->openid;
|
||
}else{
|
||
$openId = $wechatUser->getId();
|
||
}
|
||
$viewer_id = Viewer::where('openid', $openId)->value('id');
|
||
|
||
$opus = Opus::withCount('ballot')->where('viewer_id', $viewer_id)->get();
|
||
foreach ($opus as $val){
|
||
$rank = Opus::where('ballot_count', '>', $val->ballot_count)->count();
|
||
$val->rank = $rank+1;
|
||
// $val->created_at = date('Y-m-d H:i', strtotime($val->created_at));
|
||
}
|
||
return $this->success('ok', $opus);
|
||
}
|
||
|
||
//是否投票过
|
||
public function can_ballot($opus_id, $viewer_id){
|
||
|
||
$beginToday=date('Y-m-d');
|
||
$endToday=date("Y-m-d",strtotime("+1 day"));
|
||
// $ballot_count = Ballot::whereBetween('created_at', [$beginToday, $endToday])->where('viewer_id', $viewer_id)->count();
|
||
// if($ballot_count > 3){
|
||
// return 0;
|
||
// }
|
||
$has_baoolt = Ballot::whereBetween('created_at', [$beginToday, $endToday])->where('opus_id', $opus_id)->where('viewer_id', $viewer_id)->count();
|
||
if($has_baoolt){
|
||
return 0;
|
||
}
|
||
return 1;
|
||
}
|
||
|
||
|
||
//是否关注公众号
|
||
public function is_subscribe($openid){
|
||
$config = [
|
||
'app_id' => config('wechat.official_account.new.app_id'),
|
||
'secret' => config('wechat.official_account.new.secret'),
|
||
];
|
||
$app = Factory::officialAccount($config);
|
||
$wechatUser = $app->user->get($openid);
|
||
$result = !empty($wechatUser)&&isset($wechatUser['subscribe']);
|
||
$is_subscribe = $result?$wechatUser['subscribe']:0;
|
||
return $is_subscribe;
|
||
}
|
||
|
||
//发表文章
|
||
public function addArticle(Request $request){
|
||
$user_id = auth()->id();
|
||
$viewer_id = Viewer::where('user_id', $user_id)->value('id');
|
||
$is_messenger = Messenger::where('viewer_id', $viewer_id)->where('is_audit', 1)->count();
|
||
if(empty($is_messenger)){
|
||
return $this->failure('成为福恋使者以后才能发表文章');
|
||
}
|
||
|
||
if(!$request->title){
|
||
return $this->failure('请输入标题');
|
||
}
|
||
if(!$request->input('content')){
|
||
return $this->failure('请输入内容');
|
||
}
|
||
if(!$request->pic){
|
||
return $this->failure('请输入图片');
|
||
}
|
||
|
||
$article = New Article();
|
||
$article->title = $request->title;
|
||
$article->content = $request->input('content');
|
||
$article->pic = $request->pic;
|
||
$article->user_id = $user_id;
|
||
$article->is_original = empty($request->source) ? 1 : 0;
|
||
$article->source = $request->source;
|
||
$article->save();
|
||
return $this->success('ok');
|
||
}
|
||
|
||
//文章列表
|
||
public function articles(Request $request){
|
||
|
||
$user = auth()->user();
|
||
if (empty($user)) {
|
||
$user = $this->authCheck();
|
||
}
|
||
$articles = Article::select('id', 'title', 'content', 'pic', 'created_at', 'pv', 'is_audit', 'reason', 'is_original');
|
||
if($request->keyword){
|
||
$keyword = $request->keyword;
|
||
$articles = $articles->where(function ($query) use ($keyword){
|
||
$query->where('title', 'Like', '%'.$keyword.'%')->where('content', 'Like', '%'.$keyword.'%');
|
||
});
|
||
}
|
||
if(is_numeric($request->is_audit)){
|
||
$articles = $articles->where('is_audit', $request->is_audit);
|
||
}
|
||
if(is_numeric($request->user_id)){
|
||
$articles = $articles->where('user_id', $request->user_id);
|
||
}
|
||
$articles = $articles->orderBy('id', 'desc')->paginate();
|
||
foreach ($articles as $article){
|
||
//评论数
|
||
$article->comment_count = $article->totalCommentsCount();
|
||
$is_self = 0;
|
||
if(!empty($user)){
|
||
if($user->id = $article->user_id){
|
||
$is_self = 1;
|
||
}
|
||
}
|
||
$article->is_self = $is_self;
|
||
}
|
||
return $this->success('ok', $articles);
|
||
}
|
||
|
||
//我的文章列表
|
||
public function myArticles(Request $request){
|
||
$user = auth()->user();
|
||
$articles = Article::select('id', 'title', 'content', 'pic', 'created_at', 'pv', 'is_audit', 'reason', 'is_original')->where('user_id', $user->id);
|
||
if($request->keyword){
|
||
$articles = $articles->where('title', 'Like', '%'.$request->keyword.'%');
|
||
}
|
||
if(is_numeric($request->is_audit)){
|
||
$articles = $articles->where('is_audit', $request->is_audit);
|
||
}
|
||
$articles = $articles->orderBy('id', 'desc')->paginate();
|
||
foreach ($articles as $article){
|
||
//评论数
|
||
$article->comment_count = $article->totalCommentsCount();
|
||
$is_self = 0;
|
||
if(!empty($user)){
|
||
if($user->id = $article->user_id){
|
||
$is_self = 1;
|
||
}
|
||
}
|
||
$article->is_self = $is_self;
|
||
}
|
||
return $this->success('ok', $articles);
|
||
}
|
||
|
||
//文章详情
|
||
public function article(Request $request, $article_id){
|
||
$user = auth()->user();
|
||
if (empty($user)) {
|
||
$user = $this->authCheck();
|
||
}
|
||
$article = Article::with('user')->find($article_id);
|
||
|
||
// //是否点赞
|
||
if (empty($user)) {
|
||
$isLkerArticle = 0;
|
||
$is_self = 0;
|
||
}else{
|
||
$isLkerArticle = $user->hasLiked($article) ?1:0;
|
||
$is_self = $article->user_id == $user->id?1:0;
|
||
}
|
||
$article->is_self = $is_self;
|
||
$article->isLkerArticle = $isLkerArticle;
|
||
//点赞数
|
||
$article->ArticleLikerCount = $article->likers()->get()->count()?:0;
|
||
//点赞人列表
|
||
$article->likers = $article->likers->all();
|
||
//是否收藏
|
||
if (empty($user)) {
|
||
$article->is_favorite = 0;
|
||
}else{
|
||
$article->is_favorite = $user->hasFavorited($article)?1:0;
|
||
}
|
||
//收藏数
|
||
$article->favoriteCount = $article->favoriters()->count()?:0;
|
||
if(empty($article->user->photo)){
|
||
$article->user->photo = $article->user->circle_avatar;
|
||
}
|
||
|
||
$has_read = UserArticle::where('user_id', $user->id)->where('article_id', $article_id)->count();
|
||
if(empty($has_read)){
|
||
$user_article = New UserArticle();
|
||
$user_article->user_id = $user->id;
|
||
$user_article->article_id = $article_id;
|
||
$user_article->save();
|
||
|
||
Article::where('id', $article_id)->increment('pv');
|
||
}
|
||
return $this->success('ok', $article);
|
||
}
|
||
|
||
//评论列表
|
||
public function comments(Request $request, $article_id){
|
||
$article = Article::with('user')->find($article_id);
|
||
$comments = $article->comments()->orderBy('id', 'desc')->paginate();
|
||
foreach ($comments as $comment){
|
||
$comment->user = User::select('id','photo','name', 'circle_avatar', 'nickname')->where('id', $comment->commented_id)->orderBy('id', 'desc')->first();
|
||
if(empty($comment->user->photo)){
|
||
$comment->user->photo = $comment->user->circle_avatar;
|
||
}
|
||
if(empty($comment->user->name)){
|
||
$comment->user->name = $comment->user->nickname;
|
||
}
|
||
$date = $comment->created_at->toDateTimeString();
|
||
$comment->time = $this->changeTime($date);
|
||
}
|
||
return $this->success('ok', $comments);
|
||
}
|
||
//点赞文章
|
||
public function likeArticle(Request $request, $article_id){
|
||
$user = auth()->user();
|
||
$article = Article::find($article_id);
|
||
if ($user->hasLiked($article)) {
|
||
$user->unlike($article);
|
||
}else{
|
||
$user->like($article);
|
||
}
|
||
return $this->success('ok');
|
||
}
|
||
|
||
//收藏文章
|
||
public function favoriteArticle(Request $request, $article_id)
|
||
{
|
||
$user = auth()->user();
|
||
$article = Article::find($article_id);
|
||
if ($user->hasFavorited($article)) {
|
||
$user->unfavorite($article);
|
||
}else{
|
||
$user->favorite($article);
|
||
}
|
||
return $this->success('ok');
|
||
}
|
||
|
||
//评论文章
|
||
public function commentArticle(Request $request, $article_id){
|
||
$article = Article::find($article_id);
|
||
if($article->is_audit != 1){
|
||
return $this->failure('未发布的文章不能评论');
|
||
}
|
||
|
||
$comment = $request->input('comment');
|
||
if (empty($comment)) {
|
||
return $this->failure('请输入评论内容');
|
||
}
|
||
$user = auth()->user();
|
||
$user->comment($article, $comment);
|
||
return $this->success('ok');
|
||
}
|
||
|
||
//首页推荐
|
||
public function homeHot(Request $request){
|
||
$user = auth()->user();
|
||
if (empty($user)) {
|
||
$user = $this->authCheck();
|
||
}
|
||
$lives = Live::where('recommend', 1)->get();
|
||
$stars = CommunityStar::select('id', 'user_id', 'created_at')->with(['user:id,photo,name,circle_avatar,nickname,app_avatar,is_approved,is_real_approved', 'profile'])->where('is_audit', 1)->where('is_show', 1)->orderBy('sort', 'desc')->orderBy('audited_at', 'desc')->get();
|
||
$articles = Article::where('is_hot', 1)->get();
|
||
foreach ($stars as $star){
|
||
if(empty($star->user->photo)){
|
||
$star->user->photo = empty($star->user->app_avatar) ? "https://images.ufutx.com/202007/01/e0de60525143427d4dd19515a5b387ba.png" :$star->user->app_avatar;
|
||
}
|
||
$star->user->isSuperRank = $star->user->isSuperRank();
|
||
if(empty($star->profile->age)){
|
||
$star->profile->age = $this->getAge($star->profile->birthday);
|
||
}
|
||
}
|
||
foreach ($lives as $live){
|
||
$live->pv = Redis::zscore('page_pv', $live->id);
|
||
}
|
||
foreach ($articles as $article){
|
||
$is_self = 0;
|
||
if(!empty($user)){
|
||
if($user->id = $article->user_id){
|
||
$is_self = 1;
|
||
}
|
||
}
|
||
$article->is_self = $is_self;
|
||
}
|
||
$communities = Community::withCount('members')->where('is_hot', 1)->get();
|
||
foreach ($communities as $community){
|
||
$community->members = $community->members()->with('user')->where('status', 1)->limit(4)->get();
|
||
}
|
||
$banner = Redis::get('home_banner');
|
||
$banner = json_decode($banner);
|
||
return $this->success('ok', compact('lives', 'stars', 'communities', 'articles', 'banner'));
|
||
}
|
||
|
||
//直播指南 福恋直播平台
|
||
//筛选直播
|
||
|
||
public function screeningLives(Request $request){
|
||
$anchors = Anchor::where('is_show',1)->where('status',2)->orderBy('sort','desc')->select('id','name','sort')->get();
|
||
|
||
$start_time = $request->start_time.' 00:00:00';
|
||
$anchor_id = $request->anchor_id;
|
||
$lives = Live::with('teacher')->where('is_show',1);
|
||
if ($start_time) {
|
||
$end_time = date('Y-m-d 00:00:00',strtotime('+1 months',strtotime($start_time)));
|
||
$live_0 = Live::where('is_show',1)->where('status',0)->whereBetween('start_time',[$start_time,$end_time])->orderBy('start_time','asc')->pluck('id')->toArray();//未开播
|
||
$live_1 = Live::where('is_show',1)->where('status',1)->whereBetween('start_time',[$start_time,$end_time])->orderBy('start_time','desc')->pluck('id')->toArray();//直播中
|
||
$live_2 = Live::where('is_show',1)->where('status',2)->whereBetween('start_time',[$start_time,$end_time])->orderBy('start_time','desc')->pluck('id')->toArray();//已结束
|
||
$array = array_merge($live_1,$live_0);
|
||
$final_array = array_merge($array,$live_2);
|
||
$str = implode(',',$final_array);
|
||
$lives = $lives->whereBetween('start_time',[$start_time,$end_time]);
|
||
}
|
||
if ($anchor_id) {
|
||
$lives = $lives->where('anchor_id',$anchor_id);
|
||
}
|
||
$lives = $lives->where('id', '!=', 108)->orderByRaw(DB::raw('FIELD(id,'.$str.') asc'))->paginate();
|
||
foreach ($lives as $live) {
|
||
$s_time = date('H:i', strtotime($live->start_time));
|
||
$e_time = date('H:i', strtotime($live->end_time));
|
||
$live->during = $s_time.'~'.$e_time;
|
||
}
|
||
return $this->success('ok',compact('lives','anchors'));
|
||
}
|
||
// public function screeningLives(Request $request){
|
||
// $anchors = Anchor::where('is_show',1)->where('status',2)->orderBy('sort','desc')->select('id','name','sort')->get();
|
||
// $start_time = $request->start_time.' 00:00:00';
|
||
// $anchor_id = $request->anchor_id;
|
||
// $lives = Live::with('teacher')->where('is_show',1);
|
||
// if ($start_time) {
|
||
// // $end_time = date('Y-m-d 00:00:00',strtotime('+1 day',strtotime($start_time)));
|
||
|
||
// $end_time = date('Y-m-d 00:00:00',strtotime('+1 months',strtotime($start_time)));
|
||
// $lives = $lives->whereBetween('start_time',[$start_time,$end_time]);
|
||
// }
|
||
// if ($anchor_id) {
|
||
// $lives = $lives->where('anchor_id',$anchor_id);
|
||
// }
|
||
// $lives = $lives->where('id', '!=', 108)->orderByRaw(DB::raw('FIELD(status, 1,3,0,2) asc'))->orderBy('start_time','desc')->paginate();
|
||
// foreach ($lives as $live) {
|
||
// $s_time = date('H:i', strtotime($live->start_time));
|
||
// $e_time = date('H:i', strtotime($live->end_time));
|
||
// $live->during = $s_time.'~'.$e_time;
|
||
// }
|
||
// return $this->success('ok',compact('lives','anchors'));
|
||
// }
|
||
|
||
//获取老师最近一场直播时间
|
||
public function getNearestLive(Request $request){
|
||
$anchor_id = $request->anchor_id;
|
||
$date = date('Y-m-d H:i:s');
|
||
$times = strtotime($date);//当前时间 时间戳
|
||
// dd($times);
|
||
$lives = Live::where('anchor_id',$anchor_id)->orderBy('id','desc')->pluck('start_time')->toArray();
|
||
|
||
if(empty($lives)) {
|
||
$live_ids = Live::whereNotNull('start_time')->pluck('id')->toArray();
|
||
// dd($live_ids[0]);
|
||
$live = Live::where('id',$live_ids[0])->select('id','start_time')->first();
|
||
$live->id = 0;
|
||
$live->start_time = '';
|
||
$live->is_subscribe = $live->is_subscribe = $this->getSubscribeStatus($request->openid);;
|
||
return $this->success('ok',$live);
|
||
}
|
||
// dd($lives);
|
||
if(!empty($lives)){
|
||
$num = [];
|
||
foreach ($lives as $key => $live) {
|
||
$time = strtotime($live); //每个直播开始时间 时间戳
|
||
$num[] = $times-$time; //当前时间减去 每个直播开始时间
|
||
}
|
||
}
|
||
// dd($num);
|
||
if(!empty($num)){
|
||
$t = [];
|
||
foreach ($num as $n) {
|
||
$n = abs($n);//取绝对值最小值 即为最近一场直播
|
||
$t[] = $n;
|
||
}
|
||
}
|
||
// dd($t);
|
||
$key = array_search(min($t),$t);
|
||
// dd($key);
|
||
// dd($lives[$key]);
|
||
$live = Live::where('start_time',$lives[$key])->select('id','start_time')->first();
|
||
$live->start_time = date('Y-m-d',strtotime($live->start_time));
|
||
$live->is_subscribe = $live->is_subscribe = $this->getSubscribeStatus($request->openid);
|
||
return $this->success('ok',$live);
|
||
|
||
}
|
||
|
||
public function createWyyUser($data)
|
||
{
|
||
$im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
|
||
$result = $im_service->createUserId($data['openid'],$data['nickname'],$props='{}',null);
|
||
//\Log::info('创建网易云账号');
|
||
//\Log::info($result);
|
||
if ($result['code'] == 200) {
|
||
$wyyUser = new WangYiYunUser;
|
||
$wyyUser->accid = $data['openid'];
|
||
$wyyUser->name = $data['nickname'];
|
||
$wyyUser->gender = $data['sex'];
|
||
$wyyUser->token = $result['info']['token'];
|
||
$wyyUser->save();
|
||
}elseif ($result['code'] == 414 && $result['desc'] == 'already register') {
|
||
|
||
$result = $im_service->getUinfos([$data['openid']]);
|
||
if ($result['code'] == 200) {
|
||
$info = $result['uinfos'][0];
|
||
$accid = '';
|
||
$name = '';
|
||
$gender = '';
|
||
if(array_key_exists("accid",$info)){
|
||
$accid = $info['accid'];
|
||
}
|
||
if(array_key_exists("name",$info)){
|
||
$name = $info['name'];
|
||
}
|
||
if(array_key_exists("gender",$info)){
|
||
$gender = $info['gender'];
|
||
}
|
||
$wyyUser = new WangYiYunUser;
|
||
$wyyUser->accid = $accid;
|
||
$wyyUser->name = $name;
|
||
$wyyUser->gender = $gender;
|
||
$wyyUser->save();
|
||
}
|
||
}
|
||
}
|
||
|
||
/** sass系统分享二维码 */
|
||
public function sassShareQrcode(Request $request)
|
||
{
|
||
try {
|
||
$openid = $request->openid;
|
||
if (empty($openid)) return $this->failure('未获取到用户信息');
|
||
$is_subscribe = $this->getSubscribeStatus($openid);
|
||
$plat = $request->input('plat');
|
||
if ($plat == 'love') {//福恋sass
|
||
$url = env('APP_URL')."/api/official/live/wechat/oauth?from_openid=".$openid."&url=https%3A%2F%2Flove.ufutx.com%2Fh5%2F%23%2FrecruitingTeacher";
|
||
} elseif ($plat == 'ufutx') {//友福同享
|
||
$url = "http://h5.fulllinkai.com/#/creatapp?from_openid=".$openid;
|
||
} else {
|
||
return $this->failure("请选择服务平台");
|
||
}
|
||
$key = $plat.':ssas:share:qrcode:'.$openid;
|
||
$qrcode = Redis::get($key);
|
||
if (empty($qrcode)){
|
||
$pic_path = storage_path('qrcode/'.time().'sasss_sharer_qrcode.png');
|
||
//二维码图片
|
||
QrCode::format('png')->margin(1)->size(300)->generate($url, $pic_path);
|
||
$qrcode = '';
|
||
if(file_exists($pic_path)){
|
||
$qrcode = $this->uploadFile($pic_path);
|
||
Redis::set($key, $qrcode);
|
||
try{
|
||
unlink($pic_path);
|
||
}catch(\Exception $e) {
|
||
throw new \Exception("删除图片路径失败");
|
||
}
|
||
}
|
||
}
|
||
$official_qrcode = '';
|
||
if(empty($is_subscribe)){
|
||
$official_key = 'sass_official_qrcode';
|
||
$official_qrcode = Redis::get($official_key);
|
||
if (empty($official_qrcode)) {
|
||
$official_qrcode = $this->qrcodeWithScene($official_key);
|
||
}
|
||
|
||
}
|
||
return $this->success('ok', compact('qrcode', 'is_subscribe', 'official_qrcode'));
|
||
} catch (\Exception $e) {
|
||
$this->getError($e);
|
||
return $this->failure('服务器开小差。请稍后再试');
|
||
}
|
||
}
|
||
}
|