|
@@ -0,0 +1,251 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace app\api\controller\user;
|
|
|
+
|
|
|
+use app\admin\model\card\Card;
|
|
|
+use app\admin\model\card\CardOrder;
|
|
|
+use app\admin\model\card\CardProject;
|
|
|
+use app\admin\model\diagnosis\DiagnosisCate;
|
|
|
+use app\admin\model\diagnosis\DiagnosisService;
|
|
|
+use app\admin\model\user\UserCard;
|
|
|
+use app\admin\model\user\UserEquity;
|
|
|
+use app\models\user\UserBill;
|
|
|
+use crmeb\basic\BaseModel;
|
|
|
+use app\admin\model\system\{
|
|
|
+ SystemAttachment, ShippingTemplates
|
|
|
+};
|
|
|
+use app\admin\model\user\User;
|
|
|
+use app\Request;
|
|
|
+use crmeb\services\{
|
|
|
+ CacheService,
|
|
|
+ ExpressService,
|
|
|
+ SystemConfigService,
|
|
|
+ UtilService
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 优惠卡
|
|
|
+ * Class StoreOrderController
|
|
|
+ * @package app\api\controller\order
|
|
|
+ */
|
|
|
+class CardController
|
|
|
+{
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 优惠卡列表
|
|
|
+ * @param Request $request
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function list(Request $request)
|
|
|
+ {
|
|
|
+ $data = UtilService::getMore([
|
|
|
+ ['page', 1],
|
|
|
+ ['limit', 10]
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $list = Card::order('id DESC')->page($data['page'],$data['limit'])->select();
|
|
|
+
|
|
|
+ $list = count($list) > 0 ? $list->toArray() : [];
|
|
|
+
|
|
|
+ return app('json')->successful($list);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 优惠卡详情
|
|
|
+ * @param Request $request
|
|
|
+ * @param $id
|
|
|
+ * @return mixed
|
|
|
+ * @throws \think\db\exception\DataNotFoundException
|
|
|
+ * @throws \think\db\exception\DbException
|
|
|
+ * @throws \think\db\exception\ModelNotFoundException
|
|
|
+ */
|
|
|
+ public function details(Request $request,$id)
|
|
|
+ {
|
|
|
+ if (!$id) return app('json')->fail('传入id');
|
|
|
+
|
|
|
+ $details = Card::order('id DESC')->where('id', $id)->find()->toArray();
|
|
|
+
|
|
|
+ return app('json')->successful($details);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 小程序购买
|
|
|
+ *
|
|
|
+ * @param Request $request
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function routine(Request $request)
|
|
|
+ {
|
|
|
+ list($card_id, $type) = UtilService::postMore([['card_id', 0], ['type', 0]], $request, true);
|
|
|
+ switch ((int)$type) {
|
|
|
+ case 0: //支付购买会员卡
|
|
|
+ $order = CardOrder::addCard($request->uid(), $card_id, 'routine');
|
|
|
+ if (!$order) return app('json')->fail('订单生成失败!');
|
|
|
+ try {
|
|
|
+ return app('json')->successful(CardOrder::jsPay($order));
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ return app('json')->fail($e->getMessage());
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ return app('json')->fail('缺少参数');
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 公众号购买
|
|
|
+ *
|
|
|
+ * @param Request $request
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function wechat(Request $request)
|
|
|
+ {
|
|
|
+ list($card_id, $type, $from) = UtilService::postMore([['card_id', 0], ['type', 0]], $request, true);
|
|
|
+ switch ((int)$type) {
|
|
|
+ case 0: //支付充值余额
|
|
|
+ $order = CardOrder::addCard($request->uid(), $card_id, 'weixinh5');
|
|
|
+ if (!$order) return app('json')->fail('订单生成失败!');
|
|
|
+ try {
|
|
|
+ if ($from == 'weixinh5') {
|
|
|
+ $recharge = CardOrder::wxH5Pay($order);
|
|
|
+ } else {
|
|
|
+ $recharge = CardOrder::wxPay($order);
|
|
|
+ }
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ return app('json')->fail($e->getMessage());
|
|
|
+ }
|
|
|
+ return app('json')->successful(['type' => $from, 'data' => $recharge]);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ return app('json')->fail('缺少参数');
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用户优惠券
|
|
|
+ * @param Request $request
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function user_card(Request $request)
|
|
|
+ {
|
|
|
+ $data = UtilService::getMore([
|
|
|
+ ['status'],
|
|
|
+ ['page', 1],
|
|
|
+ ['limit', 10]
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $model = UserCard::alias('a')
|
|
|
+ ->field('a.*,b.name')
|
|
|
+ ->leftJoin('card b', 'a.card_id = b.id')
|
|
|
+ ->where('a.uid', $request->uid())
|
|
|
+ ->order('a.id DESC');
|
|
|
+
|
|
|
+ if ($data['status'] == 1){
|
|
|
+ $model = $model->where('a.status', 0);// 未使用
|
|
|
+ }elseif ($data['status'] == 2){
|
|
|
+ $model = $model->where('a.status', 1)->where('use', 1);//使用中
|
|
|
+ }elseif ($data['status'] ==3){
|
|
|
+ $model = $model->where('a.status', 1)->where('use', -1);//失效
|
|
|
+ }
|
|
|
+ $list = $model->page($data['page'], $data['limit'])->select();
|
|
|
+
|
|
|
+ $list = count($list) > 0 ? $list->toArray() : [];
|
|
|
+
|
|
|
+ return app('json')->successful($list);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 使用优惠卡
|
|
|
+ * @param Request $request
|
|
|
+ * @param $id
|
|
|
+ * @return mixed
|
|
|
+ * @throws \think\db\exception\DataNotFoundException
|
|
|
+ * @throws \think\db\exception\DbException
|
|
|
+ * @throws \think\db\exception\ModelNotFoundException
|
|
|
+ */
|
|
|
+ public function use_card(Request $request,$id)
|
|
|
+ {
|
|
|
+ if (!$id) return app('json')->fail('传入id');
|
|
|
+ $data = UtilService::postMore([
|
|
|
+ ['card'],
|
|
|
+ ['pwd']
|
|
|
+ ]);
|
|
|
+ BaseModel::beginTrans();
|
|
|
+ $user_card = UserCard::where('card', $data['card'])->where('pwd', $data['pwd'])->find();
|
|
|
+ if (!$user_card) return app('json')->fail('卡密账号错误');
|
|
|
+ if ($user_card['status'] > 0) return app('json')->fail('优惠卡已使用');
|
|
|
+
|
|
|
+ $card = Card::where('id', $user_card['card_id'])->find();
|
|
|
+ $card_pro = CardProject::where('card_id', $card['id'])->select();
|
|
|
+
|
|
|
+ if (!$user_card) return app('json')->fail('优惠卡不存在');
|
|
|
+ $uid = $user_card['uid'];
|
|
|
+
|
|
|
+ if ($user_card['uid'] != $request->uid()){
|
|
|
+ $user_card['uid'] = $request->uid();
|
|
|
+ }
|
|
|
+ $user_card['status'] = 1;
|
|
|
+ $user_card['start_time'] = time();
|
|
|
+ $user_card['end_time'] = time() + 86400*$card['time'];
|
|
|
+
|
|
|
+ try {
|
|
|
+ $res = $user_card->save();
|
|
|
+ foreach ($card_pro as $item)
|
|
|
+ {
|
|
|
+ UserEquity::create([
|
|
|
+ 'uid' => $request->uid(),
|
|
|
+ 'user_card_id' => $id,
|
|
|
+ 'c_id' => $item['c_id'],
|
|
|
+ 'type' => $item['type'],
|
|
|
+ 'number' => $item['number'],
|
|
|
+ 'start_week' => $card['start_week'],
|
|
|
+ 'end_week' => $card['end_week'],
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ UserBill::expend('使用优惠卡', $request->uid(), 'now_money', 'card', 0, '', 0, '使用优惠卡'.$card['name'].',优惠卡原拥有人'.$uid);
|
|
|
+ BaseModel::commitTrans();
|
|
|
+ return app('json')->success('使用成功');
|
|
|
+ } catch (\Exception $e) {
|
|
|
+
|
|
|
+ BaseModel::rollbackTrans();
|
|
|
+ return app('json')->fail($e->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 使用优惠卡后,用户的拥有详情
|
|
|
+ * @param Request $request
|
|
|
+ * @param $id
|
|
|
+ * @return mixed
|
|
|
+ * @throws \think\db\exception\DataNotFoundException
|
|
|
+ * @throws \think\db\exception\DbException
|
|
|
+ * @throws \think\db\exception\ModelNotFoundException
|
|
|
+ */
|
|
|
+ public function user_card_details(Request $request,$id)
|
|
|
+ {
|
|
|
+ if (!$id) return app('json')->fail('传入id');
|
|
|
+ $list = UserEquity::alias('a')
|
|
|
+ ->field('a.*,b.name')
|
|
|
+ ->leftJoin('diagnosis_cate b', 'a.c_id = b.id')
|
|
|
+ ->where('a.user_card_id', $id)
|
|
|
+ ->where('a.uid', $request->uid())
|
|
|
+ ->select();
|
|
|
+ if (count($list) == 0) return app('json')->fail('优惠卡未使用');
|
|
|
+
|
|
|
+ $list = count($list) > 0 ? $list->toArray() : [];
|
|
|
+
|
|
|
+ return app('json')->successful($list);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|