当前位置:   article > 正文

微信小程序个人资料(修改头像,上传图片和拍照)_微信小程序更换头像代码

微信小程序更换头像代码

首先是wxml

  1. <view style="padding-top: {{pageTopHeight}}px;">
  2. <view style="background-color: #fff;margin-top: 40rpx;padding: 10rpx 20rpx;font-size: 16px;">
  3. <view class="con_wrapd">
  4. <view>头像</view>
  5. <view class="wrap_img" bindtap="sculpture">
  6. <view class="cu-avatar lg round margin-left" style="background-
  7. image:url({{user.img}});margin-right: 60rpx;"></view>
  8. <image src="/Images/youjiantou.png"></image>
  9. </view>
  10. </view>
  11. <view class="con_wrapd">
  12. <view>昵称</view>
  13. <view class="wrap_img" bindtap="nickname" data-vlaue="昵称">
  14. <text style="margin-right: 60rpx;">{{user.name}}</text>
  15. <image src="/Images/youjiantou.png"></image>
  16. </view>
  17. </view>
  18. <view class="con_wrapd">
  19. <view>地区</view>
  20. <view class="wrap_img" bindtap="nickname" data-vlaue="地区" >
  21. <text style="margin-right: 60rpx;">{{user.region}}</text>
  22. <image src="/Images/youjiantou.png"></image>
  23. </view>
  24. </view>
  25. <view class="con_wrapd">
  26. <view>手机号</view>
  27. <view class="wrap_img" >{{user.phone}}</view>
  28. </view>
  29. <view class="con_wrapd">
  30. <view>我的ID</view>
  31. <view class="wrap_img" >{{user.id}}</view>
  32. </view>
  33. <view class="con_wrapd" style="border: none;">
  34. <view>推荐人ID</view>
  35. <view class="wrap_img" bindtap="nickname" data-vlaue="推荐人ID">
  36. <text style="margin-right: 60rpx;">点击绑定</text>
  37. <image src="/Images/youjiantou.png"></image>
  38. </view>
  39. </view>
  40. </view>
  41. </view>
  42. <!--头像 -->
  43. <view class="zan-dialog {{ showdDialog ? 'zan-dialog--show' : '' }}" catchtouchmove='ture' >
  44. <view class="zan-dialog__mask" bindtap="hidedType"/>
  45. <view class="zan-dialog__container">
  46. <view>用微信头像</view>
  47. <view class="cu-avatar radius margin-left" style="background-image:url({{user.img}});"></view>
  48. <view bindtap="chooseImage" style="margin-top: 40rpx;">从相册选择</view>
  49. <!-- <camera device-position="back" flash="off" binderror="error" style="width: 100%; height: 300px;"></camera> -->
  50. <view bindtap="takePhoto" style="margin-top: 40rpx;">拍照</view>
  51. <view style="margin-top: 40rpx;border-top: solid 10px rgb(241, 239, 239);padding-top: 40rpx;" bindtap="hidedType">取消</view>
  52. </view>
  53. </view>

开始他的js,有两种方法,第一张自己写的js

  1. onLoad: function (options) {
  2. setInterval(() => { 
  3. var user = wx.getStorageSync('muserlist')//个人资料
  4. this.setData({
  5. user:user
  6. })
  7. },100)
  8. },
  9. //头像遮盖层
  10. hidedType:function(e){
  11. this.setData({
  12. showdDialog:false
  13. })
  14. },
  15. //头像
  16. sculpture:function(e){
  17. this.setData({
  18. showdDialog:true
  19. })
  20. },
  21. //从相册选择
  22. chooseImage() {
  23. wx.chooseImage({
  24. count: 1, //默认9
  25. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  26. sourceType: ['album'], //从相册选择
  27. success: (res) => {
  28. if (this.data.imgList.length != 0) {
  29. this.setData({
  30. imgList: this.data.imgList.concat(res.tempFilePaths)
  31. })
  32. this.data.user.img=this.data.imgList
  33. wx.setStorageSync('muserlist',this.data.user)//添加
  34. } else {
  35. this.setData({
  36. imgList: res.tempFilePaths
  37. })
  38. this.data.user.img=this.data.imgList
  39. wx.setStorageSync('muserlist',this.data.user)
  40. }
  41. }
  42. });
  43. this.setData({
  44. showdDialog:false
  45. })
  46. },
  47. // 拍照
  48. takePhoto:function(){
  49. wx.chooseImage({
  50. count: 1, //默认9
  51. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  52. sourceType: ['camera'], //从相机
  53. success: (res) => {
  54. if (this.data.imgList.length != 0) {
  55. this.setData({
  56. imgLists: this.data.imgList.concat(res.tempFilePaths)
  57. })
  58. this.data.user.img=this.data.imgLists
  59. wx.setStorageSync('muserlist',this.data.user)
  60. } else {
  61. this.setData({
  62. imgLists: res.tempFilePaths
  63. })
  64. this.data.user.img=this.data.imgLists
  65. wx.setStorageSync('muserlist',this.data.user)
  66. }
  67. }
  68. });
  69. this.setData({
  70. showdDialog:false
  71. })
  72. },

第二种微信自带的wxml

  1. <view class="con_wrapd">
  2. <view>头像</view>
  3. <button class="wrap_img" open-type="chooseAvatar" bindchooseavatar="assd">
  4. <view class="cu-avatar lg round " style="background-
  5. image:url({{user.avatar}});margin-right: 30rpx;"></view>
  6. <image src="/Images/youjiantou.png"></image>
  7. </button>
  8. </view>

 

wxss

  1. .con_wrapd{
  2. display: flex;
  3. border-bottom: 1px solid rgb(236, 235, 235);
  4. line-height: 130rpx;
  5. }
  6. .wrap_img{
  7. flex: 1;
  8. text-align: end;
  9. color: #99949d;
  10. position: relative;
  11. /* margin-top: 10rpx; */
  12. }
  13. .wrap_img image{
  14. width: 45rpx;
  15. height: 45rpx;
  16. position: absolute;
  17. right: 20rpx;
  18. top: 43rpx;
  19. }
  20. /* 件数弹窗 */
  21. .zan-dialog__mask {
  22. position: absolute;
  23. top: 0;
  24. left: 0;
  25. right: 0;
  26. bottom: 0;
  27. z-index: 110;
  28. background: rgba(0, 0, 0, 0.3);
  29. height: 100vh;
  30. width: 100%;
  31. /* //设置阴影半透明背景如: background: rgba(0, 0, 0, 0.4); */
  32. display: none;
  33. }
  34. .zan-dialog__container {
  35. position: fixed;
  36. bottom: 0rpx;
  37. width: 100%;
  38. /* //弹窗布局宽 */
  39. height: 35%;
  40. /* //弹窗布局高,与下面弹出距离transform有关 */
  41. border-radius:20rpx 20rpx 0 0 ;
  42. background: #f8f8f8;
  43. transform: translateY(300%);
  44. /* //弹框弹出距离,与弹框布局高度有关,如300%表示弹起距离为3倍弹窗高度 */
  45. transition: all 0.4s ease;
  46. z-index: 112;
  47. text-align: center;
  48. line-height: 60rpx;
  49. padding-top: 25rpx;
  50. /* //弹框的悬浮阴影效果,如不需要可注释该行 */
  51. }
  52. .zan-dialog--show .zan-dialog__container{
  53. transform: translateY(0);
  54. }
  55. .zan-dialog--show .zan-dialog__mask {
  56. display: block;
  57. }

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

闽ICP备14008679号