| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741 |
- <?php
- namespace app\admin\controller\store;
- use app\common\controller\AdminController;
- use app\http\IResponse;
- use EasyWeChat\Factory;
- class Farmland extends AdminController
- {
- /**
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @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([],'删除产品分类成功');
- }
- }
|