敢为人鲜小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

281 lines
5.3 KiB

5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
  1. <template>
  2. <view class="profile-page">
  3. <!-- 头部导航栏 -->
  4. <navbar title="资料修改" bgColor="#019245" color="#fff" leftClick @leftClick="$utils.navigateBack" />
  5. <!-- 基本资料区域 -->
  6. <view class="main-content">
  7. <view class="section-title">
  8. <view class="title-indicator"></view>
  9. <text>基本资料</text>
  10. </view>
  11. <!-- 头像选择区域 -->
  12. <view class="avatar-section">
  13. <button class="chooseAvatar" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
  14. <image class="avatar-img" :src="form.headImage" mode="aspectFill" />
  15. <!-- <image class="avatar-img" src="https://img.yzcdn.cn/vant/ipad.png" mode="aspectFill"></image> -->
  16. </button>
  17. <text class="avatar-hint">点击更换头像</text>
  18. </view>
  19. <!-- 表单区域 -->
  20. <view class="form-section">
  21. <view class="form-item">
  22. <text class="label">昵称</text>
  23. <input class="input" type="nickname" placeholder="请输入" v-model="form.nickName" id="nickName"
  24. placeholderStyle="color: #999; text-align: right;" />
  25. </view>
  26. <view class="form-item">
  27. <text class="label">手机号</text>
  28. <input class="input" type="tel" placeholder="请输入" v-model="form.phone"
  29. placeholderStyle="color: #999; text-align: right;" />
  30. </view>
  31. </view>
  32. <!-- 保存按钮 -->
  33. <view class="save-button" @tap="submit">
  34. <text>保存</text>
  35. </view>
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. import { mapState } from 'vuex'
  41. import navbar from '@/components/base/navbar.vue'
  42. export default {
  43. components: {
  44. navbar
  45. },
  46. data() {
  47. return {
  48. form: {
  49. headImage: '',
  50. nickName: '',
  51. phone: ''
  52. },
  53. back: '',
  54. }
  55. },
  56. computed: {
  57. ...mapState(['userInfo']),
  58. },
  59. onLoad({ back }) {
  60. this.back = back
  61. },
  62. onShow() {
  63. this.getUserInfo()
  64. },
  65. methods: {
  66. onChooseAvatar(res) {
  67. let self = this
  68. self.$Oss.ossUpload(res.detail.avatarUrl)
  69. .then(url => {
  70. self.form.headImage = url
  71. })
  72. },
  73. getUserInfo() {
  74. // 使用mock数据
  75. this.form.headImage = this.userInfo.headImage || this.form.headImage
  76. this.form.nickName = this.userInfo.nickName || this.form.nickName
  77. this.form.phone = this.userInfo.phone || this.form.phone
  78. },
  79. submit() {
  80. let self = this
  81. // 之所以手动获取dom的input值 而不是v-model 是为了保证跨平台安全生效
  82. uni.createSelectorQuery().in(this)
  83. .select("#nickName")
  84. .fields({
  85. properties: ["value"],
  86. })
  87. .exec((res) => {
  88. const nickName = res?.[0]?.value
  89. self.form.nickName = nickName
  90. if (self.$utils.verificationAll(self.form, {
  91. headImage: '请选择头像',
  92. nickName: '请填写昵称',
  93. phone: '请填写手机号'
  94. })) {
  95. return
  96. }
  97. if (!self.$utils.verificationPhone(self.form.phone)) {
  98. uni.showToast({
  99. icon: 'none',
  100. title: '请填写正确的手机号'
  101. })
  102. return
  103. }
  104. this.$api('updateUser', {
  105. ...this.form,
  106. }, res => {
  107. if (res.code == 200) {
  108. uni.showToast({
  109. title: '保存成功',
  110. icon: 'success'
  111. })
  112. this.$store.commit('getUserInfo')
  113. // 返回上一页
  114. setTimeout(() => {
  115. this.$utils.navigateBack()
  116. }, 1500)
  117. }
  118. })
  119. })
  120. }
  121. }
  122. }
  123. </script>
  124. <style lang="scss" scoped>
  125. .profile-page {
  126. min-height: 100vh;
  127. background-color: #f5f5f5;
  128. }
  129. .nav-bar {
  130. height: 180rpx;
  131. background-color: #019245;
  132. display: flex;
  133. align-items: center;
  134. justify-content: space-between;
  135. padding: 0 30rpx;
  136. padding-top: 60rpx;
  137. box-sizing: border-box;
  138. color: #fff;
  139. .back-btn {
  140. width: 60rpx;
  141. height: 60rpx;
  142. display: flex;
  143. align-items: center;
  144. }
  145. .nav-title {
  146. font-size: 36rpx;
  147. font-weight: 500;
  148. }
  149. .right-actions {
  150. display: flex;
  151. align-items: center;
  152. .icon-divider {
  153. width: 2rpx;
  154. height: 30rpx;
  155. background-color: rgba(255, 255, 255, 0.5);
  156. margin: 0 20rpx;
  157. }
  158. }
  159. }
  160. .main-content {
  161. padding: 30rpx;
  162. }
  163. .section-title {
  164. display: flex;
  165. align-items: center;
  166. margin-bottom: 30rpx;
  167. .title-indicator {
  168. width: 8rpx;
  169. height: 32rpx;
  170. background-color: $uni-color;
  171. margin-right: 15rpx;
  172. border-radius: 4rpx;
  173. }
  174. text {
  175. font-size: 32rpx;
  176. font-weight: 500;
  177. color: #333;
  178. }
  179. }
  180. .avatar-section {
  181. display: flex;
  182. flex-direction: column;
  183. align-items: center;
  184. margin: 50rpx 0;
  185. .chooseAvatar {
  186. width: 180rpx;
  187. height: 180rpx;
  188. border-radius: 50%;
  189. overflow: hidden;
  190. padding: 0;
  191. margin: 0;
  192. background: none;
  193. &::after {
  194. border: none;
  195. }
  196. .avatar-img {
  197. width: 100%;
  198. height: 100%;
  199. border-radius: 50%;
  200. }
  201. }
  202. .avatar-hint {
  203. font-size: 26rpx;
  204. color: $uni-color-third;
  205. margin-top: 20rpx;
  206. }
  207. }
  208. .form-section {
  209. background-color: #fff;
  210. border-radius: 30rpx;
  211. overflow: hidden;
  212. margin-bottom: 60rpx;
  213. .form-item {
  214. display: flex;
  215. height: 100rpx;
  216. align-items: center;
  217. padding: 0 30rpx;
  218. border-bottom: 1rpx solid #f5f5f5;
  219. &:last-child {
  220. border-bottom: none;
  221. }
  222. .label {
  223. width: 150rpx;
  224. font-size: 30rpx;
  225. color: $uni-color-third;
  226. }
  227. .input {
  228. flex: 1;
  229. height: 100%;
  230. font-size: 30rpx;
  231. color: #666;
  232. }
  233. }
  234. }
  235. .save-button {
  236. width: 90%;
  237. margin: 160rpx auto 0;
  238. height: 100rpx;
  239. background-color: $uni-color;
  240. border-radius: 45rpx;
  241. display: flex;
  242. align-items: center;
  243. justify-content: center;
  244. color: #fff;
  245. font-size: 32rpx;
  246. box-shadow: 0 6rpx 20rpx rgba(1, 146, 69, 0.3);
  247. }
  248. </style>