0
0

GGoods.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. class GGoods extends Base
  5. {
  6. public function updates(){
  7. $data = request()->post();
  8. $data['org_id'] = cur_org_id();
  9. $result = validate('GGoods')->check($data,[],'');
  10. if(true !== $result){
  11. $this->error = validate('GGoods')->getError();
  12. return false;
  13. }
  14. if(!empty($data['imgs'])){
  15. $data['imgs']=implode(',',$data['imgs']);;
  16. }
  17. $id = $data['id'];
  18. unset($data['id']);
  19. if($id > 0){
  20. $data['update_time'] = date('Y-m-d H:i:s');
  21. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  22. }else{
  23. $data['create_time'] = date('Y-m-d H:i:s');
  24. $ret = $this->allowField(true)->save($data);
  25. }
  26. if(!$ret){
  27. $this->error = '操作失败';
  28. return false;
  29. }
  30. return true;
  31. }
  32. public function lists($cateId,$title,$page,$size,$orgId){
  33. $map[] = ['org_id','=',$orgId];
  34. $map[] = ['enable','=',1];
  35. $map[] = ['del','=',0];
  36. if($cateId > 0){
  37. $map[] = ['cate_id','=',$cateId];
  38. }
  39. if($title != ''){
  40. $map[] = ['title','like','%'.$title.'%'];
  41. }
  42. $ret=Db::name('g_goods')
  43. ->field('id,title,price,img,sale_price,type')
  44. ->where($map)
  45. ->order(['sort'=>'asc','id'=>'desc'])
  46. ->page($page,$size)
  47. ->select();
  48. foreach ($ret as $k=>$v){
  49. if($v['type'] == 1){
  50. $ret[$k]['price']=$v['sale_price'];
  51. }
  52. unset($ret[$k]['sale_price']);
  53. }
  54. return $ret?$ret:[];
  55. }
  56. public function details($id){
  57. $ret=Db::name('g_goods')
  58. ->field('id,title,price,img,imgs,sale,stock,content,type,sale_price')
  59. ->where('id',$id)
  60. ->find();
  61. if($ret){
  62. // $ret['content']=format_editor($ret['content']);
  63. $ret['imgs']=$ret['imgs']?explode(',',$ret['imgs']):[];
  64. if($ret['type'] == 1){
  65. $ret['price']=$ret['sale_price'];
  66. }
  67. unset($ret['sale_price']);
  68. }
  69. return $ret;
  70. }
  71. public function saleList($orgId){
  72. $map[] = ['org_id','=',$orgId];
  73. $map[] = ['enable','=',1];
  74. $map[] = ['del','=',0];
  75. $map[] = ['type','=',1];
  76. $ret=Db::name('g_goods')
  77. ->field('id,title,img,sale_price as price')
  78. ->where($map)
  79. ->order(['sort'=>'asc','id'=>'desc'])
  80. ->limit(4)
  81. ->select();
  82. return $ret?$ret:[];
  83. }
  84. }