MotorAgent.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace app\api\controller\v1\user;
  3. use app\api\controller\ApiController;
  4. use app\api\model\taxi\UserPaymentOrder;
  5. use app\api\service\JWTAuth as IAuth;
  6. use app\common\model\Seller;
  7. use app\common\model\TaxiOrder;
  8. use think\App;
  9. use think\Exception;
  10. use think\Request;
  11. class MotorAgent extends ApiController
  12. {
  13. protected $model;
  14. public function __construct(App $app = null, IAuth $auth)
  15. {
  16. parent::__construct($app, $auth);
  17. $this->model = new \app\api\model\user\MotorAgent();
  18. }
  19. /**
  20. * 显示资源列表
  21. *
  22. * @return \think\Response
  23. */
  24. public function index()
  25. {
  26. //
  27. }
  28. /**
  29. * 显示创建资源表单页.
  30. *
  31. * @return \think\Response
  32. */
  33. public function create()
  34. {
  35. //
  36. }
  37. /**
  38. * 保存新建的资源
  39. *
  40. * @param \think\Request $request
  41. * @return \think\Response
  42. * @throws \Lettered\Support\Exceptions\FailedException
  43. * @throws \think\exception\PDOException
  44. */
  45. public function save(Request $request)
  46. {
  47. $params = input();
  48. $validate = validate('\app\api\validate\user\MotorAgent');
  49. if (!$validate->scene('save')->check($params)) {
  50. return $this->ApiJson(-1, $validate->getError());
  51. }
  52. $params['user_id'] = $this->auth->user()['id'];
  53. $params['account'] = $params['mobile'];
  54. $result = false;
  55. $this->model->startTrans();
  56. try {
  57. $result = $this->model::create($params, true);
  58. $this->model->commit();
  59. }
  60. catch(Exception $e) {
  61. $this->model->rollback();
  62. }
  63. if ($result) {
  64. return $this->ApiJson(0, '成功', $result);
  65. }
  66. return $this->ApiJson(-1, '失败');
  67. }
  68. /**
  69. * 显示指定的资源
  70. *
  71. * @return \think\Response
  72. * @throws \Lettered\Support\Exceptions\FailedException
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. * @throws \think\exception\DbException
  76. */
  77. public function read()
  78. {
  79. $user = $this->auth->user();
  80. $row = $this->model->field('created_at,updated_at', true)
  81. ->where('user_id', $user['id'])
  82. ->find();
  83. if ($row) {
  84. $row->price = sys_config('agent_cost', 'agent');
  85. if ($row['status'] >= 40) {
  86. $Seller = new Seller();
  87. $TaxiOrder = new TaxiOrder();
  88. // 商户数量
  89. $row->total_fee = $Seller->where(['area_id' => $row['area_id']])->count();
  90. // 订单数据
  91. // 摩的订单,技能订单,商品订单,配送订单
  92. $taxi_order = $TaxiOrder->whereIn('area_id', $row['area_id'])->whereNotIn('status', [1])->column('price', 'id');
  93. $row->total_order = count($taxi_order);
  94. $row->total_store = array_sum($taxi_order);
  95. }
  96. return $this->ApiJson(0, '成功', $row);
  97. }
  98. return $this->ApiJson(-1, '失败');
  99. }
  100. /**
  101. * 显示编辑资源表单页.
  102. *
  103. * @param int $id
  104. * @return \think\Response
  105. */
  106. public function edit($id)
  107. {
  108. //
  109. }
  110. /**
  111. * 保存更新的资源
  112. *
  113. * @param \think\Request $request
  114. * @param int $id
  115. * @return \think\Response
  116. */
  117. public function update(Request $request, $id)
  118. {
  119. //
  120. }
  121. /**
  122. * 删除指定资源
  123. *
  124. * @param int $id
  125. * @return \think\Response
  126. */
  127. public function delete($id)
  128. {
  129. //
  130. }
  131. public function placeOrder()
  132. {
  133. $user = $this->auth->user();
  134. $UserPaymentOrder = new UserPaymentOrder();
  135. $row = $UserPaymentOrder->where('user_id', $user['id'])
  136. ->where('type', 20)
  137. ->find();
  138. if ($row && $row['status'] == 2) {
  139. return $this->ApiJson(-1,'已支付费用');
  140. }
  141. $result = false;
  142. $this->model->startTrans();
  143. try {
  144. if (!$row) {
  145. // 下订单
  146. $row = $UserPaymentOrder::create([
  147. 'user_id' => $user['id'],
  148. 'order_no' => get_order_no(),
  149. 'price' => sys_config('agent_cost', 'agent'),
  150. 'type' => 20
  151. ],true);
  152. }
  153. // 创建对应支付记录
  154. $trade_no = get_order_no();
  155. $result = model('common/OrderPaylog')->storeBy([
  156. 'out_trade_no' => $trade_no,
  157. 'total_price' => sys_config('agent_cost', 'agent'),
  158. 'order_idx' => $row['id'],
  159. 'ascription' => 'motor_agent' // 归属订单
  160. ]);
  161. $this->model->commit();
  162. }
  163. catch(Exception $e) {
  164. $this->model->rollback();
  165. }
  166. if ($result) {
  167. return $this->ApiJson(0,'成功', [
  168. 'type' => 'wx',
  169. 'success' => "ok!",
  170. 'trade_no' => $trade_no
  171. ]);
  172. }
  173. return $this->ApiJson(-1,'失败');
  174. }
  175. }