Index.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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\ais\controller\store;
  9. use app\ais\model\AisStoreWorker;
  10. use app\common\facade\AliApi;
  11. use app\common\model\SystemUser;
  12. use app\ais\controller\Common;
  13. use app\ais\model\AisCoupon;
  14. use app\ais\model\AisStoreCate;
  15. use app\ais\model\AisStore;
  16. use app\ais\model\AisCouponUser;
  17. use app\ais\model\AisOrder;
  18. use app\ais\model\AisConfig;
  19. use app\common\facade\Inform;
  20. use think\helper\Time;
  21. use think\facade\Request;
  22. use think\facade\Validate;
  23. class Index extends Common{
  24. public function initialize(){
  25. parent::initialize();
  26. $this->assign('pathMaps',[['name'=>'商家管理','url'=>url("ais/store.index/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. if($types){
  66. $condition[] = ['is_lock','=',$types == 2 ? 1 : 0];
  67. }
  68. $view['lists'] = AisStore::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]]);
  69. $view['keyword'] = $keyword;
  70. $view['types'] = $types;
  71. $view['time'] = $time;
  72. $view['starttime'] = $starttime;
  73. $view['endtime'] = $endtime;
  74. return view()->assign($view);
  75. }
  76. /**
  77. * 好店预览
  78. */
  79. public function review(int $id){
  80. $condition['id'] = $id;
  81. $view['info'] = AisStore::where($condition)->find();
  82. if(!$view['info']){
  83. $this->error('内容不存在');
  84. }
  85. $view['user'] = AisStoreWorker::where($this->mini_program)->where(['store_id' => $id])->order('id desc')->select();
  86. $view['manage'] = SystemUser::where($this->mini_program)->where(['id' => $view['info']['manage_uid']])->find();
  87. $view['orders'] = AisOrder::where($this->mini_program)->where(['store_id'=>$id,'state' => 1])->order('id desc')->paginate(10,false,['query' => ['id' => $id]]);
  88. return view()->assign($view);
  89. }
  90. //编辑
  91. public function edit(){
  92. if(request()->isAjax()){
  93. $param = [
  94. 'member_miniapp_id' => $this->member_miniapp_id,
  95. 'id' => Request::param('id/d'),
  96. 'manage_uid' => Request::param('manage_uid/d',0),
  97. 'cate_id' => Request::param('cate_id/d',0),
  98. 'cate_sid' => Request::param('cate_sid/d',0),
  99. 'province' => Request::param('province/d'),
  100. 'city' => Request::param('city/d'),
  101. 'district' => Request::param('district/d'),
  102. 'citycode' => Request::param('citycode/d'),
  103. 'name' => Request::param('name/s'),
  104. 'address' => Request::param('address/s'),
  105. 'longitude' => Request::param('longitude/s'),
  106. 'latitude' => Request::param('latitude/s'),
  107. 'telphone' => Request::param('telphone/s'),
  108. 'img' => Request::param('img/s'),
  109. 'imgs' => Request::param('imgs/a'),
  110. 'mch_id' => Request::param('mch_id/s'),
  111. 'tags' => Request::param('tags/s'),
  112. 'charge' => Request::param('charge/f'),
  113. ];
  114. $validate = $this->validate($param,'Store.edit');
  115. if(true !== $validate){
  116. return json(['code'=>0,'msg'=>$validate]);
  117. }
  118. $condition[] = ['manage_uid','=',$param['manage_uid']];
  119. if($param['id']){
  120. $condition[] = ['id','<>',$param['id']];
  121. }
  122. $store = AisStore::where($condition)->find();
  123. if($store){
  124. return json(['code'=>0,'msg'=>'禁止绑定,设置的店长已属于['.$store->name.']的店长']);
  125. }
  126. $param['citypath'] = [
  127. 'province' => $param['province'],
  128. 'city' => $param['city'],
  129. 'district' => $param['district'],
  130. ];
  131. $store_id = AisStore::edit($param);
  132. return json(['code'=>200,'url'=>url('store/index',['cate_id' => $param['cate_id']]),'msg'=>'操作成功']);
  133. }else{
  134. $view['info'] = AisStore::where($this->mini_program)->where(['id' => $this->request->param('id/d')])->find();
  135. $view['cate'] = AisStoreCate::where($this->mini_program)->where(['parent_id' => 0])->field('name,id')->order('sort desc,id desc')->select();
  136. $view['setting'] = AisConfig::getConfig($this->member_miniapp_id);
  137. $view['cate_sub'] = [];
  138. if($view['info']){
  139. $view['cate_sub'] = AisStoreCate::where($this->mini_program)->where(['parent_id' => $view['info']->cate_id])->field('name,id')->order('sort desc,id desc')->select();
  140. }
  141. return view()->assign($view);
  142. }
  143. }
  144. /**
  145. * 排序
  146. */
  147. public function sort(){
  148. if(request()->isAjax()){
  149. $data = [
  150. 'sort' => $this->request->param('sort/d'),
  151. 'id' => $this->request->param('id/d'),
  152. ];
  153. $validate = $this->validate($data,'Category.sort');
  154. if(true !== $validate){
  155. return json(['code'=>0,'msg'=>$validate]);
  156. }
  157. $result = AisStore::where(['id' => $data['id']])->update(['sort' => $data['sort']]);
  158. if($result){
  159. return json(['code'=>200,'msg'=>'操作成功']);
  160. }else{
  161. return json(['code'=>0,'msg'=>'操作失败']);
  162. }
  163. }
  164. }
  165. /**
  166. * 置顶/取消
  167. * @param integer $id 用户ID
  168. */
  169. public function isTop(int $id){
  170. $result = AisStore::isTop($id);
  171. if(!$result){
  172. return json(['code'=>0,'message'=>'操作失败']);
  173. }else{
  174. return json(['code'=>200,'message'=>'操作成功']);
  175. }
  176. }
  177. /**
  178. * 置顶/取消
  179. * @param integer $id 用户ID
  180. */
  181. public function isLock(int $id){
  182. $result = AisStore::isLock($id,$this->member_miniapp_id);
  183. if(!$result){
  184. return json(['code'=>0,'message'=>'操作失败']);
  185. }else{
  186. return json(['code'=>200,'message'=>'操作成功']);
  187. }
  188. }
  189. //删除
  190. public function delete(int $id){
  191. $coupon = AisCoupon::where($this->mini_program)->where(['store_id' => $id])->count();
  192. if($coupon){
  193. return json(['code'=>403,'msg'=>'请先删除好店优惠券']);
  194. }
  195. $couponUser = AisCouponUser::where($this->mini_program)->where(['store_id' => $id])->count();
  196. if($couponUser){
  197. return json(['code'=>403,'msg'=>'请先删除用户已领优惠券']);
  198. }
  199. AisStore::where($this->mini_program)->where(['id' => $id])->delete(); //删除好店
  200. return json(['code'=>200,'msg'=>'操作成功']);
  201. }
  202. /**
  203. * 读取好店分类
  204. * @return void
  205. */
  206. public function storecate(int $cate_id){
  207. $cate = AisStoreCate::where(['member_miniapp_id' => $this->member_miniapp_id,'parent_id' => $cate_id])->field('id,name')->order('sort desc,id desc')->select();
  208. return json(['code'=>200,'msg'=>'成功','data' => $cate]);
  209. }
  210. /**
  211. * 根据地址位置转账
  212. * @return void
  213. */
  214. public function baidu($address){
  215. $view['address'] = $address;
  216. $this->view->engine->layout(false);
  217. return view()->assign($view);
  218. }
  219. /**
  220. * 根据地址位置读取地址
  221. * @return void
  222. */
  223. public function maps($address){
  224. $rel = AliApi::address($address);
  225. if(!$rel){
  226. return enjson(0,'请确认是否帐号欠费');
  227. }
  228. $address = $rel['geocodes'][0];
  229. $location = explode(',',$address['location']);
  230. return enjson(200,'成功',$location);
  231. }
  232. /**
  233. * 选择商家列表
  234. */
  235. public function selectStore($select_uid = 0){
  236. $condition = [];
  237. $condition[] = ['is_lock','=',0];
  238. $view['keyword'] = $this->request->param('keyword/s');
  239. if(!empty($view['keyword'])){
  240. $condition[] = ['name','like','%'.$view['keyword'].'%'];
  241. }
  242. $view['lists'] = AisStore::where($this->mini_program)->where($condition)->order('is_top desc,sort desc,id desc')->paginate(20);
  243. $view['input'] = $this->request->param('input');
  244. $view['select_uid'] = $select_uid;
  245. return view()->assign($view);
  246. }
  247. /**
  248. * 选择所属用户
  249. */
  250. public function selectUid(){
  251. $view['keyword'] = Request::param('keyword');
  252. $view['input'] = Request::param('input');
  253. $condition = [];
  254. if(!empty($view['keyword'])){
  255. if(Validate::isMobile($view['keyword'])){
  256. $condition[] = ['phone_uid','=',$view['keyword']];
  257. }else{
  258. $condition[] = ['nickname','like','%'.$view['keyword'].'%'];
  259. }
  260. }
  261. $view['list'] = SystemUser::where($this->mini_program)->where($condition)->whereNotIn('id',AisStore::where($this->mini_program)
  262. ->column('manage_uid'))->order('id desc')
  263. ->paginate(10,false,['query' => ['input' => $view['input'],'keyword' => $view['keyword']]]);
  264. return view()->assign($view);
  265. }
  266. /**
  267. * 给好店添加管理员
  268. */
  269. public function winUser(int $store_id){
  270. if(request()->isAjax()){
  271. $ids = Request::param('ids/s');
  272. if(empty($ids)){
  273. return json(['code'=>0,'msg'=>'请选择要关联的员工']);
  274. }
  275. $uid = (array)ids($ids,true);
  276. $worker_uid = AisStoreWorker::where($this->mini_program)->where(['uid' => $uid])->column('uid'); //查询添加的用户是否在其它点做员工
  277. $uid = array_diff($uid,$worker_uid);//如果存在就去重
  278. $data = [];
  279. foreach ($uid as $key => $value) {
  280. $data[$key]['member_miniapp_id'] = $this->member_miniapp_id;
  281. $data[$key]['uid'] = $value;
  282. $data[$key]['store_id'] = $store_id;
  283. }
  284. $result = AisStoreWorker::insertAll($data);
  285. if($result){
  286. //批量添加的员工为当前店铺的会员
  287. foreach ($data as $key => $value) {
  288. //通知到申请者微信
  289. Inform::sms($value['uid'],$this->member_miniapp_id,['title' =>'业务进展通知','type' => '身份变更','state' => '成功','content' =>'您的身份已变更为员工']);
  290. }
  291. return json(['code'=>200,'msg'=>'添加员工成功','data' =>[]]);
  292. }else{
  293. return json(['code'=>0,'msg'=>'添加员工失败']);
  294. }
  295. }else{
  296. //读好好店员工和好店老板的UID,做筛查排除
  297. $uid = AisStoreWorker::where($this->mini_program)->column('uid'); //好店员工
  298. $keyword = Request::param('keyword');
  299. $condition = [];
  300. $condition[] = ['id','NOT IN',$uid];
  301. if(!empty($keyword)){
  302. if(Validate::isMobile($keyword)){
  303. $condition[] = ['phone_uid','=',$keyword];
  304. }else{
  305. $condition[] = ['nickname','LIKE','%'.$keyword.'%'];
  306. }
  307. }
  308. $view['list'] = SystemUser::where($this->mini_program)->where($condition)->order('id desc')->paginate(20,false,['query' => ['store_id' => $store_id]]);
  309. $view['store_id'] = $store_id;
  310. $view['keyword'] = $keyword;
  311. return view()->assign($view);
  312. }
  313. }
  314. //删除员工
  315. public function delUser(int $id){
  316. $result = AisStoreWorker::where($this->mini_program)->where(['uid' => $id])->delete();
  317. if($result){
  318. return json(['code'=>200,'msg'=>'操作成功']);
  319. }else{
  320. return json(['code'=>403,'msg'=>'删除失败']);
  321. }
  322. }
  323. /**
  324. * 设置好店收银员
  325. * @param integer $id 用户ID
  326. */
  327. public function isCashier(int $id){
  328. $result = AisStoreWorker::setWorker($id);
  329. if(!$result){
  330. return json(['code'=>0,'message'=>'操作失败']);
  331. }else{
  332. return json(['code'=>200,'message'=>'操作成功']);
  333. }
  334. }
  335. }