love_php/app/Models/Server/MerchantAccount.php

323 lines
11 KiB
PHP
Raw Permalink Normal View History

2026-04-02 09:20:51 +08:00
<?php
namespace App\Models\Server;
use App\Contracts\Collageable;
use App\Models\AnchorVideo;
use App\Models\Live\Live;
use App\Traits\CanCollage;
use Illuminate\Database\Eloquent\Model;
use App\Http\Response\ResponseJson;
use App\Models\CommunityActivity;
use App\Models\Consultation;
use App\Models\Course\Course;
use App\Models\Server\MEarning;
use App\Models\Server\MEarningAccount;
use App\Models\Live\Anchor;
use App\Models\MerchantInfo;
use App\Models\MerchantShop;
use App\Models\QATest;
use App\Services\IMService;
use App\Models\WangYiYunUser;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
class MerchantAccount extends Model implements Collageable
{
use ResponseJson;
use CanCollage;
public const TLHY_MERCHANT_ID = 1094; //天路合一id
//增加收益记录
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 const MERCHANTKEY = 'merchant:account:';
public function addEarning($type, $amount, $order_id)
{
try{
\DB::beginTransaction();
//获取账号
$account = $this->getMEarningAccount();
//增加收益记录
switch($type){
case 'reward':
$earning = MEarning::create([
'm_id'=>$account->m_id,
'm_user_id'=>0,
'm_order_id'=>$order_id,
'type'=>'reward',
'sharer'=>'merchant',
'ratio'=>1,
'sub_ratio'=>1,
'value'=>$amount,
'status'=>'finished',
]);
break;
case 'transfer':
$earning = MEarning::create([
'm_id'=>$account->m_id,
'm_user_id'=>0,
'm_order_id'=>$order_id,
'type'=>'transfer',
'sharer'=>'merchant',
'ratio'=>1,
'sub_ratio'=>1,
'value'=>$amount,
'status'=>'finished',
]);
break;
default:
\DB::rollback();
return false;
}
//修改余额
$account->increment('balance', $amount);
$account->increment('total_value', $amount);
\DB::commit();
return $earning;
}catch(\Exception $e){
\DB::rollback();
$this->getError($e);
return false;
}
}
public function getMEarningAccount()
{
$m_id = $this->id;
$account = MEarningAccount::firstOrCreate(['m_id'=> $m_id, 'm_user_id'=>0]);
return $account;
}
public function anchor(){
return $this->hasOne(Anchor::class,'openid','openid')->select('id','name','openid','pic','mobile');
}
public function anchorV2(){
return $this->hasOne(Anchor::class,'m_id','id')->select('id','m_id','name','openid','pic','mobile','channel','designation');
}
public function follow(){
return $this->hasOne(MerchantFollow::class,'merchant_id','id')->orderBy('id','desc');
}
public function information(){
return $this->hasOne(MerchantInformation::class,'merchant_id','id');
}
public function service(){
return $this->hasOne(CommunityActivity::class,'merchant_id','id')->where('class','many')->where('type','business');
}
public function activity(){
return $this->hasOne(CommunityActivity::class,'merchant_id','id')->where('class','one')->where('type','business');
}
public function course(){
return $this->hasOne(Course::class,'merchant_id','id')->where('type','business');
}
public function test(){
return $this->hasOne(QATest::class,'merchant_id','id');
}
public function shop(){
return $this->hasOne(MerchantShop::class,'merchant_id','id');
}
public function info()
{
return $this->hasOne(MerchantInfo::class,'m_id','id');
}
public function video(){
return $this->hasOne(AnchorVideo::class,'m_id','id');
}
public function enterprise(){
return $this->hasOne(EnterpriseSettlement::class,'m_id','id')->select('id','name','mobile','pic','address','industry');
}
public function orders() {
return $this->hasMany(TouristOrder::class, 'merchant_id', 'id');
}
public function servicesStatus()
{
// $service_types = ['service','activity','course','evaluate','consult','test','shop'];
//服务图标是否显示
$service_statuses = MerchantServiceStatus::where('m_id', $this->id)->select('m_id', 'type', 'is_show')->orderBy('sort','asc')->get();
if($service_statuses->count() == 0){
$show_services = [
'is_show_activity' => 0,
'is_show_consult' => 0,
'is_show_course' => 0,
'is_show_evaluate' => 0,
'is_show_service' => 0,
'is_show_shop' => 0,
'is_show_test' => 0,
];
}else {
// $show_services = [];
// foreach ($service_statuses as $status) {
// $show_services['is_show_' . $status->type] = MerchantServiceStatus::where('m_id', $this->id)
// ->where('type', $status->type)->where('is_show', 1)
// ->first() ? 1 : 0;
// }
$show_services = [
'is_show_activity' => 0,
'is_show_consult' => 0,
'is_show_course' => 0,
'is_show_evaluate' => 0,
'is_show_service' => 0,
'is_show_shop' => 0,
'is_show_test' => 0,
];
$typeList = MerchantServiceStatus::where("m_id", $this->id)->where("is_show", 1)->pluck("type")->toArray();
$list = [
["key"=>"activity", "is_show"=>0],
["key"=>"course", "is_show"=>0],
["key"=>"evaluate", "is_show"=>0],
["key"=>"service", "is_show"=>0],
["key"=>"shop", "is_show"=>0],
["key"=>"test", "is_show"=>0],
["key"=>"consult", "is_show"=>0],
];
foreach($list as &$item) {
if (in_array($item["key"], $typeList)) {
$show_services["is_show_".$item["key"]] = 1;
}
}
}
return $show_services;
}
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 createIMUser($merchant)
{
$im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
$result = $im_service->createUserId($merchant['accid'],$merchant['name'],$props='{}',$merchant['pic']);
if ($result['code'] == 200) {
$IMUser = WangYiYunUser::where('accid',$merchant['accid'])->first();
if (empty($IMUser)) {
$wyyUser = new WangYiYunUser();
$wyyUser->accid = $merchant['accid'];
$wyyUser->name = $merchant['name'];
$wyyUser->icon = $merchant['pic'];
$wyyUser->mobile = $merchant['mobile'];
$wyyUser->gender = 0;
$wyyUser->token = $result['info']['token'];
$wyyUser->save();
}
}elseif ($result['code'] == 414 && $result['desc'] == 'already register') {
$IMUser = WangYiYunUser::where('accid',$merchant['accid'])->first();
if (empty($IMUser)) {
$result = $im_service->getUinfos([$merchant['accid']]);
if ($result['code'] == 200) {
$info = $result['uinfos'][0];
$name = '';
$gender = '';
$icon = '';
if(array_key_exists("name",$info)){
$name = $info['name'];
}
if(array_key_exists("gender",$info)){
$gender = $info['gender'];
}
if(array_key_exists("icon",$info)){
$icon = $info['icon'];
}
$wyyUser = new WangYiYunUser();
$wyyUser->anchor_id = $merchant['anchor_id'];
$wyyUser->accid = $merchant['accid'];
$wyyUser->name = $name;
$wyyUser->gender = $gender;
$wyyUser->icon = $icon;
//更新网易云token
$result = $im_service->updateUserToken($merchant['accid']);
if ($result['code'] == 200) {
$wyyUser->token = $result['info']['token'];
}
$wyyUser->save();
}
}
}
return $wyyUser;
}
public function earnings()
{
return $this->hasMany(MEarning::class, 'm_id', 'id');
}
public function blacklist()
{
return $this->hasMany(MerchantBlacklist::class, 'm_id', 'id');
}
public static function getRedisMerchant($id=null, $openid=null)
{
if ($id) {
$key = self::MERCHANTKEY.$id;
$data = ['id'=>$id];
}else {
$key = self::MERCHANTKEY.$openid;
$data = ['openid'=>$openid];
}
if (Cache::has($key)) {
$merchant = Cache::get($key);
}else {
$merchant = self::where($data)->first();
Cache::put($key, $merchant, 24 *60);
}
return $merchant;
}
/**
* 同步缓存
* @param $id
* @return void
*/
public static function syncRedisMerchant($id)
{
if(empty($id)){
return;
}
$merchant = self::where('id',$id)->first();
if(!$merchant){
return;
}
$key = self::MERCHANTKEY.$id;
Cache::put($key, $merchant, 24 *60);
}
public static function payType($merchnat_id)
{
$applyment = MerchantApplyment::where('m_id', $merchnat_id)->first();
if($applyment && $applyment->state == 1 && $applyment->sub_mch_id) return ["partner", $applyment->sub_mch_id];
return ['wechat', null];
}
}