BalanceController.php 7.0 KB

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