User.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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\widget\Inform;
  10. use think\facade\Request;
  11. use think\facade\Validate;
  12. use app\common\model\SystemUser;
  13. use app\allwin\model\AllwinUserAgent;
  14. use app\allwin\model\Worker;
  15. use app\allwin\model\StoreWorker;
  16. use app\allwin\model\AllwinStore;
  17. use app\allwin\model\AllwinUser;
  18. use app\allwin\model\Vip;
  19. use think\helper\Time;
  20. use util\Util;
  21. class User extends Common{
  22. public $mini_program = [];
  23. public function initialize() {
  24. parent::initialize();
  25. $this->assign('pathMaps', [['name'=>'社群主管理','url'=>url("user/index")]]);
  26. }
  27. /**
  28. * 社群主管理
  29. */
  30. public function index($types = 0){
  31. $uid = trim(input('get.uid',''));
  32. $condition[] = ['member_miniapp_id','=',$this->member_miniapp_id] ;
  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. if(!empty($uid)){
  62. $condition[] = ['uid','=',$uid] ;
  63. }
  64. $view['pass_num'] = AllwinUserAgent::where($condition)->where(['is_pass' => 0])->count();
  65. $view['lock_num'] = AllwinUserAgent::where($condition)->where(['is_lock' => 1])->count();
  66. if($types == 1){
  67. $condition[] = ['is_lock','=',1];
  68. }else if($types == 2){
  69. $condition[] = ['is_pass','=',0];
  70. }else{
  71. $condition[] = ['is_pass','=',1];
  72. $condition[] = ['is_lock','=',0];
  73. }
  74. $view['uid'] = $uid;
  75. $view['types'] = $types;
  76. $view['starttime'] = $starttime;
  77. $view['endtime'] = $endtime;
  78. $view['time'] = $time;
  79. $view['worker_num'] = AllwinUserAgent::where($condition)->count();
  80. $view['sum_price'] = AllwinUserAgent::where($this->mini_program)->where($condition)->sum('price');
  81. $view['list'] = AllwinUserAgent::where($condition)->where($this->mini_program)->paginate(20);
  82. return view()->assign($view);
  83. }
  84. /**
  85. * 添加社群主
  86. */
  87. public function select(){
  88. if(request()->isAjax()){
  89. $ids = input('post.ids/s');
  90. if(empty($ids)){
  91. return json(['code'=>0,'msg'=>'请选择要添加社群主的用户']);
  92. }
  93. $ida = (array)ids($ids,true);
  94. if(empty($ida)){
  95. return json(['code'=>0,'msg'=>'请选择要添加社群主的用户']);
  96. }
  97. $result = AllwinUserAgent::add($this->member_miniapp_id,$ida);
  98. if($result){
  99. return json(['code'=>200,'msg'=>'社群主添加成功','data' =>[]]);
  100. }else{
  101. return json(['code'=>0,'msg'=>'社群主添加操作失败']);
  102. }
  103. }else{
  104. $keyword = trim(input('get.keyword','','htmlspecialchars'));
  105. $condition['is_lock'] = 0;
  106. $condition['member_miniapp_id'] = $this->member_miniapp_id;
  107. if(!empty($keyword)){
  108. $condition['phone_uid'] = $keyword;
  109. }
  110. $view['keyword'] = input('get.keyword');
  111. $view['list'] = AllwinUserAgent::selects($condition);
  112. return view()->assign($view);
  113. }
  114. }
  115. //删除
  116. public function agentdelete(){
  117. $id = input('get.id/d');
  118. $result = AllwinUserAgent::where($this->mini_program)->where(['uid' => $id])->delete();
  119. if($result){
  120. AllwinUser::editer($id,0); //取消店铺关系
  121. return json(['code'=>200,'msg'=>'操作成功']);
  122. }else{
  123. return json(['code'=>403,'msg'=>'删除失败']);
  124. }
  125. }
  126. /**
  127. * 员工管理
  128. */
  129. public function worker(int $types = 0){
  130. $uid = trim(input('get.uid',''));
  131. $condition[] = ['member_miniapp_id','=',$this->member_miniapp_id] ;
  132. $time = Request::param('time/d',0);
  133. $starttime = Request::param('starttime/s');
  134. $endtime = Request::param('endtime/s');
  135. if($time){
  136. switch ($time) {
  137. case 2:
  138. list($start, $end) = Time::yesterday();
  139. break;
  140. case 30:
  141. list($start, $end) = Time::month();
  142. break;
  143. case 60:
  144. list($start, $end) = Time::lastMonth();
  145. break;
  146. default:
  147. list($start, $end) = Time::today();
  148. break;
  149. }
  150. $condition[] = ['create_time','>=',$start];
  151. $condition[] = ['create_time','<=',$end];
  152. }else{
  153. if($starttime){
  154. $condition[] = ['create_time','>=',strtotime($starttime)];
  155. }
  156. if($endtime){
  157. $condition[] = ['create_time','<=',strtotime($endtime)];
  158. }
  159. }
  160. if(!empty($uid)){
  161. $condition[] = ['uid','=',$uid] ;
  162. }
  163. $view['pass_num'] = Worker::where($condition)->where(['is_pass' => 0])->count();
  164. $view['lock_num'] = Worker::where($condition)->where(['is_lock' => 1])->count();
  165. if($types == 1){
  166. $condition[] = ['is_lock','=',1];
  167. }else if($types == 2){
  168. $condition[] = ['is_pass','=',0];
  169. }else{
  170. $condition[] = ['is_pass','=',1];
  171. $condition[] = ['is_lock','=',0];
  172. }
  173. $view['uid'] = $uid;
  174. $view['types'] = $types;
  175. $view['starttime'] = $starttime;
  176. $view['endtime'] = $endtime;
  177. $view['time'] = $time;
  178. $view['worker_num'] = Worker::where($condition)->count();
  179. $view['sum_price'] = Worker::where($this->mini_program)->where($condition)->sum('price');
  180. $view['list'] = Worker::where($condition)->where($this->mini_program)->paginate(20);
  181. $view['pathMaps'] = [['name' => ' 合伙人', 'url'=>url("user/worker")]];
  182. return view()->assign($view);
  183. }
  184. /**
  185. * 选择员工
  186. */
  187. public function selectWorker(){
  188. if(request()->isAjax()){
  189. $ids = input('post.ids/s');
  190. if(empty($ids)){
  191. return json(['code'=>0,'msg'=>'请选择用户']);
  192. }
  193. $ida = (array)ids($ids,true);
  194. if(empty($ida)){
  195. return json(['code'=>0,'msg'=>'请选择要添加的合伙人']);
  196. }
  197. $result = Worker::add($this->member_miniapp_id,$ida);
  198. if($result){
  199. return json(['code'=>302,'msg'=>'公司员工添加成功','data' =>[]]);
  200. }else{
  201. return json(['code'=>0,'msg'=>'公司员工添加失败']);
  202. }
  203. }else{
  204. $keyword = trim(input('get.keyword','','htmlspecialchars'));
  205. $condition[] = ['is_lock','=',0] ;
  206. $condition[] = ['member_miniapp_id','=',$this->member_miniapp_id] ;
  207. if(!empty($keyword)){
  208. if(Validate::isMobile($keyword)){
  209. $condition[] = ['phone_uid','=',$keyword] ;
  210. }else{
  211. $condition[] = ['nickname','like','%'.$keyword.'%'];
  212. }
  213. }
  214. $view['keyword'] = input('get.keyword');
  215. $view['list'] = Worker::selects($condition,$this->member_miniapp_id);
  216. return view()->assign($view);
  217. }
  218. }
  219. //删除员工
  220. public function workerDelete(){
  221. $id = input('get.id/d');
  222. $result = Worker::where(['member_miniapp_id' => $this->member_miniapp_id,'id' =>$id])->delete();
  223. if($result){
  224. return json(['code'=>200,'msg'=>'操作成功']);
  225. }else{
  226. return json(['code'=>403,'msg'=>'删除失败']);
  227. }
  228. }
  229. //预览用户信息
  230. public function review(int $uid){
  231. $view['user'] = SystemUser::where($this->mini_program)->where(['id' => $uid])->find();
  232. if(empty($view['user'])){
  233. $this->error('用户不存在');
  234. }
  235. $view['store'] = AllwinStore::where($this->mini_program)->where(['manage_uid' => $uid])->select();
  236. $view['vip'] = Vip::where($this->mini_program)->where(['user_id' => $uid,'is_lock' => 0])->find();
  237. $worker = Worker::where($this->mini_program)->where(['uid' => $uid])->field('store_ids')->find();
  238. if(!empty($worker)){
  239. $view['worker_store'] = AllwinStore::where($this->mini_program)->whereIn('id',$worker->store_ids)->select();
  240. }
  241. $view['worker'] = $worker;
  242. $view['store_worker'] = StoreWorker::where($this->mini_program)->where(['uid' => $uid])->select();
  243. $view['agent'] = AllwinUserAgent::where($this->mini_program)->where(['uid' => $uid])->count();
  244. return view()->assign($view);
  245. }
  246. //所属店铺
  247. public function workerStore(int $id){
  248. $user = Worker::where($this->mini_program)->where(['id' => $id])->find();
  249. if(empty($user)){
  250. $this->error('不存在');
  251. }
  252. $ids = empty($user->store_ids) ? [] : explode(',',$user->store_ids);
  253. $view['store'] = AllwinStore::where($this->mini_program)->whereIN('id',$ids)->select();
  254. $view['worker_id'] = $id;
  255. $view['pathMaps'] = [['name'=>'合伙人','url'=>url('user/worker')],['name'=>'所属好店','url'=>'javascript:;']];
  256. return view()->assign($view);
  257. }
  258. /**
  259. * 选择所属好店
  260. */
  261. public function selectWorkerStore(int $worker_id){
  262. if(request()->isAjax()){
  263. $ids = input('post.ids/s');
  264. if(empty($ids)){
  265. return json(['code'=>0,'msg'=>'请选择要关联的商家']);
  266. }
  267. $ids = (array)ids($ids,true);
  268. $user = Worker::where($this->mini_program)->where(['id' => $worker_id])->find();
  269. if(empty($user)){
  270. return json(['code'=>0,'msg'=>'资源不存在']);
  271. }
  272. $store_ids = empty($user->store_ids) ? [] : explode(',',$user->store_ids);
  273. $user->store_ids = implode(',',Util::unique_array(array_merge($ids,$store_ids)));
  274. $user->save();
  275. return json(['code'=>200,'msg'=>'操作成功','data' =>[]]);
  276. }else{
  277. $keyword = trim(input('get.keyword',''));
  278. //查询店铺ID
  279. $worker = Worker::where($this->mini_program)->select();
  280. $store_id = [];
  281. foreach ($worker as $value) {
  282. $ids = empty($value->store_ids) ? [] : explode(',',$value->store_ids);
  283. $store_id = array_merge($store_id,$ids);
  284. }
  285. $condition = [];
  286. $condition[] = ['is_lock','=',0];
  287. if(!empty($keyword)){
  288. $condition[] = ['name','like','%'.$keyword.'%'];
  289. }
  290. $view['lists'] = AllwinStore::where($this->mini_program)->where($condition)->whereNotIn('id',$store_id)->order('is_top desc,sort desc,id desc')->paginate(20);
  291. $view['keyword'] = $keyword;
  292. $view['worker_id'] = $worker_id;
  293. return view()->assign($view);
  294. }
  295. }
  296. /**
  297. * 删除所属好店
  298. */
  299. public function workerStoreDelete(int $worker_id,int $store_id){
  300. $user = Worker::where($this->mini_program)->where(['id' => $worker_id])->find();
  301. if(empty($user)){
  302. return json(['code'=>0,'msg'=>'资源不存在']);
  303. }
  304. $user->store_ids = ids(array_values_unset($store_id,(array)ids($user->store_ids,true)));
  305. $user->save();
  306. return json(['code'=>200,'msg'=>'操作成功']);
  307. }
  308. public function isLock(int $id){
  309. $result = Worker::isLock($id);
  310. if(!$result){
  311. return json(['code'=>0,'message'=>'操作失败']);
  312. }else{
  313. return json(['code'=>200,'message'=>'操作成功']);
  314. }
  315. }
  316. public function isPass(int $id){
  317. $result = Worker::isPass($id,$this->member_miniapp_id);
  318. if(!$result){
  319. return json(['code'=>0,'message'=>'操作失败']);
  320. }else{
  321. return json(['code'=>200,'message'=>'操作成功']);
  322. }
  323. }
  324. public function agentLock(int $id){
  325. $result = AllwinUserAgent::isLock($id);
  326. if(!$result){
  327. return json(['code'=>0,'message'=>'操作失败']);
  328. }else{
  329. return json(['code'=>200,'message'=>'操作成功']);
  330. }
  331. }
  332. public function agentPass(int $id){
  333. $result = AllwinUserAgent::isPass($id,$this->member_miniapp_id);
  334. if(!$result){
  335. return json(['code'=>0,'message'=>'操作失败']);
  336. }else{
  337. return json(['code'=>200,'message'=>'操作成功']);
  338. }
  339. }
  340. }