GreenShop.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 商品管理(SPU)
  7. */
  8. namespace app\green\model;
  9. use think\Model;
  10. class GreenShop extends Model{
  11. protected $autoWriteTimestamp = true;
  12. protected $createTime = false;
  13. /**
  14. * 搜索封装
  15. */
  16. public function searchNameAttr($query, $value, $data){
  17. if(empty($value)){
  18. return;
  19. }
  20. $query->where('name','like','%' . $value . '%');
  21. }
  22. /**
  23. * 读取商品列表
  24. * @param string $status
  25. * @param string $keyword
  26. * @return void
  27. */
  28. public static function list(int $miniapp_id,string $status,string $keyword = null,$input = null){
  29. $condition[] = ["member_miniapp_id","=",$miniapp_id];
  30. switch ($status) {
  31. case 'trash': //回收站
  32. $condition[] = ["is_del","=",1];
  33. break;
  34. case 'off_sale': //在售
  35. $condition[] = ["is_del","=",1];
  36. $condition[] = ["is_del","=",0];
  37. break;
  38. case 'on_sale': //下架
  39. $condition[] = ["is_del","=",0];
  40. $condition[] = ["is_del","=",0];
  41. break;
  42. default:
  43. $condition[] = ["is_del","=",0];
  44. break;
  45. }
  46. return self::where($condition)->withSearch(['name'],['name' => $keyword])->order('id desc')->paginate(20,false,['query'=>['status' => $status,'keyword' => $keyword,'input'=> $input]]);
  47. }
  48. /**
  49. * 检测单个SPU商品是否上架
  50. * @param integer $spu_id
  51. * @return void
  52. */
  53. public static function isSell(int $spu_id){
  54. return self::where(['id' => $spu_id,'is_sale'=>2])->find();
  55. }
  56. //添加或编辑
  57. public static function edit(int $miniapp_id,array $param){
  58. $data['member_miniapp_id'] = $miniapp_id;
  59. $data['name'] = $param['name'];
  60. $data['note'] = $param['note'];
  61. $data['points'] = (int)$param['points'];
  62. $data['content'] = $param['content'];
  63. $data['img'] = $param['img'];
  64. $data['imgs'] = json_encode($param['imgs']);
  65. $data['update_time'] = time();
  66. if(isset($param['id'])){
  67. $id = (int)$param['id'];
  68. self::where('id',$id)->update($data);
  69. return $id;
  70. }else{
  71. $data['is_sale'] = 0;
  72. return self::insertGetId($data);
  73. }
  74. }
  75. //批量操作
  76. public static function ids_action(int $is_sale,string $ids){
  77. switch ($is_sale) {
  78. case 1:
  79. $data['is_sale'] = 1; //回收站
  80. break;
  81. case 2:
  82. $data['is_del'] = 0; //恢复
  83. $data['is_sale'] = 0; //下架
  84. break;
  85. default:
  86. $data['is_sale'] = 0; //下架
  87. break;
  88. }
  89. return self::whereIn('id',ids($ids))->data($data)->update(); //操作所有SPU商品
  90. }
  91. /**
  92. * 删除SPU商品
  93. *
  94. * @param [type] $id
  95. * @param [type] $ids
  96. * @return void
  97. */
  98. public static function spu_delete(int $id,$ids){
  99. if(!empty($id)){
  100. $ids = (int)$id;
  101. }elseif(!empty($ids)){
  102. $ids = ids($ids);
  103. }else{
  104. return false;
  105. }
  106. $rel = self::whereIn('id',$ids)->field('is_del,id,imgs,img,content')->select()->toArray();
  107. if(!empty($rel)){
  108. $del_data = [];
  109. $up_data = [];
  110. foreach ($rel as $key => $value) {
  111. if($value['is_del'] == 1){
  112. $del_data[] = $value['id'];
  113. $imgs[] = json_decode($value['imgs']);
  114. $content[] = $value['content'];
  115. $src[] = $value['img'];
  116. }else{
  117. $up_data[] = $value['id'];
  118. }
  119. }
  120. if(!empty($del_data)){
  121. $img = [];
  122. array_walk_recursive($imgs, function($value) use (&$img) {
  123. array_push($img,$value);
  124. });
  125. $imgc = [];
  126. array_walk_recursive($content, function($value) use (&$img) {
  127. array_push($img, $value);
  128. });
  129. $imgfile = [];
  130. foreach ($img as $key => $value) {
  131. foreach ($src as $rs) {
  132. if($value != $rs){
  133. $str = PATH_PUBLIC.$value;
  134. if(file_exists($str)){
  135. unlink($str);
  136. }
  137. }
  138. }
  139. }
  140. self::whereIn('id',$del_data)->delete();
  141. }
  142. if(!empty($up_data)){
  143. self::whereIn('id',$up_data)->update(['is_del' => 1]);
  144. }
  145. }
  146. return true;
  147. }
  148. }