* @date 2020/7/15 18:20 * * @return mixed * @throws \think\exception\DbException */ public function index() { $where = []; (!empty(input('status')) || input('status') == '0') && $where[] = ['status', 'eq', input('status')]; !empty(input('name')) && $where[] = ['name', 'like', '%' . input('name') . '%']; //组合搜索 $farmland = model('common/Farmland'); return IResponse::paginate($farmland->where($where) ->paginate(input('limit'),false)); } /** * 新增 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/06/07 23:59 * * @return mixed */ public function save() { // 接收数据 $params = $this->request->param(); // 数据校验 $valid = $this->validate($params, [ 'name|技能名称' => 'require', 'cover_img|展示图片' => 'require' ]); // 错误返回 (true !== $valid) && IResponse::failure($valid); // 保存数据 $skillId = model("common/Farmland")->storeBy($params); return $skillId ? IResponse::success([],'新增信息成功'): IResponse::failure('新增信息异常'); } /** * 更新数据 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/06/07 14:24 * * @param $id * @return \think\response\Json */ public function update($id) { // 接收数据 $params = $this->request->param(); // 查询用户 $skill = model('common/Farmland')->findBy($id); // 是否更改状态操作 if (isset($params['status']) && $params['status'] != '') { $valid = $this->validate($params, [ 'status|配置状态' => 'require|integer' ]); }else { // 数据校验 $valid = $this->validate($params, [ 'name|技能名称' => 'require', 'cover_img|展示图片' => 'require' ]); } // 错误返回 (true !== $valid) && IResponse::failure($valid); // 更新信息 $skill->updateBy($id, $params); return IResponse::success('更新信息成功'); } /** * 删除 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/3/16 14:22 * * @param $id * @return \think\response\Json */ public function delete($id) { model('common/Farmland')->deleteBy($id); return IResponse::success([],'删除技能成功'); } /** * 批量操作 * * @return mixed */ public function plectron(){ // 收参数 $params = $this->request->param(); foreach (str2arr($params['ids']) as $id){ $skills = model('common/Farmland')->getBy($id); if ($this->request->isDelete()){ $skills->deleteBy($id); } $skills->allowField(true)->updateBy($id, $params); } return IResponse::success([],'操作成功'); } public function getClassData() { $where = []; //组合搜索 $user = model('common/FarmlandClass'); return IResponse::paginate($user->where($where) ->paginate(input('limit'),false)); } /** * 新增分类 * * @return mixed */ public function createClass() { // 接收数据 $params = $this->request->param(); // 数据校验 $valid = $this->validate($params,[ 'name|分类' => 'require|unique:FarmlandClass', ],[ 'email.unique' => '重复分类名称!' ]); (true !== $valid) && IResponse::failure($valid); // 保存数据 $goodsId = model('common/FarmlandClass')->storeBy($params); return $goodsId ? IResponse::success([],'新增分类成功'): IResponse::failure('新增分类异常'); } /** * 更新分类 * * @return mixed */ public function updateClass($id) { // 接收数据 $params = $this->request->param(); // 查询 $goods = model('common/FarmlandClass')->findBy($id); // 是否更改状态操作 if (isset($params['status']) && $params['status'] != '') { $valid = $this->validate($params, [ 'status|配置状态' => 'require|integer' ]); }else { // 数据校验 $valid = $this->validate($params,[ 'name|产品分类' => 'require', ]); } // 错误返回 (true !== $valid) && IResponse::failure($valid); // 更新用户信息 $goods->updateBy($id, $params); return IResponse::success('更新信息成功'); } /** * 删除分类 * @param $id * * @return mixed */ public function deleteClass($id) { model('common/FarmlandClass')->deleteBy($id); return IResponse::success([],'删除产品分类成功'); } public function getFarmlandDetail() { $where = []; //组合搜索 $user = model('common/FarmlandDetail'); return IResponse::paginate($user->where($where)->with(['farm','classify']) ->paginate(input('limit'),false)); } /** * 新增分类 * * @return mixed */ public function createFarmlandDetail() { // 接收数据 $params = $this->request->param(); // 数据校验 $valid = $this->validate($params,[ 'name|分类' => 'require|unique:FarmlandClass', ],[ 'email.unique' => '重复分类名称!' ]); (true !== $valid) && IResponse::failure($valid); // 保存数据 $goodsId = model('common/FarmlandDetail')->storeBy($params); return $goodsId ? IResponse::success([],'新增分类成功'): IResponse::failure('新增分类异常'); } /** * 更新分类 * * @return mixed */ public function updateFarmlandDetail($id) { // 接收数据 $params = $this->request->param(); // 查询 $goods = model('common/FarmlandDetail')->findBy($id); // 是否更改状态操作 if (isset($params['status']) && $params['status'] != '') { $valid = $this->validate($params, [ 'status|配置状态' => 'require|integer' ]); }else { // 数据校验 $valid = $this->validate($params,[ 'name|产品分类' => 'require', ]); } // 错误返回 (true !== $valid) && IResponse::failure($valid); // 更新用户信息 $goods->updateBy($id, $params); return IResponse::success('更新信息成功'); } /** * 删除分类 * @param $id * * @return mixed */ public function deleteFarmlandDetail($id) { model('common/FarmlandDetail')->deleteBy($id); return IResponse::success([],'删除产品分类成功'); } /******************** 订单 *********************/ /** * @return mixed * @throws \think\exception\DbException */ public function orders() { $where = []; (!empty(input('status')) || input('status') == '0') && $where[] = ['status', 'eq', input('status')]; !empty(input('keywords')) && $where[] = ['order_no|user_id', 'like', '%' . input('keywords') . '%']; // 时间处理 if (!empty(input('created_at'))){ list($start, $end) = str2arr(input('created_at'),'-'); $where[] = ['created_at', 'between', [strtotime($start), strtotime($end)]]; } //组合搜索 $user = model('common/FarmlandOrder'); return IResponse::paginate($user->where($where)->with(['block','user'])->order(['created_at' => 'desc']) ->paginate(input('limit'),false)); } /** * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2021/1/25 17:15 * * @param $id * @return mixed * @throws \think\exception\DbException */ public function orderShipping($id) { //组合搜索 $model = model('common/FarmlandShipping'); // 快递单号 $shipping_no = $this->request->param('shipping_no'); if ($this->request->isPost()){ $shipping = $model->findBy($id); // 状态以及时间 $model->updateBy($id,[ 'status' => 2, 'shipping_no' => $shipping_no, 'shipping_at' => time() ]); // 微信模板消息通知 // 加载配置 $config = sys_config('','wechat'); $wechat = Factory::miniProgram([ 'app_id' => $config['mini_appid'], 'secret' => $config['mni_secret_key'], 'response_type' => 'array', 'log' => [ 'level' => 'debug', 'file' => app()->getRuntimePath() . 'log/'.date('Ym').'/wechat_debug.log', ], ]); // 查订单 $order = model('common/FarmlandOrder')->findBy($shipping['order_id']); // 查找用户 $user = model('common/Users')->findBy($order['user_id']); // 发送消息 $wechat->subscribe_message->send([ 'template_id' => 'IrwlsHyxg7Lf2tZsDy6NwzkcAas8GuFP14NIxj5CHyU', // 所需下发的订阅模板id 'touser' => $user['open_id'], // 接收者(用户)的 openid 'page' => '/pages/index/index', // 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。 'data' => [ // 模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } } 'number2' => [ 'value' => $order['order_no'], // 订单号 ], 'thing1' => [ 'value' => '家庭农场采摘配送', // 名字 ], 'character_string13' => [ 'value' => $shipping_no, // 单号 ], 'date4' => [ 'value' => date('Y-m-d H:i:s'), // 时间 ] ] ]); return IResponse::success("发货成功"); } return IResponse::paginate($model->where(['order_id' => $id])->with(['order'])->order(['created_at' => 'desc']) ->paginate(input('limit'),false)); } /** * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2021/2/5 14:23 * * @param $id * @return mixed * @throws \think\exception\DbException */ public function orderDetail($id) { //组合搜索 $model = model('common/FarmlandOrderDetail'); if ($this->request->isPost()){ // // 标记采摘 // $model->updateBy($id,[ // 'status' => 2, // 'shipping_at' => time() // ]); // return IResponse::success("标记采摘"); } return IResponse::paginate($model->where(['order_id' => $id])->order(['created_at' => 'desc']) ->paginate(input('limit'),false)); } public function orderDetailAction($id) { $method = strtolower($this->request->method()); $param = $this->request->param(); //组合搜索 $model = model('common/FarmlandOrderDetail'); // 新增 if ($method == 'post'){ $model->storeBy($param); return IResponse::success([],"新增动态成功"); }else if ($method == 'put'){ $model->updateBy($param['id'], $param); // 更新 return IResponse::success("更新完成"); }else if ($method == 'delete'){ // 删除 return IResponse::failure("暂时无法删除"); } } /** * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2021/2/5 14:44 * * @param $id * @return mixed * @throws \think\exception\DbException */ public function orderImage($id) { //组合搜索 $model = model('common/FarmlandOrderImage'); return IResponse::paginate($model->where(['order_id' => $id])->order(['created_at' => 'desc']) ->paginate(input('limit'),false)); } /** * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2021/2/5 15:14 * */ public function orderImageAction() { $method = strtolower($this->request->method()); $param = $this->request->param(); //组合搜索 $model = model('common/FarmlandOrderImage'); // 新增 if ($method == 'post'){ $model->storeBy($param); return IResponse::success([],"新增动态成功"); }else if ($method == 'put'){ $model->updateBy($param['id'], $param); // 更新 return IResponse::success("更新完成"); }else if ($method == 'delete'){ // 删除 return IResponse::failure("暂时无法删除"); } } /** * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2021/2/5 17:42 * * @param $id * @return mixed * @throws \think\Exception * @throws \think\exception\PDOException */ public function actionOrder($id) { $param = $this->request->param(); $valid = $this->validate($param, [ 'action|操作状态' => 'require' ]); // 错误返回 (true !== $valid) && IResponse::failure($valid); $model = model('common/FarmlandOrder'); $msg = "操作"; /*1 . 下单未支付 unpay 2 . 支付待服务 wait 3 . 服务中 serve 4 . 服务结束 */ $serve = $model->findBy($id); switch ($param['action']){ case "dispense": $msg = "种植/养殖"; // 品种开始种植 model('common/FarmlandOrderDetail')->where(['order_id' => $serve['id']])->update(['status' => 2]); // 当前订单状态以及时间 $model->updateBy($id,[ 'status' => 3, 'start_at' => time() ]); break; case "complete": $msg = "结束服务 "; $model->updateBy($id,[ 'status' => 4 ]); model('common/FarmlandOrderDetail')->where(['order_id' => $serve['id']])->update(['status' => 4]); model('common/FarmlandBlock')->updateBy($serve['block_id'], [ 'status' => 1 ]); break; case "closed": $msg = "关闭服务"; $model->updateBy($id,[ 'status' => 0 ]); // 已支付的数据要返还金额 if($serve['status'] == 2){ // 先返款 model('common/Users')->changeBalance( $serve['user_id'], round($serve['price'], 2), '田地服务关闭,退还【' . $serve['price'] . '】', true ); } model('common/FarmlandOrderDetail')->where(['order_id' => $serve['id']])->update(['status' => 0]); model('common/FarmlandBlock')->updateBy($serve['block_id'], [ 'status' => 1 ]); break; } return IResponse::success($serve,$msg . '成功'); } /** * @param $id * * @return mixed */ public function deleteOrder($id) { model('common/FarmlandOrder')->deleteBy($id); return IResponse::success([],'删除订单成功'); } /** * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2021/2/5 11:42 * * @return mixed * @throws \think\exception\DbException */ public function getFarmlandBlockData() { $where = []; !empty(input('name')) && $where[] = ['name', 'like', '%' . input('name') . '%']; !empty(input('farm_id')) && $where[] = ['farm_id', 'eq', input('farm_id')]; //组合搜索 $user = model('common/FarmlandBlock'); return IResponse::paginate($user->where($where)->with(['farm'])->order(['created_at' => 'desc']) ->paginate(input('limit'),false)); } /** * 新增分类 * * @return mixed */ public function createFarmlandBlock() { // 接收数据 $params = $this->request->param(); // 数据校验 $valid = $this->validate($params,[ 'name|分类' => 'require|unique:FarmlandBlock', ],[ 'email.unique' => '重复分类名称!' ]); (true !== $valid) && IResponse::failure($valid); // 保存数据 $goodsId = model('common/FarmlandBlock')->storeBy($params); return $goodsId ? IResponse::success([],'新增分类成功'): IResponse::failure('新增分类异常'); } /** * 更新分类 * * @return mixed */ public function updateFarmlandBlock($id) { // 接收数据 $params = $this->request->param(); // 查询 $goods = model('common/FarmlandBlock')->findBy($id); // 是否更改状态操作 if (isset($params['status']) && $params['status'] != '') { $valid = $this->validate($params, [ 'status|配置状态' => 'require|integer' ]); }else { // 数据校验 $valid = $this->validate($params,[ 'name|产品分类' => 'require', ]); } // 错误返回 (true !== $valid) && IResponse::failure($valid); // 更新用户信息 $goods->updateBy($id, $params); return IResponse::success('更新信息成功'); } /** * 删除分类 * @param $id * * @return mixed */ public function deleteFarmlandBlock($id) { model('common/FarmlandBlock')->deleteBy($id); return IResponse::success([],'删除产品分类成功'); } /** * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2021/2/5 11:42 * * @return mixed * @throws \think\exception\DbException */ public function getFarmlandVarietyData() { $where = []; !empty(input('name')) && $where[] = ['name', 'like', '%' . input('name') . '%']; !empty(input('block_id')) && $where[] = ['block_id', 'eq', input('block_id')]; //组合搜索 $user = model('common/FarmlandVariety'); return IResponse::paginate($user->where($where)->with(['block'])->order(['created_at' => 'desc']) ->paginate(input('limit'),false)); } public function createFarmlandVariety() { // 接收数据 $params = $this->request->param(); if(strpos($params['img'],'[') !== false){ $params['img']=substr($params['img'], 2, -2); } // 数据校验 $valid = $this->validate($params,[ 'name|分类' => 'require', ]); (true !== $valid) && IResponse::failure($valid); // 保存数据 $goodsId = model('common/FarmlandVariety')->storeBy($params); return $goodsId ? IResponse::success([],'新增分类成功'): IResponse::failure('新增分类异常'); } public function updateFarmlandVariety($id) { // 接收数据 $params = $this->request->param(); if(strpos($params['img'],'[') !== false){ $params['img']=substr($params['img'], 2, -2); } // 查询 $goods = model('common/FarmlandVariety')->findBy($id); // 是否更改状态操作 if (isset($params['status']) && $params['status'] != '') { $valid = $this->validate($params, [ 'status|配置状态' => 'require|integer' ]); }else { // 数据校验 $valid = $this->validate($params,[ 'name|产品分类' => 'require', ]); } // 错误返回 (true !== $valid) && IResponse::failure($valid); // 更新用户信息 $goods->updateBy($id, $params); return IResponse::success('更新信息成功'); } /** * 删除分类 * @param $id * * @return mixed */ public function deleteFarmlandVariety($id) { model('common/FarmlandVariety')->deleteBy($id); return IResponse::success([],'删除产品分类成功'); } }