ShopGoods.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. class ShopGoods extends Auth {
  5. public function index() {
  6. if (request()->isAjax()) {
  7. //分页参数
  8. $length = input('rows', 10, 'intval'); //每页条数
  9. $page = input('page', 1, 'intval'); //第几页
  10. $start = ($page - 1) * $length; //分页开始位置
  11. //排序
  12. $sortRow = input('sidx', 'id', 'trim'); //排序列
  13. $sort = input('sord', 'asc', 'trim'); //排序方式
  14. $order = $sortRow . ' ' . $sort;
  15. $title = input('title', '', 'trim');
  16. if ($title) {
  17. $map[] = ['title', 'like', '%' . $title . '%'];
  18. }
  19. $enable = input('enable', '', 'trim');
  20. if ($enable != '') {
  21. $map[] = ['enable', '=', $enable];
  22. }
  23. $map[] = ['org_id', '=', $this->orgId];
  24. $map[] = ['del', '=', 0];
  25. $map = empty($map) ? true : $map;
  26. //数据查询
  27. $lists = Db::name('shop_goods')
  28. ->where($map)->limit($start, $length)
  29. ->order($order)->select();
  30. foreach ($lists as $k => $v) {
  31. $lists[$k]['cate_name'] = Db::name('shop_goods_cate')
  32. ->where('id', $v['cate_id'])->value('name');
  33. }
  34. //数据返回
  35. $totalCount = Db::name('shop_goods')->where($map)->count();
  36. $totalPage = ceil($totalCount / $length);
  37. $result['page'] = $page;
  38. $result['total'] = $totalPage;
  39. $result['records'] = $totalCount;
  40. $result['rows'] = $lists;
  41. return json($result);
  42. }
  43. else {
  44. $this->assign('meta_title', '商品列表');
  45. return $this->fetch();
  46. }
  47. }
  48. /**
  49. * 新增/编辑
  50. */
  51. public function add($id = 0) {
  52. if (request()->isPost()) {
  53. $model = new \app\common\model\ShopGoods();
  54. $res = $model->updates();
  55. if ($res) {
  56. $this->success('操作成功', url('index'));
  57. }
  58. else {
  59. $this->error($model->getError());
  60. }
  61. }
  62. else {
  63. $meta_title = '新增商品';
  64. if ($id) {
  65. $info = Db::name('shop_goods')->where('id', $id)->find();
  66. $this->assign('info', $info);
  67. $meta_title = '编辑商品';
  68. }
  69. //获取分类
  70. $map['org_id'] = $this->orgId;
  71. $cate = Db::name('shop_goods_cate')
  72. ->field('id as cate_id,name')->where($map)->select();
  73. $this->assign('goods_cate', $cate);
  74. $this->assign('meta_title', $meta_title);
  75. return $this->fetch();
  76. }
  77. }
  78. /**
  79. * 删除记录
  80. * @param int $id
  81. */
  82. public function del($id = 0) {
  83. if (!$id) {
  84. $this->error('参数错误');
  85. }
  86. $res = Db::name('shop_goods')->where('id', $id)->update(['del' => 1]);
  87. if ($res) {
  88. $this->success('删除成功');
  89. }
  90. else {
  91. $this->error('删除失败');
  92. }
  93. }
  94. /**
  95. * 改变字段值
  96. * @param int $fv
  97. * @param string $fn
  98. * @param int $fv
  99. */
  100. public function changeField($id = 0, $fn = '', $fv = 0) {
  101. if (!$fn || !$id) {
  102. $this->error('参数错误');
  103. }
  104. $res = Db::name('shop_goods')->where('id', $id)->update([$fn => $fv]);
  105. if ($res) {
  106. $this->success('操作成功');
  107. }
  108. else {
  109. $this->error('操作失败');
  110. }
  111. }
  112. }