123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- <?php
- namespace App\Http\Controllers\Admin;
- use App\Http\Controllers\BaseController;
- use App\Servers\SmsServer;
- use Illuminate\Contracts\Routing\ResponseFactory;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Storage;
- class IndexController extends BaseController
- {
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
- *
- * 后台首页
- */
- public function index()
- {
- // request()->session()->put('aaa','12122121');
- // request()->session()->save();
- // dd(request()->session()->all());
- return view('admin.index');
- }
- /**
- * @param Request $request
- * @param ResponseFactory $response
- * @return \Illuminate\Http\JsonResponse
- *
- * bootstrap-table 表单 yeOrNo 数据处理
- * 处理请求 yes or no 参数 (全局函数)
- */
- public function switchYesOrNo(Request $request, ResponseFactory $response)
- {
- try {
- if ($request->isMethod('post') && $request->ajax()) {
- $id = $request->id;
- $field = $request->field;
- $table = str_plural($request->table);
- $pk = $request->input('pk', 'id');
- $model = DB::table($table);
- $flag = false;
- $updateFieldData = 0;
- $datum = $model->select($field)->where($pk, $id)->first();
- if ($datum) {
- $datum->{$field} == 1 ? $updateFieldData = 0 : $updateFieldData = 1;
- // 更新指定表的指定字段 ::=> {$table}更新表 -> {$field}更新字段 -> {$updateFieldData}更新为指定值
- $result = $model
- ->where($pk, $id)
- ->update([
- $field => $updateFieldData
- ]);
- $result ? $flag = true : null;
- }
- if ($flag) {
- if ($table == 'members' && $field == 'is_show' && $updateFieldData == 0) {
- }
- }
- $flag ?
- $returnDatum = ['status' => 0, 'message' => '', 'value' => $updateFieldData]
- :
- $returnDatum = ['status' => 1, 'message' => '操作失败,请重试!', 'value' => $updateFieldData];
- if ($request->wantsJson()) {
- return $response->json($returnDatum);
- }
- }
- } catch (\Exception $e) {
- return $response->json(
- array('status' => 1, 'message' => $e->getMessage(), 'value' => '')
- );
- }
- }
- /**
- * @param Request $request
- * @param ResponseFactory $response
- * @return \Illuminate\Http\JsonResponse
- * 修改sort字段 (全局函数)
- */
- public function updateSort(Request $request, ResponseFactory $response)
- {
- try {
- if ($request->isMethod('post') && $request->ajax()) {
- $id = $request->id;
- $field = $request->field;
- // 表名称
- $table = $request->table;
- $pk = $request->input('pk', 'id');
- $value = $request->value;
- $model = DB::table($table);
- $datum = $model->where($pk, $id)->first();
- if (is_null($datum)) {
- // 该数据不存在
- $returnDatum = ['status' => 2, 'message' => '数据不存在', 'value' => ''];
- } else {
- // 修改值 sort
- $result = $model->where($pk, $id)->update(array(
- $field => $value
- ));
- if ($result) {
- $returnDatum = ['status' => 0, 'message' => '', 'value' => $value];
- } else {
- $returnDatum = ['status' => 1, 'message' => '保存失败', 'value' => ''];
- }
- }
- if ($request->wantsJson()) {
- return $response->json($returnDatum);
- }
- }
- } catch (\Exception $e) {
- return $response->json(array(
- 'status' => 1, 'message' => $e->getMessage(), 'value' => ''
- ));
- }
- }
- /**
- * @param Request $request
- * @param ResponseFactory $response
- * @return mixed
- * 单图上传
- */
- public function oneUpdateImage(Request $request, ResponseFactory $response)
- {
- if ($request->isMethod('post')) {
- $returnDatum = array('code' => 1, 'message' => '', 'url' => '');
- if ($request->hasFile('file_image')) {
- // 文件后缀判断
- $ext = $request->file("file_image")->extension();
- if (!in_array($ext, ["jpg", "jpeg", "gif", "png", "bmp", "webp"])) {
- $returnDatum['message'] = '上传图片仅支持png、jpg、jpeg、gif、bmp、webp后缀';
- return $response->json($returnDatum);
- }
- // 文件大小是否满足 2M = 2*1024*1024 B
- if ($request->file("file_image")->getClientSize() > 20971552) {
- $returnDatum['message'] = '上传图片大小超过5M';
- return $response->json($returnDatum);
- }
- // 上传过程是否出错
- if (!$request->file("file_image")->isValid()) {
- $returnDatum['message'] = '上传文件出错,请重试';
- return $response->json($returnDatum);
- }
- // 文件保存 生成一个唯一的文件名称
- $fileName = time() . str_random(4) . mt_rand(1000, 9999) . ".{$ext}";
- $pathA = 'image/' . date('y-m', time()) . '/' . $fileName;
- // oss 上传图片
- $oss = Storage::disk('public');
- $update = $oss->put($pathA, file_get_contents($request->{'file_image'}->getRealPath()));
- if ($update) {
- return $response->json(['success' => 0, 'message' => '', 'url' => $oss->url($pathA)]);
- } else {
- $returnDatum['message'] = '图片上传保存失败,请重试';
- return $response->json($returnDatum);
- }
- } else {
- $returnDatum['message'] = '请上传图片文件';
- return $response->json($returnDatum);
- }
- }
- }
- // 文件上传
- public function fileApkOrIos(Request $request, ResponseFactory $response)
- {
- $pathA = $request->input('path_a', 'down/android');
- $type = $request->input('type', 'oss');
- if ($request->isMethod('post')) {
- $returnDatum = array('code' => 1, 'message' => '', 'url' => '');
- if ($request->hasFile('file')) {
- // 文件后缀判断
- $ext = $request->file("file")->clientExtension();
- if (!in_array(strtolower($ext), ["apk", "bin", "mp4"])) {
- $returnDatum['message'] = '上传文件支持apk或ipa';
- return $response->json($returnDatum);
- }
- if ($ext == 'bin') {
- $ext = 'ipa';
- }
- // 文件大小是否满足 200M = 200*1024*1024 B
- if ($request->file("file")->getClientSize() > 2097155200) {
- $returnDatum['message'] = '上传文件最大200M';
- return $response->json($returnDatum);
- }
- // 上传过程是否出错
- if (!$request->file("file")->isValid()) {
- $returnDatum['message'] = '上传文件出错';
- return $response->json($returnDatum);
- }
- // 文件保存 生成一个唯一的文件名称
- if ($request->input('path_a', null)) {
- $fileName = time() . str_random(4) . mt_rand(1000, 9999) . ".{$ext}";
- } else {
- $fileName = $request->file("file")->getClientOriginalName();
- }
- if ($type !== 'local') {
- // oss文件上传
- $oss = Storage::disk('oss');
- $update = $oss->put($pathA . DIRECTORY_SEPARATOR . $fileName, file_get_contents($request->{'file'}->getRealPath()));
- return $response->json(['success' => 0, 'message' => '', 'url' => $oss->url($pathA . DIRECTORY_SEPARATOR . $fileName)]);
- } else {
- // 本地上传
- $path = $request->file("file")->storeAs($pathA, $fileName, 'public_storage');
- if ($path) {
- return $response->json(['success' => 0, 'message' => '', 'url' => '/storage/' . $path]);
- } else {
- $returnDatum['message'] = '图片上传保存失败';
- return $response->json($returnDatum);
- }
- }
- } else {
- $returnDatum['message'] = '请上传图片文件';
- return $response->json($returnDatum);
- }
- }
- }
- // 修改密码
- public function authPassword()
- {
- $user = Auth::user();
- return response()->view('admin.password_index', compact('user'))->header('Content-Type', 'text/html');
- }
- // 修改密码
- public function save()
- {
- $user = Auth::user();
- if ($user->{"email"} == 'admin@admin.com') {
- if (empty(request()->input('email', ''))) {
- return $this->response("邮箱必填!", 'error');
- }
- $pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/";
- // 验证游戏格式
- if (!preg_match($pattern, request()->input('email', ''))) {
- return $this->response("邮箱格式错误!", 'error');
- }
- $user->{"email"} = request()->input('email', '');
- }
- if (empty(request()->input('password', ''))) {
- return $this->response("邮箱必填!", 'error');
- }
- $user->{"password"} = bcrypt(request()->input('password'));
- $user->save();
- return $this->response(static::SUCCESS_MSG, 'success', '', route('admin.index'));
- }
- public function appAjax()
- {
- $msg = \App\Models\Message::where('created_at', '>=', session('msg_time', '2019-08-06 12:00:00'))->count();
- // $bonus = \App\Models\Bonu::where('created_at', '>=', session('bonus_time', '2019-08-06 12:00:00'))->count();
- //
- // $order = \App\Models\Order::where('created_at', '>=', session('order_time', '2019-08-06 12:00:00'))->count();
- return response()->json([
- 'code' => 0,
- 'data' => [
- 'msg' => $msg,
- 'bonus' => 0,
- 'order' => 0,
- ]
- ]);
- }
- function getCity()
- {
- $parent_id = request()->get('p_id', '');
- $select_id = request()->get('select_id', '');
- $categories = Area::where('parent_id', $parent_id)->select(['id', 'name'])->get();
- $option = [
- [
- 'value' => 0,
- 'label' => "请选择城市",
- 'disabled' => empty($select_id) ? true : false,
- 'notice' => '*'
- ]
- ];
- foreach ($categories as $category) {
- $option[] = [
- 'value' => $category->id,
- 'label' => $category->name,
- 'disabled' => $select_id == $category->id ? true : false,
- 'notice' => $category->name
- ];
- }
- return response()->json([
- 'status' => '1',
- 'code' => '1',
- 'message' => '',
- 'data' => $option
- ]);
- }
- function sendCode()
- {
- $phone = request()->input('phone', '');
- $server=SmsServer::creatServer();
- $ret=$server->sendCode($phone);
- if(empty($ret)){
- return response()->json([
- 'status' => 0,
- 'code' => 0,
- 'message' =>$server->getErrorMsg(),
- 'data' => []
- ]);
- }else{
- return response()->json([
- 'status' => '1',
- 'code' => '1',
- 'message' => '短信发送成功',
- 'data' => []
- ]);
- }
- }
- }
|