Explorar o código

Merge branch 'test' of qk:zy_duanju/duanju_manage into test

liuzejian hai 1 ano
pai
achega
897b345253

+ 18 - 0
app/Libs/Helpers.php

@@ -343,3 +343,21 @@ if (!function_exists("random")) {
         return $hash;
     }
 }
+
+if(!function_exists('getMiniProgramTableName')){
+    /**
+     *  获取小程序表名  表名前添加前缀:wechat_miniprogram_ 或者 douyin_miniprogram_
+     * name: getMiniProgramTableName
+     * @param int $from 来源 1 微信小程序
+     * @param string $name 表名
+     * @return string
+     * date 2023/05/19 09:18
+     */
+    function getMiniProgramTableName(int $from, string $name): string
+    {
+        return sprintf('%s_%s', [
+            '1' => 'wechat_miniprogram',
+            '2' => 'douyin_miniprogram'
+        ][$from],$name);
+    }
+}

+ 83 - 7
modules/Channel/Http/Controllers/PayTemplateController.php

@@ -76,6 +76,9 @@ class PayTemplateController extends CatchController
         $option_cache = [];
         $type_list = collect($this->optionTypeList())->pluck('value');
         foreach($option_list as $option){
+            if(!is_numeric($option['price']) || !is_numeric($option['given'])){
+                ChannelBusinessException::throwError(Errors::PARAM_ERROR);
+            }
             if($option['type'] == 'COIN' || $option['type'] == 'FIRST_COIN'){
                 if($option['given'] > 3*$option['price']*100){
                     ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_GIVEN_TOO_MUCH);
@@ -126,7 +129,8 @@ class PayTemplateController extends CatchController
      */
     public function show($id)
     {
-        $uid = $this->getLoginUser()->id;
+        //$uid = $this->getLoginUser()->id;
+        $uid= 1;
         if(UserService::userHasRole($uid,'administrator')){
             $uid = 0;
         }
@@ -197,6 +201,7 @@ class PayTemplateController extends CatchController
 
         if($status == 1){
             $this->payTemplate->where('uid',$uid)->update(['status'=>0]);
+            $this->payTemplate->where('id',$id)->update(['status'=>1]);
         }
         if($template_info_update){
             $exists->save();
@@ -262,6 +267,67 @@ class PayTemplateController extends CatchController
 
 
     /**
+     * 编辑单条选项
+     *
+     * @param [type] $id
+     * @param Request $request
+     * @return void
+     */
+    public function updatePayTemplateItem($id,Request $request){
+        $info = $this->payTemplateItem->find($id);
+        $price = $request->post('price');
+        $type = $request->post('type');
+        if(empty($price) || empty($type)){
+            ChannelBusinessException::throwError(Errors::PARAM_EMPTY);
+        }
+        $given = $request->post('given',0);
+        if($given > 3*$price*100){
+            ChannelBusinessException::throwError(Errors::PAY_TEMPLATE_GIVEN_TOO_MUCH);
+        }
+
+        $sequence = $request->post('sequence',0);
+        $is_default = $request->post('is_default',0);
+        $is_first_pay = 0;
+        if($type == 'FIRST_COIN'){
+            $type = 'COIN';
+            $is_first_pay = 1;
+        }
+        $product_info = $this->getPayProduct($price,$type,$given);
+        if($info->pay_product_id == $product_info->id){
+            $info->is_first_pay = $is_first_pay; 
+            $info->is_default = $is_default;
+            $info->save();
+        }else{
+            $info->status = 0;
+            $info->save();
+            $data = [
+                'pay_template_id'=>$info->pay_template_id,'pay_product_id'=>$product_info->id,
+                'is_first_pay'=>$is_first_pay,'is_default'=>$is_default,
+                'status'=>1,'sequence'=>$sequence,'created_at'=>Carbon::now(),'updated_at'=>Carbon::now()  
+            ];
+            $this->payTemplateItem->insert($data);
+        }
+
+        return [];
+    }
+
+
+
+    /**
+     * 删除单条选项
+     *
+     * @param [type] $id
+     * @return void
+     */
+    public function deleteOneItem($id){
+        $info = $this->payTemplateItem->find($id);
+        $info->status = 0;
+        $info->save();
+        return [];
+    }
+
+
+    /**
      * 更新模板状态
      *
      * @param int $id
@@ -319,14 +385,24 @@ class PayTemplateController extends CatchController
     }
 
 
+
+    public function optionTypeListOutPut(){
+        return collect($this->optionTypeList())->map(function($item){
+            return [
+                'type'=>$item['value'],'type_name'=>$item['name']
+            ];
+        })->all();
+    }
+
+
     public function optionSequence(){
         return [
-            ['name'=>'选项一','value'=>1],
-            ['name'=>'选项二','value'=>2],
-            ['name'=>'选项三','value'=>3],
-            ['name'=>'选项四','value'=>4],
-            ['name'=>'选项五','value'=>5],
-            ['name'=>'选项六','value'=>6]
+            ['sequence_text'=>'位置一','sequence'=>1],
+            ['sequence_text'=>'位置二','sequence'=>2],
+            ['sequence_text'=>'位置三','sequence'=>3],
+            ['sequence_text'=>'位置四','sequence'=>4],
+            ['sequence_text'=>'位置五','sequence'=>5],
+            ['sequence_text'=>'位置六','sequence'=>6]
         ];
     }
 }

+ 109 - 0
modules/Channel/Http/Controllers/WechatMinprogramUserController.php

@@ -0,0 +1,109 @@
+<?php
+/**
+ * 微信小程序用户
+ * @file:WechatMinprogramUserController.php
+ * @Date: 2023/5/19
+ * @Time: 10:24
+ */
+
+
+namespace Modules\Channel\Http\Controllers;
+
+use Catch\Base\CatchController;
+use Illuminate\Http\Request;
+use Catch\Exceptions\FailedException;
+use Modules\Channel\Services\WechatMinprogram\WechatMinprogramUserService;
+
+class WechatMinprogramUserController extends CatchController
+{
+
+    /***
+     *  获取用户信息
+     * name: userInfoDetail
+     * @param $uid
+     * @return array|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder|object
+     * date 2023/05/19 14:13
+     */
+    public function userInfoDetail($uid)
+    {
+
+        if ($uid < 1) {
+           $this->$this->errorMsg('缺少微信小程序用户id参数');
+        }
+        $userInfo = WechatMinprogramUserService::userInfoDetail($uid);
+        if (!$userInfo) {
+          $this->errorMsg('用户不存在');
+        }
+        return $userInfo;
+    }
+
+    /**
+     *  订单记录
+     * name: orderList
+     * @param Request $request
+     * date 2023/05/19 14:14
+     */
+    public function orderList(Request $request)
+    {
+        $uid = $request->input('uid');
+        if ($uid <  1){
+            $this->errorMsg("用户参数必填");
+        }
+
+        return WechatMinprogramUserService::getUserOrderList($uid);
+
+    }
+
+    /***
+     *  观看记录
+     * name: watchRecord
+     * @param Request $request
+     * date 2023/05/19 14:18
+     */
+    public  function watchRecord  (Request $request)
+    {
+        $uid = $request->input('uid');
+        if ($uid <  1){
+            $this->errorMsg("用户参数必填");
+        }
+
+        $list =  WechatMinprogramUserService::getUserWatchRecord($uid);
+        return $this->pageWithArray($request,$list,$request->input('limit',15));
+    }
+
+    /**
+     *  消费记录
+     * name: consumeRecord
+     * @param Request $request
+     * date 2023/05/19 14:19
+     */
+    public  function consumeRecord (Request $request)
+    {
+        $uid = $request->input('uid');
+        if ($uid <  1){
+            $this->errorMsg("用户参数必填");
+        }
+        return  WechatMinprogramUserService::getUserConsumeRecord($uid);
+    }
+
+    protected function errorMsg($msg,$code = 10005){
+        throw new FailedException($msg,$code );
+    }
+
+    /**
+     *  数组分页
+     * name: pageWithArray
+     * @param $request
+     * @param $array
+     * @param int $limit
+     * @return \Illuminate\Pagination\LengthAwarePaginator
+     * date 2023/05/19 16:44
+     */
+   protected function pageWithArray($request,$array,$limit = 15){
+        $currentPage = \Illuminate\Pagination\LengthAwarePaginator::resolveCurrentPage();
+        $itemCollection = collect($array);
+        $currentPageItems = $itemCollection->slice(($currentPage*$limit)-$limit,$limit)->all();
+        return new \Illuminate\Pagination\LengthAwarePaginator($currentPageItems,count($itemCollection),$limit);
+    }
+
+}

+ 14 - 0
modules/Channel/Models/MiniprogramUserVip.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace Modules\Channel\Models;
+
+
+class MiniprogramUserVip extends BaseModel
+{
+    protected $table = 'miniprogram_user_vips';
+
+    protected $fillable = [
+        'id', 'uid', 'start_time', 'end_time', 'created_at', 'updated_at',
+    ];
+
+}

+ 14 - 0
modules/Channel/Models/Order.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace Modules\Channel\Models;
+
+
+class Order extends BaseModel
+{
+    protected $table = 'orders';
+
+    protected $fillable = [
+        'id', 'uid', 'promotion_id', 'user_id', 'price', 'pay_product_id', 'create_ip', 'status', 'trade_no', 'transaction_id', 'third_orderid', 'order_type', 'pay_merchant_source', 'video_id', 'video_series_sequence', 'pay_end_at', 'updated_at', 'created_at',
+    ];
+
+}

+ 13 - 0
modules/Channel/Models/UidLogs.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace Modules\Channel\Models;
+
+class UidLogs extends \Modules\Common\Models\BaseModel
+{
+    protected $table = 'uid_logs';
+
+    protected $fillable = [
+        'id', 'use_for',
+    ];
+
+}

+ 15 - 0
modules/Channel/Models/VideoSeries.php

@@ -0,0 +1,15 @@
+<?php
+
+namespace Modules\Channel\Models;
+
+
+
+class VideoSeries extends BaseModel
+{
+    protected $table = 'video_series';
+
+    protected $fillable = [
+        'id', 'video_id', 'series_name', 'series_sequence', 'is_enabled', 'video_key', 'created_at', 'updated_at', 'duration',
+    ];
+
+}

+ 13 - 0
modules/Channel/Models/Videos.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace Modules\Channel\Models;
+
+class Videos extends BaseModel
+{
+    protected $table = 'videos';
+
+    protected $fillable = [
+        'id', 'name', 'category_id', 'd_charge_sequence', 'd_charge_coin', 'd_charge_type', 'shelf_type', 'shelf_at', 'total_episode_num', 'updated_episode_num', 'update_type', 'cp_name', 'cp_share_type', 'cover_image', 'created_at', 'updated_at', 'd_zan_num',
+    ];
+
+}

+ 164 - 0
modules/Channel/Services/WechatMinprogram/WechatMinprogramUserService.php

@@ -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;
+    }
+
+
+}

+ 13 - 4
modules/Channel/routes/route.php

@@ -4,8 +4,6 @@ use Illuminate\Support\Facades\Route;
 use Modules\Channel\Http\Controllers\AdvertiserController;
 use Modules\Channel\Http\Controllers\PayTemplateController;
 use Modules\Channel\Http\Controllers\UserMiniprogramController;
-use Modules\System\Http\Controllers\NoticesController;
-use Modules\System\Http\Controllers\NoticeTypesController;
 
 Route::prefix('channel')->group(function () {
     Route::post('advertiser/add', [AdvertiserController::class, 'addAdvertiser']);
@@ -19,15 +17,26 @@ Route::prefix('channel')->group(function () {
 
     //支付模板
     Route::prefix('paytemplate')->group(function(){
-        Route::get('optionTypeList',[PayTemplateController::class,'optionTypeList'])->withoutMiddleware(config('catch.route.middlewares'));
+        Route::get('optionTypeList',[PayTemplateController::class,'optionTypeListOutPut'])->withoutMiddleware(config('catch.route.middlewares'));
         Route::get('optionSequence',[PayTemplateController::class,'optionSequence'])->withoutMiddleware(config('catch.route.middlewares'));
         Route::get('list',[PayTemplateController::class,'index']);
         Route::get('show/{id}',[PayTemplateController::class,'show']);
         Route::post('store',[PayTemplateController::class,'store']);
         Route::post('update/{id}',[PayTemplateController::class,'update']);
         Route::post('updateStatus/{id}',[PayTemplateController::class,'updateStatus']);//->withoutMiddleware(config('catch.route.middlewares'));;
+        Route::post('updatePayTemplateItem/{id}',[PayTemplateController::class,'updatePayTemplateItem']);
+        Route::get('deleteOneItem/{id}',[PayTemplateController::class,'deleteOneItem']);
+    });
+    Route::prefix('wechat_min_user')->group(function (){
+        // 微信小程用户信息
+        Route::get('detail/{uid}',[\Modules\Channel\Http\Controllers\WechatMinprogramUserController::class,'userInfoDetail']);
+        // 微信小程序充值记录
+        Route::get('order_list',[\Modules\Channel\Http\Controllers\WechatMinprogramUserController::class,'orderList']);
+        //观看记录
+        Route::get('watch_record',[\Modules\Channel\Http\Controllers\WechatMinprogramUserController::class,'watchRecord']);
+        // 消费记录
+        Route::get('consume_record',[\Modules\Channel\Http\Controllers\WechatMinprogramUserController::class,'consumeRecord']);
     });
-
 
 });
 

+ 7 - 2
tests/Feature/MiniprogramTest.php

@@ -23,11 +23,16 @@ class MiniprogramTest extends TestCase
 
     public function  ttest_store(): void
     {
-        
+        /*
         $response = $this->postJson('/api/channel/paytemplate/update/5',[
             'name'=>'AAAA模板11',
             'status'=>'0',
             'options'=>'[{"price":9,"type":"FIRST_COIN","given":1000,"sequence":1,"is_default":0},{"price":30,"type":"COIN","given":0,"sequence":2,"is_default":1},{"price":50,"type":"COIN","given":4000,"sequence":3,"is_default":1},{"price":190,"type":"MONTH","given":0,"sequence":4,"is_default":1},{"price":290,"type":"YEAR","given":0,"sequence":5,"is_default":0}]'
+        ]);*/
+
+
+        $response = $this->postJson('/api/channel/paytemplate/updatePayTemplateItem/31',[
+            'price'=>10,'type'=>'FIRST_COIN','given'=>1000,'is_default'=>1
         ]);
 
         //$response = $this->postJson('/api/channel/paytemplate');
@@ -52,8 +57,8 @@ class MiniprogramTest extends TestCase
     public function test_index(): void
     {
         $name = '亿';
-        //$response = $this->getJson('/api/channel/paytemplate/show/5');
         $response = $this->getJson('/api/channel/paytemplate/optionSequence');
+        //$response = $this->getJson('/api/channel/paytemplate/optionSequence');
         echo $response->getContent();
         $response->dd();
     }