Farmland.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. <?php
  2. namespace app\admin\controller\store;
  3. use app\common\controller\AdminController;
  4. use app\http\IResponse;
  5. use EasyWeChat\Factory;
  6. class Farmland extends AdminController
  7. {
  8. /**
  9. *
  10. * @author 许祖兴 < zuxing.xu@lettered.cn>
  11. * @date 2020/7/15 18:20
  12. *
  13. * @return mixed
  14. * @throws \think\exception\DbException
  15. */
  16. public function index()
  17. {
  18. $where = [];
  19. (!empty(input('status')) || input('status') == '0') &&
  20. $where[] = ['status', 'eq', input('status')];
  21. !empty(input('name')) && $where[]
  22. = ['name', 'like', '%' . input('name') . '%'];
  23. //组合搜索
  24. $farmland = model('common/Farmland');
  25. return IResponse::paginate($farmland->where($where)
  26. ->paginate(input('limit'),false));
  27. }
  28. /**
  29. * 新增
  30. *
  31. * @author 许祖兴 < zuxing.xu@lettered.cn>
  32. * @date 2020/06/07 23:59
  33. *
  34. * @return mixed
  35. */
  36. public function save()
  37. {
  38. // 接收数据
  39. $params = $this->request->param();
  40. // 数据校验
  41. $valid = $this->validate($params, [
  42. 'name|技能名称' => 'require',
  43. 'cover_img|展示图片' => 'require'
  44. ]);
  45. // 错误返回
  46. (true !== $valid) && IResponse::failure($valid);
  47. // 保存数据
  48. $skillId = model("common/Farmland")->storeBy($params);
  49. return $skillId ? IResponse::success([],'新增信息成功'):
  50. IResponse::failure('新增信息异常');
  51. }
  52. /**
  53. * 更新数据
  54. *
  55. * @author 许祖兴 < zuxing.xu@lettered.cn>
  56. * @date 2020/06/07 14:24
  57. *
  58. * @param $id
  59. * @return \think\response\Json
  60. */
  61. public function update($id)
  62. {
  63. // 接收数据
  64. $params = $this->request->param();
  65. // 查询用户
  66. $skill = model('common/Farmland')->findBy($id);
  67. // 是否更改状态操作
  68. if (isset($params['status']) && $params['status'] != '') {
  69. $valid = $this->validate($params, [
  70. 'status|配置状态' => 'require|integer'
  71. ]);
  72. }else {
  73. // 数据校验
  74. $valid = $this->validate($params, [
  75. 'name|技能名称' => 'require',
  76. 'cover_img|展示图片' => 'require'
  77. ]);
  78. }
  79. // 错误返回
  80. (true !== $valid) && IResponse::failure($valid);
  81. // 更新信息
  82. $skill->updateBy($id, $params);
  83. return IResponse::success('更新信息成功');
  84. }
  85. /**
  86. * 删除
  87. *
  88. * @author 许祖兴 < zuxing.xu@lettered.cn>
  89. * @date 2020/3/16 14:22
  90. *
  91. * @param $id
  92. * @return \think\response\Json
  93. */
  94. public function delete($id)
  95. {
  96. model('common/Farmland')->deleteBy($id);
  97. return IResponse::success([],'删除技能成功');
  98. }
  99. /**
  100. * 批量操作
  101. *
  102. * @return mixed
  103. */
  104. public function plectron(){
  105. // 收参数
  106. $params = $this->request->param();
  107. foreach (str2arr($params['ids']) as $id){
  108. $skills = model('common/Farmland')->getBy($id);
  109. if ($this->request->isDelete()){
  110. $skills->deleteBy($id);
  111. }
  112. $skills->allowField(true)->updateBy($id, $params);
  113. }
  114. return IResponse::success([],'操作成功');
  115. }
  116. public function getClassData()
  117. {
  118. $where = [];
  119. //组合搜索
  120. $user = model('common/FarmlandClass');
  121. return IResponse::paginate($user->where($where)
  122. ->paginate(input('limit'),false));
  123. }
  124. /**
  125. * 新增分类
  126. *
  127. * @return mixed
  128. */
  129. public function createClass()
  130. {
  131. // 接收数据
  132. $params = $this->request->param();
  133. // 数据校验
  134. $valid = $this->validate($params,[
  135. 'name|分类' => 'require|unique:FarmlandClass',
  136. ],[
  137. 'email.unique' => '重复分类名称!'
  138. ]);
  139. (true !== $valid) && IResponse::failure($valid);
  140. // 保存数据
  141. $goodsId = model('common/FarmlandClass')->storeBy($params);
  142. return $goodsId ? IResponse::success([],'新增分类成功'):
  143. IResponse::failure('新增分类异常');
  144. }
  145. /**
  146. * 更新分类
  147. *
  148. * @return mixed
  149. */
  150. public function updateClass($id)
  151. {
  152. // 接收数据
  153. $params = $this->request->param();
  154. // 查询
  155. $goods = model('common/FarmlandClass')->findBy($id);
  156. // 是否更改状态操作
  157. if (isset($params['status']) && $params['status'] != '') {
  158. $valid = $this->validate($params, [
  159. 'status|配置状态' => 'require|integer'
  160. ]);
  161. }else {
  162. // 数据校验
  163. $valid = $this->validate($params,[
  164. 'name|产品分类' => 'require',
  165. ]);
  166. }
  167. // 错误返回
  168. (true !== $valid) && IResponse::failure($valid);
  169. // 更新用户信息
  170. $goods->updateBy($id, $params);
  171. return IResponse::success('更新信息成功');
  172. }
  173. /**
  174. * 删除分类
  175. * @param $id
  176. *
  177. * @return mixed
  178. */
  179. public function deleteClass($id)
  180. {
  181. model('common/FarmlandClass')->deleteBy($id);
  182. return IResponse::success([],'删除产品分类成功');
  183. }
  184. public function getFarmlandDetail()
  185. {
  186. $where = [];
  187. //组合搜索
  188. $user = model('common/FarmlandDetail');
  189. return IResponse::paginate($user->where($where)->with(['farm','classify'])
  190. ->paginate(input('limit'),false));
  191. }
  192. /**
  193. * 新增分类
  194. *
  195. * @return mixed
  196. */
  197. public function createFarmlandDetail()
  198. {
  199. // 接收数据
  200. $params = $this->request->param();
  201. // 数据校验
  202. $valid = $this->validate($params,[
  203. 'name|分类' => 'require|unique:FarmlandClass',
  204. ],[
  205. 'email.unique' => '重复分类名称!'
  206. ]);
  207. (true !== $valid) && IResponse::failure($valid);
  208. // 保存数据
  209. $goodsId = model('common/FarmlandDetail')->storeBy($params);
  210. return $goodsId ? IResponse::success([],'新增分类成功'):
  211. IResponse::failure('新增分类异常');
  212. }
  213. /**
  214. * 更新分类
  215. *
  216. * @return mixed
  217. */
  218. public function updateFarmlandDetail($id)
  219. {
  220. // 接收数据
  221. $params = $this->request->param();
  222. // 查询
  223. $goods = model('common/FarmlandDetail')->findBy($id);
  224. // 是否更改状态操作
  225. if (isset($params['status']) && $params['status'] != '') {
  226. $valid = $this->validate($params, [
  227. 'status|配置状态' => 'require|integer'
  228. ]);
  229. }else {
  230. // 数据校验
  231. $valid = $this->validate($params,[
  232. 'name|产品分类' => 'require',
  233. ]);
  234. }
  235. // 错误返回
  236. (true !== $valid) && IResponse::failure($valid);
  237. // 更新用户信息
  238. $goods->updateBy($id, $params);
  239. return IResponse::success('更新信息成功');
  240. }
  241. /**
  242. * 删除分类
  243. * @param $id
  244. *
  245. * @return mixed
  246. */
  247. public function deleteFarmlandDetail($id)
  248. {
  249. model('common/FarmlandDetail')->deleteBy($id);
  250. return IResponse::success([],'删除产品分类成功');
  251. }
  252. /******************** 订单 *********************/
  253. /**
  254. * @return mixed
  255. * @throws \think\exception\DbException
  256. */
  257. public function orders()
  258. {
  259. $where = [];
  260. (!empty(input('status')) || input('status') == '0') &&
  261. $where[] = ['status', 'eq', input('status')];
  262. !empty(input('keywords')) && $where[]
  263. = ['order_no|user_id', 'like', '%' . input('keywords') . '%'];
  264. // 时间处理
  265. if (!empty(input('created_at'))){
  266. list($start, $end) = str2arr(input('created_at'),'-');
  267. $where[] = ['created_at', 'between', [strtotime($start), strtotime($end)]];
  268. }
  269. //组合搜索
  270. $user = model('common/FarmlandOrder');
  271. return IResponse::paginate($user->where($where)->with(['block','user'])->order(['created_at' => 'desc'])
  272. ->paginate(input('limit'),false));
  273. }
  274. /**
  275. *
  276. * @author 许祖兴 < zuxing.xu@lettered.cn>
  277. * @date 2021/1/25 17:15
  278. *
  279. * @param $id
  280. * @return mixed
  281. * @throws \think\exception\DbException
  282. */
  283. public function orderShipping($id)
  284. {
  285. //组合搜索
  286. $model = model('common/FarmlandShipping');
  287. // 快递单号
  288. $shipping_no = $this->request->param('shipping_no');
  289. if ($this->request->isPost()){
  290. $shipping = $model->findBy($id);
  291. // 状态以及时间
  292. $model->updateBy($id,[
  293. 'status' => 2,
  294. 'shipping_no' => $shipping_no,
  295. 'shipping_at' => time()
  296. ]);
  297. // 微信模板消息通知
  298. // 加载配置
  299. $config = sys_config('','wechat');
  300. $wechat = Factory::miniProgram([
  301. 'app_id' => $config['mini_appid'],
  302. 'secret' => $config['mni_secret_key'],
  303. 'response_type' => 'array',
  304. 'log' => [
  305. 'level' => 'debug',
  306. 'file' => app()->getRuntimePath() . 'log/'.date('Ym').'/wechat_debug.log',
  307. ],
  308. ]);
  309. // 查订单
  310. $order = model('common/FarmlandOrder')->findBy($shipping['order_id']);
  311. // 查找用户
  312. $user = model('common/Users')->findBy($order['user_id']);
  313. // 发送消息
  314. $wechat->subscribe_message->send([
  315. 'template_id' => 'IrwlsHyxg7Lf2tZsDy6NwzkcAas8GuFP14NIxj5CHyU', // 所需下发的订阅模板id
  316. 'touser' => $user['open_id'], // 接收者(用户)的 openid
  317. 'page' => '/pages/index/index', // 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
  318. 'data' => [ // 模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } }
  319. 'number2' => [
  320. 'value' => $order['order_no'], // 订单号
  321. ],
  322. 'thing1' => [
  323. 'value' => '家庭农场采摘配送', // 名字
  324. ],
  325. 'character_string13' => [
  326. 'value' => $shipping_no, // 单号
  327. ],
  328. 'date4' => [
  329. 'value' => date('Y-m-d H:i:s'), // 时间
  330. ]
  331. ]
  332. ]);
  333. return IResponse::success("发货成功");
  334. }
  335. return IResponse::paginate($model->where(['order_id' => $id])->with(['order'])->order(['created_at' => 'desc'])
  336. ->paginate(input('limit'),false));
  337. }
  338. /**
  339. *
  340. * @author 许祖兴 < zuxing.xu@lettered.cn>
  341. * @date 2021/2/5 14:23
  342. *
  343. * @param $id
  344. * @return mixed
  345. * @throws \think\exception\DbException
  346. */
  347. public function orderDetail($id)
  348. {
  349. //组合搜索
  350. $model = model('common/FarmlandOrderDetail');
  351. if ($this->request->isPost()){
  352. // // 标记采摘
  353. // $model->updateBy($id,[
  354. // 'status' => 2,
  355. // 'shipping_at' => time()
  356. // ]);
  357. // return IResponse::success("标记采摘");
  358. }
  359. return IResponse::paginate($model->where(['order_id' => $id])->order(['created_at' => 'desc'])
  360. ->paginate(input('limit'),false));
  361. }
  362. public function orderDetailAction($id)
  363. {
  364. $method = strtolower($this->request->method());
  365. $param = $this->request->param();
  366. //组合搜索
  367. $model = model('common/FarmlandOrderDetail');
  368. // 新增
  369. if ($method == 'post'){
  370. $model->storeBy($param);
  371. return IResponse::success([],"新增动态成功");
  372. }else if ($method == 'put'){
  373. $model->updateBy($param['id'], $param);
  374. // 更新
  375. return IResponse::success("更新完成");
  376. }else if ($method == 'delete'){
  377. // 删除
  378. return IResponse::failure("暂时无法删除");
  379. }
  380. }
  381. /**
  382. *
  383. * @author 许祖兴 < zuxing.xu@lettered.cn>
  384. * @date 2021/2/5 14:44
  385. *
  386. * @param $id
  387. * @return mixed
  388. * @throws \think\exception\DbException
  389. */
  390. public function orderImage($id)
  391. {
  392. //组合搜索
  393. $model = model('common/FarmlandOrderImage');
  394. return IResponse::paginate($model->where(['order_id' => $id])->order(['created_at' => 'desc'])
  395. ->paginate(input('limit'),false));
  396. }
  397. /**
  398. *
  399. * @author 许祖兴 < zuxing.xu@lettered.cn>
  400. * @date 2021/2/5 15:14
  401. *
  402. */
  403. public function orderImageAction()
  404. {
  405. $method = strtolower($this->request->method());
  406. $param = $this->request->param();
  407. //组合搜索
  408. $model = model('common/FarmlandOrderImage');
  409. // 新增
  410. if ($method == 'post'){
  411. $model->storeBy($param);
  412. return IResponse::success([],"新增动态成功");
  413. }else if ($method == 'put'){
  414. $model->updateBy($param['id'], $param);
  415. // 更新
  416. return IResponse::success("更新完成");
  417. }else if ($method == 'delete'){
  418. // 删除
  419. return IResponse::failure("暂时无法删除");
  420. }
  421. }
  422. /**
  423. *
  424. * @author 许祖兴 < zuxing.xu@lettered.cn>
  425. * @date 2021/2/5 17:42
  426. *
  427. * @param $id
  428. * @return mixed
  429. * @throws \think\Exception
  430. * @throws \think\exception\PDOException
  431. */
  432. public function actionOrder($id)
  433. {
  434. $param = $this->request->param();
  435. $valid = $this->validate($param, [
  436. 'action|操作状态' => 'require'
  437. ]);
  438. // 错误返回
  439. (true !== $valid) && IResponse::failure($valid);
  440. $model = model('common/FarmlandOrder');
  441. $msg = "操作";
  442. /*1 . 下单未支付 unpay
  443. 2 . 支付待服务 wait
  444. 3 . 服务中 serve
  445. 4 . 服务结束 */
  446. $serve = $model->findBy($id);
  447. switch ($param['action']){
  448. case "dispense":
  449. $msg = "种植/养殖";
  450. // 品种开始种植
  451. model('common/FarmlandOrderDetail')->where(['order_id' => $serve['id']])->update(['status' => 2]);
  452. // 当前订单状态以及时间
  453. $model->updateBy($id,[
  454. 'status' => 3,
  455. 'start_at' => time()
  456. ]);
  457. break;
  458. case "complete":
  459. $msg = "结束服务 ";
  460. $model->updateBy($id,[
  461. 'status' => 4
  462. ]);
  463. model('common/FarmlandOrderDetail')->where(['order_id' => $serve['id']])->update(['status' => 4]);
  464. model('common/FarmlandBlock')->updateBy($serve['block_id'], [
  465. 'status' => 1
  466. ]);
  467. break;
  468. case "closed":
  469. $msg = "关闭服务";
  470. $model->updateBy($id,[
  471. 'status' => 0
  472. ]);
  473. // 已支付的数据要返还金额
  474. if($serve['status'] == 2){
  475. // 先返款
  476. model('common/Users')->changeBalance(
  477. $serve['user_id'],
  478. round($serve['price'], 2),
  479. '田地服务关闭,退还【' . $serve['price'] . '】',
  480. true
  481. );
  482. }
  483. model('common/FarmlandOrderDetail')->where(['order_id' => $serve['id']])->update(['status' => 0]);
  484. model('common/FarmlandBlock')->updateBy($serve['block_id'], [
  485. 'status' => 1
  486. ]);
  487. break;
  488. }
  489. return IResponse::success($serve,$msg . '成功');
  490. }
  491. /**
  492. * @param $id
  493. *
  494. * @return mixed
  495. */
  496. public function deleteOrder($id)
  497. {
  498. model('common/FarmlandOrder')->deleteBy($id);
  499. return IResponse::success([],'删除订单成功');
  500. }
  501. /**
  502. *
  503. * @author 许祖兴 < zuxing.xu@lettered.cn>
  504. * @date 2021/2/5 11:42
  505. *
  506. * @return mixed
  507. * @throws \think\exception\DbException
  508. */
  509. public function getFarmlandBlockData()
  510. {
  511. $where = [];
  512. !empty(input('name')) && $where[]
  513. = ['name', 'like', '%' . input('name') . '%'];
  514. !empty(input('farm_id')) && $where[]
  515. = ['farm_id', 'eq', input('farm_id')];
  516. //组合搜索
  517. $user = model('common/FarmlandBlock');
  518. return IResponse::paginate($user->where($where)->with(['farm'])->order(['created_at' => 'desc'])
  519. ->paginate(input('limit'),false));
  520. }
  521. /**
  522. * 新增分类
  523. *
  524. * @return mixed
  525. */
  526. public function createFarmlandBlock()
  527. {
  528. // 接收数据
  529. $params = $this->request->param();
  530. // 数据校验
  531. $valid = $this->validate($params,[
  532. 'name|分类' => 'require|unique:FarmlandBlock',
  533. ],[
  534. 'email.unique' => '重复分类名称!'
  535. ]);
  536. (true !== $valid) && IResponse::failure($valid);
  537. // 保存数据
  538. $goodsId = model('common/FarmlandBlock')->storeBy($params);
  539. return $goodsId ? IResponse::success([],'新增分类成功'):
  540. IResponse::failure('新增分类异常');
  541. }
  542. /**
  543. * 更新分类
  544. *
  545. * @return mixed
  546. */
  547. public function updateFarmlandBlock($id)
  548. {
  549. // 接收数据
  550. $params = $this->request->param();
  551. // 查询
  552. $goods = model('common/FarmlandBlock')->findBy($id);
  553. // 是否更改状态操作
  554. if (isset($params['status']) && $params['status'] != '') {
  555. $valid = $this->validate($params, [
  556. 'status|配置状态' => 'require|integer'
  557. ]);
  558. }else {
  559. // 数据校验
  560. $valid = $this->validate($params,[
  561. 'name|产品分类' => 'require',
  562. ]);
  563. }
  564. // 错误返回
  565. (true !== $valid) && IResponse::failure($valid);
  566. // 更新用户信息
  567. $goods->updateBy($id, $params);
  568. return IResponse::success('更新信息成功');
  569. }
  570. /**
  571. * 删除分类
  572. * @param $id
  573. *
  574. * @return mixed
  575. */
  576. public function deleteFarmlandBlock($id)
  577. {
  578. model('common/FarmlandBlock')->deleteBy($id);
  579. return IResponse::success([],'删除产品分类成功');
  580. }
  581. /**
  582. *
  583. * @author 许祖兴 < zuxing.xu@lettered.cn>
  584. * @date 2021/2/5 11:42
  585. *
  586. * @return mixed
  587. * @throws \think\exception\DbException
  588. */
  589. public function getFarmlandVarietyData()
  590. {
  591. $where = [];
  592. !empty(input('name')) && $where[]
  593. = ['name', 'like', '%' . input('name') . '%'];
  594. !empty(input('block_id')) && $where[]
  595. = ['block_id', 'eq', input('block_id')];
  596. //组合搜索
  597. $user = model('common/FarmlandVariety');
  598. return IResponse::paginate($user->where($where)->with(['block'])->order(['created_at' => 'desc'])
  599. ->paginate(input('limit'),false));
  600. }
  601. public function createFarmlandVariety()
  602. {
  603. // 接收数据
  604. $params = $this->request->param();
  605. if(strpos($params['img'],'[') !== false){
  606. $params['img']=substr($params['img'], 2, -2);
  607. }
  608. // 数据校验
  609. $valid = $this->validate($params,[
  610. 'name|分类' => 'require',
  611. ]);
  612. (true !== $valid) && IResponse::failure($valid);
  613. // 保存数据
  614. $goodsId = model('common/FarmlandVariety')->storeBy($params);
  615. return $goodsId ? IResponse::success([],'新增分类成功'):
  616. IResponse::failure('新增分类异常');
  617. }
  618. public function updateFarmlandVariety($id)
  619. {
  620. // 接收数据
  621. $params = $this->request->param();
  622. if(strpos($params['img'],'[') !== false){
  623. $params['img']=substr($params['img'], 2, -2);
  624. }
  625. // 查询
  626. $goods = model('common/FarmlandVariety')->findBy($id);
  627. // 是否更改状态操作
  628. if (isset($params['status']) && $params['status'] != '') {
  629. $valid = $this->validate($params, [
  630. 'status|配置状态' => 'require|integer'
  631. ]);
  632. }else {
  633. // 数据校验
  634. $valid = $this->validate($params,[
  635. 'name|产品分类' => 'require',
  636. ]);
  637. }
  638. // 错误返回
  639. (true !== $valid) && IResponse::failure($valid);
  640. // 更新用户信息
  641. $goods->updateBy($id, $params);
  642. return IResponse::success('更新信息成功');
  643. }
  644. /**
  645. * 删除分类
  646. * @param $id
  647. *
  648. * @return mixed
  649. */
  650. public function deleteFarmlandVariety($id)
  651. {
  652. model('common/FarmlandVariety')->deleteBy($id);
  653. return IResponse::success([],'删除产品分类成功');
  654. }
  655. }