WxGoods.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. class WxGoods extends Auth
  6. {
  7. protected function initialize()
  8. {
  9. parent::initialize(); // TODO: Change the autogenerated stub
  10. }
  11. public function __construct(App $app = null) {
  12. parent::__construct($app);
  13. $this->table = 'wx_goods';
  14. $this->model = 'WxGoods';
  15. }
  16. public function index(){
  17. if(request()->isAjax()){
  18. //分页参数
  19. $length = input('rows',10,'intval'); //每页条数
  20. $page = input('page',1,'intval'); //第几页
  21. $start = ($page - 1) * $length; //分页开始位置
  22. //排序
  23. $sortRow = input('sidx','sort','trim'); //排序列
  24. $sort = input('sord','asc','trim'); //排序方式
  25. $order = $sortRow.' '.$sort.' ,id desc';
  26. $title = input('title','','trim');
  27. if($title){
  28. $map[] = ['title','like','%'.$title.'%'];
  29. }
  30. $enable = input('enable','','trim');
  31. if($enable != ''){
  32. $map[] = ['enable','=',$enable];
  33. }
  34. $is_tj = input('is_tj','','trim');
  35. if($is_tj != ''){
  36. $map[] = ['is_tj','=',$is_tj];
  37. }
  38. $cate_id = input('cate_id','','trim');
  39. if($cate_id != ''){
  40. $map[] = ['cate_id','=',$cate_id];
  41. }
  42. $map[] = ['org_id','=',cur_org_id()];
  43. $map[] = ['del','=',0];
  44. $map= empty($map) ? true: $map;
  45. //数据查询
  46. $lists = Db::name($this->table)
  47. ->where($map)->limit($start,$length)
  48. ->order($order)->select();
  49. foreach ($lists as $k=>$v){
  50. $lists[$k]['cate_name']=Db::name('wx_goods_cate')
  51. ->where('id',$v['cate_id'])->value('title');
  52. $lists[$k]['label_name']=Db::name('wx_goods_label')
  53. ->where('id',$v['label'])->value('title');
  54. }
  55. //数据返回
  56. $totalCount = Db::name($this->table)->where($map)->count();
  57. $totalPage = ceil($totalCount/$length);
  58. $result['page'] = $page;
  59. $result['total'] = $totalPage;
  60. $result['records'] = $totalCount;
  61. $result['rows'] = $lists;
  62. return json($result);
  63. }else{
  64. $cateList = model('WxGoodsCate')->lists($this->orgId);
  65. $label =model('WxGoodsCate')->getLabel();
  66. $this->assign('cateList',$cateList);
  67. $this->assign('label',$label);
  68. $this->assign('meta_title','商品列表');
  69. return $this->fetch();
  70. }
  71. }
  72. /**
  73. * 新增/编辑
  74. */
  75. public function add($id=0){
  76. if(request()->isPost()){
  77. $res = model($this->model)->updates();
  78. if($res){
  79. $this->success('操作成功',url('index'));
  80. }else{
  81. $this->error(model($this->model)->getError());
  82. }
  83. }else{
  84. $meta_title = '新增商品';
  85. if($id){
  86. $info = Db::name($this->table)->where('id',$id)->find();
  87. $this->assign('info',$info);
  88. $cateInfo = Db::name('wx_goods_cate')
  89. ->where('id',$info['cate_id'])
  90. ->find();
  91. $this->assign('cateInfo',$cateInfo);
  92. $meta_title = '编辑商品';
  93. }
  94. $cateList = model('WxGoodsCate')->lists($this->orgId);
  95. $label =model('WxGoodsCate')->getLabel();
  96. $barrel =model('WxBarrel')->lists($this->orgId);
  97. $this->assign('cateList',$cateList);
  98. $this->assign('label',$label);
  99. $this->assign('barrel',$barrel);
  100. $this->assign('meta_title',$meta_title);
  101. return $this->fetch();
  102. }
  103. }
  104. /**
  105. * 删除记录
  106. * @param int $id
  107. */
  108. public function del($id=0){
  109. if(!$id){
  110. $this->error('参数错误');
  111. }
  112. $res = Db::name($this->table)->where('id',$id)->update(['del'=>1,'update_time'=>getTime()]);
  113. if($res){
  114. $this->success('删除成功');
  115. }else{
  116. $this->error('删除失败');
  117. }
  118. }
  119. /**
  120. * 改变字段值
  121. * @param int $fv
  122. * @param string $fn
  123. * @param int $fv
  124. */
  125. public function changeField($id=0,$fn='',$fv=0){
  126. if(!$fn||!$id){
  127. $this->error('参数错误');
  128. }
  129. $res = Db::name($this->table)->where('id',$id)->update([$fn => $fv]);
  130. if($res){
  131. $this->success('操作成功');
  132. }else{
  133. $this->error('操作失败');
  134. }
  135. }
  136. /**
  137. * 排序
  138. * @param int $id
  139. * @param int $sort
  140. */
  141. public function changeSort($id=0,$sort=0){
  142. if($id<0||$sort<0){
  143. $this->error('参数错误');
  144. }
  145. $res = Db::name($this->table)->where('id',$id)->update(['sort'=>$sort]);
  146. if($res){
  147. $this->success('操作成功');
  148. }else{
  149. $this->error('操作失败');
  150. }
  151. }
  152. public function checkIsWater(){
  153. $id =input('id/d',0);
  154. $info = Db::name('wx_goods_cate')
  155. ->where('id',$id)
  156. ->find();
  157. $this->success('操作成功','',$info);
  158. }
  159. }