From fed1bf4e9dde465b8a1ef040320cd64c96dc9134 Mon Sep 17 00:00:00 2001 From: guozhigq Date: Tue, 1 Oct 2024 22:17:47 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=B5=84=E6=96=99=E7=BC=96=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/http/api.dart | 6 + lib/http/user.dart | 50 ++++++++ lib/pages/member/widgets/profile.dart | 3 +- lib/pages/mine_edit/controller.dart | 45 +++++++ lib/pages/mine_edit/index.dart | 4 + lib/pages/mine_edit/view.dart | 177 ++++++++++++++++++++++++++ lib/router/app_pages.dart | 3 + 7 files changed, 286 insertions(+), 2 deletions(-) create mode 100644 lib/pages/mine_edit/controller.dart create mode 100644 lib/pages/mine_edit/index.dart create mode 100644 lib/pages/mine_edit/view.dart diff --git a/lib/http/api.dart b/lib/http/api.dart index df0b9c85..0ad15f8f 100644 --- a/lib/http/api.dart +++ b/lib/http/api.dart @@ -592,4 +592,10 @@ class Api { /// 直播间记录 static const String liveRoomEntry = '${HttpString.liveBaseUrl}/xlive/web-room/v1/index/roomEntryAction'; + + /// 用户信息 + static const String accountInfo = '/x/member/web/account'; + + /// 更新用户信息 + static const String updateAccountInfo = '/x/member/web/update'; } diff --git a/lib/http/user.dart b/lib/http/user.dart index f4535905..207274e1 100644 --- a/lib/http/user.dart +++ b/lib/http/user.dart @@ -1,6 +1,7 @@ import 'dart:convert'; import 'dart:developer'; +import 'package:dio/dio.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:html/parser.dart'; import 'package:pilipala/models/video/later.dart'; @@ -537,4 +538,53 @@ class UserHttp { .toList() }; } + + static Future getAccountInfo() async { + var res = await Request().get( + Api.accountInfo, + data: {'web_location': 333.33}, + ); + if (res.data['code'] == 0) { + return { + 'status': true, + 'data': res.data['data'], + }; + } else { + return { + 'status': false, + 'data': {}, + 'mag': res.data['message'], + }; + } + } + + static Future updateAccountInfo({ + required String uname, + required String sign, + required String sex, + required String birthday, + }) async { + var res = await Request().post( + Api.updateAccountInfo, + data: { + 'uname': uname, + 'usersign': sign, + 'sex': sex, + 'birthday': birthday, + 'csrf': await Request.getCsrf(), + }, + options: Options(contentType: Headers.formUrlEncodedContentType), + ); + if (res.data['code'] == 0) { + return { + 'status': true, + 'msg': '更新成功', + }; + } else { + return { + 'status': false, + 'msg': res.data['message'], + }; + } + } } diff --git a/lib/pages/member/widgets/profile.dart b/lib/pages/member/widgets/profile.dart index 8c6385db..596ace46 100644 --- a/lib/pages/member/widgets/profile.dart +++ b/lib/pages/member/widgets/profile.dart @@ -1,5 +1,4 @@ import 'package:flutter/material.dart'; -import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:get/get.dart'; import 'package:pilipala/common/widgets/network_img_layer.dart'; import 'package:pilipala/models/live/item.dart'; @@ -233,7 +232,7 @@ class ProfilePanel extends StatelessWidget { if (ctr.ownerMid == ctr.mid && ctr.ownerMid != -1) ...[ TextButton( onPressed: () { - SmartDialog.showToast('功能开发中 💪'); + Get.toNamed('/mineEdit'); }, style: TextButton.styleFrom( padding: const EdgeInsets.only(left: 80, right: 80), diff --git a/lib/pages/mine_edit/controller.dart b/lib/pages/mine_edit/controller.dart new file mode 100644 index 00000000..9bc43246 --- /dev/null +++ b/lib/pages/mine_edit/controller.dart @@ -0,0 +1,45 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; +import 'package:get/get.dart'; +import 'package:hive/hive.dart'; +import 'package:pilipala/http/user.dart'; +import 'package:pilipala/utils/storage.dart'; + +class MineEditController extends GetxController { + Box userInfoCache = GStrorage.userInfo; + final formKey = GlobalKey(); + final TextEditingController unameCtr = TextEditingController(); + final TextEditingController useridCtr = TextEditingController(); + final TextEditingController signCtr = TextEditingController(); + final TextEditingController birthdayCtr = TextEditingController(); + String? sex; + dynamic userInfo; + + @override + void onInit() { + super.onInit(); + userInfo = userInfoCache.get('userInfoCache'); + } + + Future getAccountInfo() async { + var res = await UserHttp.getAccountInfo(); + if (res['status']) { + unameCtr.text = res['data']['uname']; + useridCtr.text = res['data']['userid']; + signCtr.text = res['data']['sign']; + birthdayCtr.text = res['data']['birthday']; + sex = res['data']['sex']; + } + return res; + } + + Future updateAccountInfo() async { + var res = await UserHttp.updateAccountInfo( + uname: unameCtr.text, + sign: signCtr.text, + sex: sex!, + birthday: birthdayCtr.text, + ); + SmartDialog.showToast(res['status'] ? res['msg'] : "更新失败:${res['msg']}"); + } +} diff --git a/lib/pages/mine_edit/index.dart b/lib/pages/mine_edit/index.dart new file mode 100644 index 00000000..88806448 --- /dev/null +++ b/lib/pages/mine_edit/index.dart @@ -0,0 +1,4 @@ +library mine_edit; + +export './controller.dart'; +export './view.dart'; diff --git a/lib/pages/mine_edit/view.dart b/lib/pages/mine_edit/view.dart new file mode 100644 index 00000000..700c707e --- /dev/null +++ b/lib/pages/mine_edit/view.dart @@ -0,0 +1,177 @@ +import 'package:flutter/material.dart'; +import 'package:get/get.dart'; + +import 'controller.dart'; + +class MineEditPage extends StatefulWidget { + const MineEditPage({super.key}); + + @override + State createState() => _MineEditPageState(); +} + +class _MineEditPageState extends State { + final MineEditController ctr = Get.put(MineEditController()); + late Future _futureBuilderFuture; + + @override + void initState() { + super.initState(); + _futureBuilderFuture = ctr.getAccountInfo(); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text('编辑资料'), + ), + body: SingleChildScrollView( + padding: const EdgeInsets.all(16.0), + child: FutureBuilder( + future: _futureBuilderFuture, + builder: ((context, snapshot) { + if (snapshot.connectionState == ConnectionState.done) { + if (snapshot.data == null) { + return const SizedBox(); + } + if (snapshot.data['status']) { + return Form( + key: ctr.formKey, + child: Column( + children: [ + // 用户头像 + // InkWell( + // onTap: () {}, + // child: CircleAvatar( + // radius: 50, + // backgroundColor: Colors.transparent, + // backgroundImage: + // NetworkImage(ctr.userInfo.face), // 替换为实际的头像路径 + // ), + // ), + const SizedBox(height: 24.0), + // 昵称 + TextFormField( + controller: ctr.unameCtr, + decoration: const InputDecoration( + labelText: '昵称', + border: OutlineInputBorder(), + ), + validator: (value) { + if (value == null || value.isEmpty) { + return '请输入昵称'; + } + return null; + }, + ), + const SizedBox(height: 20.0), + // 用户名 + TextFormField( + controller: ctr.useridCtr, + decoration: const InputDecoration( + labelText: '用户名', + border: OutlineInputBorder(), + enabled: false, + ), + readOnly: true, + validator: (value) { + if (value == null || value.isEmpty) { + return '请输入用户名'; + } + return null; + }, + ), + const SizedBox(height: 20.0), + // 签名 + TextFormField( + controller: ctr.signCtr, + decoration: const InputDecoration( + labelText: '签名', + border: OutlineInputBorder(), + ), + validator: (value) { + if (value == null || value.isEmpty) { + return '请输入签名'; + } + return null; + }, + ), + const SizedBox(height: 20.0), + // 性别 + DropdownButtonFormField( + value: ctr.sex, + decoration: const InputDecoration( + labelText: '性别', + border: OutlineInputBorder(), + ), + items: ['男', '女', '保密'].map((String value) { + return DropdownMenuItem( + value: value, + child: Text(value), + ); + }).toList(), + onChanged: (newValue) { + ctr.sex = newValue; + }, + validator: (value) { + if (value == null || value.isEmpty) { + return '请选择性别'; + } + return null; + }, + ), + const SizedBox(height: 20.0), + // 出生日期 + TextFormField( + controller: ctr.birthdayCtr, + decoration: const InputDecoration( + labelText: '出生日期', + border: OutlineInputBorder(), + ), + enabled: false, + readOnly: true, + onTap: () async { + // DateTime? pickedDate = await showDatePicker( + // context: context, + // initialDate: DateTime(1995, 12, 23), + // firstDate: DateTime(1900), + // lastDate: DateTime(2100), + // ); + // if (pickedDate != null) { + // ctr.birthdayCtr.text = + // "${pickedDate.toLocal()}".split(' ')[0]; + // } + }, + validator: (value) { + if (value == null || value.isEmpty) { + return '请选择出生日期'; + } + return null; + }, + ), + const SizedBox(height: 30.0), + // 提交按钮 + ElevatedButton( + onPressed: () { + if (ctr.formKey.currentState!.validate()) { + // 处理表单提交 + ctr.updateAccountInfo(); + } + }, + child: const Text('提交'), + ), + ], + ), + ); + } else { + return const SizedBox(); + } + } else { + return const SizedBox(); + } + })), + ), + ); + } +} diff --git a/lib/router/app_pages.dart b/lib/router/app_pages.dart index dec72b06..7a14b499 100644 --- a/lib/router/app_pages.dart +++ b/lib/router/app_pages.dart @@ -11,6 +11,7 @@ import 'package:pilipala/pages/message/like/index.dart'; import 'package:pilipala/pages/message/reply/index.dart'; import 'package:pilipala/pages/message/system/index.dart'; import 'package:pilipala/pages/mine/index.dart'; +import 'package:pilipala/pages/mine_edit/index.dart'; import 'package:pilipala/pages/opus/index.dart'; import 'package:pilipala/pages/read/index.dart'; import 'package:pilipala/pages/setting/pages/logs.dart'; @@ -196,6 +197,8 @@ class Routes { // 用户专栏 CustomGetPage( name: '/memberArticle', page: () => const MemberArticlePage()), + // 用户信息编辑 + CustomGetPage(name: '/mineEdit', page: () => const MineEditPage()), ]; }