BalanceController.php 7.0 KB

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