|  | @@ -0,0 +1,164 @@
 | 
	
		
			
				|  |  | +<?php
 | 
	
		
			
				|  |  | +/**
 | 
	
		
			
				|  |  | + *
 | 
	
		
			
				|  |  | + * @file:WechatMinprogramUserService.php
 | 
	
		
			
				|  |  | + * @Date: 2023/5/19
 | 
	
		
			
				|  |  | + * @Time: 11:07
 | 
	
		
			
				|  |  | + */
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +namespace Modules\Channel\Services\WechatMinprogram;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +use Carbon\Carbon;
 | 
	
		
			
				|  |  | +use Illuminate\Pagination\LengthAwarePaginator;
 | 
	
		
			
				|  |  | +use Illuminate\Support\Facades\DB;
 | 
	
		
			
				|  |  | +use Illuminate\Support\Facades\Redis;
 | 
	
		
			
				|  |  | +use Modules\Channel\Models\MiniprogramUserVip;
 | 
	
		
			
				|  |  | +use Modules\Channel\Models\Order;
 | 
	
		
			
				|  |  | +use Modules\Channel\Models\UidLogs;
 | 
	
		
			
				|  |  | +use Modules\Channel\Models\Videos;
 | 
	
		
			
				|  |  | +use Modules\Channel\Models\VideoSeries;
 | 
	
		
			
				|  |  | +use Modules\Manage\Models\Miniprogram;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +class WechatMinprogramUserService
 | 
	
		
			
				|  |  | +{
 | 
	
		
			
				|  |  | +    public const WATCH_RECORD_REDIS_KEY = 'watchrecord:uid:%s';
 | 
	
		
			
				|  |  | +    public const WATCH_RECORD_REDIS_FIELD_PREFIX = 'video_id:';
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     *  获取微信小程序用户信息
 | 
	
		
			
				|  |  | +     * name: userInfoDetail
 | 
	
		
			
				|  |  | +     * @param $uid
 | 
	
		
			
				|  |  | +     * date 2023/05/19 11:08
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static function userInfoDetail($uid)
 | 
	
		
			
				|  |  | +    {
 | 
	
		
			
				|  |  | +        $userInfo = DB::table(getMiniProgramTableName(1, 'users'))->where('id', $uid)->first();
 | 
	
		
			
				|  |  | +        if (!$userInfo) {
 | 
	
		
			
				|  |  | +            return $userInfo;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        $ju_chang = Miniprogram::where('id', $userInfo->miniprogram_id)->value('play_name');
 | 
	
		
			
				|  |  | +        $result = [
 | 
	
		
			
				|  |  | +            'uid' => $userInfo->id,
 | 
	
		
			
				|  |  | +            'openid' => $userInfo->openid,
 | 
	
		
			
				|  |  | +            'yu_chang' => $ju_chang,
 | 
	
		
			
				|  |  | +            'ranse_start_at' => $userInfo->ranse_start_at ?: "",
 | 
	
		
			
				|  |  | +            'charge_coin' => $userInfo->charge_coin,
 | 
	
		
			
				|  |  | +            'reward_coin' => $userInfo->reward_coin,
 | 
	
		
			
				|  |  | +        ];
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return array_merge($result, self::getLevelText($uid));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private static function getLevelText($uid)
 | 
	
		
			
				|  |  | +    {
 | 
	
		
			
				|  |  | +        $record = self::userVipRecord($uid);
 | 
	
		
			
				|  |  | +        if ($record && Carbon::now()->lt(Carbon::createFromTimestamp(strtotime($record->end_time)))) {
 | 
	
		
			
				|  |  | +            return ['is_vip' => 1, 'vip_text' => "vip会员", 'vip_end' => $record->end_time];
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return ['is_vip' => 0, 'vip_text' => "-", 'vip_end' => ""];
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public static function isVipUser(int $uid)
 | 
	
		
			
				|  |  | +    {
 | 
	
		
			
				|  |  | +        $record = self::userVipRecord($uid);
 | 
	
		
			
				|  |  | +        if (!$record) {
 | 
	
		
			
				|  |  | +            return false;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return Carbon::now()->lt(Carbon::createFromTimestamp(strtotime($record->end_time)));
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private static function userVipRecord(int $uid)
 | 
	
		
			
				|  |  | +    {
 | 
	
		
			
				|  |  | +        return MiniprogramUserVip::where('uid', $uid)->first();
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     *  根据用户查充值记录
 | 
	
		
			
				|  |  | +     * name: getUserOrderList
 | 
	
		
			
				|  |  | +     * @param int $uid
 | 
	
		
			
				|  |  | +     * date 2023/05/19 14:57
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static function getUserOrderList(int $uid)
 | 
	
		
			
				|  |  | +    {
 | 
	
		
			
				|  |  | +        $pool = [
 | 
	
		
			
				|  |  | +            'MONTH' => '包月',
 | 
	
		
			
				|  |  | +            'QUARTER' => '包季度',
 | 
	
		
			
				|  |  | +            'YEAR' => '包年',
 | 
	
		
			
				|  |  | +        ];
 | 
	
		
			
				|  |  | +        $list = Order::join('pay_products', 'pay_products.id', '=', 'orders.pay_product_id')
 | 
	
		
			
				|  |  | +            ->select('orders.price', 'orders.trade_no', "orders.video_id", 'orders.pay_end_at', 'pay_products.type', 'pay_products.price as product_price', 'pay_products.given')
 | 
	
		
			
				|  |  | +            ->where('orders.status', 'PAID')
 | 
	
		
			
				|  |  | +            ->where('orders.uid', $uid)
 | 
	
		
			
				|  |  | +            ->orderBy('orders.id', 'desc')
 | 
	
		
			
				|  |  | +            ->simplePaginate(15);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        foreach ($list as $item) {
 | 
	
		
			
				|  |  | +            $item->pay_name = '微信支付';
 | 
	
		
			
				|  |  | +            $item->status = '已完成';
 | 
	
		
			
				|  |  | +            if ($item->type == 'COIN') {
 | 
	
		
			
				|  |  | +                $item->rechare_coin = $item->product_price * 100;
 | 
	
		
			
				|  |  | +                $item->pay_result = $item->product_price * 100 + $item->given;
 | 
	
		
			
				|  |  | +            } elseif (isset($pool[$item->type])) {
 | 
	
		
			
				|  |  | +                $item->rechare_coin = "-";
 | 
	
		
			
				|  |  | +                $item->pay_result = $pool[$item->type];
 | 
	
		
			
				|  |  | +            } else {
 | 
	
		
			
				|  |  | +                $item->rechare_coin = "-";
 | 
	
		
			
				|  |  | +                $item->pay_result = '充值';
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            $item->from_page = $item->video_id > 0 ? "播放页" : "充值页";
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return $list;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     *  观看记录
 | 
	
		
			
				|  |  | +     * name: getUserWatchRecord
 | 
	
		
			
				|  |  | +     * @param mixed $uid 用户id
 | 
	
		
			
				|  |  | +     * @return array
 | 
	
		
			
				|  |  | +     * date 2023/05/19 15:57
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static function getUserWatchRecord($uid)
 | 
	
		
			
				|  |  | +    {
 | 
	
		
			
				|  |  | +        $key = sprintf(self::WATCH_RECORD_REDIS_KEY, $uid);
 | 
	
		
			
				|  |  | +        $record = Redis::hgetall($key);
 | 
	
		
			
				|  |  | +        $result = [];
 | 
	
		
			
				|  |  | +        foreach ($record as $video_field => $watch_info) {
 | 
	
		
			
				|  |  | +            if (!Str::startsWith($video_field, self::WATCH_RECORD_REDIS_FIELD_PREFIX)) {
 | 
	
		
			
				|  |  | +                continue;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            $video_id = Str::replace(self::WATCH_RECORD_REDIS_FIELD_PREFIX, '', $video_field);
 | 
	
		
			
				|  |  | +            $info = explode('_', $watch_info);
 | 
	
		
			
				|  |  | +            $result[] = [
 | 
	
		
			
				|  |  | +                'video_id' => $video_id,
 | 
	
		
			
				|  |  | +                'video_series_sequence' => $info[0],
 | 
	
		
			
				|  |  | +                'watch_at' => get_date($info[1]),
 | 
	
		
			
				|  |  | +                'watch_time' => $info[1]
 | 
	
		
			
				|  |  | +            ];
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        usort($result, function ($item1, $item2) {
 | 
	
		
			
				|  |  | +            return $item1['watch_time'] > $item2['watch_time'];
 | 
	
		
			
				|  |  | +        });
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return $result;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public static function getUserConsumeRecord(mixed $uid)
 | 
	
		
			
				|  |  | +    {
 | 
	
		
			
				|  |  | +        $tableName = 'coin_cost_record_' . ($uid % 8);
 | 
	
		
			
				|  |  | +        $result = DB::table($tableName)->where('uid', $uid)->orderBy('id', 'desc')->simplePaginate();
 | 
	
		
			
				|  |  | +        foreach ($result as $item) {
 | 
	
		
			
				|  |  | +            $item->series_name = VideoSeries::where('video_id', $item->video_id)->where('series_sequence', $item->sequence)->select('series_name')->first()->series_name;
 | 
	
		
			
				|  |  | +            $item->video_name = Videos::where('id', $item->video_id)->value('name');
 | 
	
		
			
				|  |  | +            $item->coin_cost = $item->charge_coin_cost + $item->reward_coin_cost;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return $result;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +}
 |