BalanceController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Requests\BalanceCreateRequest;
  4. use App\Http\Requests\BalanceUpdateRequest;
  5. use App\Models\AppBalance;
  6. use App\Models\Balance;
  7. use Illuminate\Http\Request;
  8. use App\Http\Controllers\Controller;
  9. use App\Models\Role;
  10. class BalanceController extends Controller
  11. {
  12. /**
  13. * Display a listing of the resource.
  14. *
  15. * @return \Illuminate\Http\Response
  16. */
  17. public function party(Request $request)
  18. {
  19. $partyId = $request->get('party_id', '0');
  20. return view('admin.balance.index')->with('party_id', $partyId);
  21. }
  22. public function partyData(Request $request)
  23. {
  24. $partyId = $request->get('partyId');
  25. $party_name = $request->get('party_name');
  26. $app_name = $request->get('app_name');
  27. $party_username = $request->get('party_username');
  28. $type = $request->get('type', 0);
  29. $model = new Balance();
  30. $model = $model
  31. ->select(['ag_balance_log.*','ag_app.appname','ag_party.name'])
  32. ->leftJoin('ag_app', 'ag_balance_log.app_id', '=', 'ag_app.id')
  33. ->leftJoin('ag_party', 'ag_balance_log.party_id', '=', 'ag_party.id');
  34. if(!empty($partyId)){
  35. $model->where('party_id', $partyId);
  36. }
  37. if(!empty($party_name)){
  38. $model->where('ag_party.name', 'like', '%'.$party_name.'%');
  39. }
  40. if(!empty($app_name)){
  41. $model->where('ag_app.appname', 'like', '%'.$app_name.'%');
  42. }
  43. if(!empty($party_username)){
  44. $model->where('party_username', 'like', '%'.$party_username.'%');
  45. }
  46. if(!empty($type)){
  47. $model->where('type', $type);
  48. }
  49. $res = $model->orderBy('ag_balance_log.id','desc')
  50. ->paginate($request->get('limit', 30))
  51. ->toArray();
  52. ;
  53. foreach ($res['data'] as $key=>$val){
  54. $res['data'][$key]['typename'] = $val['type'] == 1 ? '增加' : '减少';
  55. }
  56. $data = [
  57. 'code' => 0,
  58. 'msg' => '正在请求中...',
  59. 'count' => $res['total'],
  60. 'data' => $res['data']
  61. ];
  62. return response()->json($data);
  63. }
  64. public function app(Request $request)
  65. {
  66. $app_id = $request->get('app_id', '0');
  67. return view('admin.balance.appIndex')->with('app_id', $app_id);
  68. }
  69. public function appData(Request $request)
  70. {
  71. $app_id = $request->get('app_id');
  72. $party_name = $request->get('party_name');
  73. $app_name = $request->get('app_name');
  74. $app_username = $request->get('app_username');
  75. $type = $request->get('type', 0);
  76. $model = new AppBalance();
  77. $model = $model
  78. ->select(['ag_app_balance_log.*','ag_app.appname','ag_party.name'])
  79. ->leftJoin('ag_app', 'ag_app_balance_log.app_id', '=', 'ag_app.id')
  80. ->leftJoin('ag_party', 'ag_app_balance_log.party_id', '=', 'ag_party.id');
  81. if(!empty($app_id)){
  82. $model->where('app_id', $app_id);
  83. }
  84. if(!empty($party_name)){
  85. $model->where('ag_party.name', 'like', '%'.$party_name.'%');
  86. }
  87. if(!empty($app_name)){
  88. $model->where('ag_app.appname', 'like', '%'.$app_name.'%');
  89. }
  90. if(!empty($app_username)){
  91. $model->where('app_username', 'like', '%'.$app_username.'%');
  92. }
  93. if(!empty($type)){
  94. $model->where('type', $type);
  95. }
  96. $res = $model->orderBy('ag_app_balance_log.id','desc')
  97. ->paginate($request->get('limit', 30))
  98. ->toArray();
  99. ;
  100. foreach ($res['data'] as $key=>$val){
  101. $res['data'][$key]['typename'] = $val['type'] == 1 ? '增加' : '减少';
  102. }
  103. $data = [
  104. 'code' => 0,
  105. 'msg' => '正在请求中...',
  106. 'count' => $res['total'],
  107. 'data' => $res['data']
  108. ];
  109. return response()->json($data);
  110. }
  111. /**
  112. * Show the form for creating a new resource.
  113. *
  114. * @return \Illuminate\Http\Response
  115. */
  116. public function create()
  117. {
  118. return view('admin.party.create')->with(['party'=>array()]);
  119. }
  120. /**
  121. * Store a newly created resource in storage.
  122. *
  123. * @param \Illuminate\Http\Request $request
  124. * @return \Illuminate\Http\Response
  125. */
  126. public function store(BalanceCreateRequest $request)
  127. {
  128. $data = $request->all();
  129. if (Balance::create($data)){
  130. return redirect()->to(route('admin.party'))->with(['status'=>'添加成功']);
  131. }
  132. return redirect()->to(route('admin.party'))->withErrors('系统错误');
  133. }
  134. /**
  135. * Display the specified resource.
  136. *
  137. * @param int $id
  138. * @return \Illuminate\Http\Response
  139. */
  140. public function show($id)
  141. {
  142. //
  143. }
  144. /**
  145. * Show the form for editing the specified resource.
  146. *
  147. * @param int $id
  148. * @return \Illuminate\Http\Response
  149. */
  150. public function edit($id)
  151. {
  152. $party = Balance::where('id', $id)->first();
  153. return view('admin.party.edit',compact('party'));
  154. }
  155. /**
  156. * Update the specified resource in storage.
  157. *
  158. * @param \Illuminate\Http\Request $request
  159. * @param int $id
  160. * @return \Illuminate\Http\Response
  161. */
  162. public function update(BalanceUpdateRequest $request, $id)
  163. {
  164. // $party = Balance::findOrFail($id);
  165. $data = $request->all();
  166. if(empty($data['key'])){
  167. unset($data['key']);
  168. }
  169. if(empty($data['secret'])){
  170. unset($data['secret']);
  171. }
  172. $party = Balance::findOrFail($id);
  173. if ($party->update($data)){
  174. return redirect()->to(route('admin.party'))->with(['status'=>'更新成功']);
  175. }
  176. return redirect()->to(route('admin.party'))->withErrors('系统错误');
  177. }
  178. /**
  179. * Remove the specified resource from storage.
  180. *
  181. * @param int $id
  182. * @return \Illuminate\Http\Response
  183. */
  184. public function destroy(Request $request)
  185. {
  186. $ids = $request->get('ids');
  187. if (empty($ids)){
  188. return response()->json(['code'=>1,'msg'=>'请选择删除项']);
  189. }
  190. if (Balance::destroy($ids)){
  191. return response()->json(['code'=>0,'msg'=>'删除成功']);
  192. }
  193. return response()->json(['code'=>1,'msg'=>'删除失败']);
  194. }
  195. /**
  196. * 分配角色
  197. */
  198. public function role(Request $request,$id)
  199. {
  200. $party = Balance::findOrFail($id);
  201. $roles = Role::get();
  202. $hasRoles = $party->roles();
  203. foreach ($roles as $role){
  204. $role->own = $party->hasRole($role) ? true : false;
  205. }
  206. return view('admin.party.role',compact('roles','party'));
  207. }
  208. /**
  209. * 更新分配角色
  210. */
  211. public function assignRole(Request $request,$id)
  212. {
  213. $party = Balance::findOrFail($id);
  214. $roles = $request->get('roles',[]);
  215. if ($party->syncRoles($roles)){
  216. return redirect()->to(route('admin.party'))->with(['status'=>'更新用户角色成功']);
  217. }
  218. return redirect()->to(route('admin.party'))->withErrors('系统错误');
  219. }
  220. /**
  221. * 分配权限
  222. */
  223. public function permission(Request $request,$id)
  224. {
  225. $party = Balance::findOrFail($id);
  226. $permissions = $this->tree();
  227. foreach ($permissions as $key1 => $item1){
  228. $permissions[$key1]['own'] = $party->hasDirectPermission($item1['id']) ? 'checked' : false ;
  229. if (isset($item1['_child'])){
  230. foreach ($item1['_child'] as $key2 => $item2){
  231. $permissions[$key1]['_child'][$key2]['own'] = $party->hasDirectPermission($item2['id']) ? 'checked' : false ;
  232. if (isset($item2['_child'])){
  233. foreach ($item2['_child'] as $key3 => $item3){
  234. $permissions[$key1]['_child'][$key2]['_child'][$key3]['own'] = $party->hasDirectPermission($item3['id']) ? 'checked' : false ;
  235. }
  236. }
  237. }
  238. }
  239. }
  240. return view('admin.party.permission',compact('party','permissions'));
  241. }
  242. /**
  243. * 存储权限
  244. */
  245. public function assignPermission(Request $request,$id)
  246. {
  247. $party = Balance::findOrFail($id);
  248. $permissions = $request->get('permissions');
  249. if (empty($permissions)){
  250. $party->permissions()->detach();
  251. return redirect()->to(route('admin.party'))->with(['status'=>'已更新用户直接权限']);
  252. }
  253. $party->syncPermissions($permissions);
  254. return redirect()->to(route('admin.party'))->with(['status'=>'已更新用户直接权限']);
  255. }
  256. }