Shop.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. * 商品管理
  7. */
  8. namespace app\allwin\controller;
  9. use app\allwin\model\AllwinShop;
  10. use app\allwin\model\AllwinShopCate;
  11. use app\allwin\model\AllwinShopCoupon;
  12. use app\allwin\model\Coupon as CouponModel;
  13. use app\allwin\model\AllwinStore;
  14. use app\allwin\model\Coupon;
  15. class Shop extends Common{
  16. public function initialize() {
  17. parent::initialize();
  18. $this->assign('pathMaps',[['name'=>'优选宝贝','url'=>url("allwin/shop/index")]]);
  19. }
  20. /**
  21. * 列表
  22. */
  23. public function index(){
  24. $condition = [];
  25. $status = $this->request->param('status');
  26. switch ($status) {
  27. case 'trash': //回收站
  28. $condition[] = ['is_del','=',1];
  29. break;
  30. case 'off_sale': //在售
  31. $condition[] = ['is_sale','=',1];
  32. $condition[] = ['is_del','=',0];
  33. break;
  34. case 'on_sale': //下架
  35. $condition[] = ['is_sale','=',0];
  36. $condition[] = ['is_del','=',0];
  37. break;
  38. default:
  39. $condition[] = ['is_del','=',0];
  40. break;
  41. }
  42. $keyword = $this->request->param('keyword');
  43. if(!empty($keyword)){
  44. $condition[] = ["name","like","%{$keyword}%"];
  45. }
  46. $cate_id = $this->request->param('cate_id/d',0);
  47. if($cate_id){
  48. $condition[] = ["category_id","=",$cate_id];
  49. }
  50. $view['lists'] = AllwinShop::where($this->mini_program)->where($condition)->order('id desc')->paginate(20,false,['query' => ['cate_id'=>$cate_id,'status' => $status,'keyword' => $keyword]]);
  51. $view['keyword'] = $keyword;
  52. $view['status'] = $status;
  53. $view['cate_id'] = $cate_id;
  54. $view['page'] = $this->request->param('page/d',0);
  55. return view()->assign($view);
  56. }
  57. /**
  58. * 添加
  59. */
  60. public function add(){
  61. if(request()->isAjax()){
  62. $param = [
  63. 'category_id' => $this->request->param('category_id/d'),
  64. 'store_id' => $this->request->param('store_id/d'),
  65. 'types' => $this->request->param('types/d', 0),
  66. 'name' => $this->request->param('name/s'),
  67. 'title' => $this->request->param('title/s'),
  68. 'sell_price' => $this->request->param('sell_price/f'),
  69. 'market_price' => $this->request->param('market_price/f', 0),
  70. 'cost_price' => $this->request->param('cost_price/f', 0),
  71. 'warehouse_num' => $this->request->param('warehouse_num/d', 0),
  72. 'warehouse_sellnum' => $this->request->param('warehouse_sellnum/d', 0),
  73. 'coupon_id' => $this->request->param('coupon_id/d', 0),
  74. 'vip_price' => $this->request->param('vip_price/f', 0),
  75. 'points' => $this->request->param('points/d', 0),
  76. 'unit' => $this->request->param('unit/s'),
  77. 'imgs' => $this->request->param('imgs/a'),
  78. 'img' => $this->request->param('img/s'),
  79. 'content' => $this->request->param('content/s'),
  80. 'group_title' => $this->request->param('group_title/s'),
  81. 'group_note' => $this->request->param('group_note/s'),
  82. 'group_img' => $this->request->param('group_img/s'),
  83. 'notice' => $this->request->param('notice/s'),
  84. 'detective_price' => $this->request->param('detective_price/s'),
  85. 'detective_uid' => $this->request->param('detective_uid/s'),
  86. 'share_price' => $this->request->param('share_price/s'),
  87. 'keyword' => $this->request->param('keyword/s'),
  88. 'end_time' => $this->request->param('end_time/s'),
  89. ];
  90. $validate = $this->validate($param,'item.save');
  91. if(true !== $validate){
  92. return enjson(0,$validate);
  93. }
  94. if($param['detective_uid'] && empty($param['detective_price'])){
  95. return enjson(0,'经纪人所得必须填写');
  96. }
  97. $param['member_miniapp_id'] = $this->member_miniapp_id;
  98. $result = AllwinShop::edit($param);
  99. if($result){
  100. return enjson(200,'操作成功',['url' => url('allwin/shop/index')]);
  101. }else{
  102. return enjson(0,'操作失败');
  103. }
  104. }else{
  105. $view['cate'] = AllwinShopCate::where($this->mini_program)->select();
  106. return view()->assign($view);
  107. }
  108. }
  109. /**
  110. * 编辑
  111. */
  112. public function edit(){
  113. if(request()->isAjax()){
  114. $param = [
  115. 'id' => $this->request->param('id/d'),
  116. 'store_id' => $this->request->param('store_id/d'),
  117. 'category_id' => $this->request->param('category_id/d'),
  118. 'types' => $this->request->param('types/d', 0),
  119. 'name' => $this->request->param('name/s'),
  120. 'title' => $this->request->param('title/s'),
  121. 'sell_price' => $this->request->param('sell_price/f'),
  122. 'market_price' => $this->request->param('market_price/f', 0),
  123. 'cost_price' => $this->request->param('cost_price/f', 0),
  124. 'warehouse_num' => $this->request->param('warehouse_num/d', 0),
  125. 'warehouse_sellnum' => $this->request->param('warehouse_sellnum/d', 0),
  126. 'coupon_id' => $this->request->param('coupon_id/d', 0),
  127. 'vip_price' => $this->request->param('vip_price/f', 0),
  128. 'points' => $this->request->param('points/d', 0),
  129. 'imgs' => $this->request->param('imgs/a'),
  130. 'img' => $this->request->param('img/s'),
  131. 'content' => $this->request->param('content/s'),
  132. 'notice' => $this->request->param('notice/s'),
  133. 'detective_price' => $this->request->param('detective_price/s'),
  134. 'detective_uid' => $this->request->param('detective_uid/d', 0),
  135. 'share_price' => $this->request->param('share_price/s'),
  136. 'keyword' => $this->request->param('keyword/s'),
  137. 'group_title' => $this->request->param('group_title/s'),
  138. 'group_note' => $this->request->param('group_note/s'),
  139. 'group_img' => $this->request->param('group_img/s'),
  140. 'end_time' => $this->request->param('end_time/s'),
  141. ];
  142. $validate = $this->validate($param,'item.save');
  143. if(true !== $validate){
  144. return enjson(0,$validate);
  145. }
  146. if($param['detective_uid'] && empty($param['detective_price'])){
  147. return enjson(0,'经纪人所得必须填写');
  148. }
  149. $result = AllwinShop::edit($param);
  150. if($result){
  151. return enjson(200,'操作成功',['url' => url('allwin/shop/index')]);
  152. }else{
  153. return enjson(0,'操作失败');
  154. }
  155. }else{
  156. $view['id'] = $this->request->param('id/d');
  157. $view['info'] = AllwinShop::where(['id' => $view['id']])->find();
  158. $view['imgs'] = json_decode($view['info']['imgs'],true);
  159. if(!empty($view['info']['store_id'])){
  160. $state = AllwinStore::where(['member_miniapp_id' => $this->member_miniapp_id,'id' => $view['info']['store_id']])->find();
  161. $view['storename'] = $state->name;
  162. }else{
  163. $view['storename'] = '请选择所属商家';
  164. }
  165. $view['cate'] = AllwinShopCate::where($this->mini_program)->select();
  166. return view()->assign($view);
  167. }
  168. }
  169. /**
  170. * 删除
  171. */
  172. public function delete(){
  173. $id = $this->request->param('id/d',0);
  174. $ids = $this->request->param('ids/s');
  175. $result = AllwinShop::ids_delete($id,$ids);
  176. if($result){
  177. return enjson(200);
  178. }else{
  179. return enjson(403);
  180. }
  181. }
  182. /**
  183. * 上架,下架,从回收站恢复
  184. */
  185. public function ids_action(){
  186. if(request()->isAjax()){
  187. $issale = $this->request->param('issale/d');
  188. $ids = $this->request->param('ids/s');
  189. if(empty($ids)){
  190. return enjson(403,'没有选择任何要操作商品');
  191. }else{
  192. AllwinShop::ids_action($issale,$ids,$this->member_miniapp_id);
  193. return enjson(200);
  194. }
  195. }
  196. }
  197. /**
  198. * 商品栏目
  199. */
  200. public function category(){
  201. if(request()->isAjax()){
  202. $info = AllwinShopCate::where(['member_miniapp_id' => $this->member_miniapp_id,'parent_id' => $this->request->param('parent_id/d',0)])
  203. ->field('id,parent_id,title')
  204. ->order(['sort'=>'desc','id'=>'desc'])
  205. ->select();
  206. return enjson(200,'操作成功',$info);
  207. }else{
  208. $view['input'] = $this->request->param('input');
  209. $view['path'] = $this->request->param('path');
  210. return view('category',$view);
  211. }
  212. }
  213. /**
  214. *返回当前路径
  215. */
  216. public function category_path(){
  217. $info = AllwinShopCate::getPath($this->member_miniapp_id,$this->request->param('parent_id/d',0));
  218. if($info){
  219. $category = [];
  220. foreach ($info as $key => $value) {
  221. $category[] = $value['id'];
  222. }
  223. $category_id = implode(',',$category);
  224. return json(['code'=>200,'msg'=>'操作成功','data'=>$info,'category_id' => $category_id]);
  225. }
  226. return enjson(403,'读取商品分类路径失败');
  227. }
  228. /**
  229. * 计算价格
  230. */
  231. public function calculatePrice(){
  232. $coupon_id = $this->request->param('coupon_id/d');
  233. $sell_price = $this->request->param('sell_price/f');
  234. $coupon = Coupon::where(['id' => $coupon_id])->find();
  235. if($coupon){
  236. if($coupon->types){
  237. $coupon_user_price = $sell_price - $sell_price * $coupon->discount/10;
  238. }else{
  239. $coupon_user_price = $coupon->price;
  240. }
  241. }else{
  242. $coupon_user_price = 0;
  243. }
  244. return enjson(200,'成功',$coupon_user_price);
  245. }
  246. /**
  247. * 列表
  248. */
  249. public function coupon(int $id = 0){
  250. $view['lists'] = CouponModel::where($this->mini_program)->whereIn('id', AllwinShopCoupon::where($this->mini_program)->where(['shop_id' => $id])->column('coupon_id'))->order('is_platform desc,sort desc,is_lock desc,update_time desc')->paginate(10, false);
  251. $view['page'] = $this->request->param('page/d', 0);
  252. $view['shop_id'] = $id;
  253. return view()->assign($view);
  254. }
  255. /**
  256. * 优惠券选项
  257. */
  258. public function selectId(){
  259. $view['shop_id'] = $this->request->param('shop_id/d', 0);
  260. $view['keyword'] = $this->request->param('keyword');
  261. $view['lists'] = CouponModel::where($this->mini_program)->withSearch(['name'],['name' => $view['keyword']])->whereNotIn('id',AllwinShopCoupon::where($this->mini_program)->where(['shop_id' => $view['shop_id']])->column('coupon_id'))->order('is_top desc,sort desc,is_lock desc,update_time desc')->paginate(10,false);
  262. return view()->assign($view);
  263. }
  264. public function setId(){
  265. $shop_id = $this->request->param('shop_id/d');
  266. $coupon_id = $this->request->param('coupon_id/d');
  267. AllwinShopCoupon::create(['shop_id' => $shop_id,'coupon_id' => $coupon_id,'member_miniapp_id' =>$this->member_miniapp_id,'create_time' => time()]);
  268. $this->redirect('coupon', ['id' => $shop_id]);
  269. }
  270. public function deleteShopCoupon(){
  271. $shop_id = $this->request->param('shop_id/d');
  272. $coupon_id = $this->request->param('coupon_id/d');
  273. $result = AllwinShopCoupon::where(['shop_id' => $shop_id,'coupon_id' => $coupon_id])->delete();
  274. if($result){
  275. return enjson(200);
  276. }else{
  277. return enjson(403);
  278. }
  279. }
  280. }