IndexController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\BaseController;
  4. use App\Servers\SmsServer;
  5. use Illuminate\Contracts\Routing\ResponseFactory;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Storage;
  10. class IndexController extends BaseController
  11. {
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. }
  16. /**
  17. * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
  18. *
  19. * 后台首页
  20. */
  21. public function index()
  22. {
  23. // request()->session()->put('aaa','12122121');
  24. // request()->session()->save();
  25. // dd(request()->session()->all());
  26. return view('admin.index');
  27. }
  28. /**
  29. * @param Request $request
  30. * @param ResponseFactory $response
  31. * @return \Illuminate\Http\JsonResponse
  32. *
  33. * bootstrap-table 表单 yeOrNo 数据处理
  34. * 处理请求 yes or no 参数 (全局函数)
  35. */
  36. public function switchYesOrNo(Request $request, ResponseFactory $response)
  37. {
  38. try {
  39. if ($request->isMethod('post') && $request->ajax()) {
  40. $id = $request->id;
  41. $field = $request->field;
  42. $table = str_plural($request->table);
  43. $pk = $request->input('pk', 'id');
  44. $model = DB::table($table);
  45. $flag = false;
  46. $updateFieldData = 0;
  47. $datum = $model->select($field)->where($pk, $id)->first();
  48. if ($datum) {
  49. $datum->{$field} == 1 ? $updateFieldData = 0 : $updateFieldData = 1;
  50. // 更新指定表的指定字段 ::=> {$table}更新表 -> {$field}更新字段 -> {$updateFieldData}更新为指定值
  51. $result = $model
  52. ->where($pk, $id)
  53. ->update([
  54. $field => $updateFieldData
  55. ]);
  56. $result ? $flag = true : null;
  57. }
  58. if ($flag) {
  59. if ($table == 'members' && $field == 'is_show' && $updateFieldData == 0) {
  60. }
  61. }
  62. $flag ?
  63. $returnDatum = ['status' => 0, 'message' => '', 'value' => $updateFieldData]
  64. :
  65. $returnDatum = ['status' => 1, 'message' => '操作失败,请重试!', 'value' => $updateFieldData];
  66. if ($request->wantsJson()) {
  67. return $response->json($returnDatum);
  68. }
  69. }
  70. } catch (\Exception $e) {
  71. return $response->json(
  72. array('status' => 1, 'message' => $e->getMessage(), 'value' => '')
  73. );
  74. }
  75. }
  76. /**
  77. * @param Request $request
  78. * @param ResponseFactory $response
  79. * @return \Illuminate\Http\JsonResponse
  80. * 修改sort字段 (全局函数)
  81. */
  82. public function updateSort(Request $request, ResponseFactory $response)
  83. {
  84. try {
  85. if ($request->isMethod('post') && $request->ajax()) {
  86. $id = $request->id;
  87. $field = $request->field;
  88. // 表名称
  89. $table = $request->table;
  90. $pk = $request->input('pk', 'id');
  91. $value = $request->value;
  92. $model = DB::table($table);
  93. $datum = $model->where($pk, $id)->first();
  94. if (is_null($datum)) {
  95. // 该数据不存在
  96. $returnDatum = ['status' => 2, 'message' => '数据不存在', 'value' => ''];
  97. } else {
  98. // 修改值 sort
  99. $result = $model->where($pk, $id)->update(array(
  100. $field => $value
  101. ));
  102. if ($result) {
  103. $returnDatum = ['status' => 0, 'message' => '', 'value' => $value];
  104. } else {
  105. $returnDatum = ['status' => 1, 'message' => '保存失败', 'value' => ''];
  106. }
  107. }
  108. if ($request->wantsJson()) {
  109. return $response->json($returnDatum);
  110. }
  111. }
  112. } catch (\Exception $e) {
  113. return $response->json(array(
  114. 'status' => 1, 'message' => $e->getMessage(), 'value' => ''
  115. ));
  116. }
  117. }
  118. /**
  119. * @param Request $request
  120. * @param ResponseFactory $response
  121. * @return mixed
  122. * 单图上传
  123. */
  124. public function oneUpdateImage(Request $request, ResponseFactory $response)
  125. {
  126. if ($request->isMethod('post')) {
  127. $returnDatum = array('code' => 1, 'message' => '', 'url' => '');
  128. if ($request->hasFile('file_image')) {
  129. // 文件后缀判断
  130. $ext = $request->file("file_image")->extension();
  131. if (!in_array($ext, ["jpg", "jpeg", "gif", "png", "bmp", "webp"])) {
  132. $returnDatum['message'] = '上传图片仅支持png、jpg、jpeg、gif、bmp、webp后缀';
  133. return $response->json($returnDatum);
  134. }
  135. // 文件大小是否满足 2M = 2*1024*1024 B
  136. if ($request->file("file_image")->getClientSize() > 20971552) {
  137. $returnDatum['message'] = '上传图片大小超过5M';
  138. return $response->json($returnDatum);
  139. }
  140. // 上传过程是否出错
  141. if (!$request->file("file_image")->isValid()) {
  142. $returnDatum['message'] = '上传文件出错,请重试';
  143. return $response->json($returnDatum);
  144. }
  145. // 文件保存 生成一个唯一的文件名称
  146. $fileName = time() . str_random(4) . mt_rand(1000, 9999) . ".{$ext}";
  147. $pathA = 'image/' . date('y-m', time()) . '/' . $fileName;
  148. // oss 上传图片
  149. $oss = Storage::disk('public');
  150. $update = $oss->put($pathA, file_get_contents($request->{'file_image'}->getRealPath()));
  151. if ($update) {
  152. return $response->json(['success' => 0, 'message' => '', 'url' => $oss->url($pathA)]);
  153. } else {
  154. $returnDatum['message'] = '图片上传保存失败,请重试';
  155. return $response->json($returnDatum);
  156. }
  157. } else {
  158. $returnDatum['message'] = '请上传图片文件';
  159. return $response->json($returnDatum);
  160. }
  161. }
  162. }
  163. // 文件上传
  164. public function fileApkOrIos(Request $request, ResponseFactory $response)
  165. {
  166. $pathA = $request->input('path_a', 'down/android');
  167. $type = $request->input('type', 'oss');
  168. if ($request->isMethod('post')) {
  169. $returnDatum = array('code' => 1, 'message' => '', 'url' => '');
  170. if ($request->hasFile('file')) {
  171. // 文件后缀判断
  172. $ext = $request->file("file")->clientExtension();
  173. if (!in_array(strtolower($ext), ["apk", "bin", "mp4"])) {
  174. $returnDatum['message'] = '上传文件支持apk或ipa';
  175. return $response->json($returnDatum);
  176. }
  177. if ($ext == 'bin') {
  178. $ext = 'ipa';
  179. }
  180. // 文件大小是否满足 200M = 200*1024*1024 B
  181. if ($request->file("file")->getClientSize() > 2097155200) {
  182. $returnDatum['message'] = '上传文件最大200M';
  183. return $response->json($returnDatum);
  184. }
  185. // 上传过程是否出错
  186. if (!$request->file("file")->isValid()) {
  187. $returnDatum['message'] = '上传文件出错';
  188. return $response->json($returnDatum);
  189. }
  190. // 文件保存 生成一个唯一的文件名称
  191. if ($request->input('path_a', null)) {
  192. $fileName = time() . str_random(4) . mt_rand(1000, 9999) . ".{$ext}";
  193. } else {
  194. $fileName = $request->file("file")->getClientOriginalName();
  195. }
  196. if ($type !== 'local') {
  197. // oss文件上传
  198. $oss = Storage::disk('oss');
  199. $update = $oss->put($pathA . DIRECTORY_SEPARATOR . $fileName, file_get_contents($request->{'file'}->getRealPath()));
  200. return $response->json(['success' => 0, 'message' => '', 'url' => $oss->url($pathA . DIRECTORY_SEPARATOR . $fileName)]);
  201. } else {
  202. // 本地上传
  203. $path = $request->file("file")->storeAs($pathA, $fileName, 'public_storage');
  204. if ($path) {
  205. return $response->json(['success' => 0, 'message' => '', 'url' => '/storage/' . $path]);
  206. } else {
  207. $returnDatum['message'] = '图片上传保存失败';
  208. return $response->json($returnDatum);
  209. }
  210. }
  211. } else {
  212. $returnDatum['message'] = '请上传图片文件';
  213. return $response->json($returnDatum);
  214. }
  215. }
  216. }
  217. // 修改密码
  218. public function authPassword()
  219. {
  220. $user = Auth::user();
  221. return response()->view('admin.password_index', compact('user'))->header('Content-Type', 'text/html');
  222. }
  223. // 修改密码
  224. public function save()
  225. {
  226. $user = Auth::user();
  227. if ($user->{"email"} == 'admin@admin.com') {
  228. if (empty(request()->input('email', ''))) {
  229. return $this->response("邮箱必填!", 'error');
  230. }
  231. $pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/";
  232. // 验证游戏格式
  233. if (!preg_match($pattern, request()->input('email', ''))) {
  234. return $this->response("邮箱格式错误!", 'error');
  235. }
  236. $user->{"email"} = request()->input('email', '');
  237. }
  238. if (empty(request()->input('password', ''))) {
  239. return $this->response("邮箱必填!", 'error');
  240. }
  241. $user->{"password"} = bcrypt(request()->input('password'));
  242. $user->save();
  243. return $this->response(static::SUCCESS_MSG, 'success', '', route('admin.index'));
  244. }
  245. public function appAjax()
  246. {
  247. $msg = \App\Models\Message::where('created_at', '>=', session('msg_time', '2019-08-06 12:00:00'))->count();
  248. // $bonus = \App\Models\Bonu::where('created_at', '>=', session('bonus_time', '2019-08-06 12:00:00'))->count();
  249. //
  250. // $order = \App\Models\Order::where('created_at', '>=', session('order_time', '2019-08-06 12:00:00'))->count();
  251. return response()->json([
  252. 'code' => 0,
  253. 'data' => [
  254. 'msg' => $msg,
  255. 'bonus' => 0,
  256. 'order' => 0,
  257. ]
  258. ]);
  259. }
  260. function getCity()
  261. {
  262. $parent_id = request()->get('p_id', '');
  263. $select_id = request()->get('select_id', '');
  264. $categories = Area::where('parent_id', $parent_id)->select(['id', 'name'])->get();
  265. $option = [
  266. [
  267. 'value' => 0,
  268. 'label' => "请选择城市",
  269. 'disabled' => empty($select_id) ? true : false,
  270. 'notice' => '*'
  271. ]
  272. ];
  273. foreach ($categories as $category) {
  274. $option[] = [
  275. 'value' => $category->id,
  276. 'label' => $category->name,
  277. 'disabled' => $select_id == $category->id ? true : false,
  278. 'notice' => $category->name
  279. ];
  280. }
  281. return response()->json([
  282. 'status' => '1',
  283. 'code' => '1',
  284. 'message' => '',
  285. 'data' => $option
  286. ]);
  287. }
  288. function sendCode()
  289. {
  290. $phone = request()->input('phone', '');
  291. $server=SmsServer::creatServer();
  292. $ret=$server->sendCode($phone);
  293. if(empty($ret)){
  294. return response()->json([
  295. 'status' => 0,
  296. 'code' => 0,
  297. 'message' =>$server->getErrorMsg(),
  298. 'data' => []
  299. ]);
  300. }else{
  301. return response()->json([
  302. 'status' => '1',
  303. 'code' => '1',
  304. 'message' => '短信发送成功',
  305. 'data' => []
  306. ]);
  307. }
  308. }
  309. }