No Description

AdminController.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. /**
  3. * Created by Sublime.
  4. * User: Williamslife Wang
  5. * Date: 17/10/18
  6. * Time: 上午11:20
  7. */
  8. namespace App\Http\Controllers\Admin;
  9. use App\Http\Controllers\Controller;
  10. use App\Admin;
  11. use Illuminate\Http\Request;
  12. use App\Role;
  13. use App\AdminRole;
  14. use App\Services\OssServices as oss;
  15. use Illuminate\Support\Facades\DB;
  16. class AdminController extends Controller
  17. {
  18. /**
  19. * 一开始做为公用的继承文件
  20. * @return \Illuminate\View\View
  21. */
  22. // public function index()
  23. // {
  24. // return view('admin/admin');
  25. // }
  26. /**
  27. * @return \Illuminate\View\View
  28. */
  29. public function all()
  30. {
  31. $ids = DB::table('admin_role')->whereIn('role_name', ['管理员', '销售'])->lists('user_id');
  32. $admin = Admin::whereIn('id', $ids)->get();
  33. foreach($admin as $item){
  34. #获取团队
  35. $team = DB::table('teams')->select('name')->where('id', $item->team_id)->first();
  36. $item->team_name = isset($team->name) ? $team->name : '';
  37. #获取角色名称
  38. $roleName = DB::table('admin_role')->where('user_id',$item->id)->pluck('role_name');
  39. $item->role_name = $roleName;
  40. }
  41. return view('admin/adminall', ['admin' => $admin]);
  42. }
  43. /**
  44. * @return \Illuminate\View\View
  45. */
  46. public function teamindex()
  47. {
  48. $team = DB::table('teams')->get();
  49. $team = json_decode(json_encode($team),true);
  50. foreach($team as $k=>&$v){
  51. if($v['type'] == 1){
  52. $v['type'] = '销售团队';
  53. }elseif($v['type'] == 2){
  54. $v['type'] = '分销团队';
  55. }else{
  56. $v['type'] = '其他';
  57. }
  58. }
  59. return view('admin/teamindex', ['team' => $team]);
  60. }
  61. /**
  62. * @return \Illuminate\View\View
  63. */
  64. public function create()
  65. {
  66. $roles = Role::select('id','name')->where('is_show', 1)->where('name', '!=', '超级管理员')->where('name', '!=', '售后管理员')->groupBy('name')->get();
  67. $team = DB::table('teams')->get();
  68. $team = json_decode(json_encode($team),true);
  69. return view('admin/admincreate', ['roles' => $roles, 'team'=>$team]);
  70. }
  71. public function del($id){
  72. if($id > 0 ){
  73. Admin::where('id', $id)->update(['is_use'=> 0]);
  74. // exit('1');
  75. echo '1';die;
  76. }
  77. // exit('0');
  78. echo '0';die;
  79. }
  80. public function up($id){
  81. if($id > 0 ){
  82. Admin::where('id', $id)->update(['is_use'=> 1]);
  83. // exit('1');
  84. echo '1';die;
  85. }
  86. // exit('0');
  87. echo '0';die;
  88. }
  89. /**
  90. * @param Request $request
  91. * @return \Illuminate\Http\RedirectResponse
  92. */
  93. public function store(Request $request)
  94. {
  95. $this->validate($request, [
  96. 'username' => 'required|unique:admin',
  97. 'password' => 'required|confirmed',
  98. 'password_confirmation' => 'required',
  99. 'roles' => 'required',
  100. 'phone' => 'regex:/^1[3456789]\d{9}$/',
  101. ], [
  102. 'username.required' => '登录名称不能为空',
  103. 'username.unique' => '登录名称已存在',
  104. 'password.required' => '密码不能为空',
  105. 'password_confirmation.required' => '再次输入密码为空',
  106. 'password.confirmed' => '两次密码不一致',
  107. 'roles.required' => '请至少选择一个角色',
  108. 'phone.regex' => '手机号格式有误',
  109. ]);
  110. $admin = new Admin();
  111. $admin->username = $request->input('username');
  112. $admin->password = bcrypt($request->input('password'));
  113. $admin->realname = $request->input('realname');
  114. $admin->team_id = $request->input('team_id');
  115. $admin->wx_id = $request->input('wx_id');
  116. $admin->phone = $request->input('phone');
  117. $admin->is_use = 1;
  118. //图片上传 阿里云oss
  119. if ($request->hasFile('qrcode') && $request->file('qrcode')->isValid()) {
  120. $file = $request->file('qrcode');
  121. $ossClient=new oss();
  122. // 上传阿里云
  123. $file = $ossClient->upload($file->getClientOriginalExtension(), $file->getRealPath(), 'upload/seafoodPic'.date("Y-m-d",time()).'/'.date('His'));
  124. $qrcode=$file['oss-request-url'];
  125. $admin->qrcode=str_replace("kx-youhuiquan.oss-cn-beijing.aliyuncs.com","imgs.726p.com",$qrcode);
  126. }
  127. $admin->save();
  128. $user_id = $admin->id;
  129. $roles = $request->input('roles');
  130. foreach($roles as $v){
  131. $admin_role = new AdminRole();
  132. $admin_role->user_id = $user_id;
  133. $admin_role->role_name = $v;
  134. $admin_role->create_time = time();
  135. $admin_role->save();
  136. }
  137. return redirect('/admin/admin/all')->with('info', '添加管理员成功');
  138. }
  139. /**
  140. * @param $id
  141. * @return \Illuminate\View\View
  142. */
  143. public function edit($id)
  144. {
  145. $admin = Admin::findOrFail($id);//帐号密码信息
  146. $admin_roles = AdminRole::select('role_name')->where('user_id', '=', $id)->get();//角色信息
  147. $roles = Role::select('id','name')->where('is_show', 1)->where('name', '!=', '超级管理员')->where('name', '!=', '售后管理员')->groupBy('name')->get();
  148. $team = DB::table('teams')->get();
  149. $team = json_decode(json_encode($team),true);
  150. return view('admin/adminedit', ['roles' => $roles, 'admin' => $admin, 'admin_roles' => $admin_roles, 'team'=>$team]);
  151. }
  152. /**
  153. * @param Request $request
  154. * @return \Illuminate\Http\RedirectResponse
  155. */
  156. public function update(Request $request)
  157. {
  158. $this->validate($request, [
  159. 'password' => 'confirmed',
  160. 'roles' => 'required',
  161. 'phone' => 'regex:/^1[3456789]\d{9}$/',
  162. ], [
  163. 'password.confirmed' => '两次密码不一致',
  164. 'roles.required' => '请至少选择一个角色',
  165. 'phone.regex' => '手机号格式有误',
  166. ]);
  167. $password = $request->input('password');
  168. $user_id = $request->input('id');
  169. $admin = Admin::findOrFail($user_id);
  170. $admin->realname = $request->input('realname');
  171. $admin->team_id = $request->input('team_id');
  172. $admin->wx_id = $request->input('wx_id');
  173. $admin->phone = $request->input('phone');
  174. if($password){
  175. $admin->password = bcrypt($password);
  176. }
  177. //图片上传 阿里云oss
  178. if ($request->hasFile('qrcode') && $request->file('qrcode')->isValid()) {
  179. $file = $request->file('qrcode');
  180. $ossClient=new oss();
  181. // 上传阿里云
  182. $file = $ossClient->upload($file->getClientOriginalExtension(), $file->getRealPath(), 'upload/seafoodPic'.date("Y-m-d",time()).'/'.date('His'));
  183. $qrcode=$file['oss-request-url'];
  184. $admin->qrcode=str_replace("kx-youhuiquan.oss-cn-beijing.aliyuncs.com","imgs.726p.com",$qrcode);
  185. }
  186. $admin->save();
  187. $roles = $request->input('roles');
  188. $admin_role = new AdminRole();
  189. $admin_role->where('user_id', '=', $user_id)->delete();
  190. foreach($roles as $v){
  191. $admin_role = new AdminRole();
  192. $admin_role->user_id = $user_id;
  193. $admin_role->role_name = $v;
  194. $admin_role->create_time = time();
  195. $admin_role->save();
  196. }
  197. return redirect('/admin/admin/all')->with('info', '修改管理员成功');
  198. }
  199. /**
  200. * @return \Illuminate\View\View
  201. */
  202. public function teamcreate()
  203. {
  204. return view('admin/teamcreate');
  205. }
  206. /**
  207. * @param Request $request
  208. * @return \Illuminate\Http\RedirectResponse
  209. */
  210. public function teamstore(Request $request)
  211. {
  212. $this->validate($request, [
  213. 'name' => 'required|unique:teams',
  214. ], [
  215. 'name.required' => '团队名称不能为空',
  216. 'name.unique' => '团队名称已存在',
  217. ]);
  218. $data = array();
  219. $data['name'] = $request->input('name');
  220. $data['type'] = (int)$request->input('type');
  221. $res = DB::table('teams')->insert($data);
  222. return redirect('/admin/admin/teamindex')->with('info', '添加成功');
  223. }
  224. /**
  225. * @param $id
  226. * @return \Illuminate\View\View
  227. */
  228. public function teamedit($id)
  229. {
  230. $team = DB::table('teams')->where('id', $id)->first();
  231. $team = json_decode(json_encode($team),true);
  232. return view('admin/teamedit', ['team' => $team]);
  233. }
  234. /**
  235. * @param Request $request
  236. * @return \Illuminate\Http\RedirectResponse
  237. */
  238. public function teamupdate(Request $request)
  239. {
  240. $id = (int)$request->input('id');
  241. $this->validate($request, [
  242. 'name' => 'required|unique:teams,name,'.$id.',id',
  243. 'id' => 'required'
  244. ], [
  245. 'name.required' => '团队名称不能为空',
  246. 'name.unique' => '团队名称已存在',
  247. ]);
  248. $data = array();
  249. $data['name'] = $request->input('name');
  250. $data['type'] = (int)$request->input('type');
  251. $res = DB::table('teams')->where('id', $id)->update($data);
  252. return redirect('/admin/admin/teamindex')->with('info', '修改成功');
  253. }
  254. /**
  255. * 销售上传二维码
  256. */
  257. public function uploadQrcodeEdit(Request $request){
  258. $admin_id = session('admin_id');
  259. $qrcode = Admin::where('id', $admin_id)->pluck('qrcode');
  260. return view('admin/uploadqrcode', ['qrcode'=>$qrcode, 'id'=>$admin_id]);
  261. }
  262. /**
  263. * 销售上传二维码
  264. */
  265. public function uploadQrcode(Request $request){
  266. $admin_id = session('admin_id');
  267. //图片上传 阿里云oss
  268. $qrcode = null;
  269. if ($request->hasFile('img') && $request->file('img')->isValid()) {
  270. $file = $request->file('img');
  271. $ossClient=new oss();
  272. // 上传阿里云
  273. $file = $ossClient->upload($file->getClientOriginalExtension(), $file->getRealPath(), 'upload/seafoodPic'.date("Y-m-d",time()).'/'.date('His'));
  274. $img=$file['oss-request-url'];
  275. $qrcode=str_replace("kx-youhuiquan.oss-cn-beijing.aliyuncs.com","imgs.726p.com",$img);
  276. }
  277. if($qrcode){
  278. $res = Admin::where('id', $admin_id)->update(['qrcode'=>$qrcode]);
  279. return redirect('/admin/admin/uploadQrcodeEdit')->with('info', '上传成功');
  280. }
  281. return redirect('/admin/admin/uploadQrcodeEdit')->with('info', '未做修改');
  282. }
  283. /**
  284. * 管理自己团队的成员
  285. */
  286. public function teamSalers(Request $request)
  287. {
  288. $self_role = session('role_name');
  289. if($self_role == '超级管理员'){
  290. $admin = Admin::get();
  291. }else{
  292. $admin_id = session('admin_id');
  293. $team_id = Admin::where('id', $admin_id)->pluck('team_id');
  294. $admin = Admin::where('team_id', $team_id)->get();
  295. }
  296. return view('admin/teamSalers', ['admin' => $admin]);
  297. }
  298. public function qrcode_del($id){
  299. if($id > 0 ){
  300. Admin::where('id', $id)->update(['is_qrcode_use'=> 0]);
  301. // exit('1');
  302. echo '1';die;
  303. }
  304. // exit('0');
  305. echo '0';die;
  306. }
  307. public function qrcode_up($id){
  308. if($id > 0 ){
  309. Admin::where('id', $id)->update(['is_qrcode_use'=> 1]);
  310. // exit('1');
  311. echo '1';die;
  312. }
  313. // exit('0');
  314. echo '0';die;
  315. }
  316. }