You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Oncecard.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. namespace app\controller\api\project;
  3. use app\controller\store\project\logic\order\OrderLogic;
  4. use app\model\store\logs\ProjectWriteOffModel;
  5. use app\controller\api\AuthController;
  6. use app\model\store\project\MemberOnceCardModel;
  7. use app\model\store\project\OnceCardModel;
  8. use app\model\user\User;
  9. use app\Request;
  10. use app\services\activity\coupon\StoreCouponUserServices;
  11. use think\facade\Db;
  12. class Oncecard extends AuthController
  13. {
  14. //获取我的次卡列表
  15. public function getMemberOncecardList(Request $request)
  16. {
  17. $member_id = (int)$request->uid();
  18. $type = $this->request->param('type/d');
  19. $page = $this->request->param('page/d', 1);
  20. $limit = $this->request->param('limit/d', 15);
  21. $where = [
  22. ['member_id', '=', $member_id]
  23. ];
  24. $today = date('Y-m-d', time());
  25. if ($type == 0) {
  26. $where[] = ['status', '=', 1];
  27. $where[] = ['valid_end_time', '>=', $today];
  28. $query = MemberOnceCardModel::where($where);
  29. $total = $query->count();
  30. } else {
  31. $where1 = $where;
  32. $where2 = $where;
  33. $where1[] = ['status', '=', 8];
  34. $where2[] = ['valid_end_time', '<', $today];
  35. $query = MemberOnceCardModel::whereOr([$where1, $where2]);
  36. $total = $query->count();
  37. }
  38. $datas = $query->order(['member_once_card_id' => 'desc'])->page($page)->limit($limit)->select()->append(['status_another'])->toArray();
  39. // $query2 = new MemberOnceCardModel();
  40. // foreach ($datas as $k => $v) {
  41. // $datas[$k]['status_another'] = $query2->getStatusAnotherAttr(null, $v);
  42. // }
  43. return $this->success(['rows' => $datas, 'total' => $total]);
  44. }
  45. public function getMemberQrcode(Request $request)
  46. {
  47. $user = new User();
  48. $user = $user->where('uid', $request->uid())->find();
  49. if (empty($user)) {
  50. return $this->fail('用户不存在');
  51. }
  52. $code = rand(111111, 999999);
  53. $user->code = $code;
  54. $user->save();
  55. return $this->success([
  56. 'member_id' => $request->uid(),
  57. 'code' => 'https://api.qrserver.com/v1/create-qr-code/?size=180x180&data=' . $code
  58. ]);
  59. }
  60. public function getOncecardList()
  61. {
  62. $where = [
  63. ['is_delete', '=', 0],
  64. ['status', '=', 1],
  65. ];
  66. $page = $this->request->param('page/d', 1);
  67. $limit = $this->request->param('limit/d', 15);
  68. $once_card_model = new \app\model\store\project\OnceCardModel();
  69. $order = ['sort' => 'desc', 'add_time' => 'desc'];
  70. $field = 'once_card_id,name,cover_img,price,worth,coin,is_flash_sale,flash_price,flash_num,start_time,end_time,fictitious_take_count';
  71. $append = ['is_flash_sale_expire', 'flash_sale_djs', 'save_money'];
  72. $datas = $once_card_model->where($where)->order($order)
  73. ->field($field)
  74. ->limit($limit)
  75. ->page($page)
  76. ->select()
  77. ->append($append)
  78. ->toArray();
  79. $return['data'] = $datas;
  80. $return['total'] = $once_card_model->where($where)->count();
  81. return $this->success($return);
  82. }
  83. public function getOncecardDetail()
  84. {
  85. $once_card_id = $this->request->param('once_card_id/d', 0);
  86. if ($once_card_id == 0) return $this->fail('次卡不存在');
  87. if (null === $once_card = OnceCardModel::find($once_card_id)) return $this->fail('次卡不存在');
  88. if ($once_card->is_delete != 0) return $this->fail('次卡不存在');
  89. $items = \app\model\store\project\OnceCardItemModel::with(['project' => function ($query) {
  90. $query->field('project_id,cover_img,abbreviation,market_price');
  91. }])->where([
  92. ['once_card_id', '=', $once_card->once_card_id],
  93. ['is_delete', '=', 0]
  94. ])->field('project_id,num')->select()->toArray();
  95. $detail = [
  96. 'name' => $once_card->name,
  97. 'price' => $once_card->price,
  98. 'is_flash_sale' => $once_card->is_flash_sale,
  99. 'flash_price' => $once_card->flash_price,
  100. 'is_flash_sale_expire' => $once_card->is_flash_sale_expire,
  101. 'flash_sale_djs' => $once_card->flash_sale_djs,
  102. 'save_money' => $once_card->save_money,
  103. 'valid_time' => $once_card->valid_time,
  104. 'valid_unit' => $once_card->valid_unit,
  105. 'fictitious_take_count' => $once_card->fictitious_take_count,
  106. 'cover_img' => $once_card->cover_img,
  107. 'rotation' => $once_card->rotation,
  108. 'items' => $items,
  109. 'use_info' => $once_card->use_info,
  110. ];
  111. return $this->success(['detail' => $detail]);
  112. }
  113. //计算价格
  114. public function computed(Request $request)
  115. {
  116. $uid = $request->uid();
  117. $member_once_card_id = $this->request->param('member_once_card_id/d');
  118. $check = MemberOnceCardModel::where('member_once_card_id', $member_once_card_id)->where('member_id', $uid)->field('once_card_id')->find();
  119. if (empty($check)) {
  120. return $this->fail('记录不存在');
  121. }
  122. $once_card = OnceCardModel::find($check['once_card_id']);
  123. if (empty($once_card)) return $this->fail('次卡不存在');
  124. if ($once_card->is_delete != 0) return $this->fail('次卡不存在');
  125. if ($once_card->status != 1) return $this->fail('次卡已下架');
  126. //秒杀活动时间内判断数量
  127. if ($flash_status = $this->checkFlashStatus($once_card)) {
  128. if ($once_card->flash_num < 1) return $this->fail('秒杀数量不足够');
  129. }
  130. //判断总价
  131. $total_price = $price = $flash_status ? $once_card->flash_price : $once_card->price;
  132. $integral = 0;
  133. $balance = 0;
  134. //判断优惠卷
  135. $coupon_id = $this->request->param('coupon_id/d', 0);
  136. $couponBalance = 0;
  137. if (!empty($coupon_id)) {
  138. /** @var StoreCouponUserServices $couponServices */
  139. $couponServices = app()->make(StoreCouponUserServices::class);
  140. $couponInfo = $couponServices->getOne([['id', '=', $coupon_id], ['uid', '=', $uid], ['is_fail', '=', 0], ['status', '=', 0], ['start_time', '<=', $_SERVER['REQUEST_TIME']], ['end_time', '>=', $_SERVER['REQUEST_TIME']]], '*', ['issue']);
  141. if (!$couponInfo) {
  142. return $this->fail('优惠券不存在');
  143. }
  144. $type = $couponInfo['applicable_type'] ?? 0;
  145. if ($couponInfo['use_min_price'] > $total_price || $type != 0) {//只能是通用券
  146. return $this->fail('不满足优惠劵的使用条件!');
  147. }
  148. //满减券
  149. if ($couponInfo['coupon_type'] == 1) {
  150. $couponPrice = $couponInfo['coupon_price'];
  151. } else {
  152. if ($couponInfo['coupon_price'] <= 0) {//0折
  153. $couponPrice = $total_price;
  154. } else if ($couponInfo['coupon_price'] >= 100) {
  155. $couponPrice = 0;
  156. } else {
  157. $truePrice = (float)bcmul((string)$total_price, bcdiv((string)$couponInfo['coupon_price'], '100', 2), 2);
  158. $couponPrice = (float)bcsub((string)$total_price, (string)$truePrice, 2);
  159. }
  160. }
  161. if ($couponPrice < $total_price) {
  162. $need_pay = (float)bcsub((string)$total_price, (string)$couponPrice, 2);
  163. } else {
  164. $couponPrice = $total_price;
  165. $need_pay = 0;
  166. }
  167. $couponBalance = $couponPrice;
  168. } else {
  169. //获取需要支付的金额
  170. $need_pay = round((($total_price) * 100 - $couponBalance * 100 - $balance * 100) / 100, 2);
  171. }
  172. return ['need_pay' => $need_pay, 'couponBalance' => $couponBalance];
  173. }
  174. protected function checkFlashStatus($once_card)
  175. {
  176. return ($once_card->is_flash_sale == 1 && $once_card->is_flash_sale_expire == 0);
  177. }
  178. //下单
  179. public function create()
  180. {
  181. $once_card_id = $this->request->param('once_card_id/d', 0);
  182. $coupon_id = $this->request->param('coupon_id/d', 0);
  183. $params = [
  184. 'member_id' => $this->request->uid(),
  185. 'coupon_id' => $coupon_id,
  186. 'once_card_id' => $once_card_id,
  187. 'ip' => $this->request->ip(),
  188. 'mark' => $this->request->param('mark', ''),
  189. 'payType' => $this->request->param('payType', ''),
  190. 'is_coin' => $this->request->param('is_coin/d', 0),
  191. ];
  192. Db::startTrans();
  193. try {
  194. $order_logic = OrderLogic::init();
  195. $order_id = $order_logic['once_card']->createOrder($params);
  196. Db::commit();
  197. return $this->success(['order_id' => $order_id]);
  198. } catch (\Exception $e) {
  199. Db::rollback();
  200. return $this->fail($e->getMessage());
  201. }
  202. }
  203. /******************************** 下面接口暂未对接 ***************************************/
  204. public function getMemberOncecardDetail()
  205. {
  206. $member_once_card_id = $this->request->param('member_once_card_id/d');
  207. if (empty($member_once_card_id)) {
  208. return $this->fail('请选择要查看的次卡');
  209. }
  210. if (null === $card = MemberOnceCardModel::find($member_once_card_id)) {
  211. return $this->fail('请选择要查看的次卡');
  212. }
  213. $card->items;
  214. $card->append(['status_means', 'status_another', 'pay_time_format', 'pay_type_mean'])->toArray();
  215. return $this->success([
  216. 'detail' => $card
  217. ]);
  218. }
  219. public function getMemberOncecardLogs()
  220. {
  221. $member_once_card_id = $this->request->param('member_once_card_id/d');
  222. if (empty($member_once_card_id)) {
  223. return $this->fail('请选择要查看的次卡');
  224. }
  225. if (null === $card = MemberOnceCardModel::find($member_once_card_id)) {
  226. return $this->fail('请选择要查看的次卡');
  227. }
  228. $where = [
  229. ['member_id', '=', $this->request->uid()],
  230. ['source', '=', 'once_card'],
  231. ['member_once_card_id', '=', $member_once_card_id]
  232. ];
  233. $datas = ProjectWriteOffModel::where($where)->select()->append(['add_time_format'])->toArray();
  234. return $this->success([
  235. 'datas' => $datas
  236. ]);
  237. }
  238. public function cancelOrder()
  239. {
  240. $once_card_order_id = $this->request->param('once_card_order_id/d', 0);
  241. $params = [
  242. 'member_id' => $this->request->uid(),
  243. 'once_card_order_id' => $once_card_order_id,
  244. ];
  245. Db::startTrans();
  246. try {
  247. $order_logic = OrderLogic::init();
  248. $order_logic['once_card']->cancelOrder($params);
  249. Db::commit();
  250. return $this->success('取消订单成功');
  251. } catch (\Exception $e) {
  252. Db::rollback();
  253. return $this->fail($e->getMessage());
  254. }
  255. }
  256. }