uid(); $now = time(); $list = Db::name('coupon') ->where('status', 'normal') ->where(function ($q) use ($now) { $q->where('end_time', 0)->whereOr('end_time', '>', $now); }) ->order('amount desc,id desc') ->select(); $receivedIds = Db::name('user_coupon')->where('user_id', $uid)->column('coupon_id'); foreach ($list as &$row) { $row['amount'] = (float)$row['amount']; $row['min_amount'] = (float)$row['min_amount']; $row['discount'] = (float)$row['discount']; $row['total'] = (int)$row['total']; $row['received'] = (int)$row['received']; $row['received_by_me'] = in_array((int)$row['id'], array_map('intval', $receivedIds), true); $row['sold_out'] = $row['total'] > 0 && $row['received'] >= $row['total']; $row['time_text'] = (int)$row['end_time'] > 0 ? date('Y.m.d', (int)$row['end_time']) . ' 到期' : '长期有效'; $row['value_text'] = $row['type'] === 'cash' ? ('¥' . (float)$row['amount']) : (rtrim(rtrim(number_format((float)$row['discount'] * 10, 1, '.', ''), '0'), '.') . ' 折'); $row['rule_text'] = $row['min_amount'] > 0 ? ('满 ¥' . (float)$row['min_amount'] . ' 可用') : '无门槛'; } unset($row); $this->success('', ['list' => $list]); } /** * 我的优惠券 * @ApiParams (name="status", type="string", required=false, description="unused|used|expired") */ public function my() { $status = $this->request->param('status', 'unused'); if (!in_array($status, ['unused', 'used', 'expired'], true)) { $status = 'unused'; } $uid = $this->uid(); $now = time(); $list = Db::name('user_coupon')->alias('uc') ->join('coupon c', 'c.id = uc.coupon_id', 'LEFT') ->field('uc.id,uc.status,uc.use_time,uc.createtime,c.name,c.type,c.amount,c.discount,c.min_amount,c.end_time') ->where('uc.user_id', $uid) ->where('uc.status', $status) ->order('uc.id desc') ->select(); $counts = ['unused' => 0, 'used' => 0, 'expired' => 0]; $all = Db::name('user_coupon')->where('user_id', $uid)->field('status,count(*) as c')->group('status')->select(); foreach ($all as $r) { $counts[$r['status']] = (int)$r['c']; } foreach ($list as &$row) { $row['amount'] = (float)$row['amount']; $row['min_amount'] = (float)$row['min_amount']; $row['discount'] = (float)$row['discount']; $row['value_text'] = $row['type'] === 'cash' ? ('¥' . (float)$row['amount']) : (rtrim(rtrim(number_format((float)$row['discount'] * 10, 1, '.', ''), '0'), '.') . ' 折'); $row['rule_text'] = $row['min_amount'] > 0 ? ('满 ¥' . (float)$row['min_amount'] . ' 可用') : '无门槛'; $row['time_text'] = (int)$row['end_time'] > 0 ? ('有效期至 ' . date('Y.m.d', (int)$row['end_time'])) : '长期有效'; $row['expired'] = (int)$row['end_time'] > 0 && (int)$row['end_time'] < $now; } unset($row); $this->success('', ['list' => $list, 'counts' => $counts]); } /** * 领取优惠券 * @ApiMethod (POST) */ public function receive() { $couponId = (int)$this->request->post('coupon_id'); $coupon = Db::name('coupon')->where('id', $couponId)->where('status', 'normal')->find(); if (!$coupon) { $this->error('优惠券不存在或已下架'); } if ((int)$coupon['end_time'] > 0 && (int)$coupon['end_time'] < time()) { $this->error('优惠券已过期'); } if ((int)$coupon['total'] > 0 && (int)$coupon['received'] >= (int)$coupon['total']) { $this->error('优惠券已被领完'); } $has = Db::name('user_coupon')->where('user_id', $this->uid())->where('coupon_id', $couponId)->find(); if ($has) { $this->error('您已领取过该优惠券'); } Db::startTrans(); try { Db::name('user_coupon')->insert([ 'user_id' => $this->uid(), 'coupon_id' => $couponId, 'status' => 'unused', 'createtime' => time(), ]); Db::name('coupon')->where('id', $couponId)->setInc('received'); Db::commit(); } catch (\Exception $e) { Db::rollback(); $this->error('领取失败:' . $e->getMessage()); } $this->notify('user', $this->uid(), '优惠券领取成功', '「' . $coupon['name'] . '」已放入我的优惠券', 'system', 0); $this->success('领取成功'); } }