123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- <?php
-
- namespace app\controller\api\project;
-
- use app\controller\store\project\logic\order\OrderLogic;
- use app\model\store\logs\ProjectWriteOffModel;
- use app\controller\api\AuthController;
- use app\model\store\project\MemberOnceCardModel;
- use app\model\store\project\OnceCardModel;
- use app\model\user\User;
- use app\Request;
- use app\services\activity\coupon\StoreCouponUserServices;
- use think\facade\Db;
-
- class Oncecard extends AuthController
- {
-
- //获取我的次卡列表
- public function getMemberOncecardList(Request $request)
- {
- $member_id = (int)$request->uid();
- $type = $this->request->param('type/d');
- $page = $this->request->param('page/d', 1);
- $limit = $this->request->param('limit/d', 15);
- $where = [
- ['member_id', '=', $member_id]
- ];
- $today = date('Y-m-d', time());
-
- if ($type == 0) {
- $where[] = ['status', '=', 1];
- $where[] = ['valid_end_time', '>=', $today];
- $query = MemberOnceCardModel::where($where);
- $total = $query->count();
- } else {
- $where1 = $where;
- $where2 = $where;
- $where1[] = ['status', '=', 8];
- $where2[] = ['valid_end_time', '<', $today];
- $query = MemberOnceCardModel::whereOr([$where1, $where2]);
- $total = $query->count();
- }
- $datas = $query->order(['member_once_card_id' => 'desc'])->page($page)->limit($limit)->select()->append(['status_another'])->toArray();
-
- // $query2 = new MemberOnceCardModel();
- // foreach ($datas as $k => $v) {
- // $datas[$k]['status_another'] = $query2->getStatusAnotherAttr(null, $v);
- // }
- return $this->success(['rows' => $datas, 'total' => $total]);
- }
-
- public function getMemberQrcode(Request $request)
- {
- $user = new User();
- $user = $user->where('uid', $request->uid())->find();
- if (empty($user)) {
- return $this->fail('用户不存在');
- }
- $code = rand(111111, 999999);
- $user->code = $code;
- $user->save();
- return $this->success([
- 'member_id' => $request->uid(),
- 'code' => 'https://api.qrserver.com/v1/create-qr-code/?size=180x180&data=' . $code
- ]);
- }
-
- public function getOncecardList()
- {
- $where = [
- ['is_delete', '=', 0],
- ['status', '=', 1],
- ];
- $page = $this->request->param('page/d', 1);
- $limit = $this->request->param('limit/d', 15);
- $once_card_model = new \app\model\store\project\OnceCardModel();
- $order = ['sort' => 'desc', 'add_time' => 'desc'];
- $field = 'once_card_id,name,cover_img,price,worth,coin,is_flash_sale,flash_price,flash_num,start_time,end_time,fictitious_take_count';
- $append = ['is_flash_sale_expire', 'flash_sale_djs', 'save_money'];
- $datas = $once_card_model->where($where)->order($order)
- ->field($field)
- ->limit($limit)
- ->page($page)
- ->select()
- ->append($append)
- ->toArray();
- $return['data'] = $datas;
- $return['total'] = $once_card_model->where($where)->count();
- return $this->success($return);
- }
-
- public function getOncecardDetail()
- {
- $once_card_id = $this->request->param('once_card_id/d', 0);
- if ($once_card_id == 0) return $this->fail('次卡不存在');
-
- if (null === $once_card = OnceCardModel::find($once_card_id)) return $this->fail('次卡不存在');
- if ($once_card->is_delete != 0) return $this->fail('次卡不存在');
-
- $items = \app\model\store\project\OnceCardItemModel::with(['project' => function ($query) {
- $query->field('project_id,cover_img,abbreviation,market_price');
- }])->where([
- ['once_card_id', '=', $once_card->once_card_id],
- ['is_delete', '=', 0]
- ])->field('project_id,num')->select()->toArray();
-
- $detail = [
- 'name' => $once_card->name,
- 'price' => $once_card->price,
- 'is_flash_sale' => $once_card->is_flash_sale,
- 'flash_price' => $once_card->flash_price,
- 'is_flash_sale_expire' => $once_card->is_flash_sale_expire,
- 'flash_sale_djs' => $once_card->flash_sale_djs,
- 'save_money' => $once_card->save_money,
- 'valid_time' => $once_card->valid_time,
- 'valid_unit' => $once_card->valid_unit,
- 'fictitious_take_count' => $once_card->fictitious_take_count,
- 'cover_img' => $once_card->cover_img,
- 'rotation' => $once_card->rotation,
- 'items' => $items,
- 'use_info' => $once_card->use_info,
- ];
-
- return $this->success(['detail' => $detail]);
- }
-
- //计算价格
- public function computed(Request $request)
- {
- $uid = $request->uid();
- $member_once_card_id = $this->request->param('member_once_card_id/d');
- $check = MemberOnceCardModel::where('member_once_card_id', $member_once_card_id)->where('member_id', $uid)->field('once_card_id')->find();
- if (empty($check)) {
- return $this->fail('记录不存在');
- }
- $once_card = OnceCardModel::find($check['once_card_id']);
- if (empty($once_card)) return $this->fail('次卡不存在');
- if ($once_card->is_delete != 0) return $this->fail('次卡不存在');
- if ($once_card->status != 1) return $this->fail('次卡已下架');
- //秒杀活动时间内判断数量
- if ($flash_status = $this->checkFlashStatus($once_card)) {
- if ($once_card->flash_num < 1) return $this->fail('秒杀数量不足够');
- }
-
- //判断总价
- $total_price = $price = $flash_status ? $once_card->flash_price : $once_card->price;
-
- $integral = 0;
- $balance = 0;
-
- //判断优惠卷
- $coupon_id = $this->request->param('coupon_id/d', 0);
- $couponBalance = 0;
- if (!empty($coupon_id)) {
- /** @var StoreCouponUserServices $couponServices */
- $couponServices = app()->make(StoreCouponUserServices::class);
- $couponInfo = $couponServices->getOne([['id', '=', $coupon_id], ['uid', '=', $uid], ['is_fail', '=', 0], ['status', '=', 0], ['start_time', '<=', $_SERVER['REQUEST_TIME']], ['end_time', '>=', $_SERVER['REQUEST_TIME']]], '*', ['issue']);
- if (!$couponInfo) {
- return $this->fail('优惠券不存在');
- }
- $type = $couponInfo['applicable_type'] ?? 0;
- if ($couponInfo['use_min_price'] > $total_price || $type != 0) {//只能是通用券
- return $this->fail('不满足优惠劵的使用条件!');
- }
- //满减券
- if ($couponInfo['coupon_type'] == 1) {
- $couponPrice = $couponInfo['coupon_price'];
- } else {
- if ($couponInfo['coupon_price'] <= 0) {//0折
- $couponPrice = $total_price;
- } else if ($couponInfo['coupon_price'] >= 100) {
- $couponPrice = 0;
- } else {
- $truePrice = (float)bcmul((string)$total_price, bcdiv((string)$couponInfo['coupon_price'], '100', 2), 2);
- $couponPrice = (float)bcsub((string)$total_price, (string)$truePrice, 2);
- }
- }
- if ($couponPrice < $total_price) {
- $need_pay = (float)bcsub((string)$total_price, (string)$couponPrice, 2);
- } else {
- $couponPrice = $total_price;
- $need_pay = 0;
- }
- $couponBalance = $couponPrice;
- } else {
- //获取需要支付的金额
- $need_pay = round((($total_price) * 100 - $couponBalance * 100 - $balance * 100) / 100, 2);
- }
- return ['need_pay' => $need_pay, 'couponBalance' => $couponBalance];
- }
-
- protected function checkFlashStatus($once_card)
- {
- return ($once_card->is_flash_sale == 1 && $once_card->is_flash_sale_expire == 0);
- }
-
- //下单
- public function create()
- {
- $once_card_id = $this->request->param('once_card_id/d', 0);
- $coupon_id = $this->request->param('coupon_id/d', 0);
- $params = [
- 'member_id' => $this->request->uid(),
- 'coupon_id' => $coupon_id,
- 'once_card_id' => $once_card_id,
- 'ip' => $this->request->ip(),
- 'mark' => $this->request->param('mark', ''),
- 'payType' => $this->request->param('payType', ''),
- 'is_coin' => $this->request->param('is_coin/d', 0),
- ];
- Db::startTrans();
- try {
- $order_logic = OrderLogic::init();
- $order_id = $order_logic['once_card']->createOrder($params);
- Db::commit();
- return $this->success(['order_id' => $order_id]);
- } catch (\Exception $e) {
- Db::rollback();
- return $this->fail($e->getMessage());
- }
- }
-
- /******************************** 下面接口暂未对接 ***************************************/
-
- public function getMemberOncecardDetail()
- {
- $member_once_card_id = $this->request->param('member_once_card_id/d');
- if (empty($member_once_card_id)) {
- return $this->fail('请选择要查看的次卡');
- }
- if (null === $card = MemberOnceCardModel::find($member_once_card_id)) {
- return $this->fail('请选择要查看的次卡');
- }
-
- $card->items;
- $card->append(['status_means', 'status_another', 'pay_time_format', 'pay_type_mean'])->toArray();
- return $this->success([
- 'detail' => $card
- ]);
- }
-
- public function getMemberOncecardLogs()
- {
- $member_once_card_id = $this->request->param('member_once_card_id/d');
- if (empty($member_once_card_id)) {
- return $this->fail('请选择要查看的次卡');
- }
- if (null === $card = MemberOnceCardModel::find($member_once_card_id)) {
- return $this->fail('请选择要查看的次卡');
- }
-
- $where = [
- ['member_id', '=', $this->request->uid()],
- ['source', '=', 'once_card'],
- ['member_once_card_id', '=', $member_once_card_id]
- ];
- $datas = ProjectWriteOffModel::where($where)->select()->append(['add_time_format'])->toArray();
- return $this->success([
- 'datas' => $datas
- ]);
- }
-
- public function cancelOrder()
- {
- $once_card_order_id = $this->request->param('once_card_order_id/d', 0);
- $params = [
- 'member_id' => $this->request->uid(),
- 'once_card_order_id' => $once_card_order_id,
- ];
- Db::startTrans();
- try {
- $order_logic = OrderLogic::init();
- $order_logic['once_card']->cancelOrder($params);
- Db::commit();
- return $this->success('取消订单成功');
- } catch (\Exception $e) {
- Db::rollback();
- return $this->fail($e->getMessage());
- }
- }
-
- }
|