User.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. if($this->model->where(['mobile'=>$params['mobile']])->find()){
  42. return $this->ApiJson(-1, '手机号码已存在,请更换或联系客服');
  43. }
  44. $result = false;
  45. $this->model->startTrans();
  46. try {
  47. $params['user_id'] = $user['id'];
  48. if($params['password']){
  49. $params['password'] = password_hash($params['password'],PASSWORD_DEFAULT);
  50. }
  51. $result = $this->model::create($params, true);
  52. $this->model->commit();
  53. }
  54. catch(Exception $e) {
  55. $this->model->rollback();
  56. p($e->getTraceAsString(), 1);
  57. return $this->ApiJson(-1, $e->getMessage());
  58. }
  59. if ($result) {
  60. return $this->ApiJson(0, '成功', $result);
  61. }
  62. return $this->ApiJson(-1, '失败');
  63. }
  64. /**
  65. * @desc desc
  66. * @return \think\response\Json
  67. * @throws \Lettered\Support\Exceptions\FailedException
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\ModelNotFoundException
  70. * @throws \think\exception\DbException
  71. * @author weichuanbao<654745815@qq.com>
  72. * @date 2021/12/7 0007
  73. */
  74. public function read()
  75. {
  76. $user = $this->auth->user();
  77. $row = $this->model->field('created_at,updated_at', true)
  78. ->where('user_id', $user['id'])
  79. ->find();
  80. if ($row) {
  81. $row->price = sys_config('driver_cost', 'driver');
  82. return $this->ApiJson(0, '成功', $row);
  83. }
  84. return $this->ApiJson(-1, '失败');
  85. }
  86. /**
  87. * @desc desc
  88. * @return \think\response\Json
  89. * @throws \think\exception\PDOException
  90. * @author weichuanbao<654745815@qq.com>
  91. * @date 2021/12/14 0014
  92. */
  93. public function placeOrder()
  94. {
  95. $user = $this->auth->user();
  96. $UserPaymentOrder = new UserPaymentOrder();
  97. $row = $UserPaymentOrder->where('user_id', $user['id'])
  98. ->where('type', 10)
  99. ->find();
  100. if ($row && $row['status'] == 2) {
  101. return $this->ApiJson(-1,'已支付费用');
  102. }
  103. $result = false;
  104. $this->model->startTrans();
  105. try {
  106. if (!$row) {
  107. // 下订单
  108. $row = $UserPaymentOrder::create([
  109. 'user_id' => $this->auth->user()['id'],
  110. 'order_no' => get_order_no(),
  111. 'price' => sys_config('driver_cost', 'driver'),
  112. ],true);
  113. }
  114. // 创建对应支付记录
  115. $trade_no = get_order_no();
  116. $result = model('common/OrderPaylog')->storeBy([
  117. 'out_trade_no' => $trade_no,
  118. 'total_price' => sys_config('driver_cost', 'driver'),
  119. 'order_idx' => $row['id'],
  120. 'ascription' => 'motor_driver' // 归属订单
  121. ]);
  122. $this->model->commit();
  123. }
  124. catch(Exception $e) {
  125. $this->model->rollback();
  126. }
  127. if ($result) {
  128. return $this->ApiJson(0,'成功', [
  129. 'type' => 'wx',
  130. 'success' => "ok!",
  131. 'trade_no' => $trade_no
  132. ]);
  133. }
  134. return $this->ApiJson(-1,'失败');
  135. }
  136. }