Business.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Http\Requests\Admin\Business as Request;
  3. use App\Models\Business as Model;
  4. use App\Models\AdminUser;
  5. use App\Models\City;
  6. use App\Models\AuthRule;
  7. use App\Models\AdminRule;
  8. use Illuminate\Support\Carbon;
  9. use Illuminate\Support\Facades\DB;
  10. /**
  11. * 商业公司管理
  12. *
  13. * @author 刘相欣
  14. *
  15. */
  16. class Business extends Auth{
  17. protected function _initialize(){
  18. parent::_initialize();
  19. $this->assign('breadcrumb1','商家管理');
  20. $this->assign('breadcrumb2','商业公司');
  21. }
  22. /**
  23. * 列表页
  24. *
  25. * */
  26. public function index(Model $Model,City $City,AdminUser $AdminUser){
  27. // 接受参数
  28. $code = request('business_code','');
  29. $phone = request('phone','');
  30. $status = request('status');
  31. $startTime = request('start_time','');
  32. // 编码转ID
  33. $id = $Model->codeToId($code);
  34. // 查询条件
  35. $map = [];
  36. // 编码ID
  37. if( $id ) $map[] = ['id','=',$id];
  38. if( $phone ) $map[] = ['phone','=',$phone];
  39. if( $startTime ) $map[] = ['insert_time','>=',Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp()];
  40. if( $startTime ) $map[] = ['insert_time','<=',Carbon::createFromFormat('Y-m-d',$startTime)->endOfDay()->getTimestamp()];
  41. if( !is_null($status) ) $map[] = ['status','=',$status];
  42. $session = session('userRule');
  43. if ($session){
  44. $map[] = ['company_id','=',$session['company_id']];
  45. if ($session['business_id']){
  46. $map[] = ['id','=',$session['business_id']];
  47. }
  48. if ($session['menu_type'] == 1 && $session['data_type'] == 2){
  49. $map[] = ['leader_uid','=',$session['admin_uid']];
  50. }
  51. }
  52. // 查询数据
  53. $list = $Model->query()->where($map)->orderByDesc('id')->paginate(config('page_num',10));
  54. // 循环处理数据
  55. foreach ($list as $key => $value) {
  56. // id转编号
  57. $value['business_code'] = $Model->idToCode($value['id']);
  58. // id转编号
  59. $value['leader_name'] = $AdminUser->getOne($value['leader_uid'],'username');
  60. // 重组
  61. $list[$key] = $value;
  62. if( $value['city_ids'] ) {
  63. // 解析数组
  64. $cityids = explode(',',$value['city_ids']);
  65. // 获取城市
  66. foreach ($cityids as $kk=>$vv) {
  67. // 获取值
  68. $vv = $City->getOne($vv,'name');
  69. // 获取城市名
  70. $cityids[$kk] = $vv;
  71. }
  72. // 城市列表
  73. $value['city_ids'] = implode('、',$cityids);
  74. }
  75. }
  76. // 分配数据
  77. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  78. $this->assign('list',$list);
  79. // 加载模板
  80. return $this->fetch();
  81. }
  82. /**
  83. * 添加
  84. *
  85. * */
  86. public function add(Request $request,Model $Model,City $City,AdminUser $AdminUser,AdminRule $AdminRule){
  87. if( request()->isMethod('post') ){
  88. // 验证参数
  89. $request->scene('add')->validate();
  90. // 接收数据
  91. $data['name'] = request('name','');
  92. $data['phone'] = request('phone','');
  93. $data['logopic'] = request('logopic','');
  94. $data['desc'] = request('desc','');
  95. $data['leader_uid'] = request('leader_uid','');
  96. $data['address'] = request('address','');
  97. $data['city_ids'] = request('city_ids','');
  98. $data['city_ids'] = implode(',',$data['city_ids']);
  99. $custom_name = request('custom_name','');
  100. $phone = request('phone','');
  101. $custom_password = request('custom_password','');
  102. // 角色数据
  103. $ruleData = ['menu_type'=>2,'company_id'=>1,'business_id'=>0];
  104. // 当前登录用户角色数据
  105. $session = session('userRule') ? session('userRule') : ['menu_type'=>0,'company_id'=>0,'business_id'=>0];
  106. // 修改数据
  107. if( $session['menu_type'] ) $ruleData['menu_type'] = $session['menu_type'];
  108. if( $session['company_id'] ) $ruleData['company_id'] = $session['company_id'];
  109. $userData['username'] = $custom_name;
  110. $userData['phone'] = $phone;
  111. $userData['password'] = md5($custom_password);
  112. // 开启事务
  113. DB::beginTransaction();
  114. try {
  115. // 写入数据表
  116. $id = $Model->add($data);
  117. // 如果操作失败
  118. if( !$id ){
  119. return json_send(['code'=>'error','msg'=>'新增店铺失败']);
  120. }
  121. // 写入数据表
  122. $uid = $AdminUser->add($userData);
  123. // 如果操作失败
  124. if( !$uid ){
  125. // 回滚事务
  126. DB::rollBack();
  127. return json_send(['code'=>'error','msg'=>'新增管理员失败']);
  128. }
  129. $ruleData['admin_uid'] = $uid;
  130. $ruleData['business_id'] = $id;
  131. // 写入数据表
  132. $ruleUid = $AdminRule->add($ruleData);
  133. // 如果操作失败
  134. if( !$ruleUid ){
  135. // 回滚事务
  136. DB::rollBack();
  137. return json_send(['code'=>'error','msg'=>'新增管理员角色失败']);
  138. }
  139. // 写入数据表
  140. $re = $Model->edit($id,['admin_uid'=>$uid]);
  141. // 如果操作失败
  142. if( !$re ){
  143. // 回滚事务
  144. DB::rollBack();
  145. return json_send(['code'=>'error','msg'=>'新增失败','data'=>'新增店铺管理员']);
  146. }
  147. // 写入数据表
  148. $re = DB::table('auth_group_access')->insert(['group_id'=>5,'user_uid'=>$uid]);
  149. // 如果操作失败
  150. if( !$re ){
  151. // 回滚事务
  152. DB::rollBack();
  153. return json_send(['code'=>'error','msg'=>'新增失败','data'=>'新增店铺管理员']);
  154. }
  155. }catch (\Exception $e){
  156. // 回滚事务
  157. DB::rollBack();
  158. // 告知错误
  159. return json_send(['code'=>'error','msg'=>'新增失败','data'=>json_encode($e->getMessage())]);
  160. }
  161. // 提交事务
  162. DB::commit();
  163. // 记录行为
  164. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  165. // 告知结果
  166. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  167. }
  168. // 获取列表
  169. $cityList = $City->getCityList();
  170. $session = session('userRule');
  171. $mp = [];
  172. if ($session){
  173. $mp[] = ['admin_rule.company_id','=',$session['company_id']];
  174. if ($session['menu_type'] == 1 && $session['data_type'] == 2){
  175. $mp[] = ['admin_rule.admin_uid','=',$session['admin_uid']];
  176. }else{
  177. $mp[] = ['admin_rule.menu_type','=',1];
  178. $mp[] = ['admin_rule.data_type','=',2];
  179. }
  180. }
  181. $leaderList = $AdminRule::query()
  182. ->join('admin','admin.uid','=','admin_rule.admin_uid')
  183. ->where($mp)
  184. ->get(['admin.uid','admin.username']);
  185. // 分配数据
  186. $this->assign('crumbs','新增');
  187. $this->assign('cityList',$cityList);
  188. $this->assign('leaderList',$leaderList);
  189. // 加载模板
  190. return $this->fetch();
  191. }
  192. /**
  193. * 修改
  194. *
  195. * */
  196. public function edit(Request $request,Model $Model,AdminUser $AdminUser,City $City,AdminRule $AdminRule){
  197. // 接收参数
  198. $id = request('id',0);
  199. // 查询用户
  200. $oldData = $Model->where(['id'=>$id])->first();
  201. // 修改
  202. if(request()->isMethod('post')){
  203. // 验证参数
  204. $request->scene('edit')->validate();
  205. // 接收数据
  206. $data['name'] = request('name','');
  207. $data['phone'] = request('phone','');
  208. $data['logopic'] = request('logopic','');
  209. $data['desc'] = request('desc','');
  210. $data['leader_uid'] = request('leader_uid','');
  211. $data['address'] = request('address','');
  212. $data['city_ids'] = request('city_ids','');
  213. $custom_name = request('custom_name','');
  214. $phone = request('phone','');
  215. $custom_password = request('custom_password','');
  216. if ($data['city_ids']){
  217. $data['city_ids'] = implode(',',$data['city_ids']);
  218. }
  219. // 写入数据表
  220. $result = $Model->edit($id,$data);
  221. // 如果操作失败
  222. if( !$result ) return json_send(['code'=>'error','msg'=>'新增失败']);
  223. $userData['username'] = $custom_name;
  224. $userData['phone'] = $phone;
  225. if ($custom_password){
  226. $userData['password'] = md5($custom_password);
  227. }
  228. //获取店铺信息
  229. $info = $Model->getOne($id);
  230. // 写入数据表
  231. $result = $AdminUser->edit($info['admin_uid'],$userData);
  232. // 如果操作失败
  233. if( !$result ) return json_send(['code'=>'error','msg'=>'新增失败']);
  234. //var_dump($data);exit();
  235. // 记录行为
  236. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,$data);
  237. // 告知结果
  238. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  239. }
  240. // 错误告知
  241. if( !$oldData ) return $this->error('查无数据');
  242. //查询管理账户信息
  243. $adminInfo = $AdminUser->getOne($oldData['admin_uid']);
  244. // 错误告知
  245. if( !$adminInfo ) return $this->error('查无管理员账户数据');
  246. $oldData['city_ids'] = explode(',',$oldData['city_ids']);
  247. // 获取列表
  248. $cityList = $City->getCityList();
  249. $session = session('userRule');
  250. $mp = [];
  251. if ($session){
  252. $mp[] = ['admin_rule.company_id','=',$session['company_id']];
  253. if ($session['menu_type'] == 1 && $session['data_type'] == 2){
  254. $mp[] = ['admin_rule.admin_uid','=',$session['admin_uid']];
  255. }else{
  256. $mp[] = ['admin_rule.menu_type','=',1];
  257. $mp[] = ['admin_rule.data_type','=',2];
  258. }
  259. }
  260. $leaderList = $AdminRule::query()
  261. ->join('admin','admin.uid','=','admin_rule.admin_uid')
  262. ->where($mp)
  263. ->get(['admin.uid','admin.username']);
  264. // 分配数据
  265. $this->assign('oldData',$oldData);
  266. $this->assign('adminInfo',$adminInfo);
  267. $this->assign('cityList',$cityList);
  268. $this->assign('leaderList',$leaderList);
  269. $this->assign('crumbs','修改');
  270. // 加载模板
  271. return $this->fetch();
  272. }
  273. /**
  274. * 修改状态
  275. *
  276. * */
  277. public function set_status(Request $request,Model $Model){
  278. // 验证参数
  279. $request->scene('set_status')->validate();
  280. // 设置状态
  281. $id = request('id',0);
  282. $status = request('status',0);
  283. // 查询用户
  284. $oldData = $Model->where(['id'=>$id])->first();
  285. // 如果用户不存在
  286. if( !$oldData ) return json_send(['code'=>'error','msg'=>'用户不存在']);
  287. // 执行修改
  288. $result = $Model->edit($id,['status'=>$status]);
  289. // 提示新增失败
  290. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  291. // 记录行为
  292. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
  293. // 告知结果
  294. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  295. }
  296. }