Leon 2 rokov pred
rodič
commit
0852319b37

+ 14 - 1
src/pages/account/EditPage.tsx

@@ -1,4 +1,5 @@
 import { update } from "@/services/user";
+import { updateSuccess } from "@/store/user";
 import { Input, View, Text } from "@tarojs/components";
 import Taro from "@tarojs/taro";
 import { useState } from "react";
@@ -21,7 +22,19 @@ export default function Page() {
             })
             return;
         }
-        dispatch(update({nickname:value}) as any)
+        update({ nickname: value }).then(res => {
+            dispatch(updateSuccess({ nickname: value }));
+            Taro.showToast({
+                title: '更新成功',
+                icon: 'none'
+            })
+
+            setTimeout(() => {
+                Taro.navigateBack()
+            }, 2000)
+
+        })
+        // dispatch(update({nickname:value}).then() as any)
     }
 
     return <View>

+ 6 - 3
src/pages/account/Profile.tsx

@@ -38,7 +38,8 @@ export default function Page() {
 
     }
 
-    function tapProfile() {
+    function tapProfile(e) {
+        e.stopPropagation()
         if (user.isLogin) {
             Taro.navigateTo({
                 url: '/pages/account/ProfileSetting'
@@ -51,7 +52,7 @@ export default function Page() {
         }
     }
 
-    function tapBalance() {
+    function tapBalance(e) {
         if (user.isLogin) {
 
         }
@@ -59,14 +60,16 @@ export default function Page() {
             Taro.navigateTo({
                 url: '/pages/account/ChooseAuth'
             })
+            e.stopPropagation()
         }
 
     }
 
-    function goSetting() {
+    function goSetting(e) {
         Taro.navigateTo({
             url: '/pages/account/Setting'
         })
+        e.stopPropagation()
     }
 
     function detail() {

+ 53 - 2
src/pages/account/ProfileSetting.tsx

@@ -1,5 +1,5 @@
 import Buttons from "@/components/basic/Buttons";
-import { clear, logout } from "@/services/user";
+import { clear, logout, update } from "@/services/user";
 import { View, Text, Image, Button } from "@tarojs/components";
 import { useDispatch, useSelector } from "react-redux";
 import './ProfileSetting.scss'
@@ -7,6 +7,8 @@ import Box from "@/components/layout/Box";
 import Taro from "@tarojs/taro";
 import { useEffect } from "react";
 import { useTranslation } from "react-i18next";
+import { baseUrl } from "@/services/http/api";
+import { updateSuccess } from "@/store/user";
 
 export default function Page() {
     const dispatch = useDispatch();
@@ -25,11 +27,60 @@ export default function Page() {
         })
     }
 
+    function chooseAvatar(e) {
+        const { avatarUrl } = e.detail
+        var dot = avatarUrl.lastIndexOf('.')
+        var fileExt = dot > 0 ? avatarUrl.substring(dot) : ''
+        // console.log(avatarUrl)
+        Taro.request({
+            method: 'GET',
+            url: `${baseUrl}/api/thirdparty/aliyun/oss-form-upload`,
+            header: {
+                'Authorization': 'bearer ' + global.token
+            },
+            data: {
+                type: 'AVATAR',
+                file_ext: fileExt
+            },
+            success: (rsp) => {
+                debugger
+                Taro.uploadFile({
+                    url: rsp.data.upload_url,
+                    filePath: avatarUrl,
+                    name: 'file',
+                    formData: rsp.data.fields,
+                    success: rlt => {
+                        console.log(rlt)
+                        uploadAvatar(rsp.data.view_url)
+                        // _this.changeAvatar(rsp.data.view_url);
+                    },
+                    fail: rlt => {
+                        Taro.showModal({
+                            content: rlt.errMsg
+                        })
+                    }
+                })
+            }
+        })
+    }
+
+    function uploadAvatar(url) {
+        update({ avatar: url }).then(res => {
+            dispatch(updateSuccess({ avatar: url }));
+            Taro.showToast({
+                title: '更新成功',
+                icon: 'none'
+            })
+
+
+        })
+    }
+
     return <View className="container">
         <View className="row">
             <Text>头像</Text>
             <Image src={user.avatar} className="avatar" />
-            <Button className="btn" openType="chooseAvatar"></Button>
+            <Button className="btn" openType="chooseAvatar" onChooseAvatar={chooseAvatar}></Button>
         </View>
 
 

+ 13 - 10
src/services/user.tsx

@@ -1,20 +1,20 @@
 import Taro from '@tarojs/taro'
-import { API_OAUTH_LOGIN, API_REGISTER,API_LOGIN, API_LOGOUT, API_CLEAR_USER, API_USER_INFO,API_CHECK_UNIQUE } from './http/api'
+import { API_OAUTH_LOGIN, API_REGISTER, API_LOGIN, API_LOGOUT, API_CLEAR_USER, API_USER_INFO, API_CHECK_UNIQUE } from './http/api'
 import { request } from './http/request'
 import { clearSuccess, getInfoSuccess, loginSuccess, logoutSuccess, registerSuccess, updateSuccess } from '@/store/user'
 
 
-export const checkUnique = (username?: string,email?:string) => {
+export const checkUnique = (username?: string, email?: string) => {
     return new Promise((resolve, reject) => {
         request({
-            url: API_CHECK_UNIQUE, method: 'GET', data: { username:username,email:email }
+            url: API_CHECK_UNIQUE, method: 'GET', data: { username: username, email: email }
         }).then(res => {
             resolve(res);
             console.log(res);
             // dispatch(loginSuccess(res));
         })
     })
-    
+
 }
 
 export const login = (username: string, password: string) => (dispatch: any) => {
@@ -25,7 +25,7 @@ export const login = (username: string, password: string) => (dispatch: any) =>
     })
 }
 
-export const wxLogin = (code: string/*, encryptedData: string, iv: string*/) => (dispatch: any)=> {
+export const wxLogin = (code: string/*, encryptedData: string, iv: string*/) => (dispatch: any) => {
     request({
         url: API_OAUTH_LOGIN, method: 'POST',
         data: {
@@ -90,11 +90,14 @@ export const getInfo = () => (dispatch: any) => {
 }
 
 
-export const update = (params:any) => (dispatch: any) => {
-    request({
-        url: API_USER_INFO, method: 'POST', data: params
-    }).then(res => {
-        dispatch(updateSuccess(params));
+export const update = (params: any) => {
+    return new Promise((resolve, reject) => {
+        request({
+            url: API_USER_INFO, method: 'POST', data: params
+        }).then(res => {
+            // dispatch(updateSuccess(params));
+            resolve(res)
+        })
     })
 }