敢为人鲜小程序前端代码仓库
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.

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