【微信小程序】上传头像

news/2024/7/20 2:27:50 标签: 微信小程序, javascript, 小程序

效果

代码

editInfo.js

javascript">// pages/editInfo/editInfo.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList: []
  },

  /**
   * 获得图片本地路径
   */
  chooseWxImage: function () {
    const that = this;
    wx.chooseImage({
      // 最多可以选择的图片张数
      count: 1,
      // 所选的图片的尺寸
      sizeType: ['original', 'compressed'],
      // 选择图片的来源
      sourceType: ['album', 'camera'],
      success: function (res) {
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 10000
        });
        console.log(res);
        console.log(res.tempFilePaths[0]);
        that.upImgs(res.tempFilePaths[0], 0); // 调用上传方法
      },
      fail: function (res) {
        console.log('fail', res);
      },
      complete: function (res) {

      }
    });
  },

  /**
   * 上传服务器
   * @param {*} imgurl
   * @param {*} index
   */
  upImgs: function (imgurl, index) {
    const that = this;
    wx.uploadFile({
      url: `http://api`,
      // 小程序本地的路径
      filePath: imgurl,
      // 后台获取我们图片的key
      name: 'file',
      header: {
        'content-type': 'multipart/form-data'
      },
      // 额外的参数formData
      formData: {},
      success: function (res) {
        const datas = JSON.parse(res.data);
        console.log(datas);
        if (datas.code === 1) {
          wx.setStorageSync('PROFILEURL', 'http://' + datas.data);
          const profileUrl = wx.getStorageSync('PROFILEURL');
          that.setData({
            profileUrl: profileUrl
          });
          wx.showToast({
            title: '头像上传成功',
            icon: 'success',
            duration: 2000,
            mask: true,
            success: res => {}
          });
          console.log('success', res); // 接口返回网络路径
        } else {
          wx.showModal({
            title: '错误提示',
            content: datas.message,
            showCancel: false,
            success: function (res) { }
          });
          console.log('fail', datas.message);
        }
      },
      fail: function (res) {
        console.log('fail', res);
        wx.hideToast();
        wx.showModal({
          title: '错误提示',
          content: '上传图片失败',
          showCancel: false,
          success: function (res) { }
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});

profile.js

javascript">// pages/profile/profile.js

// 获取本地缓存
const USERID = wx.getStorageSync('USERID');
const ROLEID = wx.getStorageSync('ROLEID');
const PROFILEURL = wx.getStorageSync('PROFILEURL');
const NICKNAME = wx.getStorageSync('NICKNAME');
const POSTNUM = wx.getStorageSync('POSTNUM');
const FANSNUM = wx.getStorageSync('FANSNUM');
const INTRODUCE = wx.getStorageSync('INTRODUCE');
const TAGLIST = wx.getStorageSync('TAGLIST');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    UserInfo: [{
      profileUrl: PROFILEURL,
      nickname: NICKNAME,
      postNum: POSTNUM,
      fansNum: FANSNUM,
      introduce: INTRODUCE,
      tagList: TAGLIST
    }],
    pageNum: 1,
    userPageSize: 4,
    personalId: USERID,
    userId: USERID,
    onRefresh: false,
    roleId: ROLEID
  },

  /**
   * 刷新普通用户信息
   * @param {*} event
   */
  updateUserInfo: function () {
    // 获取本地缓存
    const PROFILEURL = wx.getStorageSync('PROFILEURL');
    const NICKNAME = wx.getStorageSync('NICKNAME');
    const POSTNUM = wx.getStorageSync('POSTNUM');
    const FANSNUM = wx.getStorageSync('FANSNUM');
    const INTRODUCE = wx.getStorageSync('INTRODUCE');
    const TAGLIST = wx.getStorageSync('TAGLIST');
    // 更新缓存
    const that = this;
    for (let i = 0; i < that.data.UserInfo.length; i++) {
      // console.log(that.data.UserInfo.length);
      const profileUrl = 'UserInfo[' + i + '].profileUrl';
      const nickname = 'UserInfo[' + i + '].nickname';
      const postNum = 'UserInfo[' + i + '].postNum';
      const fansNum = 'UserInfo[' + i + '].fansNum';
      const introduce = 'UserInfo[' + i + '].introduce';
      const tagList = 'UserInfo[' + i + '].tagList';
      that.setData({
        [profileUrl]: PROFILEURL,
        [nickname]: NICKNAME,
        [postNum]: POSTNUM,
        [fansNum]: FANSNUM,
        [introduce]: INTRODUCE,
        [tagList]: TAGLIST
      });
    }
    // console.log(that.data.UserInfo[0].profileUrl);
  },

  /**
   * 跳转编辑资料界面
   */
  gotoEditInfo: function (event) {
    // 当前要跳转到另一个界面,但是会保留现有界面
    wx.navigateTo({
      url: `../editInfo/editInfo?${this.data.dataList}`
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    if (USERID === 0) {
      wx.redirectTo({ url: 'pages/login/login' });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 每次回到这个页面都要更新缓存
    const that = this;
    if (ROLEID === 1) {
      that.updateUserInfo();
    } else {
      wx.showToast({
        title: '用户未登录!', // 提示的内容,
        icon: 'none', // 图标,
        duration: 2000, // 延迟时间,
        mask: true, // 显示透明蒙层,防止触摸穿透,
        success: res => {}
      });
      console.log('用户未登录!');
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});

http://www.niftyadmin.cn/n/1770033.html

相关文章

python 图片识别_python爬虫20 | 小帅b教你如何识别图片验证码

当你在爬取某些网站的时候对于你的一些频繁请求对方会阻碍你常见的方式就是使用验证码验证码的主要功能就是区分你是人还是鬼(机器人)人想法设法的搞一些手段来对付技术而技术又能对付人们的想法一来一去就有了各种各样的变态验证码也有了各种各样的应对方式常见的验证码有这么…

axure-chrome-extension 插件安装

场景 在我们收到axure-chrome-extension画的界面&#xff0c;通过浏览器无法打开时&#xff0c;在网上又无法找到安装包&#xff1b; 安装方法 我们打开页面原型的路径下的 resources 的 chrome 目录里面有 axure-chrome-extension.crx 复制一份 更名为 rar 解压之后 打…

【Vue3】创建 vite + vue3 + Ant Design Vue 项目

搭建脚手架 创建项目&#xff1a; 然后按照它的指示运行项目 配置文件 安装构建 vue-router&#xff1a; npm i vue-routernext创建 router/index.js 文件&#xff1a; import { createRouter, createWebHistory } from vue-routerconst routes [{path: /,name: Hom…

python ftp遍历文件夹_Python FTP两个文件夹间的同步实例代码

具体代码如下所示&#xff1a;# -*- coding: utf-8 -*-ftp自动检测源文件夹的更新&#xff0c;将源文件夹更新的内容拷贝到目标文件夹中使用树的层序遍历算法&#xff0c;支持深度目录拷贝import osfrom ftplib import FTPimport os,sys,string,datetime,timeimport shutilimpo…

【微信小程序】悬浮按钮

效果 需要制作一个底部居右的悬浮按钮&#xff1a; 需要制作一个固定在底部居中的悬浮按钮&#xff1a; 代码 底部居右 index.wxml <!-- 发布悬浮按钮 --> <view class"releaseBtn" bind:tap"gotoRelease"><image class"relea…

python搭建简单服务器_Python3之简单搭建自带服务器的实例讲解

WEB开发&#xff0c;我们先从搭建一个简单的服务器开始&#xff0c;Python自带服务模块&#xff0c;且python3相比于python2有很大不同&#xff0c;在Python2.6版本里&#xff0c;/usr/bin/lib/python2.6/ 目录下会有 BaseHTTPServer.py, SimpleHTTPServer.py, CGIHTTPServer.p…

【微信小程序】JavaScript 从数组中删除指定值元素的方法封装

效果 const somearray ["mon", "tue", "wed", "thur"] removeByValue(somearray, "tue"); //somearray will now have "mon", "wed", "thur"方法 在 src/utils/util.js 中定义函数 remove…

大连华宇和华信哪个好_大连柜式加油装置哪个好

大连柜式加油装置哪个好准确地说就是将各个重点科室工作完全集中&#xff0c;以利于自动进气控制与取排风系统的开发。目前&#xff0c;市场上的木质塑料加油站有铁皮类加油站和铸铁类加油站。由于设备相对专业&#xff0c;故能更好地配合集中供气系统进行管道系统的安装、配电…