set.vue
6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<template>
<view class="set-page">
<!-- 公共组件-每个页面必须引入 -->
<public-module></public-module>
<view @click="upAvatar" class="u-flex set-main">
<view class="main-image"><image class="image" :src="avatar || '/static/default.png'"></image></view>
<view class="main-right-image"><image class="image" src="../../static/arrow-right.png"></image></view>
</view>
<view class="u-m-t-20 basic-text"><text class="text">基本资料</text></view>
<view class="basic-main">
<u--form labelPosition="left" :model="myInfoModel" :rules="rules" ref="myInfoFormRef">
<u-form-item labelWidth="80px" label="真实姓名" prop="userInfo.realName" borderBottom ref="item1">
<u--input placeholder="请输入真实姓名" v-model="myInfoModel.userInfo.realName" border="none"></u--input>
</u-form-item>
<u-form-item labelWidth="80px" label="手机号码" prop="userInfo.phoneNumber" borderBottom ref="item1">
<u--input placeholder="请输入手机号码" v-model="myInfoModel.userInfo.phoneNumber" border="none"></u--input>
</u-form-item>
<u-form-item labelWidth="80px" label="用户账号 " prop="userInfo.username" borderBottom ref="item1">
<u--input placeholder="请输入用户账号 " v-model="myInfoModel.userInfo.username" border="none"></u--input>
</u-form-item>
<u-form-item labelWidth="80px" label="邮箱地址" prop="userInfo.email" borderBottom ref="item1">
<u--input placeholder="请输入邮箱地址" v-model="myInfoModel.userInfo.email" border="none"></u--input>
</u-form-item>
<u-form-item
@click="
showDate = true;
hideKeyboard();
"
labelWidth="80px"
label="有效期"
prop="userInfo.accountExpireTime"
borderBottom
ref="item1"
>
<u--input v-model="myInfoModel.userInfo.accountExpireTime" placeholder="请选择有效期" border="none"></u--input>
<u-datetime-picker
:formatter="formatter"
:show="showDate"
:value="datetime"
mode="dateTime"
closeOnClickOverlay
@confirm="dateConfirm"
@cancel="dateClose"
@close="dateClose"
></u-datetime-picker>
</u-form-item>
</u--form>
</view>
<view class="basic-bottom"><button class="submit" size="default" @click="onSubmitFunc" :style="{ background: PrimaryColor }">确认</button></view>
<!-- #ifdef MP -->
<view class="u-m-t-40"><text style="visibility: hidden;">#</text></view>
<!-- #endif -->
</view>
</template>
<script>
export default {
data() {
return {
PrimaryColor: '#0079fe', //主题色
myInfoModel: {
userInfo: {
realName: '',
phoneNumber: '',
username: '',
email: '',
accountExpireTime: ''
}
},
showDate: false,
dateTime: Number(new Date()),
avatar: '',
id: '',
rules: {
'userInfo.realName': [
{
type: 'string',
required: true,
message: '请填写姓名',
trigger: ['blur', 'change']
},
{
validator: (rule, value, callback) => {
return uni.$u.test.chinese(value);
},
message: '姓名必须为中文',
trigger: ['change', 'blur']
}
],
'userInfo.phoneNumber': [
{
type: 'string',
required: true,
message: '请填写手机号码',
trigger: ['blur', 'change']
},
{
// 此为同步验证,可以直接返回true或者false,如果是异步验证,稍微不同,见下方说明
validator: (rule, value, callback) => {
return uni.$u.test.mobile(value);
},
message: '手机必须为数字',
trigger: ['change', 'blur']
}
],
'userInfo.email': [
{
type: 'string',
required: true,
message: '请填写邮箱',
trigger: ['blur', 'change']
},
{
validator: (rule, value, callback) => {
return uni.$u.test.email(value);
},
message: '邮箱必须带有@',
trigger: ['change', 'blur']
}
]
}
};
},
onLoad(e) {
if (e.data !== null) {
let params = JSON.parse(e.data);
this.myInfoModel.userInfo.realName = params.data.realName;
this.myInfoModel.userInfo.phoneNumber = params.data.phoneNumber;
this.myInfoModel.userInfo.username = params.data.username;
this.myInfoModel.userInfo.email = params.data.email;
this.myInfoModel.userInfo.accountExpireTime = params.data.accountExpireTime;
this.avatar = params.data.avatar == undefined ? '../../static/avatar-test.png' : params.data.avatar;
this.id = params.data.userId;
}
},
methods: {
// 修改头像
upAvatar() {
var that = this;
uni.chooseImage({
count: 1,
sourceType: ['camera', 'album'],
success: res => {
const tempFilePaths = res.tempFilePaths;
//限制上传的图片大小不超过2M
let resSize = res.tempFiles[0].size;
if (resSize > 2097152) {
uni.showToast({
title: '上传的图片大小不能超过2M',
icon: 'none',
duration: 2000,
mask: true
});
return;
}
//获取图片扩展名
const fileTxt = res.tempFilePaths[0].split('.').pop();
const judgeType = fileTxt == 'jpg' || fileTxt == 'jpeg' || fileTxt == 'png';
if (!judgeType) {
uni.showToast({
title: '请上传指定图片类型(jpg、jpeg、png)',
icon: 'none',
duration: 2000,
mask: true
});
return;
}
uni.$u.http
.upload('/yt/oss/upload', {
filePath: tempFilePaths[0],
name: 'file'
})
.then(res => {
if (res) {
that.avatar = res.fileStaticUri;
return uni.$u.toast('头像上传成功');
}
});
}
});
},
onSubmitFunc() {
this.$refs.myInfoFormRef
.validate()
.then(res => {
let httpData = {
avatar: this.avatar,
email: this.myInfoModel.userInfo.email,
id: this.id,
phoneNumber: this.myInfoModel.userInfo.phoneNumber,
realName: this.myInfoModel.userInfo.realName
};
uni.$u.http
.put('/yt/user/center', httpData)
.then(res => {
if (res) {
uni.showToast({
title: '更新个人资料成功~',
icon: 'none'
});
setTimeout(() => {
uni.navigateBack({
delta: 1
});
}, 500);
}
})
.catch(e => {
uni.$u.toast(e.data.message);
});
})
.catch(errors => {
uni.$u.toast('校验失败');
});
},
dateClose() {
this.showDate = false;
},
dateConfirm(e) {
this.showDate = false;
this.myInfoModel.userInfo.accountExpireTime = uni.$u.timeFormat(e.value, 'yyyy-mm-dd hh:MM:ss');
},
//格式化日期
formatter(type, value) {
if (type === 'year') {
return `${value}年`;
}
if (type === 'month') {
return `${value}月`;
}
if (type === 'day') {
return `${value}日`;
}
if (type === 'hour') {
return `${value}时`;
}
if (type === 'minute') {
return `${value}分`;
}
return value;
},
//隐藏输入框
hideKeyboard() {
uni.hideKeyboard();
}
}
};
</script>
<style lang="scss" scoped>
@import './static/set.scss';
</style>