SchoolSpeciality.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\model\School;
  4. use app\api\model\book\Order;
  5. use app\api\service\Payment as PaymentService;
  6. use app\api\service\User as UserService;
  7. use app\common\enum\OrderType as OrderTypeEnum;
  8. use app\common\enum\book\order\PayType as OrderPayTypeEnum;
  9. use app\api\model\SchoolSpeciality as SchoolSpecialityModel;
  10. use app\common\service\Order as OrderService;
  11. use think\response\Json;
  12. /**
  13. * 学校专业控制器
  14. * Class SchoolSpeciality
  15. * @package app\api\controller
  16. */
  17. class SchoolSpeciality extends Controller
  18. {
  19. /**
  20. * 学校列表
  21. * @return \think\response\Json
  22. * @throws \think\db\exception\DbException
  23. */
  24. public function list()
  25. {
  26. // 获取列表数据
  27. $model = new SchoolSpecialityModel;
  28. $pageSize = $this->request->param('pageSize', 12);
  29. $list = $model->getList($this->request->param(), $pageSize);
  30. return $this->renderSuccess(compact('list'));
  31. }
  32. /**
  33. * 文章详情
  34. * @param int $articleId
  35. * @return array|\think\response\Json
  36. * @throws \app\common\exception\BaseException
  37. */
  38. public function detail(int $id)
  39. {
  40. $detail = SchoolSpecialityModel::getDetail($id);
  41. return $this->renderSuccess(compact('detail'));
  42. }
  43. /**
  44. * 学校专业选项
  45. * @return Json
  46. */
  47. public function options()
  48. {
  49. $model = new SchoolSpecialityModel;
  50. $list = $model->getOptionList($this->request->param());
  51. return $this->renderSuccess(compact('list'));
  52. }
  53. /**
  54. * 同类专业学校
  55. * @return Json
  56. * @throws \think\db\exception\DbException
  57. */
  58. public function sameList()
  59. {
  60. // 获取列表数据
  61. $model = new School;
  62. $pageSize = $this->request->param('pageSize', 12);
  63. $list = $model->getListBySpeciality($this->request->param(), $pageSize);
  64. return $this->renderSuccess(compact('list'));
  65. }
  66. /**
  67. * 获取热门专业
  68. * @return Json
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\DbException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. */
  73. public function hots()
  74. {
  75. $limit = $this->request->param('limit', 6);
  76. $list = (new SchoolSpecialityModel)->getHots(0, 'speciality_id,speciality_name,views', (int)$limit);
  77. return $this->renderSuccess(compact('list'));
  78. }
  79. /**
  80. * 报名
  81. * @return Json
  82. * @throws \app\common\exception\BaseException
  83. * @throws \think\db\exception\DataNotFoundException
  84. * @throws \think\db\exception\DbException
  85. * @throws \think\db\exception\ModelNotFoundException
  86. */
  87. public function book()
  88. {
  89. if (getPlatform() !== 'MP-WEIXIN') {
  90. return $this->renderError('很抱歉,报名功能暂时仅支持微信小程序端');
  91. }
  92. // 生成订单
  93. $model = new Order();
  94. if (!$model->createOrder($this->request->param())) {
  95. return $this->renderError($model->getError() ?: '报名提交失败');
  96. }
  97. // 充值状态提醒
  98. $order = ['order_id'=> $model['order_id'],'type'=>'普通订单','message' => '报名提交成功'];
  99. return $this->renderSuccess(compact('order'));
  100. }
  101. /**
  102. * @param int $order_id
  103. * @param int $payType
  104. * @return Json
  105. * @throws \app\common\exception\BaseException
  106. * @throws \think\db\exception\DataNotFoundException
  107. * @throws \think\db\exception\DbException
  108. * @throws \think\db\exception\ModelNotFoundException
  109. */
  110. public function bookPay(int $order_id, int $payType = OrderPayTypeEnum::WECHAT)
  111. {
  112. // 订单信息
  113. $model = new Order();
  114. $model = $model::getUserOrderDetail($order_id);
  115. if (!$model) {
  116. return $this->renderError($model->getError() ?: '报名订单不存在');
  117. }
  118. // 验证
  119. if(!$model->checkPay($model))
  120. {
  121. return $this->renderError($model->getError() ?: '订单支付失败');
  122. }
  123. $pilOrder = false;
  124. $userInfo = UserService::getCurrentLoginUser(true);
  125. $userId = isset($userInfo['user_id'])? $userInfo['user_id'] : 0;
  126. if($userId != $model['user_id'])
  127. {
  128. $pilOrder = true;
  129. $data['pil_user_id'] = $userId;
  130. $model->save($data);
  131. }
  132. // 构建微信支付
  133. try {
  134. $payment = PaymentService::wechat(
  135. $model['order_id'],
  136. $model['order_no'],
  137. $model['total_price'],
  138. OrderTypeEnum::BOOK
  139. );
  140. } catch (\Exception $exception) {
  141. $data = ['order_no'=> 'BK'.OrderService::createOrderNo()];
  142. $model->save($data);
  143. $payment = PaymentService::wechat(
  144. $model['order_id'],
  145. $model['order_no'],
  146. $model['total_price'],
  147. OrderTypeEnum::BOOK
  148. );
  149. }
  150. //
  151. $message = ['success' => '报名成功','type'=> $pilOrder? '代付订单':'普通订单', 'error' => '订单支付中'];
  152. return $this->renderSuccess(compact('payment', 'message'));
  153. }
  154. /**
  155. * @param int $orderId
  156. * @return Json
  157. * @throws \app\common\exception\BaseException
  158. * @throws \think\db\exception\DataNotFoundException
  159. * @throws \think\db\exception\DbException
  160. * @throws \think\db\exception\ModelNotFoundException
  161. */
  162. public function bookConfirm()
  163. {
  164. // 订单处理
  165. $model = new Order;
  166. $orderId = $this->request->param('order_id',0);
  167. $notieImg = $this->request->param('notice_img','');
  168. if(!$model->confirmOrder((int)$orderId, $notieImg)){
  169. return $this->renderError($model->getError() ?: '报名确认错误');
  170. }
  171. return $this->renderSuccess(['order_id'=>$orderId], '报名确认成功');
  172. }
  173. /**
  174. * 订单发货
  175. * @param int $orderId
  176. * @return Json
  177. * @throws \app\common\exception\BaseException
  178. * @throws \think\db\exception\DataNotFoundException
  179. * @throws \think\db\exception\DbException
  180. * @throws \think\db\exception\ModelNotFoundException
  181. */
  182. public function delivery()
  183. {
  184. // 订单处理
  185. $model = new Order;
  186. $orderId = $this->request->param('order_id',0);
  187. if(!$model->setDelivery((int)$orderId, $this->request->param())){
  188. return $this->renderError($model->getError() ?: '订单设置发货失败');
  189. }
  190. return $this->renderSuccess(['order_id'=>$orderId], '订单设置发货成功');
  191. }
  192. /**
  193. * 订单收货
  194. * @param int $orderId
  195. * @return Json
  196. * @throws \app\common\exception\BaseException
  197. * @throws \think\db\exception\DataNotFoundException
  198. * @throws \think\db\exception\DbException
  199. * @throws \think\db\exception\ModelNotFoundException
  200. */
  201. public function receipt()
  202. {
  203. // 订单处理
  204. $model = new Order;
  205. $orderId = $this->request->param('order_id',0);
  206. if(!$model->receiptOrder((int)$orderId)){
  207. return $this->renderError($model->getError() ?: '订单收货失败');
  208. }
  209. return $this->renderSuccess(['order_id'=>$orderId], '订单收货成功');
  210. }
  211. /**
  212. * 设置订单为可退款
  213. * @return Json
  214. * @throws \app\common\exception\BaseException
  215. * @throws \think\db\exception\DataNotFoundException
  216. * @throws \think\db\exception\DbException
  217. * @throws \think\db\exception\ModelNotFoundException
  218. */
  219. public function setRefund()
  220. {
  221. // 订单处理
  222. $model = new Order;
  223. $orderId = $this->request->param('order_id',0);
  224. if(!$model->setRefund((int)$orderId)){
  225. return $this->renderError($model->getError() ?: '报名订单设置退款错误');
  226. }
  227. return $this->renderSuccess(['order_id'=>$orderId], '订单成功设置为可退款');
  228. }
  229. /**
  230. * 订单退款提交
  231. * @param int $orderId
  232. * @return Json
  233. * @throws \app\common\exception\BaseException
  234. * @throws \think\db\exception\DataNotFoundException
  235. * @throws \think\db\exception\DbException
  236. * @throws \think\db\exception\ModelNotFoundException
  237. */
  238. public function refund()
  239. {
  240. // 订单处理
  241. $model = new Order;
  242. $orderId = $this->request->param('order_id',0);
  243. $refundResult = $this->request->param('refund_result','');
  244. if(!$model->refundOrder((int)$orderId, $refundResult)){
  245. return $this->renderError($model->getError() ?: '订单申请退款失败');
  246. }
  247. return $this->renderSuccess(['order_id'=>$orderId], '订单申请退款成功');
  248. }
  249. /**
  250. * 订单退款确认
  251. * @param int $orderId
  252. * @return Json
  253. * @throws \app\common\exception\BaseException
  254. * @throws \think\db\exception\DataNotFoundException
  255. * @throws \think\db\exception\DbException
  256. * @throws \think\db\exception\ModelNotFoundException
  257. */
  258. public function refundConfirm()
  259. {
  260. // 订单处理
  261. $model = new Order;
  262. $orderId = $this->request->param('order_id',0);
  263. $refundImg = $this->request->param('refund_img','');
  264. if(!$model->refundConfirm((int)$orderId, $refundImg)){
  265. return $this->renderError($model->getError() ?: '订单确认退款失败');
  266. }
  267. return $this->renderSuccess(['order_id'=>$orderId], '订单确认退款成功');
  268. }
  269. /**
  270. * 修改订单信息
  271. * @return Json
  272. */
  273. public function modifyOrder()
  274. {
  275. // 订单处理
  276. $model = new Order;
  277. $orderId = $this->request->param('order_id',0);
  278. if(!$model->modifyOrder((int)$orderId, $this->request->param())){
  279. return $this->renderError($model->getError() ?: '修改订单信息失败');
  280. }
  281. return $this->renderSuccess(['order_id'=>$orderId], '修改订单信息成功');
  282. }
  283. /**
  284. * 取消订单
  285. * @return Json
  286. */
  287. public function cancelOrder()
  288. {
  289. // 订单处理
  290. $model = new Order;
  291. $orderId = $this->request->param('order_id',0);
  292. if(!$model->cancelOrder((int)$orderId, $this->request->param('remark'))){
  293. return $this->renderError($model->getError() ?: '取消订单失败');
  294. }
  295. return $this->renderSuccess(['order_id'=>$orderId], '取消订单成功');
  296. }
  297. /**
  298. * 报名订单列表
  299. * @return Json
  300. * @throws \app\common\exception\BaseException
  301. * @throws \think\db\exception\DbException
  302. */
  303. public function orderList()
  304. {
  305. // 订单处理
  306. $model = new Order;
  307. $params = $this->request->param();
  308. $pageSize = $this->request->param('pageSize', 15);
  309. $type = $this->request->param('type', 1);
  310. $userInfo = UserService::getCurrentLoginUser(true);
  311. $userId = isset($userInfo['user_id'])? $userInfo['user_id'] : 0;
  312. if($type == 1){
  313. $params['book_user_id'] = $userId;
  314. }else{
  315. $params['user_id'] = $userId;
  316. }
  317. $list = $model->getList($params, (int)$pageSize);
  318. return $this->renderSuccess(compact('list'));
  319. }
  320. }