Store.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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\smartbc\controller;
  9. use app\smartbc\model\SmartbcCoupon;
  10. use app\smartbc\model\SmartbcQueen;
  11. use app\smartbc\model\SmartbcStoreCate;
  12. use app\smartbc\model\SmartbcStore;
  13. use app\smartbc\model\SmartbcCouponUser;
  14. use app\smartbc\model\SmartbcOrder;
  15. use app\smartbc\model\SmartbcConfig;
  16. use app\common\facade\AliApi;
  17. use app\common\model\SystemUser;
  18. use app\smartbc\model\SmartbcStoreUnion;
  19. use filter\Filter;
  20. use think\helper\Time;
  21. use think\facade\Request;
  22. use think\facade\Validate;
  23. class Store extends Common{
  24. public function initialize(){
  25. parent::initialize();
  26. $this->assign('pathMaps',[['name'=>'商家管理','url'=>url("smartbc/store/index")]]);
  27. }
  28. /**
  29. * 列表
  30. */
  31. public function index(int $types = 0){
  32. $condition = [];
  33. $time = Request::param('time/d',0);
  34. $starttime = Request::param('starttime/s');
  35. $endtime = Request::param('endtime/s');
  36. if($time){
  37. switch ($time) {
  38. case 2:
  39. list($start, $end) = Time::yesterday();
  40. break;
  41. case 30:
  42. list($start, $end) = Time::month();
  43. break;
  44. case 60:
  45. list($start, $end) = Time::lastMonth();
  46. break;
  47. default:
  48. list($start, $end) = Time::today();
  49. break;
  50. }
  51. $condition[] = ['create_time','>=',$start];
  52. $condition[] = ['create_time','<=',$end];
  53. }else{
  54. if($starttime){
  55. $condition[] = ['create_time','>=',strtotime($starttime)];
  56. }
  57. if($endtime){
  58. $condition[] = ['create_time','<=',strtotime($endtime)];
  59. }
  60. }
  61. $keyword = Request::param('keyword/s');
  62. if(!empty($keyword)){
  63. $condition[] = ['name','like','%'.$keyword.'%'];
  64. }
  65. $view['is_top_num'] = SmartbcStore::where($this->mini_program)->where($condition)->where(['is_top' => 1])->count('is_top');
  66. $view['store_num'] = SmartbcStore::where($this->mini_program)->where($condition)->count();
  67. $view['lock_num'] = SmartbcStore::where($this->mini_program)->where($condition)->where(['is_lock' => 1])->count();
  68. if($types){
  69. $condition[] = ['is_lock','=',$types == 2 ? 1 : 0];
  70. }
  71. $view['lists'] = SmartbcStore::where($this->mini_program)->where($condition)->order('is_top desc,sort desc,id desc')->paginate(20,false,['query' => ['types' => $types,'starttime' => $starttime,'endtime' => $endtime,'time'=>$time]]);
  72. $view['keyword'] = $keyword;
  73. $view['types'] = $types;
  74. $view['time'] = $time;
  75. $view['starttime'] = $starttime;
  76. $view['endtime'] = $endtime;
  77. return view()->assign($view);
  78. }
  79. /**
  80. * 选择商家列表
  81. */
  82. public function selectStore(){
  83. $condition = [];
  84. $condition[] = ['is_lock','=',0];
  85. $view['keyword'] = $this->request->param('keyword/s');
  86. if(!empty($view['keyword'])){
  87. $condition[] = ['name','like','%'.$view['keyword'].'%'];
  88. }
  89. $view['lists'] = SmartbcStore::where($this->mini_program)->where($condition)->order('is_top desc,sort desc,id desc')->paginate(20);
  90. $view['input'] = $this->request->param('input');
  91. return view()->assign($view);
  92. }
  93. /**
  94. * 好店预览
  95. */
  96. public function review(int $id){
  97. $condition['id'] = $id;
  98. $view['info'] = SmartbcStore::where($condition)->find();
  99. if(!$view['info']){
  100. $this->error('内容不存在');
  101. }
  102. $view['manage'] = SystemUser::where($this->mini_program)->where(['id' => $view['info']['manage_uid']])->find();
  103. $view['orders'] = SmartbcOrder::where($this->mini_program)->where(['store_id'=>$id,'state' => 1])->order('id desc')->paginate(10,false,['query' => ['id' => $id]]);
  104. return view()->assign($view);
  105. }
  106. /**
  107. * 添加
  108. */
  109. public function add(){
  110. if(request()->isAjax()){
  111. $data = [
  112. 'member_miniapp_id' => $this->member_miniapp_id,
  113. 'manage_uid' => Request::param('manage_uid/d', 0),
  114. 'cate_id' => Request::param('cate_id/d', 0),
  115. 'cate_sid' => Request::param('cate_sid/d', 0),
  116. 'name' => Request::param('name/s'),
  117. 'address' => Request::param('address/s'),
  118. 'longitude' => Request::param('longitude/s'),
  119. 'latitude' => Request::param('latitude/s'),
  120. 'telphone' => Request::param('telphone/s'),
  121. 'img' => Request::param('img/s'),
  122. 'imgs' => Request::param('imgs/a'),
  123. 'mch_id' => Request::param('mch_id/s'),
  124. ];
  125. $validate = $this->validate($data,'Store.edit');
  126. if(true !== $validate){
  127. return json(['code'=>0,'msg'=>$validate]);
  128. }
  129. $store = SmartbcStore::where(['manage_uid' => $data['manage_uid']])->find();
  130. if($store){
  131. return json(['code'=>0,'msg'=>'禁止绑定,设置的店长已属于['.$store->name.']的店长']);
  132. }
  133. $cate = SmartbcStoreCate::field('name')->where(['id' =>$data['cate_sid']])->find();
  134. $data['cate_name'] = empty($cate['name']) ? '' : $cate['name'];
  135. $store_id = SmartbcStore::edit($data);
  136. if($store_id){
  137. return json(['code'=>200,'url'=>url('smartbc/store/index',['cate_id' => $data['cate_id']]),'msg'=>'操作成功']);
  138. }else{
  139. return json(['code'=>0,'msg'=>'操作失败']);
  140. }
  141. }else{
  142. $view['cate'] = SmartbcStoreCate::field('name,id')->where($this->mini_program)->where(['parent_id' => 0])->order('sort desc,id desc')->select();
  143. $view['setting'] = SmartbcConfig::getConfig($this->member_miniapp_id);
  144. return view()->assign($view);
  145. }
  146. }
  147. //编辑
  148. public function edit(){
  149. if(request()->isAjax()){
  150. $data = [
  151. 'member_miniapp_id' => $this->member_miniapp_id,
  152. 'id' => Request::param('id/d'),
  153. 'manage_uid' => Request::param('manage_uid/d',0),
  154. 'cate_id' => Request::param('cate_id/d',0),
  155. 'cate_sid' => Request::param('cate_sid/d',0),
  156. 'name' => Request::param('name/s'),
  157. 'address' => Request::param('address/s'),
  158. 'longitude' => Request::param('longitude/s'),
  159. 'latitude' => Request::param('latitude/s'),
  160. 'telphone' => Request::param('telphone/s'),
  161. 'img' => Request::param('img/s'),
  162. 'imgs' => Request::param('imgs/a'),
  163. 'mch_id' => Request::param('mch_id/s'),
  164. ];
  165. $validate = $this->validate($data,'Store.edit');
  166. if(true !== $validate){
  167. return json(['code'=>0,'msg'=>$validate]);
  168. }
  169. $store = SmartbcStore::where([['manage_uid','=',$data['manage_uid']],['id','<>',$data['id']]])->find();
  170. if($store){
  171. return json(['code'=>0,'msg'=>'禁止绑定,设置的店长已属于['.$store->name.']的店长']);
  172. }
  173. $cate = SmartbcStoreCate::field('name')->where(['id' =>$data['cate_sid']])->find();
  174. $data['cate_name'] = empty($cate['name']) ? '' : $cate['name'];
  175. $store_id = SmartbcStore::edit($data);
  176. return json(['code'=>200,'url'=>url('store/index',['cate_id' => $data['cate_id']]),'msg'=>'操作成功']);
  177. }else{
  178. $info = SmartbcStore::where($this->mini_program)->where(['id' => $this->request->param('id/d')])->find();
  179. if(!$info){
  180. $this->error("404 NOT FOUND");
  181. }
  182. $view['info'] = $info;
  183. $view['imgs'] = json_decode($info['imgs'],true);
  184. $view['cate'] = SmartbcStoreCate::field('name,id')->where($this->mini_program)->where(['parent_id' => 0])->order('sort desc,id desc')->select();
  185. $view['cate_sub'] = SmartbcStoreCate::field('name,id')->where(['parent_id' => $info['cate_id'],'member_miniapp_id' => $this->member_miniapp_id])->order('sort desc,id desc')->select();
  186. $view['setting'] = SmartbcConfig::getConfig($this->member_miniapp_id);
  187. return view()->assign($view);
  188. }
  189. }
  190. /**
  191. * 排序
  192. */
  193. public function sort(){
  194. if(request()->isAjax()){
  195. $data = [
  196. 'sort' => Request::param('sort/d'),
  197. 'id' => Request::param('id/d'),
  198. ];
  199. $validate = $this->validate($data,'Store.sort');
  200. if(true !== $validate){
  201. return json(['code'=>0,'msg'=>$validate]);
  202. }
  203. $result = SmartbcStore::where(['id' => $data['id']])->update(['sort' => $data['sort']]);
  204. if($result){
  205. return json(['code'=>200,'msg'=>'操作成功']);
  206. }else{
  207. return json(['code'=>0,'msg'=>'操作失败']);
  208. }
  209. }
  210. }
  211. /**
  212. * 置顶/取消
  213. * @param integer $id 用户ID
  214. */
  215. public function isTop(int $id){
  216. $result = SmartbcStore::isTop($id);
  217. if(!$result){
  218. return json(['code'=>0,'message'=>'操作失败']);
  219. }else{
  220. return json(['code'=>200,'message'=>'操作成功']);
  221. }
  222. }
  223. /**
  224. * 置顶/取消
  225. * @param integer $id 用户ID
  226. */
  227. public function isLock(int $id){
  228. $result = SmartbcStore::isLock($id,$this->member_miniapp_id);
  229. if(!$result){
  230. return json(['code'=>0,'message'=>'操作失败']);
  231. }else{
  232. return json(['code'=>200,'message'=>'操作成功']);
  233. }
  234. }
  235. //删除
  236. public function delete(int $id){
  237. $coupon = SmartbcCoupon::where($this->mini_program)->where(['store_id' => $id,'is_end' => 0])->count();
  238. if($coupon){
  239. return json(['code'=>403,'msg'=>'请先删除好店优惠券']);
  240. }
  241. $couponUser = SmartbcCouponUser::where($this->mini_program)->where(['store_id' => $id,'is_end' => 0])->count();
  242. if($couponUser){
  243. return json(['code'=>403,'msg'=>'请先删除用户已领优惠券']);
  244. }
  245. SmartbcStore::where($this->mini_program)->where(['id' => $id])->delete(); //删除好店
  246. SmartbcStoreUnion::where($this->mini_program)->where(['store_id' => $id])->delete(); //删除好店群组关联
  247. return json(['code'=>200,'msg'=>'操作成功']);
  248. }
  249. /**
  250. * 选择所属用户
  251. */
  252. public function selectStoreManage(){
  253. $keyword = Request::param('keyword');
  254. $input = Request::param('input');
  255. $condition[] = ['is_lock','=',0];
  256. if(!empty($keyword)){
  257. if(Validate::isMobile($keyword)){
  258. $condition[] = ['phone_uid','=',$keyword];
  259. }else{
  260. $condition[] = ['nickname','like','%'.$keyword.'%'];
  261. }
  262. }
  263. $manage_uid = SmartbcStore::where($this->mini_program)->column('manage_uid');
  264. $view['list'] = SystemUser::where($this->mini_program)->where($condition)->whereNotIn('id',$manage_uid)->order('id desc')->paginate(10,false,['query' => ['input' => $input,'keyword' => $keyword]]);
  265. $view['keyword'] = $keyword;
  266. $view['input'] = $input;
  267. $view['id'] = $this->member_miniapp_id;
  268. return view()->assign($view);
  269. }
  270. /**
  271. * 读取好店分类
  272. * @return void
  273. */
  274. public function storecate(int $cate_id){
  275. $cate = SmartbcStoreCate::where(['member_miniapp_id' => $this->member_miniapp_id,'parent_id' => $cate_id])->field('id,name')->order('sort desc,id desc')->select();
  276. return json(['code'=>200,'msg'=>'成功','data' => $cate]);
  277. }
  278. /**
  279. * 根据地址位置转账
  280. * @return void
  281. */
  282. public function baidu($address){
  283. $view['address'] = $address;
  284. return view()->assign($view);
  285. }
  286. /**
  287. * 根据地址位置读取地址
  288. * @return void
  289. */
  290. public function maps($address){
  291. $rel = AliApi::address($address);
  292. if(!$rel){
  293. return enjson(0,'请确认是否帐号欠费');
  294. }
  295. $address = $rel['geocodes'][0];
  296. $location = explode(',',$address['location']);
  297. return enjson(200,'成功',$location);
  298. }
  299. /**
  300. * 微信分账队列
  301. * @return void
  302. */
  303. public function queen(int $types = 0,string $order = ''){
  304. $condition = [];
  305. if(!empty($order)){
  306. $condition['out_order_no'] = Filter::filter_escape(trim($order));
  307. }
  308. $store_id = Request::param('store_id',0);
  309. $store_name = Request::param('store_name');
  310. if($store_id){
  311. $condition['store_id'] = $store_id;
  312. }
  313. $view['amount_quree'] = SmartbcQueen::where($this->mini_program)->where($condition)->where(['is_finish' => 0])->sum('amount');
  314. $view['amount_success'] = SmartbcQueen::where($this->mini_program)->where($condition)->where(['is_finish' => 2])->sum('amount');
  315. $view['amount_fail'] = SmartbcQueen::where($this->mini_program)->where($condition)->where(['is_finish' => 1])->sum('amount');
  316. if($types){
  317. switch ($types) {
  318. case 2:
  319. $is_finish = 2;
  320. break;
  321. case 3:
  322. $is_finish = 1;
  323. break;
  324. default:
  325. $is_finish = 0;
  326. break;
  327. }
  328. $condition['is_finish'] = $is_finish;
  329. }
  330. $orderby = $types == 1 ? 'id asc':'id desc';
  331. $view['lists'] = SmartbcQueen::where($this->mini_program)->where($condition)->order($orderby)->paginate(10,false,[
  332. 'query' => ['types' => $types,'store_id' => $store_id,'store_name' => $store_name],
  333. ]);
  334. $view['types'] = $types;
  335. $view['order'] = $order;
  336. $view['store_id'] = $store_id;
  337. $view['store_name'] = $store_name;
  338. $view['pathMaps'] = [['name'=>'结算队列','url'=>url("store/queen")]];
  339. return view()->assign($view);
  340. }
  341. /**
  342. * 重置分账队列
  343. * @return void
  344. */
  345. public function resetQueen(string $order){
  346. $condition = [];
  347. $condition['out_order_no'] = Filter::filter_escape(trim($order));
  348. $condition['is_finish'] = 1;
  349. $rueen = SmartbcQueen::where($this->mini_program)->where($condition)->update(['is_finish' => 0]);
  350. if($rueen){
  351. return json(['code'=>200,'message'=>'成功']);
  352. }else{
  353. return json(['code'=>0,'message'=>'失败,只有分账失败的才允许重置队列']);
  354. }
  355. }
  356. }