allen 3 жил өмнө
parent
commit
86c8080098

+ 34 - 8
app/admin/controller/Activity.php

@@ -4,11 +4,12 @@
 
 namespace app\admin\controller;
 
+use think\activity\service\ActivityService;
 use think\admin\Controller;
-use think\admin\extend\DataExtend;
-use think\admin\service\AdminService;
-use think\admin\service\MenuService;
-use think\admin\service\NodeService;
+use think\admin\helper\QueryHelper;
+use think\admin\service\SystemService;
+use think\db\Query;
+use think\facade\Db;
 
 /**
  * 活动管理
@@ -22,7 +23,7 @@ class Activity extends Controller
      * 当前操作数据库
      * @var string
      */
-    private $table = 'activity';
+    private $table = 'Activity';
 
     /**
      * 奖品订单管理
@@ -34,8 +35,33 @@ class Activity extends Controller
      */
     public function index()
     {
-        $this->title = '活动管理';
-        $this->type = input('type', 'index');
-        $this->_query($this->table)->order('id desc')->page(false, true);
+        $this->_query($this->table)->layTable(function () {
+            $this->title = '活动管理';
+            $this->activity = [
+                "beginAt" => '2022-07-01',
+                "endAt" => '2022-07-31'
+            ];
+
+            $this->gifts = [
+                'rankGift' => [
+                    [
+                        'title' => '第一名',
+                        'name' => '投影仪',
+                    ],[
+                        'title' => '第二名~第二十名',
+                        'name' => '活动专属周边礼包',
+                    ],[
+                        'title' => '第二一名~第五十名',
+                        'name' => '电影票代金券',
+                    ]
+                ],
+                'luckDraw' => [
+                    '活动专属周边礼包',
+                    '电影票代金券'
+                ]
+            ];
+        }, function (QueryHelper $queryHelper) {
+
+        });
     }
 }

+ 102 - 9
app/admin/controller/Award.php

@@ -26,7 +26,7 @@ class Award extends Controller
     private $table = 'Order';
 
     /**
-     * 奖品订单管理
+     * 排行榜前50名
      * @auth true
      * @menu true
      * @throws \think\db\exception\DataNotFoundException
@@ -35,24 +35,116 @@ class Award extends Controller
      */
     public function index()
     {
-        $this->_query($this->table)->layTable(function (){
-            $this->title = '奖品订单';
+        $this->_query('UserTaskLog')->layTable(function (){
+            $this->title = '排行榜前50名';
             $this->type = input('type', 'award');
         },function (QueryHelper $query){
-            $query->alias('o')->leftJoin('awards_user_info u', 'o.uid = u.uid')
+            $query->alias('l')
+                ->leftJoin('awards_order o', 'o.uid = l.uid')
+                ->leftJoin('awards_user_info u', 'l.uid = u.uid')
+                ->field(['o.id', 'l.uid', 'u.nickname', 'o.name', 'o.mobile', 'o.address', 'g.name as giftName', 'FROM_UNIXTIME(o.create_at) as createAt'])
+                ->distinct('l.uid')
+                ->order('l.number', 'desc')
+                ->order('l.duration', 'asc')->limit(50);
+            $query->equal('o.uid')->like('u.nickname');
+        });
+    }
+
+    /**
+     * 导出排行榜前50名
+     * @auth true
+     * @menu true
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function award()
+    {
+        $data = $this->app->db->name('UserTaskLog')->alias('l')
+            ->leftJoin('awards_order o', 'o.uid = l.uid')
+            ->leftJoin('awards_user_info u', 'o.uid = u.uid')
+            ->leftJoin('awards_gift g', 'o.gift_id = g.id')
+            ->field(['o.id', 'l.uid', 'u.nickname', 'o.name', 'o.mobile', 'o.address', 'g.name as giftName', 'FROM_UNIXTIME(o.create_at) as createAt'])
+            ->order('l.number', 'desc')
+            ->order('l.duration', 'asc')->select();
+
+        //实例化
+        $objExcel = new \PHPExcel();
+        //设置文档属性
+        $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
+        //设置内容
+        $objActSheet = $objExcel->getActiveSheet();
+        $letter = explode(',', "A,B,C,D,E,F,G");
+        $arrHeader = ['微博UID', '微博昵称', '收件人姓名', '联系人电话', '收件地址', '奖品名称','中奖时间'];
+        //填充表头信息
+        $lenth = count($arrHeader);
+        for ($i = 0; $i < $lenth; $i++) {
+            $objActSheet->setCellValue("$letter[$i]1", "$arrHeader[$i]");
+        };
+        //填充表格信息
+        foreach ($data as $k => $v) {
+            $k += 2;
+            //表格内容
+            $objActSheet->setCellValue('A' . $k, $v['uid']." ");
+            $objActSheet->setCellValue('B' . $k, $v['nickname']);
+            $objActSheet->setCellValue('C' . $k, $v['name']);
+            $objActSheet->setCellValue('D' . $k, $v['mobile']." ");
+            $objActSheet->setCellValue('E' . $k, $v['address']);
+            $objActSheet->setCellValue('F' . $k, $v['giftName']);
+            $objActSheet->setCellValue('G' . $k, $v['createAt']);
+        }
+
+        $outfile = "中奖信息" . date('Ymd') . ".xlsx";
+        ob_end_clean();
+        header("Content-Type: application/force-download");
+        header("Content-Type: application/octet-stream");
+        header("Content-Type: application/download");
+        header('Content-Disposition:inline;filename="' . $outfile . '"');
+        header("Content-Transfer-Encoding: binary");
+        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
+        header("Pragma: no-cache");
+        $objWriter->save('php://output');
+    }
+
+    /**
+     * 中奖信息
+     * @auth true
+     * @menu true
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function luckDraw()
+    {
+        $this->_query($this->table)->layTable(function (){
+            $this->title = '中奖信息';
+            $this->type = input('type', 'luckDraw');
+        },function (QueryHelper $query){
+            $query->alias('o')
+                ->leftJoin('awards_user_info u', 'o.uid = u.uid')
                 ->leftJoin('awards_gift g', 'o.gift_id = g.id')
-                ->field(['o.id', 'u.uid', 'u.nickname', 'o.name', 'o.mobile', 'o.address', 'g.name as giftName'])
+                ->field(['o.id', 'u.uid', 'u.nickname', 'o.name', 'o.mobile', 'o.address', 'g.name as giftName', 'FROM_UNIXTIME(o.create_at) as createAt'])
+                ->where('o.type', 1)
                 ->order('o.id desc');
             $query->equal('o.uid')->like('u.nickname');
         });
     }
 
-    public function award()
+    /**
+     * 导出中奖信息
+     * @auth true
+     * @menu true
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function drawAward()
     {
         $data = $this->app->db->name('Order')->alias('o')
             ->leftJoin('awards_user_info u', 'o.uid = u.uid')
             ->leftJoin('awards_gift g', 'o.gift_id = g.id')
-            ->field(['o.id', 'u.uid', 'u.nickname', 'o.name', 'o.mobile', 'o.address', 'g.name as giftName'])
+            ->field(['o.id', 'o.uid', 'u.nickname', 'o.name', 'o.mobile', 'o.address', 'g.name as giftName', 'FROM_UNIXTIME(o.create_at) as createAt'])
+            ->where('o.type', 1)
             ->order('o.id desc')->select();
 
         //实例化
@@ -61,8 +153,8 @@ class Award extends Controller
         $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
         //设置内容
         $objActSheet = $objExcel->getActiveSheet();
-        $letter = explode(',', "A,B,C,D,E,F");
-        $arrHeader = ['微博UID', '微博昵称', '收件人姓名', '联系人电话', '收件地址', '奖品'];
+        $letter = explode(',', "A,B,C,D,E,F,G");
+        $arrHeader = ['微博UID', '微博昵称', '收件人姓名', '联系人电话', '收件地址', '奖品名称','中奖时间'];
         //填充表头信息
         $lenth = count($arrHeader);
         for ($i = 0; $i < $lenth; $i++) {
@@ -78,6 +170,7 @@ class Award extends Controller
             $objActSheet->setCellValue('D' . $k, $v['mobile']." ");
             $objActSheet->setCellValue('E' . $k, $v['address']);
             $objActSheet->setCellValue('F' . $k, $v['giftName']);
+            $objActSheet->setCellValue('G' . $k, $v['createAt']);
         }
 
         $outfile = "中奖信息" . date('Ymd') . ".xlsx";

+ 50 - 0
app/admin/view/activity/index.html

@@ -0,0 +1,50 @@
+<form class="layui-form" autocomplete="off">
+    <fieldset class="layui-elem-field layui-field-title">
+        <legend>活动配置</legend>
+    </fieldset>
+    <div class="padding-left-40">
+        <div class="layui-form-item">
+            <span class="color-green font-w7 label-required-prev">开始时间</span>
+            <span class="color-desc margin-left-5">{:$activity['beginAt']}</span>
+            <span class="help-block"></span>
+        </div>
+        <div class="layui-form-item">
+            <span class="color-green font-w7 label-required-prev">结束时间</span>
+            <span class="color-desc margin-left-5">{:$activity['beginAt']}</span>
+            <span class="help-block"></span>
+        </div>
+    </div>
+    {foreach $gifts as $key => $val}
+    {if $key == 'rankGift'}
+    <fieldset class="layui-elem-field layui-field-title">
+        <legend>排名奖品</legend>
+    </fieldset>
+    <div class="padding-left-40">
+        {foreach $val as $k => $v}
+        <div class="layui-form-item">
+            <span class="color-green font-w7 label-required-prev">{:$v['title']}</span>
+            <span class="color-desc margin-left-5">{:$v['name']}</span>
+            <span class="help-block"></span>
+        </div>
+        {/foreach}
+    </div>
+    {/if}
+    {if $key == 'luckDraw'}
+    <fieldset class="layui-elem-field layui-field-title">
+        <legend>抽奖奖品</legend>
+    </fieldset>
+    <div class="padding-left-40">
+        {foreach $val as $k => $v}
+        <div class="layui-form-item">
+            <span class="color-green font-w7 label-required-prev">奖品{:$k+1}</span>
+            <span class="color-desc margin-left-5">{:$v}</span>
+            <span class="help-block"></span>
+        </div>
+        {/foreach}
+    </div>
+    {/if}
+    {/foreach}
+</form>
+<script>
+    window.form.render();
+</script>

+ 17 - 0
app/admin/view/award/draw_search.html

@@ -0,0 +1,17 @@
+<fieldset>
+    <legend>导出</legend>
+    <form class="layui-form layui-form-pane form-search" action="{:sysuri()}" onsubmit="return false" method="get" autocomplete="off">
+        <div class="layui-form-item layui-inline">
+            <button type="button" onclick="exportAward()" class="layui-btn layui-btn-primary">
+                <i class="layui-icon layui-icon-export"></i> 导 出
+            </button>
+        </div>
+    </form>
+</fieldset>
+
+<script>
+    window.form.render();
+    function exportAward() {
+        window.location.href = '/admin/award/drawAward.html';
+    }
+</script>

+ 4 - 2
app/admin/view/award/index.html

@@ -13,15 +13,17 @@
         $('#award').layTable({
             even: true, height: 'full',
             sort: {field: 'id', type: 'desc'},
+            page: false,
             cols: [[
                 {checkbox: true},
-                {field: 'id', title: 'ID', width: 80, sort: true, align: 'center'},
+                {field: 'id', title: 'ID', width: 80, sort: false, align: 'center'},
                 {field: 'uid', title: '微博用户UID', sort: false, align: 'center'},
                 {field: 'nickname', title: '微博昵称', sort: false},
                 {field: 'name', title: '收件人姓名', sort: false},
                 {field: 'mobile', title: '联系人电话', sort: false},
                 {field: 'address', title: '收件地址', sort: false},
-                {field: 'giftName', title: '奖品', sort: false},
+                {field: 'giftName', title: '奖品名称', sort: false},
+                {field: 'createAt', title: '中奖时间', sort: false},
             ]]
         });
     });

+ 1 - 43
app/admin/view/award/index_search.html

@@ -1,21 +1,7 @@
 <fieldset>
-    <legend>条件搜索</legend>
+    <legend>导出</legend>
     <form class="layui-form layui-form-pane form-search" action="{:sysuri()}" onsubmit="return false" method="get" autocomplete="off">
         <div class="layui-form-item layui-inline">
-            <label class="layui-form-label">用户UID</label>
-            <label class="layui-input-inline">
-                <input name="uid" value="{:input('get.uid')}" placeholder="请输入用户UID" class="layui-input">
-            </label>
-        </div>
-        <div class="layui-form-item layui-inline">
-            <label class="layui-form-label">收件人姓名</label>
-            <label class="layui-input-inline">
-                <input name="nickname" value="{:input('get.nickname')}" placeholder="请输入收件人姓名" class="layui-input">
-            </label>
-        </div>
-        <div class="layui-form-item layui-inline">
-            <button type="submit" class="layui-btn layui-btn-primary"><i class="layui-icon">&#xe615;</i> 搜 索</button>
-<!--            <button type="button" onclick="exportAward()" data-form-export="{:url('award')}?type={$type|default=''}" class="layui-btn layui-btn-primary">-->
             <button type="button" onclick="exportAward()" class="layui-btn layui-btn-primary">
                 <i class="layui-icon layui-icon-export"></i> 导 出
             </button>
@@ -27,33 +13,5 @@
     window.form.render();
     function exportAward() {
         window.location.href = '/admin/award/award.html';
-        // $.ajax({
-        //     "url": '/admin/award/award.html',
-        //     success: function (content) {
-        //         const blob = new Blob([content]);
-        //         const fileName = '用户列表.xlsx';
-        //         if ('download' in document.createElement('a')) { // 非IE下载
-        //             const elink = document.createElement('a');
-        //             elink.download = fileName;
-        //             elink.style.display = 'none';
-        //             elink.href = URL.createObjectURL(blob);
-        //             document.body.appendChild(elink);
-        //             elink.click();
-        //             URL.revokeObjectURL(elink.href); // 释放URL 对象
-        //             document.body.removeChild(elink)
-        //         } else { // IE10+下载
-        //             navigator.msSaveBlob(blob, fileName)
-        //         }
-        //     }
-        // })
     }
-    // require(['excel'], function (excel) {
-    //     excel.bind(function (data) {
-    //         data.forEach(function (item, index) {
-    //             data[index] = [item.uid, item.nickname, item.name, item.mobile, item.address, item.giftName];
-    //         });
-    //         data.unshift(['微博UID', '微博昵称', '收件人姓名', '联系人电话', '收件地址', '奖品']);
-    //         return data;
-    //     }, '奖品订单列表');
-    // });
 </script>

+ 31 - 0
app/admin/view/award/luck_draw.html

@@ -0,0 +1,31 @@
+{extend name='table'}
+
+{block name="content"}
+<div class="think-box-shadow">
+    {include file='award/draw_search'}
+    <table id="luckDraw" data-url="{:sysuri()}" data-target-search="form.form-search"></table>
+</div>
+{/block}
+
+{block name='script'}
+<script>
+    $(function () {
+        $('#luckDraw').layTable({
+            even: true, height: 'full',
+            sort: {field: 'id', type: 'desc'},
+            page: false,
+            cols: [[
+                {checkbox: true},
+                {field: 'id', title: 'ID', width: 80, sort: false, align: 'center'},
+                {field: 'uid', title: '微博用户UID', sort: false, align: 'center'},
+                {field: 'nickname', title: '微博昵称', sort: false},
+                {field: 'name', title: '收件人姓名', sort: false},
+                {field: 'mobile', title: '联系人电话', sort: false},
+                {field: 'address', title: '收件地址', sort: false},
+                {field: 'giftName', title: '奖品名称', sort: false},
+                {field: 'createAt', title: '中奖时间', sort: false},
+            ]]
+        });
+    });
+</script>
+{/block}

+ 129 - 21
app/index/controller/Index.php

@@ -2,7 +2,6 @@
 
 namespace app\index\controller;
 
-use think\db\Where;
 use think\facade\Db;
 use app\middleware\Log;
 use think\facade\Cache;
@@ -360,6 +359,11 @@ class Index extends Controller
         $giftId = trim(Request::post("giftId"));
         $type = trim(Request::post("type"));
 
+        $orders = Db::table('awards_order')->where('uid', Safe::$user['uid'])->where('type', $type)->find();
+        if (empty($orders)) {
+            return $this->response(5001, '参数有误');
+        }
+
         $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
         if (empty($userInfo)) {
             return $this->response(403, '没有登录');
@@ -377,16 +381,13 @@ class Index extends Controller
             return $this->response(5001, '地址有误');
         }
 
-        $order = [
-            'uid' => Safe::$user['uid'],
+        // 更新任务状态
+        $nums = Db::table('awards_order')->where('uid', Safe::$user['uid'])->where('type', $type)->update([
             'name' => $name,
             'mobile' => $mobile,
             'address' => $address,
-            'gift_at' => $giftId,
-            'type' => $type,
-            'create_at' => time(),
-        ];
-        if (0 == Db::table('awards_order')->insert($order)) {
+        ]);
+        if ($nums < 1) {
             return $this->response(5001, '系统错误,请稍后再试~');
         }
 
@@ -431,24 +432,56 @@ class Index extends Controller
      */
     public function getRankingWinAward() {
 
+        $activity = Db::table('awards_activity')->where('begin_at', '<=', time())
+            ->where('end_at', '>=', time())->find();
+        if (!empty($activity)) {
+            return $this->response(5002, '活动末结束');
+        }
+
         $ranking = Db::table('awards_user_task_log')->alias("l")
             ->leftJoin('awards_user_info u', 'l.uid = u.uid')
             ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
+            ->distinct('l.uid')
             ->order('l.number', 'desc')
             ->order('l.duration', 'asc')
             ->limit(50)
             ->select();
 
         $isWinAward = 0;
-        foreach ($ranking as $val) {
+        $rank = 0;
+        foreach ($ranking as $key => $val) {
             if ($val['uid'] == Safe::$user['uid']) {
                 $isWinAward = 1;
+                $rank = $key +1;
+                break;
+            }
+        }
+
+        // 如果中奖 插入用户中奖信息
+        if ($isWinAward == 1) {
+            $gifts = Db::table('awards_gift')->where('type', 2)->select();
+            $giftId = 0;
+            foreach ($gifts as $val) {
+                if ($rank >= $val['min_rank'] && $rank <= $val['max_rank']) {
+                    $giftId = $val['id'];
+                    $orders = Db::table('awards_order')->where('uid', Safe::$user['uid'])->where('type', 2)->find();
+                    if (empty($orders)) {
+                        $order = [
+                            'uid' => Safe::$user['uid'],
+                            'gift_id' => $giftId,
+                            'type' => 2,
+                            'create_at' => time(),
+                        ];
+                        Db::table('awards_order')->insert($order);
+                    }
+                    break;
+                }
             }
         }
 
         return $this->successResponse([
             'isWinAward' => $isWinAward,
-            'giftId' => 0
+            'giftId' => $giftId
         ]);
     }
 
@@ -463,27 +496,102 @@ class Index extends Controller
      */
     public function getLuckDraw()
     {
-        $ranking = Db::table('awards_user_task_log')->alias("l")
-            ->leftJoin('awards_user_info u', 'l.uid = u.uid')
-            ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
-            ->order('l.number', 'desc')
-            ->order('l.duration', 'asc')
-            ->limit(50)
-            ->select();
-
+        $orders = Db::table('awards_order')->where('uid', Safe::$user['uid'])->where('type', 1)->find();
         $isWinAward = 0;
-        foreach ($ranking as $val) {
-            if ($val['uid'] == Safe::$user['uid']) {
+        if ($orders) {
+            return $this->successResponse([
+                'isWinAward' => $isWinAward,
+                'giftId' => 0
+            ]);
+        }
+
+        $gifts = Db::table('awards_gift')->where('type', 1)->select();
+        $logCount = Db::table('awards_user_task_log')->distinct('uid')->count('id');
+
+        $userRate = rand(0, 5000);
+        $giftId = 0;
+        foreach ($gifts as $gift) {
+            $orderCount = Db::table('awards_order')->where('type', 1)->where('gift_id', $gift['id'])->count('id');
+            if ($orderCount >= $gift['count']) {
+                continue;
+            }
+
+            // 如果参与活动人数 5000人内 有人中奖过,那么不在计算。
+            $total = $logCount + 1;
+            if ($orderCount == floor($total/2)) {
+                $isWinAward = 0;
+                $giftId = 0;
+                break;
+            }
+
+            for ($i=0;$i<$gift['count'];$i++) {
+                $giftRage = rand(0, 5000);
+                if ($userRate == $giftRage) {
+                    $isWinAward = 1;
+                    $giftId = $gift['id'];
+                    break;
+                }
+            }
+
+            // 如果参与活动人数 5000人内 还没有人中奖那最后一人必中。
+            if ($orderCount < floor($total/2) && $isWinAward ==0 && ($total%2) == 0) {
                 $isWinAward = 1;
+                $giftId = $gift['id'];
+                break;
+            }
+        }
+
+        // 如果中奖 插入用户中奖信息
+        if ($isWinAward == 1) {
+            $order = [
+                'uid' => Safe::$user['uid'],
+                'gift_id' => $giftId,
+                'type' => 1,
+                'create_at' => time(),
+            ];
+            if (0 == Db::table('awards_order')->insert($order)) {
+                return $this->response(5001, '系统错误,请稍后再试~');
             }
         }
 
         return $this->successResponse([
             'isWinAward' => $isWinAward,
-            'giftId' => 0
+            'giftId' => $giftId
         ]);
     }
 
+    /**
+     * 奖品记录接口
+     * @throws FuncNotFoundException
+     * @throws ClassNotFoundException
+     * @throws DbException
+     * @throws ModelNotFoundException
+     * @throws DataNotFoundException
+     * @return mixed
+     */
+    public function getAwards()
+    {
+        $awards = Db::table('awards_order')->alias('o')
+            ->leftJoin('awards_gift g', 'o.gift_id = g.id')
+            ->field(['g.name as giftName', 'o.type', 'o.create_at'])
+            ->select();
+
+        $res = [];
+        foreach ($awards as $val) {
+            $type = '抽奖';
+            if ($val['type'] == 1) {
+                $type = '排名奖';
+            }
+            $res[] = [
+                'giftName' => $val['giftName'],
+                'type' => $type,
+                'createAt' => date('y-m-d h:i:s', $val['create_at'])
+            ];
+        }
+
+        return $this->successResponse($res);
+    }
+
     /**
      * 品牌任务关注接口
      * @throws FuncNotFoundException

+ 1 - 1
vendor/zoujingli/think-library/src/service/SystemService.php

@@ -400,4 +400,4 @@ class SystemService extends Service
     {
         return array_unique(array_reverse(array_merge(...$args)));
     }
-}
+}