Order.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: thinkphp <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\model\book;
  13. use app\api\model\School;
  14. use app\api\model\SchoolSpeciality;
  15. use app\api\model\SpecialityBook;
  16. use app\api\model\User;
  17. use app\api\service\User as UserService;
  18. use app\common\library\wechat\WxPay;
  19. use app\common\model\book\Order as OrderModel;
  20. use app\common\model\wxapp\Setting as WxappSettingModel;
  21. use app\common\service\Order as OrderService;
  22. use app\common\enum\book\order\PayStatus as PayStatusEnum;
  23. use app\common\exception\BaseException;
  24. use think\model\relation\HasOne;
  25. /**
  26. * 用户报名订单模型
  27. * Class Order
  28. * @package app\api\model\book
  29. */
  30. class Order extends OrderModel
  31. {
  32. protected $globalScope = [''];
  33. /**
  34. * 隐藏字段
  35. * @var array
  36. */
  37. protected $hidden = [
  38. 'transaction_id',
  39. 'create_time',
  40. 'update_time',
  41. ];
  42. /**
  43. * 关联学校
  44. * @return HasOne
  45. */
  46. public function school(): HasOne
  47. {
  48. return $this->HasOne('app\\common\\model\\School','id','school_id')
  49. ->field('id,school_name as book_school_name,logo as school_logo')
  50. ->bind(['book_school_name','school_logo']);
  51. }
  52. /**
  53. * 代付用户
  54. * @return HasOne
  55. */
  56. public function pilUser(): HasOne
  57. {
  58. return $this->HasOne('app\\common\\model\\User','user_id','pil_user_id')
  59. ->field('user_id,nick_name as pil_user_nickname')
  60. ->bind(['pil_user_nickname']);
  61. }
  62. /**
  63. * 关联学校
  64. * @return HasOne
  65. */
  66. public function speciality(): HasOne
  67. {
  68. return $this->HasOne('app\\common\\model\\SchoolSpeciality','speciality_id','speciality_id')
  69. ->field('speciality_id,speciality_name')
  70. ->bind(['speciality_name']);
  71. }
  72. /**
  73. * 获取订单列表
  74. * @return \think\Paginator
  75. * @throws BaseException
  76. * @throws \think\db\exception\DbException
  77. */
  78. public function getList($params=[], $listRows=15, $field='')
  79. {
  80. $field = $field? $field: 'order_id,order_no,book_user_id,user_id,school_id,speciality_id,pil_user_id,price,total_price,pay_price,pay_type,pay_time,notice_img,receipt_name,receipt_mobile,receipt_address,refund_status,create_time,status';
  81. // 获取列表数据
  82. $list = $this->with(['school','speciality','pilUser'])
  83. ->where('status', '>=', PayStatusEnum::PENDING)
  84. ->where(function ($query) use($params){
  85. // 用户订单
  86. $userId = isset($params['book_user_id'])? $params['book_user_id'] : 0;
  87. if($userId>0){
  88. $query->where('book_user_id','=', $userId);
  89. }
  90. // 关键词
  91. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  92. if($keyword){
  93. $query->where('order_no','like', "%{$keyword}%");
  94. }
  95. // 老师订单
  96. $teacherId = isset($params['user_id'])? $params['user_id'] : 0;
  97. if($teacherId>0){
  98. $query->where('user_id','=', $teacherId);
  99. }
  100. $status = isset($params['status'])? $params['status'] : 0;
  101. if($status>0 || $status == -2){
  102. if($status == 1){
  103. $status = [1,2];
  104. }
  105. $status = !is_array($status)? [$status] : $status;
  106. $query->whereIn('status', $status);
  107. }
  108. })
  109. ->field($field)
  110. ->order(['create_time' => 'desc'])
  111. ->paginate($listRows)
  112. ->each(function($item, $k){
  113. $item['school_logo'] = $item['school_logo']? getPreview($item['school_logo']) : '';
  114. $item['pilUser'] = !is_null($item['pilUser'])? $item['pilUser'] : [];
  115. // $item['create_time_text'] = $item['create_time']? date('Y/m/d H:i:s', $item['create_time']) : '';
  116. });
  117. $list->hidden(['fields','update_time','is_settled']);
  118. return $list;
  119. }
  120. /**
  121. * 获取订单详情(待付款状态)
  122. * @param $orderNo
  123. * @return array|null|static
  124. */
  125. public static function getPayDetail(string $orderNo)
  126. {
  127. return self::detail(['order_no' => $orderNo, 'status' => PayStatusEnum::PENDING]);
  128. }
  129. /**
  130. * 获取用户报名订单详情(含关联数据)
  131. * @param int $orderId 订单ID
  132. * @return Order|array|null
  133. * @throws BaseException
  134. * @throws \think\db\exception\DataNotFoundException
  135. * @throws \think\db\exception\DbException
  136. * @throws \think\db\exception\ModelNotFoundException
  137. */
  138. public static function getUserOrderDetail(int $orderId)
  139. {
  140. // 查询订单记录
  141. $order = static::getDetail($orderId);
  142. // 该订单是否允许申请售后
  143. $order['isAllowRefund'] = 1;
  144. return $order;
  145. }
  146. /**
  147. * 获取用户报名订单详情(仅订单记录)
  148. * @param int $orderId
  149. * @param array $with
  150. * @return Order|array|null
  151. * @throws BaseException
  152. */
  153. public static function getDetail(int $orderId, array $with = [])
  154. {
  155. // 查询订单记录
  156. $order = static::detail($orderId, $with);
  157. empty($order) && throwError('订单不存在');
  158. return $order;
  159. }
  160. /**
  161. * 创建报名订单
  162. * @param array|null $params
  163. * @return bool|int
  164. * @throws BaseException
  165. * @throws \think\db\exception\DataNotFoundException
  166. * @throws \think\db\exception\DbException
  167. * @throws \think\db\exception\ModelNotFoundException
  168. */
  169. public function createOrder(array $params)
  170. {
  171. $teacherUserId = isset($params['user_id'])? intval($params['user_id']) : 0;
  172. $teacher = User::detail($teacherUserId,['info']);
  173. $teacherInfo = isset($teacher['info'])? $teacher['info'] : [];
  174. if(empty($teacherUserId) || empty($teacher)){
  175. $this->error = '报名咨询老师参数错误';
  176. return false;
  177. }
  178. $schoolId = isset($params['school_id'])? intval($params['school_id']) : 0;
  179. $specialityId = isset($params['speciality_id'])? intval($params['speciality_id']) : 0;
  180. if($schoolId<=0 || $specialityId <= 0){
  181. $this->error = '报名学校或报名专业参数错误';
  182. return false;
  183. }
  184. $school = School::detail($schoolId);
  185. if(empty($school)){
  186. $this->error = '报名学校不存在';
  187. return false;
  188. }
  189. // 表单参数验证
  190. $realName = isset($params['real_name'])? $params['real_name'] : '';
  191. if(empty($realName)){
  192. $this->error = '请填写报名用户真实姓名';
  193. return false;
  194. }
  195. $gender = isset($params['gender'])? intval($params['gender']) : 0;
  196. if($gender<=0){
  197. $this->error = '请选择报名用户性别';
  198. return false;
  199. }
  200. $schoolName = isset($params['school_name'])? $params['school_name'] : '';
  201. if(empty($schoolName)){
  202. $this->error = '请填写报名用户现读学校';
  203. return false;
  204. }
  205. $realName = isset($params['real_name'])? $params['real_name'] : '';
  206. if(empty($realName)){
  207. $this->error = '请填写报名用户真实姓名';
  208. return false;
  209. }
  210. $receiptName = isset($params['receipt_name'])? $params['receipt_name'] : '';
  211. if(empty($receiptName)){
  212. $this->error = '请填写收货人姓名';
  213. return false;
  214. }
  215. $receiptMobile = isset($params['receipt_mobile'])? $params['receipt_mobile'] : '';
  216. if(empty($receiptMobile)){
  217. $this->error = '请填写收货人联系电话';
  218. return false;
  219. }
  220. $receiptAddress = isset($params['receipt_address'])? $params['receipt_address'] : '';
  221. if(empty($receiptAddress)){
  222. $this->error = '请填写收货人地址';
  223. return false;
  224. }
  225. $trade = \app\store\model\Setting::getItem('trade');
  226. $price = isset($trade['books']['book_cost'])? floatval($trade['books']['book_cost']) : 0;
  227. if($price<=0){
  228. $this->error = '请先后台设置报名费用';
  229. return false;
  230. }
  231. // 专业参数验证
  232. $speciality = SchoolSpeciality::detail($specialityId);
  233. if(empty($speciality)){
  234. $this->error = '报名专业不存在';
  235. return false;
  236. }
  237. if($speciality['school_id'] != $schoolId){
  238. $this->error = '报名专业和学校不匹配';
  239. return false;
  240. }
  241. // 报名人数验证
  242. $bookNum = SpecialityBook::getBooks($specialityId);
  243. $remainBookNum = max(0, $speciality['recruit_num'] - $bookNum);
  244. if($remainBookNum <= 0){
  245. $this->error = '该专业报名人数已满';
  246. return false;
  247. }
  248. // 订单数据
  249. $fields = isset($params['fields'])? $params['fields'] : [];
  250. $fields = $fields && is_array($fields)? json_encode($fields) : '';
  251. $order = [
  252. 'book_user_id' => UserService::getCurrentLoginUserId(),
  253. 'user_id' => $teacherUserId,
  254. 'agent_id' => isset($teacherInfo['agent_id'])? $teacherInfo['agent_id'] : 0,
  255. 'order_no' => 'BK' . OrderService::createOrderNo(),
  256. 'real_name' => $realName,
  257. 'gender' => $gender,
  258. 'age' => isset($params['age'])? $params['age'] : 0,
  259. 'school_name' => $schoolName,
  260. 'school_id' => $schoolId,
  261. 'speciality_id' => $specialityId,
  262. 'price' => floatval($price),
  263. 'total_price' => floatval($price),
  264. 'receipt_name' => $receiptName,
  265. 'receipt_mobile' => $receiptMobile,
  266. 'receipt_address' => $receiptAddress,
  267. 'fields' => $fields,
  268. 'create_time' => time(),
  269. 'update_time' => time(),
  270. 'status' => 1,
  271. ];
  272. return $this->save($order);
  273. }
  274. public function modifyOrder($orderId, $params=[])
  275. {
  276. if(empty($orderId)){
  277. $this->error = '订单参数错误';
  278. return false;
  279. }
  280. $modifyType = isset($params['type'])? $params['type'] : 0;
  281. if(!in_array($modifyType, [1,2])){
  282. $this->error = '订单修改类型错误';
  283. return false;
  284. }
  285. $model = self::detail($orderId);
  286. if(empty($model)){
  287. $this->error = '订单不存在';
  288. return false;
  289. }
  290. $data = ['update_time'=> time()];
  291. if($modifyType == 1){
  292. if(!in_array($model['status'],[1,2,3])){
  293. $this->error = '订单状态不可操作';
  294. return false;
  295. }
  296. $receiptName = isset($params['receipt_name'])? $params['receipt_name'] : '';
  297. if(empty($receiptName)){
  298. $this->error = '请填写收货人姓名';
  299. return false;
  300. }
  301. $receiptMobile = isset($params['receipt_mobile'])? $params['receipt_mobile'] : '';
  302. if(empty($receiptMobile)){
  303. $this->error = '请填写收货人联系电话';
  304. return false;
  305. }
  306. $receiptAddress = isset($params['receipt_address'])? $params['receipt_address'] : '';
  307. if(empty($receiptAddress)){
  308. $this->error = '请填写收货人地址';
  309. return false;
  310. }
  311. $data = [
  312. 'receipt_name'=> $receiptName,
  313. 'receipt_mobile'=> $receiptMobile,
  314. 'receipt_address'=> $receiptAddress,
  315. ];
  316. }else if ($modifyType == 2){
  317. if(!in_array($model['status'],[1,2,3])){
  318. $this->error = '订单状态不可操作';
  319. return false;
  320. }
  321. $specialityId = isset($params['speciality_id'])? intval(['speciality_id']) : 0;
  322. if(empty($specialityId)){
  323. $this->error = '请选择修改的专业';
  324. return false;
  325. }
  326. // 专业参数验证
  327. $speciality = SchoolSpeciality::detail($specialityId);
  328. if(empty($speciality)){
  329. $this->error = '报名专业不存在';
  330. return false;
  331. }
  332. if($speciality['school_id'] != $model['school_id']){
  333. $this->error = '报名专业和学校不匹配';
  334. return false;
  335. }
  336. // 报名人数验证
  337. $bookNum = SpecialityBook::getBooks($specialityId);
  338. $remainBookNum = max(0, $speciality['recruit_num'] - $bookNum);
  339. if($remainBookNum <= 0){
  340. $this->error = '该专业报名人数已满';
  341. return false;
  342. }
  343. $data = [
  344. 'speciality_id' => $specialityId
  345. ];
  346. }
  347. return $model->save($data);
  348. }
  349. /**
  350. * 验证支付订单
  351. * @param $order
  352. * @return bool
  353. * @throws \think\db\exception\DataNotFoundException
  354. * @throws \think\db\exception\DbException
  355. * @throws \think\db\exception\ModelNotFoundException
  356. */
  357. public function checkPay($order)
  358. {
  359. $orderId = isset($order['order_id'])? $order['order_id'] : 0;
  360. if($orderId<=0){
  361. $this->error = '订单参数错误';
  362. return false;
  363. }
  364. $orderStatus = isset($order['status'])? $order['status'] : 0;
  365. if($orderStatus == 3){
  366. $this->error = '订单已支付';
  367. return false;
  368. }
  369. if($orderStatus != 2){
  370. $this->error = '订单未确认或状态不可支付';
  371. return false;
  372. }
  373. $specialityId = isset($order['speciality_id'])? intval($order['speciality_id']) : 0;
  374. // 专业参数验证
  375. $speciality = SchoolSpeciality::detail($specialityId);
  376. if(empty($speciality)){
  377. $this->error = '报名专业不存在';
  378. return false;
  379. }
  380. // 报名人数验证
  381. $bookNum = SpecialityBook::getBooks($specialityId);
  382. $remainBookNum = max(0, $speciality['recruit_num'] - $bookNum);
  383. if($remainBookNum <= 0){
  384. $this->error = '该专业报名人数已满';
  385. return false;
  386. }
  387. // 已经报名次数
  388. $trade = \app\store\model\Setting::getItem('trade');
  389. $bookLimit = isset($trade['books']['book_limit'])? intval($trade['books']['book_limit']) : 0;
  390. $bookLimit = $bookLimit? $bookLimit : 3;
  391. if(SpecialityBook::getUserBooks($orderId) >= $bookLimit){
  392. $this->error = "您已经报名了{$bookLimit}次,不能再报名";
  393. return false;
  394. }
  395. return true;
  396. }
  397. /**
  398. * 确认报名订单
  399. * @param $orderId
  400. * @param $noticeImg
  401. * @return bool
  402. * @throws BaseException
  403. * @throws \think\db\exception\DataNotFoundException
  404. * @throws \think\db\exception\DbException
  405. * @throws \think\db\exception\ModelNotFoundException
  406. */
  407. public function confirmOrder($orderId, $noticeImg)
  408. {
  409. if(empty($orderId)){
  410. $this->error = '订单参数错误';
  411. return false;
  412. }
  413. if(empty($noticeImg)){
  414. $this->error = '请上传录取通知书';
  415. return false;
  416. }
  417. $model = new Order();
  418. $model = $model::getUserOrderDetail($orderId);
  419. if(!$model){
  420. $this->error = '报名订单不存在';
  421. return false;
  422. }
  423. // 订单所属老师才何以设置
  424. $userInfo = UserService::getCurrentLoginUser(true);
  425. if($userInfo['user_id'] != $model['user_id']){
  426. $this->error = '订单无权操作';
  427. return false;
  428. }
  429. if($model['status'] != 1){
  430. $this->error = '订单已确认或状态不可操作';
  431. return false;
  432. }
  433. return $model->save(['status'=> 2,'notice_img'=> $noticeImg]);
  434. }
  435. /**
  436. * 取消订单
  437. * @param $orderId
  438. * @param string $remark
  439. * @return bool
  440. * @throws BaseException
  441. * @throws \cores\exception\BaseException
  442. * @throws \think\db\exception\DataNotFoundException
  443. * @throws \think\db\exception\DbException
  444. * @throws \think\db\exception\ModelNotFoundException
  445. */
  446. public function cancelOrder($orderId, $remark='')
  447. {
  448. if(empty($orderId)){
  449. $this->error = '订单参数错误';
  450. return false;
  451. }
  452. $model = new Order();
  453. $model = $model::getUserOrderDetail($orderId);
  454. if(!$model){
  455. $this->error = '报名订单不存在';
  456. return false;
  457. }
  458. // 订单所属老师才何以设置
  459. $userInfo = UserService::getCurrentLoginUser(true);
  460. if($userInfo['user_id'] != $model['book_user_id']){
  461. $this->error = '订单无权操作';
  462. return false;
  463. }
  464. if(!in_array($model['status'], [1,2])){
  465. $this->error = '订单状态不可操作';
  466. return false;
  467. }
  468. return $model->save(['status'=> -1,'update_time'=> time(),'remark'=> $remark]);
  469. }
  470. /**
  471. * 订单发货设置
  472. * @param int $orderId 订单ID
  473. * @param $params 发货参数:快递公司ID、快递公司名称、快递单号
  474. * @return bool
  475. * @throws BaseException
  476. * @throws \think\db\exception\DataNotFoundException
  477. * @throws \think\db\exception\DbException
  478. * @throws \think\db\exception\ModelNotFoundException
  479. */
  480. public function setDelivery(int $orderId, $params)
  481. {
  482. $data['express_id'] = isset($params['express_id'])? $params['express_id'] : '';
  483. $data['express_company'] = isset($params['express_company'])? $params['express_company'] : '';
  484. $data['express_no'] = isset($params['express_no'])? $params['express_no'] : '';
  485. if(empty($orderId)){
  486. $this->error = '订单参数错误';
  487. return false;
  488. }
  489. if(empty($data['express_id'])){
  490. $this->error = '请选择快递公司';
  491. return false;
  492. }
  493. if(empty($data['express_company'])){
  494. $this->error = '请选择快递公司';
  495. return false;
  496. }
  497. if(empty($data['express_no'])){
  498. $this->error = '请填写快递单号';
  499. return false;
  500. }
  501. $model = new Order();
  502. $model = $model::getUserOrderDetail($orderId);
  503. if(!$model){
  504. $this->error = '报名订单不存在';
  505. return false;
  506. }
  507. // 订单所属老师才何以设置对应发货信息
  508. $userInfo = UserService::getCurrentLoginUser(true);
  509. if($userInfo['user_id'] != $model['user_id']){
  510. $this->error = '订单无权操作';
  511. return false;
  512. }
  513. if(in_array($model['status'], [1,2])){
  514. $this->error = '订单未支付';
  515. return false;
  516. }
  517. if($model['status'] != 3){
  518. $this->error = '订单已发货或状态不可操作';
  519. return false;
  520. }
  521. $data['status'] = 4;
  522. $data['delivery_time'] = time();
  523. return $model->save($data);
  524. }
  525. /**
  526. * 订单确认收货
  527. * @param $orderId
  528. * @return false
  529. * @throws BaseException
  530. * @throws \cores\exception\BaseException
  531. * @throws \think\db\exception\DataNotFoundException
  532. * @throws \think\db\exception\DbException
  533. * @throws \think\db\exception\ModelNotFoundException
  534. */
  535. public function receiptOrder($orderId)
  536. {
  537. if(empty($orderId)){
  538. $this->error = '订单参数错误';
  539. return false;
  540. }
  541. $model = new Order();
  542. $model = $model::getUserOrderDetail($orderId);
  543. if(!$model){
  544. $this->error = '报名订单不存在';
  545. return false;
  546. }
  547. $userInfo = UserService::getCurrentLoginUser(true);
  548. if($userInfo['user_id'] != $model['book_user_id']){
  549. $this->error = '订单无权操作';
  550. return false;
  551. }
  552. if($model['refund_status'] != 4){
  553. $this->error = '订单已收货或状态不可操作';
  554. return false;
  555. }
  556. // 直接收货完成
  557. $data['status'] = 6;
  558. $data['receipt_time'] = time();
  559. return $model->save($data);
  560. }
  561. /**
  562. * 设置订单为可退款
  563. * @param int $orderId
  564. * @return bool
  565. * @throws BaseException
  566. * @throws \think\db\exception\DataNotFoundException
  567. * @throws \think\db\exception\DbException
  568. * @throws \think\db\exception\ModelNotFoundException
  569. */
  570. public function setRefund(int $orderId)
  571. {
  572. if(empty($orderId)){
  573. $this->error = '订单参数错误';
  574. return false;
  575. }
  576. $model = new Order();
  577. $model = $model::getUserOrderDetail($orderId);
  578. if(!$model){
  579. $this->error = '报名订单不存在';
  580. return false;
  581. }
  582. // 订单所属老师才可以操作
  583. $userInfo = UserService::getCurrentLoginUser(true);
  584. if($userInfo['user_id'] != $model['user_id']){
  585. $this->error = '订单无权操作';
  586. return false;
  587. }
  588. if(in_array($model['status'], [1,2])){
  589. $this->error = '订单未支付';
  590. return false;
  591. }
  592. if($model['refund_status'] != 1 || !in_array($model['status'], [3,4])){
  593. $this->error = '订单设置为可退款或状态不可操作';
  594. return false;
  595. }
  596. return $model->save(['refund_status'=> 2]);
  597. }
  598. /**
  599. * 订单申请退款
  600. * @param $orderId
  601. * @param $refundResult 退款原因
  602. * @return bool
  603. * @throws BaseException
  604. * @throws \think\db\exception\DataNotFoundException
  605. * @throws \think\db\exception\DbException
  606. * @throws \think\db\exception\ModelNotFoundException
  607. */
  608. public function refundOrder($orderId, $refundResult)
  609. {
  610. if(empty($orderId)){
  611. $this->error = '订单参数错误';
  612. return false;
  613. }
  614. if(empty($refundResult)){
  615. $this->error = '请填写退款原因';
  616. return false;
  617. }
  618. $model = new Order();
  619. $model = $model::getUserOrderDetail($orderId);
  620. if(!$model){
  621. $this->error = '报名订单不存在';
  622. return false;
  623. }
  624. // 订单所属用户才可以操作
  625. $userInfo = UserService::getCurrentLoginUser(true);
  626. if($userInfo['user_id'] != $model['book_user_id']){
  627. $this->error = '订单无权操作';
  628. return false;
  629. }
  630. if(in_array($model['status'], [1,2])){
  631. $this->error = '订单未支付';
  632. return false;
  633. }
  634. if($model['refund_status'] != 2 || !in_array($model['status'], [3,4])){
  635. $this->error = '订单已申请退款或状态不可操作';
  636. return false;
  637. }
  638. return $model->save(['refund_status'=> 3,'refund_result'=> $refundResult]);
  639. }
  640. /**
  641. * 订单退款确认
  642. * @param $orderId
  643. * @param $refundResult 退款凭证
  644. * @return bool
  645. * @throws BaseException
  646. * @throws \think\db\exception\DataNotFoundException
  647. * @throws \think\db\exception\DbException
  648. * @throws \think\db\exception\ModelNotFoundException
  649. */
  650. public function refundConfirm($orderId, $refundImg)
  651. {
  652. if(empty($orderId)){
  653. $this->error = '订单参数错误';
  654. return false;
  655. }
  656. if(empty($refundResult)){
  657. $this->error = '请填写退款原因';
  658. return false;
  659. }
  660. $model = new Order();
  661. $model = $model::getUserOrderDetail($orderId);
  662. if(!$model){
  663. $this->error = '报名订单不存在';
  664. return false;
  665. }
  666. // 订单所属老师才可以处理
  667. $userInfo = UserService::getCurrentLoginUser(true);
  668. if($userInfo['user_id'] != $model['user_id']){
  669. $this->error = '订单无权操作';
  670. return false;
  671. }
  672. if($model['status'] == -2){
  673. $this->error = '订单已退款';
  674. return false;
  675. }
  676. if(in_array($model['status'], [1,2])){
  677. $this->error = '订单未支付';
  678. return false;
  679. }
  680. if($model['refund_status'] != 3 || !in_array($model['status'], [3,4])){
  681. $this->error = '订单已退款或状态不可操作';
  682. return false;
  683. }
  684. // 调起退款
  685. if(!$this->refund($model)){
  686. $this->error = '退款处理失败';
  687. return false;
  688. }
  689. return $model->save(['refund_status'=> 4,'refund_img'=> $refundImg,'status'=> -2]);
  690. }
  691. public function cancel()
  692. {
  693. }
  694. /**
  695. * 退款调起
  696. * @param $order
  697. * @param int $money
  698. * @return bool
  699. * @throws BaseException
  700. * @throws \cores\exception\BaseException
  701. * @throws \think\db\exception\DataNotFoundException
  702. * @throws \think\db\exception\DbException
  703. * @throws \think\db\exception\ModelNotFoundException
  704. */
  705. private function refund($order, $money=0)
  706. {
  707. $wxConfig = WxappSettingModel::getWxappConfig(getStoreId());
  708. $WxPay = new WxPay($wxConfig, getStoreId());
  709. $money = $money? $money : $order['pay_price'];
  710. return $WxPay->refund($order['transaction_id'], $order['pay_price'], $money);
  711. }
  712. }