love_php/app/Models/MerchantAccount.php
2026-04-02 09:20:51 +08:00

76 lines
2.6 KiB
PHP

<?php
namespace App\Models;
use App\Models\Live\Anchor;
use App\Models\Live\Viewer;
use App\Models\Server\MerchantFollow;
use Illuminate\Database\Eloquent\Model;
use function GuzzleHttp\Promise\is_settled;
class MerchantAccount extends Model
{
protected $fillable = ['mobile','password','openid','type','remark','created_at','updated_at','qr_code','share_icon','share_title','share_subtitle'];
//服务列表
public const EARNINGSERVICE = ['service', 'activity', 'course', 'consult','evaluate', 'shop'];
public function anchor(){
return $this->hasOne(Anchor::class,'openid','openid')->select('name','openid','pic','mobile');
}
public function anchorV2(){
return $this->hasOne(Anchor::class,'m_id','id')->select('id','m_id','name','openid','pic','mobile','channel');
}
public function liveAnchor(){
return $this->hasOne(Anchor::class,'m_id','id');
}
public function follow(){
return $this->hasOne(MerchantFollow::class,'merchant_id','id')->orderBy('id','desc');
}
public function info(){
return $this->hasOne(MerchantInfo::class,'m_id','id');
}
public static function get_token($token)
{
try {
// $token = $request->bearerToken();
$result = decrypt($token);
if(!$result)
return false;
$key = explode('-', $result);
if(time()-$key[2]>604800)
return false;
if($key[0]){
$account_id = $key[0];
return $account_id;
}
} catch (\Exception $e) {
return false;
}
}
// 推广人数
public static function PromotionIncome($merchant_id)
{
// 用户人数
$ids = AccessRecord::where('account_id',$merchant_id)->pluck('open_id');
$sql = AccessRecord::orderBy('id','asc')->limit(1000);
$result = AccessRecord::from(\DB::raw('('.$sql->toSql().') as a'))->wherein('open_id',$ids)->groupBy('open_id')->get();
$user = [];
foreach ($result as $key => $value) {
if($value->account_id==$merchant_id){
$user[$value['open_id']]=$value->created_at->toDateTimeString();
}
}
$user_key = array_keys($user);
$viewer = Viewer::wherein('openid',$user_key)->get();
foreach ($viewer as $key => $value) {
if (!isset($user[$value['openid']])) continue;
if($value->created_at<$user[$value['openid']]){
unset( $user[$value['openid']]);
}
}
return count($user);
}
}