康巴易测肤/伤疤uniapp小程序类
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

292 lines
6.9KB

  1. <template>
  2. <view class="info">
  3. <view class="main">
  4. <view class="avatar">
  5. <button open-type="chooseAvatar" class="avatar-btn" @chooseavatar="chooseavatar"
  6. hover-class="none">
  7. <u-avatar :src="avatarSrcLoca" size="70"></u-avatar>
  8. </button>
  9. </view>
  10. <view class="form-box">
  11. <u--form labelPosition="left" :model="form" :rules="rules" ref="uForm">
  12. <u-form-item required prop="name" borderBottom>
  13. <u--input v-model="form.name" type="text" maxlength="15" border placeholder="请输入昵称"></u--input>
  14. </u-form-item>
  15. <u-form-item required prop="gender" borderBottom @click="showGender = true;">
  16. <u--input v-model="form.gender" disabled disabledColor="#ffffff" placeholder="请选择性别"
  17. border></u--input>
  18. <u-icon slot="right" name="arrow-right"></u-icon>
  19. </u-form-item>
  20. <u-form-item required labelWidth="80" prop="date" borderBottom @click="showDate = true">
  21. <u--input v-model="form.date" disabled disabledColor="#ffffff" placeholder="请选择出生日期"
  22. border></u--input>
  23. <u-icon slot="right" name="arrow-right"></u-icon>
  24. </u-form-item>
  25. <u-form-item borderBottom>
  26. <u-input v-model="form.height" border placeholder="请输入身高" type="number" maxlength="3"></u-input>
  27. </u-form-item>
  28. <u-form-item borderBottom>
  29. <u--input v-model="form.weight" border placeholder="请输入体重" type="number"
  30. maxlength="3"></u--input>
  31. </u-form-item>
  32. </u--form>
  33. </view>
  34. <!-- 性别弹窗 -->
  35. <u-action-sheet :actions="genderList" @select="selectClickGender" :show="showGender"></u-action-sheet>
  36. <!-- 出生日期弹窗 -->
  37. <u-datetime-picker ref="datetimePicker" :closeOnClickOverlay="true" :show="showDate" v-model="pickerDate" mode="date" :minDate="minDate"
  38. :maxDate="maxDate" confirmColor="#E19F4B" @confirm="onConfirmDate" @cancel="onCancelDate"
  39. :formatter="formatter"></u-datetime-picker>
  40. <view class="action-box">
  41. <view class="btn-box">
  42. <u-button @click="onSubmit()" shape="circle" :color="bgthemeColor" type="info" size="large">
  43. <text :style="{ color: themeColor, fontSize: '32rpx', fontWeight: 'bold' }">{{actionText}}</text>
  44. </u-button>
  45. </view>
  46. </view>
  47. </view>
  48. </view>
  49. </template>
  50. <script>
  51. export default {
  52. data() {
  53. return {
  54. checked: true,
  55. showDate: false,
  56. showGender: false,
  57. pickerDate: Number(new Date()),
  58. form: {
  59. name: '',
  60. gender: '',
  61. date: '',
  62. height: '',
  63. weight: '',
  64. avatarSrc: ''
  65. },
  66. genderList: [
  67. {
  68. name: '男',
  69. subname: "",
  70. color: '#000',
  71. fontSize: '16',
  72. },
  73. {
  74. name: '女',
  75. subname: "",
  76. color: '#000',
  77. fontSize: '16'
  78. },
  79. ],
  80. rules: {
  81. name: [{
  82. required: true,
  83. message: '请输入昵称',
  84. trigger: ['blur', 'change']
  85. }],
  86. gender: [{
  87. required: true,
  88. message: '请选择性别',
  89. trigger: ['blur', 'change']
  90. }],
  91. date: [{
  92. required: true,
  93. message: '请选择出生日期',
  94. trigger: ['blur', 'change']
  95. }],
  96. },
  97. action: '',
  98. avatarSrcLoca: '' || require('../../static/tabbar/user_default.png')
  99. }
  100. },
  101. onReady() {
  102. //如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则。
  103. this.$refs.uForm.setRules(this.rules)
  104. this.$refs.datetimePicker.setFormatter(this.formatter)
  105. },
  106. computed: {
  107. themeColor() {
  108. return this.$themeColor
  109. },
  110. bgthemeColor() {
  111. return '#333'
  112. },
  113. minDate() {
  114. const now = new Date();
  115. const minDate = new Date(now.getFullYear() - 50, now.getMonth(), now.getDate());
  116. return minDate
  117. },
  118. maxDate() {
  119. const now1 = new Date();
  120. const maxDate = new Date(now1.getFullYear() - 10, now1.getMonth(), now1.getDate());
  121. return maxDate.getTime()
  122. },
  123. actionText() {
  124. return this.action === 'update' ? '修改资料': '创建用户'
  125. },
  126. userInfo() {
  127. return this.$store.state.user.userInfo
  128. },
  129. },
  130. onLoad(props) {
  131. if(props) {
  132. this.action = props.action;
  133. if(props.action === 'update' && this.userInfo) {
  134. this.form = {... this.userInfo}
  135. this.pickerDate = new Date(this.userInfo.date)
  136. this.avatarSrcLoca = this.userInfo.avatarSrc
  137. }
  138. console.log("this.action", this.action);
  139. }
  140. },
  141. methods: {
  142. formatter(type, value) {
  143. if (type === 'year') {
  144. return `${value}年`
  145. }
  146. if (type === 'month') {
  147. return `${value}月`
  148. }
  149. if (type === 'day') {
  150. return `${value}日`
  151. }
  152. return value
  153. },
  154. onSubmit() {
  155. console.log("this.form", this.form);
  156. let that = this;
  157. this.$refs.uForm.validate().then( async valid => {
  158. if(valid) {
  159. uni.showLoading({
  160. title: that.action === 'update' ? '修改中': '创建中',
  161. mask: true,
  162. })
  163. setTimeout(() => {
  164. // 缓存到本地
  165. that.$store.commit('SET_USERINFO', that.form);
  166. that.$store.commit('saveAll');
  167. uni.showToast({
  168. title: that.action === 'update' ? '修改成功' : "创建成功",
  169. icon: 'success'
  170. });
  171. }, 1000)
  172. setTimeout(() => {
  173. uni.switchTab({
  174. url: '/pages/index/index',
  175. })
  176. }, 2000)
  177. }
  178. }).catch(errors => {
  179. console.log("errors", errors);
  180. })
  181. },
  182. selectClickGender(e) {
  183. console.log("e", e);
  184. this.form.gender = e.name;
  185. this.showGender = false
  186. },
  187. onConfirmDate(date) {
  188. console.log("date", date);
  189. this.form.date = this.$util.formateDate('yyyy-mm-dd', date.value);
  190. this.showDate = false;
  191. },
  192. onCancelDate() {
  193. this.showDate = false;
  194. },
  195. chooseavatar(e) {
  196. this.avatarSrcLoca = e.detail.avatarUrl; // 获取临时头像路径
  197. this.form.avatarSrc = e.detail.avatarUrl;
  198. console.log('头像路径:', this.avatarSrcLoca);
  199. }
  200. }
  201. }
  202. </script>
  203. <style lang="scss">
  204. .info {
  205. position: relative;
  206. height: 100vh;
  207. width: 100vw;
  208. overflow: hidden;
  209. .main {
  210. padding: 40rpx;
  211. .avatar {
  212. width: 100%;
  213. display: flex;
  214. justify-content: center;
  215. align-items: center;
  216. padding: 20rpx 0;
  217. .avatar-btn {
  218. height: 140rpx;
  219. width: 140rpx;
  220. border-radius: 50%;
  221. padding: 0;
  222. }
  223. }
  224. .form-box {
  225. .u-form-item {
  226. padding: 0 20rpx;
  227. border-radius: 50rpx;
  228. border: 1rpx solid $uni-text-color-grey;
  229. margin: 20rpx 0;
  230. }
  231. .u-form-item__body__left {
  232. height: 80rpx !important;
  233. width: 10rpx !important;
  234. .u-form-item__body__left__content__required {
  235. left: -10rpx !important;
  236. top: -10rpx !important;
  237. }
  238. }
  239. .u-form-item__body__right__message {
  240. margin-left: 20rpx !important;
  241. }
  242. }
  243. .action-box {
  244. width: 100%;
  245. position: fixed;
  246. bottom: 30rpx;
  247. left: 0;
  248. .btn-box {
  249. padding: 0 40rpx;
  250. margin-top: 20rpx;
  251. display: flex;
  252. justify-content: center;
  253. align-items: center;
  254. text {
  255. font-size: 36rpx;
  256. }
  257. .black {
  258. color: $uni-text-color;
  259. }
  260. .gray {
  261. color: $uni-text-color-grey;
  262. }
  263. }
  264. }
  265. }
  266. }
  267. </style>