| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- <?php
- namespace app\api\controller;
- use ba\Tree;
- use Throwable;
- use think\facade\Db;
- use think\facade\Config;
- use app\common\controller\Frontend;
- use app\common\library\token\TokenExpirationException;
- class Index extends Frontend
- {
- protected array $noNeedLogin = ['index','checkExpiredProducts'];
- public function initialize(): void
- {
- parent::initialize();
- }
- /**
- * 前台和会员中心的初始化请求
- * @throws Throwable
- */
- public function index(): void
- {
- $menus = [];
- if ($this->auth->isLogin()) {
- $rules = [];
- $userMenus = $this->auth->getMenus();
- // 首页加载的规则,验权,但过滤掉会员中心菜单
- foreach ($userMenus as $item) {
- if ($item['type'] == 'menu_dir') {
- $menus[] = $item;
- } elseif ($item['type'] != 'menu') {
- $rules[] = $item;
- }
- }
- $rules = array_values($rules);
- } else {
- // 若是从前台会员中心内发出的请求,要求必须登录,否则会员中心异常
- $requiredLogin = $this->request->get('requiredLogin/b', false);
- if ($requiredLogin) {
- // 触发可能的 token 过期异常
- try {
- $token = get_auth_token(['ba', 'user', 'token']);
- $this->auth->init($token);
- } catch (TokenExpirationException) {
- $this->error(__('Token expiration'), [], 409);
- }
- $this->error(__('Please login first'), [
- 'type' => $this->auth::NEED_LOGIN
- ], $this->auth::LOGIN_RESPONSE_CODE);
- }
- $rules = Db::name('user_rule')
- ->where('status', 1)
- ->where('no_login_valid', 1)
- ->where('type', 'in', ['route', 'nav', 'button'])
- ->order('weigh', 'desc')
- ->select()
- ->toArray();
- $rules = Tree::instance()->assembleChild($rules);
- }
- $this->success('', [
- 'site' => [
- 'siteName' => get_sys_config('site_name'),
- 'version' => get_sys_config('version'),
- 'cdnUrl' => full_url(),
- 'upload' => keys_to_camel_case(get_upload_config(), ['max_size', 'save_name', 'allowed_suffixes', 'allowed_mime_types']),
- 'recordNumber' => get_sys_config('record_number'),
- 'cdnUrlParams' => Config::get('buildadmin.cdn_url_params'),
- ],
- 'openMemberCenter' => Config::get('buildadmin.open_member_center'),
- 'userInfo' => $this->auth->getUserInfo(),
- 'rules' => $rules,
- 'menus' => $menus,
- ]);
- }
- /*
- * 批量更新过期记录
- */
- /**
- * 检查并更新过期套餐
- * @return int 更新的记录数
- */
- public function checkExpiredProducts()
- {
- $now = date('Y-m-d H:i:s');
- Db::startTrans();
- try {
- // 1. 查询所有已过期且状态仍有效的 usage 记录(加锁)
- $expiredList = Db::table('ba_user_product_usage')
- ->where('status', 1)
- ->where('end_time', '<=', $now)
- ->whereNotNull('end_time')
- ->lock(true)
- ->select()
- ->toArray();
- if (empty($expiredList)) {
- Db::commit();
- return 0;
- }
- $ids = array_column($expiredList, 'id');
- // 提取企业套餐的 allocation_id
- $allocationIds = [];
- foreach ($expiredList as $item) {
- if (!empty($item['allocation_id'])) {
- $allocationIds[] = $item['allocation_id'];
- }
- }
- $allocationIds = array_unique($allocationIds);
- // 2. 更新 usage 表状态
- Db::table('ba_user_product_usage')
- ->whereIn('id', $ids)
- ->update([
- 'status' => 2,
- 'update_time' => $now
- ]);
- // 3. 更新企业套餐分配记录状态(如果业务需要)
- if (!empty($allocationIds)) {
- Db::table('ba_company_package_allocation')
- ->whereIn('id', $allocationIds)
- ->where('allocation_status', 1) // 假设 1 表示已分配/有效
- ->update([
- 'allocation_status' => 2, // 2 表示已过期或取消
- 'update_time' => $now
- ]);
- // 可选:更新企业套餐席位表的 assigned_seats / available_seats
- // 这需要根据分配记录找到对应的企业套餐席位,然后减少 assigned_seats 并增加 available_seats
- // 此处省略,可根据实际业务补充
- }
- // 4. 处理个人套餐的 ba_user_product 表
- $personalCombos = [];
- foreach ($expiredList as $item) {
- if ($item['package_type'] == 1) { // 个人套餐
- $key = $item['user_id'] . '_' . $item['product_id'] . '_' . $item['package_price_id'];
- $personalCombos[$key] = [
- 'user_id' => $item['user_id'],
- 'product_id' => $item['product_id'],
- 'package_price_id' => $item['package_price_id']
- ];
- }
- }
- foreach ($personalCombos as $combo) {
- // 检查该组合是否还有任何有效的 usage 记录
- $hasValid = Db::table('ba_user_product_usage')
- ->where('user_id', $combo['user_id'])
- ->where('product_id', $combo['product_id'])
- ->where('package_price_id', $combo['package_price_id'])
- ->where('package_type', 1)
- ->where('status', 1)
- ->where(function ($query) use ($now) {
- $query->where('end_time', '>', $now)
- ->whereOr('end_time', 'null'); // 永久有效
- })
- ->count();
- if ($hasValid == 0) {
- // 无有效记录,将汇总表状态置为过期
- Db::table('ba_user_product')
- ->where('user_id', $combo['user_id'])
- ->where('product_id', $combo['product_id'])
- ->where('package_price_id', $combo['package_price_id'])
- ->update([
- 'status' => 2, // 已过期
- 'update_time' => $now
- ]);
- } else {
- // 还有有效记录,更新 expire_time 为最新的有效记录
- $latestEndTime = Db::table('ba_user_product_usage')
- ->where('user_id', $combo['user_id'])
- ->where('product_id', $combo['product_id'])
- ->where('package_price_id', $combo['package_price_id'])
- ->where('package_type', 1)
- ->where('status', 1)
- ->order('end_time', 'desc')
- ->value('end_time');
- if ($latestEndTime) {
- Db::table('ba_user_product')
- ->where('user_id', $combo['user_id'])
- ->where('product_id', $combo['product_id'])
- ->where('package_price_id', $combo['package_price_id'])
- ->update([
- 'expire_time' => $latestEndTime,
- 'update_time' => $now
- ]);
- }
- }
- }
- Db::commit();
- return count($expiredList);
- } catch (\Throwable $e) {
- Db::rollback();
- throw $e;
- }
- }
- }
|