当前位置:   article > 正文

uniapp修改用户头像,昵称等信息_uniapp修改头像

uniapp修改头像

效果图:

点击修改头像按钮后弹出的弹框:

 

代码:

  1. <template>
  2. <view class="userinfo">
  3. <view class="form">
  4. <button class="avatar-wrap" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
  5. <view class="label"> 头像 </view>
  6. <view class="ipt">
  7. <img class="avatar" :src="cdnUrl" />
  8. <uni-icons class="arrow-right" type="right" size="20"></uni-icons>
  9. </view>
  10. </button>
  11. </view>
  12. <view class="info_nickname">
  13. <view class="nickname_left"> 昵称 </view>
  14. <view class="nickname_right">
  15. <input v-model="nickname" placeholder="请输入您的昵称" />
  16. </view>
  17. </view>
  18. <view class="info_nickname">
  19. <view class="nickname_left">姓名</view>
  20. <view class="nickname_right">
  21. <input class="uni-input" placeholder="请输入您的姓名" @input="onName" :value="username" maxlength="10" />
  22. </view>
  23. </view>
  24. <view class="info_nickname">
  25. <view class="nickname_left"> 性别 </view>
  26. <view class="nickname_right gender">
  27. <picker @change="examinationType" :range="examinationTypeArray" style="margin-right: 10rpx">
  28. <label class="">{{ examinationTypeArray[examinationTypeIndex] }}</label>
  29. </picker>
  30. <uni-icons class="arrow-right" type="right" size="20" style="margin-top: 5rpx"></uni-icons>
  31. </view>
  32. </view>
  33. <view class="info_nickname">
  34. <view class="nickname_left"> 手机号 </view>
  35. <view class="nickname_right">
  36. <view class="showphone">
  37. {{ phone }}
  38. </view>
  39. </view>
  40. </view>
  41. <view class="btn-wrap">
  42. <button type="warn" class="btn" @click="saveinfo">保存</button>
  43. </view>
  44. </view>
  45. </template>
  46. <script>
  47. // import {getPhone} from '../../utils/phoneNumber.js'
  48. import { userinfo, profile } from '../../api/user.js'
  49. export default {
  50. data() {
  51. return {
  52. cdnUrl: getApp().globalData.cdnUrl + 'static/img-my/userpicter.png',
  53. avatar: '',
  54. nickname: '',
  55. username: '',
  56. examinationTypeArray: ['男', '女'],
  57. examinationTypeIndex: 0,
  58. examinationTypeArrayType: '',
  59. phone: '',
  60. number: 2,
  61. flag: false
  62. }
  63. },
  64. onLoad() {
  65. this.getUserinfo()
  66. },
  67. methods: {
  68. saveinfo: function () {
  69. let self = this
  70. let data = {
  71. avatar: self.avatar,
  72. nickname: self.nickname,
  73. realname: self.username,
  74. gender: self.examinationTypeIndex
  75. }
  76. profile(data).then(function (res) {
  77. if (res.code == '1') {
  78. if (self.avatar != '') {
  79. uni.setStorageSync('avatar', getApp().globalData.cdnUrl + self.avatar)
  80. }
  81. uni.setStorageSync('nickname', self.nickname)
  82. wx.showToast({
  83. title: '修改成功',
  84. icon: 'success',
  85. duration: 3000
  86. })
  87. } else {
  88. wx.showToast({
  89. title: '修改失败',
  90. icon: 'none',
  91. duration: 3000
  92. })
  93. }
  94. })
  95. },
  96. upload_file: function (e) {
  97. wx.showLoading({
  98. title: '上传中'
  99. })
  100. let self = this
  101. wx.uploadFile({
  102. url: 'https://ccb-api.znyzf.com/api/common/upload',
  103. filePath: e, //图片路径
  104. name: 'file',
  105. header: {
  106. 'Content-Type': 'multipart/form-data',
  107. token: uni.getStorageSync('token')
  108. },
  109. success: function (a) {
  110. let res = a.data
  111. res = JSON.parse(res)
  112. if (res.code == '1') {
  113. const { data } = res
  114. self.avatar = data.url
  115. self.cdnUrl = getApp().globalData.cdnUrl + data.url
  116. }
  117. wx.hideLoading()
  118. wx.showToast({
  119. title: '上传成功',
  120. icon: 'success',
  121. duration: 3000
  122. })
  123. },
  124. fail: function (a) {
  125. wx.hideLoading()
  126. wx.showToast({
  127. title: '上传失败',
  128. icon: 'none',
  129. duration: 3000
  130. })
  131. }
  132. })
  133. },
  134. getUserinfo() {
  135. let self = this
  136. userinfo().then(function (res) {
  137. const { data } = res
  138. self.cdnUrl = data.avatar
  139. self.nickname = data.nickname
  140. self.username = data.realname
  141. self.phone = data.mobile
  142. self.examinationTypeIndex = data.gender
  143. })
  144. },
  145. onChooseAvatar(res) {
  146. console.log(res)
  147. const { detail } = res
  148. this.upload_file(detail.avatarUrl)
  149. this.cdnUrl = detail.avatarUrl
  150. },
  151. // 绑定输入监听事件
  152. onName(event) {
  153. console.log(event)
  154. const { detail } = event
  155. this.username = detail.value
  156. },
  157. change(e) {
  158. console.log('e:', e)
  159. },
  160. examinationType(e) {
  161. this.examinationTypeIndex = e.target.value
  162. this.examinationTypeArrayType = this.examinationTypeArray[this.examinationTypeIndex]
  163. },
  164. changenumber() {
  165. this.number = 1
  166. this.flag = true
  167. },
  168. onPhone(event) {
  169. console.log(event)
  170. const { detail } = event
  171. this.phone = detail.value
  172. }
  173. }
  174. }
  175. </script>
  176. <style lang="scss">
  177. button::after {
  178. border-top: none;
  179. border-left: none;
  180. border-right: none;
  181. border-radius: 0;
  182. }
  183. .userinfo {
  184. font-size: 34rpx;
  185. font-weight: 400;
  186. color: #333333;
  187. height: 100%;
  188. position: relative;
  189. .form {
  190. background-color: #ffffff;
  191. }
  192. .avatar-wrap {
  193. border: 0;
  194. background-color: #ffffff;
  195. height: 158rpx;
  196. display: flex;
  197. align-items: center;
  198. justify-content: space-between;
  199. padding: 0 32rpx;
  200. .avatar {
  201. margin-right: 10rpx;
  202. }
  203. .ipt {
  204. display: flex;
  205. align-items: center;
  206. }
  207. img {
  208. width: 118rpx;
  209. height: 118rpx;
  210. }
  211. }
  212. .info_nickname {
  213. border-bottom: 1px solid #e9e9e9;
  214. height: 112rpx;
  215. background: #ffffff;
  216. display: flex;
  217. align-items: center;
  218. justify-content: space-between;
  219. padding: 0 32rpx;
  220. // justify-content: space-between;
  221. .nickname_left {
  222. }
  223. .nickname_right {
  224. input {
  225. text-align: right;
  226. }
  227. }
  228. .gender {
  229. display: flex;
  230. align-items: center;
  231. justify-content: center;
  232. }
  233. }
  234. .btn-wrap {
  235. position: absolute;
  236. bottom: 50rpx;
  237. width: 100%;
  238. text-align: center;
  239. .btn {
  240. width: 690rpx;
  241. height: 88rpx;
  242. }
  243. }
  244. }
  245. </style>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/287086
推荐阅读