Goods.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\system\controller\v1;
  4. use app\BaseController;
  5. use app\model\api\User;
  6. use app\model\api\GoodsCate;
  7. use app\model\api\Coupon as CouponModel;
  8. use app\model\api\Goods as GoodsModel;
  9. use app\Request;
  10. use library\services\UtilService;
  11. // +----------------------------------------------------------------------
  12. // | [ WE CAN DO IT MORE SIMPLE ]
  13. // +----------------------------------------------------------------------
  14. // | Copyright (c) 2018-2020 rights reserved.
  15. // +----------------------------------------------------------------------
  16. // | [ 商城管理 ]
  17. // +----------------------------------------------------------------------
  18. // | Date: 2020-09-06 21:53
  19. // +----------------------------------------------------------------------
  20. class Goods extends BaseController{
  21. private $goodsDefaultParms = [
  22. ["title"=>"赋意","content"=>""],
  23. ["title"=>"盆","content"=>""],
  24. ["title"=>"配花","content"=>""],
  25. ["title"=>"配草","content"=>""],
  26. ["title"=>"插件","content"=>""],
  27. ["title"=>"IP","content"=>""],
  28. ["title"=>"时间","content"=>""],
  29. ["title"=>"花开","content"=>""],
  30. ];
  31. /**
  32. * 商品列表
  33. * @param Request $request
  34. * @return type
  35. */
  36. public function list(Request $request) {
  37. $pageSize = 50;
  38. $post = UtilService::getMore([
  39. ['page',1],
  40. ['title',''],
  41. ['is_hot',''],
  42. ['is_hot_sell',''],
  43. ['is_ip',''],
  44. ['status',''],
  45. ['cate_id',''],
  46. ['uip',''],
  47. ['time',[]],
  48. ],$request);
  49. $where = [];
  50. if (!empty($post["title"])){
  51. $where[]=["g.title","like","%{$post['title']}%"];
  52. }
  53. if(in_array((string)$post["is_hot"],['1','0'])){
  54. $where[]=["g.is_hot","=",(int)$post["is_hot"]];
  55. }
  56. if(in_array((string)$post["is_hot_sell"],['1','0'])){
  57. $where[]=["g.is_hot_sell","=",(int)$post["is_hot_sell"]];
  58. }
  59. if(in_array((string)$post["is_ip"],['1','0'])){
  60. $where[]=["g.is_ip","=",(int)$post["is_ip"]];
  61. }
  62. if(in_array((string)$post["status"],['1','0'])){
  63. $where[]=["g.status","=",(int)$post["status"]];
  64. }
  65. if (!empty($post["cate_id"])){
  66. $where[]=["g.cate_id","=",$post['cate_id']];
  67. }
  68. if (!empty($post["uip"])){
  69. $where[]=["g.uip","=",$post['uip']];
  70. }
  71. //更新时间
  72. $startTime="";
  73. $endTime="";
  74. if(!empty($post['time'][0]) && !empty($post['time'][1])) {
  75. $startTime = strtotime($post['time'][0]);
  76. $endTime = strtotime($post['time'][1]);
  77. $where[]=["g.update_time","between","{$startTime},{$endTime}"];
  78. }
  79. $data = (new GoodsModel)
  80. ->field("g.*")
  81. ->alias("g")
  82. ->where($where)
  83. ->page((int)$post["page"], (int)$pageSize)
  84. ->order("g.update_time","desc")
  85. ->order("g.id","desc")
  86. ->select()
  87. ->toArray();
  88. $pageCount = (new GoodsModel)->alias("g")->where($where)->count();
  89. $defaultParms=$this->goodsDefaultParms;
  90. $data = empty($data)?[]:$data;
  91. foreach($data as $k=>$v){
  92. $data[$k]["imgs"] = empty($v["imgs"]) ? [] : explode(",",$v["imgs"]);
  93. $data[$k]["descs"] = empty($v["descs"]) ? [] : explode(",",$v["descs"]);
  94. $data[$k]["time"] = date("Y-m-d H:i:s",$v["time"]);
  95. $data[$k]["update_time"] = date("Y-m-d H:i:s",$v["update_time"]);
  96. $data[$k]["cate_id"] = empty($v["cate_id"])?"":$v["cate_id"];
  97. $data[$k]["data"] = empty($v["data"]) ? [] : unserialize($v["data"]);
  98. }
  99. return app('json')->success([
  100. 'list' => $data,
  101. 'pageCount' => $pageCount,
  102. 'pageSize' => $pageSize,
  103. 'page' => $post["page"],
  104. 'defaultParms' => $defaultParms,
  105. ]);
  106. }
  107. /**
  108. * 添加编辑商品
  109. * @param Request $request
  110. * @return type
  111. */
  112. public function add(Request $request){
  113. $post = UtilService::getMore([
  114. ['id','0'],
  115. ['cate_id','','empty','请选择商品分类'],
  116. ['title','','empty','请填写商品标题'],
  117. ['describe',''],
  118. ['price','0','is_numeric','请输入正确的售价'],
  119. ['old_price','0','is_numeric','请输入正确的原价'],
  120. ['imgs',[],'empty','请上传商品主图'],
  121. ['descs',[],'empty','请上传商品详情图'],
  122. ['is_hot','0'],
  123. ['is_hot_sell','0'],
  124. // ['uip',''],
  125. ['is_ip','0'],
  126. ['ip_title',''],
  127. ['ip_price','0'],
  128. ['ip_income_per','0','is_numeric','请输入IP收益百分比'],
  129. ['status','0'],
  130. ['give_score','0','is_numeric','请输入正确的赠送积分'],
  131. ['data',[]],
  132. ],$request);
  133. if($post["is_ip"]==1 && (!is_numeric(trim($post["ip_price"])) || trim($post["ip_price"])<=0)){
  134. return app('json')->fail("'请输入买断售价");
  135. }
  136. if($post["is_ip"]==1){
  137. $post["ip_price"] = floatval(trim($post["ip_price"]));
  138. }else{
  139. $post["ip_price"] = 0;
  140. }
  141. $post["ip_income_per"] = empty($post["ip_income_per"])?0:(int)$post["ip_income_per"];
  142. $post["price"] = floatval($post["price"]);
  143. $post["old_price"] = floatval($post["old_price"]);
  144. $post["imgs"] = empty($post["imgs"]) ? "" : implode(",", $post["imgs"]);
  145. $post["descs"] = empty($post["descs"]) ? "" : implode(",", $post["descs"]);
  146. $post["is_hot"] = (int)$post["is_hot"]==1?1:0;
  147. $post["is_hot_sell"] = (int)$post["is_hot_sell"]==1?1:0;
  148. $post["is_ip"] = (int)$post["is_ip"]==1?1:0;
  149. $post["ip_price"] = floatval($post["ip_price"]);
  150. $post["status"] = (int)$post["status"]==1?1:0;
  151. $post["give_score"] = (int)$post["give_score"];
  152. $post["update_time"] = time();
  153. if($post["is_ip"]==1 && $post["ip_price"]<=0){
  154. return app('json')->fail("请输入正确的买断售价");
  155. }
  156. if($post["is_ip"]==1 && ($post["ip_income_per"]<0 || $post["ip_income_per"]>100)){
  157. return app('json')->fail("请输入正确的IP分成比例");
  158. }
  159. if(!empty($post["uip"])){
  160. $udata = (new User)->where("uip",$post["uip"])->find();
  161. if(empty($udata)){
  162. return app('json')->fail("用户不存在");
  163. }
  164. $post["uid"] = $udata["uid"];
  165. $post["uip"] = $udata["uip"];
  166. }
  167. $parmsAr = empty($post["data"]) ? [] : $post["data"];
  168. $post["data"]=[];
  169. foreach($parmsAr as $k=>$v){
  170. if(!empty($v["title"]) && !empty($v["content"])){
  171. $post["data"][]=[
  172. "title" => trim($v["title"]),
  173. "content" => trim($v["content"]),
  174. ];
  175. }
  176. }
  177. $post["data"] = serialize($post["data"]);
  178. $id = $post["id"];
  179. unset($post["id"]);
  180. $r=0;
  181. if(empty($id) || $id=="0"){
  182. $post["time"] = time();
  183. $r = (new GoodsModel)->insert($post);
  184. }else{
  185. $r = (new GoodsModel)->where("id",$id)->update($post);
  186. }
  187. if($r){
  188. return app('json')->success("数据保存成功");
  189. }else{
  190. return app('json')->fail("数据保存失败");
  191. }
  192. }
  193. /**
  194. * 删除商品
  195. * @param Request $request
  196. */
  197. public function del(Request $request) {
  198. }
  199. /**
  200. * 优惠券列表
  201. * @param Request $request
  202. * @return type
  203. */
  204. public function coupon(Request $request) {
  205. $pageSize = 50;
  206. $post = UtilService::getMore([
  207. ['page',1],
  208. ['status',''],
  209. ],$request);
  210. $where=[];
  211. if((string)$post["status"]!=""){
  212. $where[]=["status","=",(int)$post["status"] == 1 ? 1 : 0];
  213. }
  214. $data = (new CouponModel)
  215. ->where($where)
  216. ->page((int)$post["page"], (int)$pageSize)
  217. ->order("id","desc")
  218. ->order("seq","desc")
  219. ->select()
  220. ->toArray();
  221. $pageCount = (new CouponModel)->where($where)->count();
  222. $data = empty($data)?[]:$data;
  223. foreach($data as $k=>$v){
  224. $data[$k]["time"] = date("Y-m-d H:i:s",$v["time"]);
  225. }
  226. return app('json')->success([
  227. 'list' => $data,
  228. 'pageCount' => $pageCount,
  229. 'pageSize' => $pageSize,
  230. 'page' => $post["page"]
  231. ]);
  232. }
  233. /**
  234. * 添加编辑优惠券
  235. * @param Request $request
  236. */
  237. public function couponAdd(Request $request){
  238. $post = UtilService::getMore([
  239. ['id','0'],
  240. ['title','','empty','请填写优惠券标题'],
  241. ['price','','is_numeric','请填写正确的优惠券面值'],
  242. ['limit_price','','is_numeric','请填写正确的满减标准'],
  243. ['day','','is_numeric','请填写正确的有效天数'],
  244. ['seq','0'],
  245. ['status','0']
  246. ],$request);
  247. $id = (int)$post["id"];
  248. unset($post["id"]);
  249. $post["price"] = floatval($post["price"]);
  250. $post["limit_price"] = floatval($post["limit_price"]);
  251. $post["day"] = intval($post["day"]);
  252. $post["seq"] = intval($post["seq"]);
  253. $post["status"] = intval($post["status"])==1?1:0;
  254. if($post["price"]<=0){
  255. return app('json')->fail("请输入正确的优惠券面值");
  256. }
  257. $r=0;
  258. if(empty($id)){
  259. $post["time"] = time();
  260. $r = (new CouponModel)->insert($post);
  261. }else{
  262. $r = (new CouponModel)->where("id",$id)->update($post);
  263. }
  264. if($r){
  265. return app('json')->success("数据保存成功");
  266. }else{
  267. return app('json')->fail("数据保存失败");
  268. }
  269. }
  270. /**
  271. * 删除优惠券
  272. * @param Request $request
  273. */
  274. public function couponDel(Request $request) {
  275. [$id] = UtilService::getMore([
  276. ['id',0,'empty','参数错误']
  277. ],$request,true);
  278. $bool = (new CouponModel)->where("id",$id)->delete();
  279. return app('json')->success("删除成功");
  280. }
  281. /**
  282. * 商品分类列表
  283. * @param Request $request
  284. * @return type
  285. */
  286. public function cate(Request $request) {
  287. $pageSize = 50;
  288. $post = UtilService::getMore([
  289. ['page',1],
  290. ['title',''],
  291. ],$request);
  292. $data = (new GoodsCate)
  293. ->when(1 == 1, function ($query) use ($request, $post) {
  294. if (!empty($post["title"])){
  295. $query->where("title",$post["title"]);
  296. }
  297. })
  298. ->page((int)$post["page"], (int)$pageSize)
  299. ->order("sort","desc")
  300. ->order("id","desc")
  301. ->select()
  302. ->toArray();
  303. $pageCount = (new GoodsCate)
  304. ->when(1 == 1, function ($query) use ($request, $post) {
  305. if (!empty($post["title"])){
  306. $query->where("title",$post["title"]);
  307. }
  308. })
  309. ->count();
  310. $data = empty($data)?[]:$data;
  311. return app('json')->success([
  312. 'list' => $data,
  313. 'pageCount' => $pageCount,
  314. 'pageSize' => $pageSize,
  315. 'page' => $post["page"]
  316. ]);
  317. }
  318. /**
  319. * 添加分类
  320. */
  321. public function cateAdd(Request $request){
  322. $post = UtilService::getMore([
  323. ['id','0'],
  324. ['title','','empty','请填写分类名称'],
  325. ['sort','0'],
  326. ['code',''],
  327. ['img','','empty','请上传分类图标'],
  328. ['is_show','0']
  329. ],$request);
  330. $id = $post["id"];
  331. unset($post["id"]);
  332. $r=0;
  333. if(empty($id) || $id=="0"){
  334. $r = (new GoodsCate)->insert($post);
  335. }else{
  336. $r = (new GoodsCate)->where("id",$id)->update($post);
  337. }
  338. if($r){
  339. return app('json')->success("数据保存成功");
  340. }else{
  341. return app('json')->fail("数据保存失败");
  342. }
  343. }
  344. /**
  345. * 分类删除
  346. * @param Request $request
  347. */
  348. public function cateDel(Request $request) {
  349. [$id] = UtilService::getMore([
  350. ['id',0,'empty','参数错误']
  351. ],$request,true);
  352. $bool = (new GoodsCate)->where("id",$id)->delete();
  353. return app('json')->success("分类删除成功");
  354. }
  355. }