Finishedproduct.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use function EasyWeChat\Kernel\Support\rsa_public_encrypt;
  5. /**
  6. * 版本管理
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Finishedproduct extends Backend
  11. {
  12. /**
  13. * Finishedproduct模型对象
  14. * @var \app\admin\model\Finishedproduct
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\Finishedproduct;
  21. $this->view->assign("statusList", $this->model->getStatusList());
  22. }
  23. /**
  24. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  25. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  26. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  27. */
  28. public function index()
  29. {
  30. // 获取前端传入的参数:搜索关键词、页码、每页数量(默认为第一页,每页10条)
  31. $search = input('get.search');
  32. $page = input('get.page', 1);
  33. $limit = input('get.limit', 10);
  34. // 连接配置中的 db3 数据库
  35. $db3 = \db()->connect(config('database.db3'));
  36. $query = $db3->name('成品入仓')
  37. ->where('jjcp_smb', '=', '末 板')
  38. ->whereNull('Mod_rq');
  39. $where = ['jjcp_cpdh'=>['like','%'.$search.'%']];
  40. $total = $query->where($where)->count();
  41. $data = $query->field('jjcp_cpdh, jjcp_cpmc, jjcp_sl, UniqId, jjcp_smb')
  42. ->where($where)
  43. ->limit(($page - 1) * $limit, $limit)
  44. ->select();
  45. foreach ($data as &$row) {
  46. $row['jjcp_smb'] = ($row['jjcp_smb'] === '末 板') ? '完工入库' : '未完工入库';
  47. }
  48. //AJAX 请求
  49. if (request()->isAjax()) {
  50. return json([
  51. 'data' => $data,
  52. 'total' => $total,
  53. 'page' => $page,
  54. 'limit' => $limit
  55. ]);
  56. }
  57. $this->assign('data', $data);
  58. return $this->fetch();
  59. }
  60. public function finished()
  61. {
  62. // 获取 JSON 格式的请求体
  63. $jsonData = file_get_contents("php://input");
  64. // 解析为 PHP 数组
  65. $data = json_decode($jsonData, true);
  66. if (!isset($data['data']) || !is_array($data['data'])) {
  67. return json(['status' => 'error', 'msg' => '无效数据']);
  68. }
  69. // 连接 MongoDB 数据库(你在 config/database.php 中的 mongodb 配置名)
  70. $mongo = \think\Db::connect('mongodb');
  71. $insertList = [];
  72. foreach ($data['data'] as $item) {
  73. $insertList[] = [
  74. 'jjcp_cpdh' => $item['jjcp_cpdh'],
  75. 'jjcp_cpmc' => $item['jjcp_cpmc'],
  76. 'jjcp_sl' => intval($item['jjcp_sl']),
  77. 'jjcp_smb' => '已入库',
  78. 'created_at' => date('Y-m-d H:i:s')
  79. ];
  80. }
  81. try {
  82. // 批量插入 MongoDB 集合(表) Finished_products
  83. $mongo->name('Finished_products')->insertAll($insertList);
  84. return json(['status' => 'success', 'msg' => '成功插入 MongoDB']);
  85. } catch (\Exception $e) {
  86. return json(['status' => 'error', 'msg' => '插入失败:' . $e->getMessage()]);
  87. }
  88. }
  89. }