* @date 2020/7/3 16:16 * * @return \think\response\Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getNearby() { $param = $this->request->param(); $limit = 10; // 数据校验 $valid = $this->validate($param, [ 'page' => 'require', ]); // 错误 if (true !== $valid){ return $this->ApiJson(-1,$valid); } // $taxi = model('common/Farmland') ->where([ 'status' => 1]) ->limit((($param['page'] - 1) * $limit) . "," . $limit) ->select(); return $this->ApiJson(0,'获取成功', $taxi); } /** * 详情 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/3 16:16 * * @param $id * @return \think\response\Json * @throws \Lettered\Support\Exceptions\EvidentException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getDetailByID($id) { (new IDMustBePositiveInt())->valid(); // 读取详情 $farmland = model('common/Farmland')->findBy($id); // 读取模块 $farmland['block'] = model('common/FarmlandBlock')->where(['farm_id' => $id])->select(); if (!$farmland){ return $this->ApiJson(-1, '不存在编号'); } return $this->ApiJson(0, '获取信息', $farmland); } /** * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2021/2/3 17:53 * * @param $id * @return mixed * @throws \Lettered\Support\Exceptions\EvidentException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getFarmBlockVarietyByID($id) { (new IDMustBePositiveInt())->valid(); // 读取详情 $block = model('common/FarmlandBlock')->findBy($id); if (!$block){ return $this->ApiJson(-1, "不存在"); } // 读取品类 $block['variety'] = model('common/FarmlandVariety')->where(['block_id' => $block['id']])->select(); return $this->ApiJson(0, '获取信息', $block); } /** * 技能服务 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/3 16:29 * * @return \think\response\Json * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException * @throws \GuzzleHttp\Exception\GuzzleException * @throws \Lettered\Support\Exceptions\FailedException */ public function userServe() { $params = $this->request->param(); // 参数校验 $valid = $this->validate($params, [ 'block_id|归属地块' => 'require', 'price|价格' => 'require', 'variety|品种' => 'require', 'date|承包到期' => 'require' ]); // 错误返回 if(true !== $valid){ return $this->ApiJson(-1, $valid); }; // 检查服务是不是已经被购买了 $block = model('common/FarmlandBlock')->findBy($params['block_id']); if (!$block){ // 不存在 return $this->ApiJson(-1, "该地块不存在..."); } if ($block['status'] == 2){ //return $this->ApiJson(-1, "农场已在服务中,无法购买了"); } // 创建订单 -- 附加数据 $params['order_no'] = get_order_no(); $params['user_id'] = $this->auth->user()['id']; // 总价计算 $params['total_price'] = round( $params['price'] * $params['month'] * (int)$params['area'],2); // 开始时间(付款成功那一刻开始算)-结束时间 $params['end_at'] = strtotime(date($params['date'] . ' 23:59:59')); // halt($params); $orderId = model('common/FarmlandOrder')->storeBy($params); if ($orderId){ // 创建订单详情 foreach (str2arr($params['variety']) as $varietyId){ $variety = model('common/FarmlandVariety')->findBy($varietyId); model('common/FarmlandOrderDetail')::create([ 'order_id' => $orderId, 'variety_id' => $varietyId, 'name' => $variety['name'] ], true); } // 创建对应支付记录 $trade_no = get_order_no(); model('common/OrderPaylog')->storeBy([ 'out_trade_no' => $trade_no, 'total_price' => $params['total_price'], 'order_idx' => $orderId, 'ascription' => 'farmland' // 归属订单 ]); // 返回支付单号 return $this->ApiJson(0,'订单提交成功', $trade_no); } return $this->ApiJson(-1,'发生异常,请骚后重试...'); } /** * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2021/2/5 20:37 * * @return mixed */ public function userServeChange() { $params = $this->request->param(); // 参数校验 $valid = $this->validate($params, [ 'order_id|订单' => 'require', 'variety_id|品种' => 'require', 'change_id|替换' => 'require' ]); // 错误返回 if(true !== $valid){ return $this->ApiJson(-1, $valid); } // 已经替换 $detail = model('common/FarmlandOrderDetail')->findBy($params['change_id']); if (!$detail){ return $this->ApiJson(-1, '错误的替换'); } if ($detail['status'] == 4){ return $this->ApiJson(-1, '已经替换,请勿重复操作'); } // todo try catch // 查找 $variety = model('common/FarmlandVariety')->findBy($params['variety_id']); // 写入新明细 $result = model('common/FarmlandOrderDetail')::create([ 'order_id' => $params['order_id'], 'variety_id' => $variety['id'], 'name' => $variety['name'] ], true); if ($result){ // 标记替换完成 model('common/FarmlandOrderDetail')->updateBy($detail['id'],[ 'status' => 4 ]); } return $this->ApiJson(0,'更新', $params); } /** * 获取订单列表 * * @return \think\response\Json * @throws \Lettered\Support\Exceptions\FailedException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function order() { $param = $this->request->param(); $limit = 10; // 数据校验 $valid = $this->validate($param, [ 'page' => 'require', ]); // 错误 if (true !== $valid) { return $this->ApiJson(-1, $valid); } $orders = model('common/FarmlandOrder') ->where(['user_id' => $this->auth->user()['id']]) ->limit((($param['page'] - 1) * $limit) . "," . $limit) ->order(['created_at' => 'desc']) ->select(); return $this->ApiJson(0,'获取成功', $orders); } /** * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2021/2/6 16:28 * * @return mixed */ public function orderShipping() { $user = $this->auth->user(); $param = $this->request->param(); if($this->request->isPost()){ // 提交数据 // 参数校验 $valid = $this->validate($param, [ 'order_id|关联订单' => 'require', // 'count|数量' => 'require', 'username|收件人姓名' => 'require', 'mobile|收件人手机' => 'require', 'address|收件人地址' => 'require' ]); // 错误返回 if(true !== $valid){ return $this->ApiJson(-1, $valid); }; // 查找订单 $order = model('common/FarmlandOrder')->findBy($param['order_id']); if(!$order){ return $this->ApiJson(-1, "订单不存在了!"); } if($order['status'] !== 3){ return $this->ApiJson(-1, "当前服务状态不接受采摘!"); } // 检查有完成采摘的没有 $picked = model('common/FarmlandOrderDetail')->where(['order_id' => $order['id'],'status' => 3])->count('id'); if ($picked <= 0){ return $this->ApiJson(-1, "亲,您还没有可采摘的农作物哟!!"); } // 一个周申请 $param['user_id'] = $user['id']; model('common/FarmlandShipping')->storeBy($param); push_socket_data('farmland',[ 'id' => $order['id'], 'msg' => '有新的农田采摘申请等待处理,订单号:' . $order['order_no'] ]); return $this->ApiJson(0,'提交成功,等待处理', $param); } // 查看申请记录 return $this->ApiJson(0,'获取成功', $param); } /** * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2021/2/5 16:25 * * @param $id * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function orderDetail($id) { return $this->ApiJson(0,'获取成功', model('common/FarmlandOrderDetail') ->where(['order_id' => $id])->order(['status' => 'asc']) ->select()); } public function orderDetailImage($id) { $param = $this->request->param(); $limit = 10; // 数据校验 $valid = $this->validate($param, [ 'page' => 'require', ]); // 错误 if (true !== $valid) { return $this->ApiJson(-1, $valid); } $orders = model('common/FarmlandOrderImage') ->where(['order_id' => $id]) ->limit((($param['page'] - 1) * $limit) . "," . $limit) ->order(['created_at' => 'desc']) ->select(); return $this->ApiJson(0,'获取成功', $orders); } }