Kirin 1 rok pred
rodič
commit
8ef682b2d3

+ 65 - 0
app/admin/controller/ump/Gacha.php

@@ -0,0 +1,65 @@
+<?php
+
+namespace app\admin\controller\ump;
+
+use app\admin\controller\AuthController;
+use app\models\user\User;
+use app\models\user\UserBill;
+use crmeb\services\UtilService as Util;
+use app\admin\model\user\UserPoint as UserPointModel;
+use think\facade\Route as Url;
+use crmeb\services\JsonService;
+
+/**
+ * 优惠券控制器
+ * Class StoreCategory
+ * @package app\admin\controller\system
+ */
+class Gacha extends AuthController
+{
+
+    /**
+     * @return mixed
+     */
+    public function index()
+    {
+        return $this->fetch();
+    }
+
+    public function gacha_list()
+    {
+        $where = Util::getMore([
+            ['status', ''],
+            ['page', 1],
+            ['limit', 10],
+        ]);
+        JsonService::successlayui(\app\models\user\Gacha::getList($where));
+    }
+
+
+    public function deal($id)
+    {
+        $orderInfo = \app\models\user\Gacha::where('id', $id)->where('status', 0)->where('limit_time', '>=', time())->find();
+        if (!$orderInfo) return app('json')->fail('核销的奖品不存在或已兑换或已过期');
+        \app\models\user\Gacha::beginTrans();
+        try {
+            if ($orderInfo['award_type'] == 1) {
+                User::bcInc($orderInfo['uid'], 'integral', $orderInfo['award'], 'uid');
+                UserBill::expend('抽奖奖品', $orderInfo['uid'], 'integral', 'gacha_get', $orderInfo['award'], $orderInfo['id'], User::where('uid', $orderInfo['uid'])->value('integral'), '抽奖奖品兑换');
+            }
+            $orderInfo->status = 1;
+            if ($orderInfo->save()) {
+                \app\models\user\Gacha::commitTrans();
+                return app('json')->success('核销成功');
+            } else {
+                \app\models\user\Gacha::rollbackTrans();
+                return app('json')->fail('核销失败');
+            }
+        } catch (\Exception $e) {
+            \app\models\user\Gacha::rollbackTrans();
+            return app('json')->fail($e->getMessage());
+        }
+    }
+
+
+}

+ 61 - 0
app/admin/view/ump/gacha/index.php

@@ -0,0 +1,61 @@
+{extend name="public/container"}
+{block name="head_top"}
+
+{/block}
+{block name="content"}
+<div class="layui-fluid" style="background: #fff;margin-top: -10px;">
+    <div class="layui-row layui-col-space15">
+        <div class="layui-col-md12">
+            <div class="layui-card">
+                <!--                <div class="layui-card-header">门店列表</div>-->
+                <div class="layui-card-body">
+                    <table class="layui-hide" id="List" lay-filter="List"></table>
+
+                    <script type="text/html" id="act">
+                        <button type="button" class="layui-btn layui-btn-xs layui-btn-normal" lay-event='deal'>
+                            兑换处理
+                        </button>
+                    </script>
+                </div>
+            </div>
+        </div>
+    </div>
+</div>
+<script src="{__ADMIN_PATH}js/layuiList.js"></script>
+{/block}
+{block name="script"}
+<script>
+    layList.tableList('List', "{:Url('gacha_list')}", function () {
+        return [
+            {field: 'id', title: 'ID', sort: true, event: 'id', width: '10%'},
+            {field: 'user', title: '用户', width: '10%'},
+            {field: 'award_text', title: '奖品'},
+            {field: 'add_time_text', title: '中奖时间', width: '15%'},
+            {field: 'limit_time_text', title: '到期时间', width: '15%'},
+            {field: 'status_text', title: '状态', width: '10%'},
+            {field: 'right', title: '操作', align: 'center', toolbar: '#act', width: '10%'},
+        ];
+    });
+    //点击事件绑定
+    layList.tool(function (event, data, obj) {
+        switch (event) {
+            case 'del':
+                var url = layList.U({c: 'ump.gacha', a: 'deal', q: {id: data.id}});
+                var code = {title: "操作提示", text: "确定处理该奖励?", type: 'info', confirm: '是的,处理'};
+                $eb.$swal('delete', function () {
+                    $eb.axios.get(url).then(function (res) {
+                        if (res.status == 200 && res.data.code == 200) {
+                            $eb.$swal('success', res.data.msg);
+                            obj.del();
+                            location.reload();
+                        } else
+                            return Promise.reject(res.data.msg || '处理失败')
+                    }).catch(function (err) {
+                        $eb.$swal('error', err);
+                    });
+                }, code)
+                break;
+        }
+    })
+</script>
+{/block}

+ 107 - 0
app/api/controller/PublicController.php

@@ -18,6 +18,7 @@ use app\models\system\SystemGroupLevel;
 use app\models\system\SystemStore;
 use app\models\system\SystemStoreStaff;
 use app\models\system\SystemStoreStock;
+use app\models\user\Gacha;
 use app\models\user\User;
 use app\models\user\UserBill;
 use app\models\user\WechatUser;
@@ -394,4 +395,110 @@ class PublicController
     }
 
 
+    public function gacha_set(Request $request)
+    {
+        $pay_way = sys_config('pay_way', 1, true);
+        $pay_num = sys_config('pay_num', 1, true);
+        $award = sys_data('gacha_set');
+        $sum_ratio = 0;
+        foreach ($award as $k => $v) {
+            $sum_ratio = $sum_ratio + $v['award_ratio'];
+            unset($award[$k]['limit_time']);
+        }
+        return app('json')->success('ok', compact('pay_way', 'pay_num', 'award', 'sum_ratio'));
+    }
+
+
+    public function gacha(Request $request)
+    {
+        $pay_way = sys_config('pay_way', 1, true);
+        $pay_num = sys_config('pay_num', 1, true);
+        if ($pay_way == 1) {
+            $pay_way = 'integral';
+        } else if ($pay_way == 2) {
+            $pay_way = 'now_money';
+        } else {
+            return app('json')->fail('抽奖尚未开启');
+        }
+
+        $user = User::get($request->uid());
+        $award = sys_data('gacha_set');
+        $sum_ratio = 0;
+        $times = Gacha::where('uid', $user['uid'])->count();
+        $real_award = [];
+        foreach ($award as $v) {
+            if ($times >= $v['limit_time']) {
+                $sum_ratio = $sum_ratio + $v['award_ratio'];
+                $real_award[] = $v;
+            }
+        }
+        if ($sum_ratio < 1) {
+            return app('json')->fail('抽奖尚未开启');
+        }
+
+        if ($user[$pay_way] < $pay_num) return app('json')->fail('余额不足');
+        User::beginTrans();
+        try {
+            $res = User::bcDec($user['uid'], $pay_way, $pay_num, 'uid');
+            $bill_res = UserBill::expend('抽奖支付', $user['uid'], $pay_way, 'gacha_pay', $pay_num, 0, bcsub($user[$pay_way], $pay_num, 2), '参与抽奖支付');
+            if (!($res && $bill_res)) return app('json')->fail('支付失败');
+            $award_gacha = rand(1, $sum_ratio);
+            foreach ($real_award as $v) {
+                if ($award_gacha <= $v['award_ratio']) {
+                    $gacha = $v;
+                    break;
+                }
+                $award_gacha -= $v['award_ratio'];
+            }
+            $gacha_res = Gacha::create([
+                'award' => $gacha['awatd'],
+                'uid' => $user['uid'],
+                'award_type' => $gacha['award_type'],
+                'add_time' => time(),
+                'limit_time' => time() + ($gacha['award_time'] * 24 * 3600),
+                'unicode' => Gacha::createCode($user['uid'])
+            ]);
+            UserBill::where('id', $bill_res['id'])->update(['link_id' => $gacha_res['id']]);
+            User::commitTrans();
+            return app('json')->success('ok', $gacha_res);
+        } catch (\Exception $e) {
+            User::rollbackTrans();
+            return app('json')->fail($e->getMessage());
+        }
+    }
+
+    public function gacha_log(Request $request)
+    {
+        $page = $request->get('page', 1);
+        $limit = $request->get('limit', 10);
+        $status = $request->get('status', '');
+        $where['uid'] = $request->uid();
+        if ($status != '') {
+            $where['status'] = $status;
+        }
+        $list = Gacha::where($where)->page($page, $limit)->select();
+        $count = Gacha::where($where)->count();
+        return app('json')->success('ok', compact('list', 'count'));
+    }
+
+    public function get_award($id, Request $request)
+    {
+        $info = Gacha::get($id);
+        if ($info['uid'] != $request->uid()) return app('json')->fail('奖励不存在');
+        if ($info['status'] != 0) return app('json')->fail('奖品已兑换');
+        if ($info['limit_time'] < time()) return app('json')->fail('奖品兑换期已过');
+        if ($info['award_type'] == 2) return app('json')->fail('实物奖品请往线下兑换点兑奖');
+        User::beginTrans();
+        try {
+            User::bcInc($request->uid(), 'integral', $info['award'], 'uid');
+            UserBill::expend('抽奖奖品', $request->uid(), 'integral', 'gacha_get', $info['award'], $info['id'], User::where('uid', $request->uid())->value('integral'), '抽奖奖品兑换');
+            Gacha::where('id', $id)->update(['status' => 1]);
+            User::commitTrans();
+            return app('json')->success('兑换成功');
+        } catch (\Exception $e) {
+            User::rollbackTrans();
+            return app('json')->fail($e->getMessage());
+        }
+    }
+
 }

+ 42 - 11
app/api/controller/admin/StoreOrderController.php

@@ -3,9 +3,7 @@
 namespace app\api\controller\admin;
 
 use app\Request;
-use app\models\user\{
-    User, UserBill
-};
+use app\models\user\{Gacha, User, UserBill};
 use crmeb\repositories\OrderRepository;
 use crmeb\repositories\ShortLetterRepositories;
 use crmeb\services\{
@@ -56,22 +54,23 @@ class StoreOrderController
             ['stop', '']
         ], $request, true);
         if (!$limit) return app('json')->successful([]);
-        $info = SystemStoreStaff::where('uid',$uid)->find()->toArray();
+        $info = SystemStoreStaff::where('uid', $uid)->find()->toArray();
 
         $data = StoreOrder::getOrderDataPriceCount($page, $limit, $start, $stop);
         if ($data) return app('json')->successful($data->toArray());
         return app('json')->successful([]);
     }
+
     public function month(Request $request)
     {
         $uid = $request->uid();
-        $month = input('month',date("Y-m"));
-        $start = date("Y-m-d H:i:s",strtotime($month."-1"));
-        $end =  date("Y-m-d H:i:s",strtotime("+1 month",strtotime($start))-1);
+        $month = input('month', date("Y-m"));
+        $start = date("Y-m-d H:i:s", strtotime($month . "-1"));
+        $end = date("Y-m-d H:i:s", strtotime("+1 month", strtotime($start)) - 1);
         if (!StoreService::orderServiceStatus($uid))
             return app('json')->fail('权限不足');
-        $info = SystemStoreStaff::where('uid',$uid)->find()->toArray();
-        return app('json')->successful(StoreOrder::getordermonth($info['store_id'],$start,$end));
+        $info = SystemStoreStaff::where('uid', $uid)->find()->toArray();
+        return app('json')->successful(StoreOrder::getordermonth($info['store_id'], $start, $end));
 
     }
 
@@ -91,12 +90,12 @@ class StoreOrderController
             ['data', ''],
             ['type', ''],
             ['order', ''],
-            ['real_name',''],
+            ['real_name', ''],
             ['page', 0],
             ['limit', 0]
         ], $request);
         if (!$where['limit']) return app('json')->successful([]);
-        $info = SystemStoreStaff::where('uid',$uid)->find()->toArray();
+        $info = SystemStoreStaff::where('uid', $uid)->find()->toArray();
         $where['store_id'] = $info['store_id'];
         return app('json')->successful(StoreOrder::orderList($where));
     }
@@ -512,5 +511,37 @@ class StoreOrderController
         }
     }
 
+    /**
+     * 门店核销
+     * @param Request $request
+     */
+    public function gacha_writer(Request $request)
+    {
+        list($verify_code, $is_confirm) = UtilService::postMore([
+            ['verify_code', ''],
+            ['is_confirm', 0]
+        ], $request, true);
+        if (!$verify_code) return app('json')->fail('缺少核销码');
+        $orderInfo = Gacha::where('unicode', $verify_code)->where('status', 0)->where('limit_time', '>=', time())->find();
+        if (!$orderInfo) return app('json')->fail('核销的奖品不存在或已兑换或已过期');
+        if (!$is_confirm) {
+            return app('json')->success($orderInfo->toArray());
+        }
+        Gacha::beginTrans();
+        try {
+            $orderInfo->status = 1;
+            if ($orderInfo->save()) {
+                Gacha::commitTrans();
+                return app('json')->success('核销成功');
+            } else {
+                Gacha::rollbackTrans();
+                return app('json')->fail('核销失败');
+            }
+        } catch (\Exception $e) {
+            Gacha::rollbackTrans();
+            return app('json')->fail($e->getMessage());
+        }
+    }
+
 
 }

+ 59 - 0
app/models/user/Gacha.php

@@ -0,0 +1,59 @@
+<?php
+
+namespace app\models\user;
+
+use crmeb\basic\BaseModel;
+use crmeb\traits\ModelTrait;
+
+class Gacha extends BaseModel
+{
+    /**
+     * 数据表主键
+     * @var string
+     */
+    protected $pk = 'id';
+
+    /**
+     * 模型名称
+     * @var string
+     */
+    protected $name = 'gacha';
+    use ModelTrait;
+
+    public static function createCode($uid)
+    {
+        do {
+            $str = md5($uid . time() . rand(1000000, 9999999)) . md5($uid);
+        } while (Gacha::be(['unicode' => $str]));
+        return $str;
+    }
+
+    public static function getList($where): array
+    {
+        $model = new self();
+        if (isset($where['status']) && $where['status'] != '') {
+            if ($where['status'] == 1) {
+                $model = $model->where('status', 1);
+
+            } elseif ($where['status'] == 2) {
+                $model = $model->where('status', 0);
+                $model = $model->where('limit_time', '<', time());
+            } elseif ($where['status'] == 0) {
+                $model = $model->where('status', 0);
+                $model = $model->where('limit_time', '>=', time());
+            }
+        }
+        $data = $model->page((int)$where['page'], (int)$where['limit'])->select();
+        foreach ($data as &$v) {
+            $user = User::get($v['uid']);
+            $v['user'] = $user['nickname'] . "[{$user['uid']}]";
+            $v['add_time_text'] = date('Y-m-d H:i:s', $v['add_time']);
+            $v['limit_time_text'] = date('Y-m-d H:i:s', $v['limit_time']);
+            $v['award_text'] = ($v['award_type'] == 1 ? ($v['award'] . '积分') : $v['award']);
+            $v['status_text'] = ($v['status'] == 1 ? "已兑换" : ($v['limit_time'] < time() ? "已过期" : "待兑换"));
+        }
+        $count = $model->count();
+        return compact('count', 'data');
+    }
+
+}

+ 6 - 1
route/api/route.php

@@ -51,6 +51,7 @@ Route::group(function () {
     Route::get('admin/order/time', 'admin.StoreOrderController/time')->name('adminOrderTime');//订单交易额时间统计
     Route::post('admin/order/offline', 'admin.StoreOrderController/offline')->name('adminOrderOffline');//订单支付
     Route::post('admin/order/refund', 'admin.StoreOrderController/refund')->name('adminOrderRefund');//订单退款
+    Route::post('admin/gacha/writer', 'admin.StoreOrderController/gacha_writer')->name('gacha_writer');//订单退款
     Route::post('order/order_verific', 'admin.StoreOrderController/order_verific')->name('order');//订单核销
     Route::post('store/goods', 'company.IndexCountroller/goods')->name('storegoods');//门店商品库存
     Route::post('store/member', 'company.IndexCountroller/member')->name('storemember');//门店会员
@@ -193,7 +194,11 @@ Route::group(function () {
     Route::post('received/collection_set_tip', 'user.PaymentReceived/collection_set_tip')->name('receivedcollection_set_tip');//已提醒
     Route::post('user/m_spread', 'user.UserController/setMSpreadUid')->name('m_spread');//已提醒
     Route::get('user/m_spread_list', 'user.UserController/m_spread_list')->name('m_spread_list');//已提醒
-
+    //抽奖
+    Route::get('gacha', 'PublicController/gacha_set')->name('gacha_set');//首页
+    Route::post('gacha', 'PublicController/gacha')->name('gacha');//首页
+    Route::get('gacha/log', 'PublicController/gacha_log')->name('gacha_log');//首页
+    Route::post('gacha/get/:id', 'PublicController/get_award')->name('get_award');//首页
 
 })->middleware(\app\http\middleware\AllowOriginMiddleware::class)->middleware(\app\http\middleware\AuthTokenMiddleware::class, true);
 //未授权接口