| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- namespace app\api\controller\v1\user;
- use app\api\controller\ApiController;
- use app\api\model\taxi\UserPaymentOrder;
- use app\api\service\JWTAuth as IAuth;
- use app\common\model\Seller;
- use app\common\model\TaxiOrder;
- use think\App;
- use think\Exception;
- use think\Request;
- class MotorAgent extends ApiController
- {
- protected $model;
- public function __construct(App $app = null, IAuth $auth)
- {
- parent::__construct($app, $auth);
- $this->model = new \app\api\model\user\MotorAgent();
- }
- /**
- * 显示资源列表
- *
- * @return \think\Response
- */
- public function index()
- {
- //
- }
- /**
- * 显示创建资源表单页.
- *
- * @return \think\Response
- */
- public function create()
- {
- //
- }
- /**
- * 保存新建的资源
- *
- * @param \think\Request $request
- * @return \think\Response
- * @throws \Lettered\Support\Exceptions\FailedException
- * @throws \think\exception\PDOException
- */
- public function save(Request $request)
- {
- $params = input();
- $validate = validate('\app\api\validate\user\MotorAgent');
- if (!$validate->scene('save')->check($params)) {
- return $this->ApiJson(-1, $validate->getError());
- }
- $params['user_id'] = $this->auth->user()['id'];
- $params['account'] = $params['mobile'];
- $result = false;
- $this->model->startTrans();
- try {
- $result = $this->model::create($params, true);
- $this->model->commit();
- }
- catch(Exception $e) {
- $this->model->rollback();
- }
- if ($result) {
- return $this->ApiJson(0, '成功', $result);
- }
- return $this->ApiJson(-1, '失败');
- }
- /**
- * 显示指定的资源
- *
- * @return \think\Response
- * @throws \Lettered\Support\Exceptions\FailedException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function read()
- {
- $user = $this->auth->user();
- $row = $this->model->field('created_at,updated_at', true)
- ->where('user_id', $user['id'])
- ->find();
- if ($row) {
- $row->price = sys_config('agent_cost', 'agent');
- if ($row['status'] >= 40) {
- $Seller = new Seller();
- $TaxiOrder = new TaxiOrder();
- // 商户数量
- $row->total_fee = $Seller->where(['area_id' => $row['area_id']])->count();
- // 订单数据
- // 摩的订单,技能订单,商品订单,配送订单
- $taxi_order = $TaxiOrder->whereIn('area_id', $row['area_id'])->whereNotIn('status', [1])->column('price', 'id');
- $row->total_order = count($taxi_order);
- $row->total_store = array_sum($taxi_order);
- }
- return $this->ApiJson(0, '成功', $row);
- }
- return $this->ApiJson(-1, '失败');
- }
- /**
- * 显示编辑资源表单页.
- *
- * @param int $id
- * @return \think\Response
- */
- public function edit($id)
- {
- //
- }
- /**
- * 保存更新的资源
- *
- * @param \think\Request $request
- * @param int $id
- * @return \think\Response
- */
- public function update(Request $request, $id)
- {
- //
- }
- /**
- * 删除指定资源
- *
- * @param int $id
- * @return \think\Response
- */
- public function delete($id)
- {
- //
- }
- public function placeOrder()
- {
- $user = $this->auth->user();
- $UserPaymentOrder = new UserPaymentOrder();
- $row = $UserPaymentOrder->where('user_id', $user['id'])
- ->where('type', 20)
- ->find();
- if ($row && $row['status'] == 2) {
- return $this->ApiJson(-1,'已支付费用');
- }
- $result = false;
- $this->model->startTrans();
- try {
- if (!$row) {
- // 下订单
- $row = $UserPaymentOrder::create([
- 'user_id' => $user['id'],
- 'order_no' => get_order_no(),
- 'price' => sys_config('agent_cost', 'agent'),
- 'type' => 20
- ],true);
- }
- // 创建对应支付记录
- $trade_no = get_order_no();
- $result = model('common/OrderPaylog')->storeBy([
- 'out_trade_no' => $trade_no,
- 'total_price' => sys_config('agent_cost', 'agent'),
- 'order_idx' => $row['id'],
- 'ascription' => 'motor_agent' // 归属订单
- ]);
- $this->model->commit();
- }
- catch(Exception $e) {
- $this->model->rollback();
- }
- if ($result) {
- return $this->ApiJson(0,'成功', [
- 'type' => 'wx',
- 'success' => "ok!",
- 'trade_no' => $trade_no
- ]);
- }
- return $this->ApiJson(-1,'失败');
- }
- }
|