User.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace app\api\controller\v1\taxi;
  3. use app\api\controller\ApiController;
  4. use app\api\model\taxi\UserPaymentOrder;
  5. use think\App;
  6. use app\api\service\JWTAuth as IAuth;
  7. use think\Exception;
  8. class User extends ApiController
  9. {
  10. protected $model;
  11. public function __construct(App $app = null, IAuth $auth)
  12. {
  13. parent::__construct($app, $auth);
  14. $this->model = new \app\api\model\taxi\User();
  15. }
  16. /**
  17. * @desc desc
  18. * @return \think\response\Json
  19. * @throws \Lettered\Support\Exceptions\FailedException
  20. * @throws \think\db\exception\DataNotFoundException
  21. * @throws \think\db\exception\ModelNotFoundException
  22. * @throws \think\exception\DbException
  23. * @throws \think\exception\PDOException
  24. * @author weichuanbao<654745815@qq.com>
  25. * @date 2021/12/14 0014
  26. */
  27. public function save()
  28. {
  29. $params = input();
  30. $validate = validate('\app\api\validate\taxi\User');
  31. if (!$validate->scene('save')->check($params)) {
  32. return $this->ApiJson(-1, $validate->getError());
  33. }
  34. $user = $this->auth->user();
  35. $row = $this->model->field('id')
  36. ->where('user_id', $user['id'])
  37. ->find();
  38. if ($row) {
  39. return $this->ApiJson(-1, '您已申请');
  40. }
  41. $result = false;
  42. $this->model->startTrans();
  43. try {
  44. $params['user_id'] = $user['id'];
  45. $result = $this->model::create($params, true);
  46. $this->model->commit();
  47. }
  48. catch(Exception $e) {
  49. $this->model->rollback();
  50. p($e->getTraceAsString(), 1);
  51. return $this->ApiJson(-1, $e->getMessage());
  52. }
  53. if ($result) {
  54. return $this->ApiJson(0, '成功', $result);
  55. }
  56. return $this->ApiJson(-1, '失败');
  57. }
  58. /**
  59. * @desc desc
  60. * @return \think\response\Json
  61. * @throws \Lettered\Support\Exceptions\FailedException
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. * @throws \think\exception\DbException
  65. * @author weichuanbao<654745815@qq.com>
  66. * @date 2021/12/7 0007
  67. */
  68. public function read()
  69. {
  70. $user = $this->auth->user();
  71. $row = $this->model->field('created_at,updated_at', true)
  72. ->where('user_id', $user['id'])
  73. ->find();
  74. if ($row) {
  75. $row->price = sys_config('driver_cost', 'driver');
  76. return $this->ApiJson(0, '成功', $row);
  77. }
  78. return $this->ApiJson(-1, '失败');
  79. }
  80. /**
  81. * @desc desc
  82. * @return \think\response\Json
  83. * @throws \think\exception\PDOException
  84. * @author weichuanbao<654745815@qq.com>
  85. * @date 2021/12/14 0014
  86. */
  87. public function placeOrder()
  88. {
  89. $user = $this->auth->user();
  90. $UserPaymentOrder = new UserPaymentOrder();
  91. $row = $UserPaymentOrder->where('user_id', $user['id'])
  92. ->where('type', 10)
  93. ->find();
  94. if ($row && $row['status'] == 2) {
  95. return $this->ApiJson(-1,'已支付费用');
  96. }
  97. $result = false;
  98. $this->model->startTrans();
  99. try {
  100. if (!$row) {
  101. // 下订单
  102. $row = $UserPaymentOrder::create([
  103. 'user_id' => $this->auth->user()['id'],
  104. 'order_no' => get_order_no(),
  105. 'price' => sys_config('driver_cost', 'driver'),
  106. ],true);
  107. }
  108. // 创建对应支付记录
  109. $trade_no = get_order_no();
  110. $result = model('common/OrderPaylog')->storeBy([
  111. 'out_trade_no' => $trade_no,
  112. 'total_price' => sys_config('driver_cost', 'driver'),
  113. 'order_idx' => $row['id'],
  114. 'ascription' => 'motor_driver' // 归属订单
  115. ]);
  116. $this->model->commit();
  117. }
  118. catch(Exception $e) {
  119. $this->model->rollback();
  120. }
  121. if ($result) {
  122. return $this->ApiJson(0,'成功', [
  123. 'type' => 'wx',
  124. 'success' => "ok!",
  125. 'trade_no' => $trade_no
  126. ]);
  127. }
  128. return $this->ApiJson(-1,'失败');
  129. }
  130. }