| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 |
- <?php
- namespace app\api\controller;
- use app\api\model\School;
- use app\api\model\book\Order;
- use app\api\service\Payment as PaymentService;
- use app\api\service\User as UserService;
- use app\common\enum\OrderType as OrderTypeEnum;
- use app\common\enum\book\order\PayType as OrderPayTypeEnum;
- use app\api\model\SchoolSpeciality as SchoolSpecialityModel;
- use app\common\service\Order as OrderService;
- use think\response\Json;
- /**
- * 学校专业控制器
- * Class SchoolSpeciality
- * @package app\api\controller
- */
- class SchoolSpeciality extends Controller
- {
- /**
- * 学校列表
- * @return \think\response\Json
- * @throws \think\db\exception\DbException
- */
- public function list()
- {
- // 获取列表数据
- $model = new SchoolSpecialityModel;
- $pageSize = $this->request->param('pageSize', 12);
- $list = $model->getList($this->request->param(), $pageSize);
- return $this->renderSuccess(compact('list'));
- }
- /**
- * 文章详情
- * @param int $articleId
- * @return array|\think\response\Json
- * @throws \app\common\exception\BaseException
- */
- public function detail(int $id)
- {
- $detail = SchoolSpecialityModel::getDetail($id);
- return $this->renderSuccess(compact('detail'));
- }
- /**
- * 学校专业选项
- * @return Json
- */
- public function options()
- {
- $model = new SchoolSpecialityModel;
- $list = $model->getOptionList($this->request->param());
- return $this->renderSuccess(compact('list'));
- }
- /**
- * 同类专业学校
- * @return Json
- * @throws \think\db\exception\DbException
- */
- public function sameList()
- {
- // 获取列表数据
- $model = new School;
- $pageSize = $this->request->param('pageSize', 12);
- $list = $model->getListBySpeciality($this->request->param(), $pageSize);
- return $this->renderSuccess(compact('list'));
- }
- /**
- * 获取热门专业
- * @return Json
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function hots()
- {
- $limit = $this->request->param('limit', 6);
- $list = (new SchoolSpecialityModel)->getHots(0, 'speciality_id,speciality_name,views', (int)$limit);
- return $this->renderSuccess(compact('list'));
- }
- /**
- * 报名
- * @return Json
- * @throws \app\common\exception\BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function book()
- {
- if (getPlatform() !== 'MP-WEIXIN') {
- return $this->renderError('很抱歉,报名功能暂时仅支持微信小程序端');
- }
- // 生成订单
- $model = new Order();
- if (!$model->createOrder($this->request->param())) {
- return $this->renderError($model->getError() ?: '报名提交失败');
- }
- // 充值状态提醒
- $order = ['order_id'=> $model['order_id'],'type'=>'普通订单','message' => '报名提交成功'];
- return $this->renderSuccess(compact('order'));
- }
- /**
- * @param int $order_id
- * @param int $payType
- * @return Json
- * @throws \app\common\exception\BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function bookPay(int $order_id, int $payType = OrderPayTypeEnum::WECHAT)
- {
- // 订单信息
- $model = new Order();
- $model = $model::getUserOrderDetail($order_id);
- if (!$model) {
- return $this->renderError($model->getError() ?: '报名订单不存在');
- }
- // 验证
- if(!$model->checkPay($model))
- {
- return $this->renderError($model->getError() ?: '订单支付失败');
- }
- $pilOrder = false;
- $userInfo = UserService::getCurrentLoginUser(true);
- $userId = isset($userInfo['user_id'])? $userInfo['user_id'] : 0;
- if($userId != $model['user_id'])
- {
- $pilOrder = true;
- $data['pil_user_id'] = $userId;
- $model->save($data);
- }
- // 构建微信支付
- try {
- $payment = PaymentService::wechat(
- $model['order_id'],
- $model['order_no'],
- $model['total_price'],
- OrderTypeEnum::BOOK
- );
- } catch (\Exception $exception) {
- $data = ['order_no'=> 'BK'.OrderService::createOrderNo()];
- $model->save($data);
- $payment = PaymentService::wechat(
- $model['order_id'],
- $model['order_no'],
- $model['total_price'],
- OrderTypeEnum::BOOK
- );
- }
- //
- $message = ['success' => '报名成功','type'=> $pilOrder? '代付订单':'普通订单', 'error' => '订单支付中'];
- return $this->renderSuccess(compact('payment', 'message'));
- }
- /**
- * @param int $orderId
- * @return Json
- * @throws \app\common\exception\BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function bookConfirm()
- {
- // 订单处理
- $model = new Order;
- $orderId = $this->request->param('order_id',0);
- $notieImg = $this->request->param('notice_img','');
- if(!$model->confirmOrder((int)$orderId, $notieImg)){
- return $this->renderError($model->getError() ?: '报名确认错误');
- }
- return $this->renderSuccess(['order_id'=>$orderId], '报名确认成功');
- }
- /**
- * 订单发货
- * @param int $orderId
- * @return Json
- * @throws \app\common\exception\BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function delivery()
- {
- // 订单处理
- $model = new Order;
- $orderId = $this->request->param('order_id',0);
- if(!$model->setDelivery((int)$orderId, $this->request->param())){
- return $this->renderError($model->getError() ?: '订单设置发货失败');
- }
- return $this->renderSuccess(['order_id'=>$orderId], '订单设置发货成功');
- }
- /**
- * 订单收货
- * @param int $orderId
- * @return Json
- * @throws \app\common\exception\BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function receipt()
- {
- // 订单处理
- $model = new Order;
- $orderId = $this->request->param('order_id',0);
- if(!$model->receiptOrder((int)$orderId)){
- return $this->renderError($model->getError() ?: '订单收货失败');
- }
- return $this->renderSuccess(['order_id'=>$orderId], '订单收货成功');
- }
- /**
- * 设置订单为可退款
- * @return Json
- * @throws \app\common\exception\BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function setRefund()
- {
- // 订单处理
- $model = new Order;
- $orderId = $this->request->param('order_id',0);
- if(!$model->setRefund((int)$orderId)){
- return $this->renderError($model->getError() ?: '报名订单设置退款错误');
- }
- return $this->renderSuccess(['order_id'=>$orderId], '订单成功设置为可退款');
- }
- /**
- * 订单退款提交
- * @param int $orderId
- * @return Json
- * @throws \app\common\exception\BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function refund()
- {
- // 订单处理
- $model = new Order;
- $orderId = $this->request->param('order_id',0);
- $refundResult = $this->request->param('refund_result','');
- if(!$model->refundOrder((int)$orderId, $refundResult)){
- return $this->renderError($model->getError() ?: '订单申请退款失败');
- }
- return $this->renderSuccess(['order_id'=>$orderId], '订单申请退款成功');
- }
- /**
- * 订单退款确认
- * @param int $orderId
- * @return Json
- * @throws \app\common\exception\BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function refundConfirm()
- {
- // 订单处理
- $model = new Order;
- $orderId = $this->request->param('order_id',0);
- $refundImg = $this->request->param('refund_img','');
- if(!$model->refundConfirm((int)$orderId, $refundImg)){
- return $this->renderError($model->getError() ?: '订单确认退款失败');
- }
- return $this->renderSuccess(['order_id'=>$orderId], '订单确认退款成功');
- }
- /**
- * 修改订单信息
- * @return Json
- */
- public function modifyOrder()
- {
- // 订单处理
- $model = new Order;
- $orderId = $this->request->param('order_id',0);
- if(!$model->modifyOrder((int)$orderId, $this->request->param())){
- return $this->renderError($model->getError() ?: '修改订单信息失败');
- }
- return $this->renderSuccess(['order_id'=>$orderId], '修改订单信息成功');
- }
- /**
- * 取消订单
- * @return Json
- */
- public function cancelOrder()
- {
- // 订单处理
- $model = new Order;
- $orderId = $this->request->param('order_id',0);
- if(!$model->cancelOrder((int)$orderId, $this->request->param('remark'))){
- return $this->renderError($model->getError() ?: '取消订单失败');
- }
- return $this->renderSuccess(['order_id'=>$orderId], '取消订单成功');
- }
- /**
- * 报名订单列表
- * @return Json
- * @throws \app\common\exception\BaseException
- * @throws \think\db\exception\DbException
- */
- public function orderList()
- {
- // 订单处理
- $model = new Order;
- $params = $this->request->param();
- $pageSize = $this->request->param('pageSize', 15);
- $type = $this->request->param('type', 1);
- $userInfo = UserService::getCurrentLoginUser(true);
- $userId = isset($userInfo['user_id'])? $userInfo['user_id'] : 0;
- if($type == 1){
- $params['book_user_id'] = $userId;
- }else{
- $params['user_id'] = $userId;
- }
- $list = $model->getList($params, (int)$pageSize);
- return $this->renderSuccess(compact('list'));
- }
- }
|