微信小程序又双叒叕改获取头像昵称的接口了

官方文档
https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/userProfile.html
https://developers.weixin.qq.com/miniprogram/dev/component/button.html
https://developers.weixin.qq.com/miniprogram/dev/component/input.html

tips:

如果调试这个接口时报错或者点着没反应,需要在开发工具中把基础库版本调至2.21.2或以上,我这边没有2.21.2选了2.21.4

获取昵称的输入框在开发环境中是没有效果的

wxml

html 复制代码
<form bindsubmit="subs">
  <view class="bodyer">
    <button class="cha" open-type="chooseAvatar" bindchooseavatar="to_cha">
      <image src="{{avatar_url?avatar_url:'/imgs/avatar.png'}}" mode="widthFix" />
      <input type="text" name="avatar" value="{{avatar_path}}" style="display: none;" />
    </button>
    <view class="boxs tit">申请以下获得权限</view>
    <view class="boxs tips">获取你的公开信息</view>
    <input type="nickname" placeholder="请输入昵称" name="nick" />
    <button class="sub" form-type="submit">授权登录</button>
  </view>
</form>

wxss

css 复制代码
page {
  text-align: center;
}

.cha {
  width: 128rpx;
  margin-top: 160rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  background: none !important;
}

.cha image {
  width: 128rpx;
  border-radius: 300rpx;
  overflow: hidden;
}

.boxs {
  margin: 30rpx;
}

.tit {
  font-weight: bold;
  margin-top: 60rpx;
}

.tips {
  color: #999999;
}

input {
  width: 350rpx;
  height: 88rpx;
  line-height: 88rpx;
  margin: 30rpx auto;
  border: 1rpx solid #dedede;
  border-radius: 10rpx;
  overflow: hidden;
}

.sub {
  width: 570rpx !important;
  height: 88rpx !important;
  line-height: 88rpx !important;
  margin-top: 80rpx !important;
  margin-bottom: 80rpx !important;
  padding: 0 !important;
  border-radius: 100rpx !important;
  overflow: hidden !important;
  color: #ffffff !important;
  background: linear-gradient(to right, #db011a 0%, #ff604a 100%) !important;
}

js

javascript 复制代码
var app = getApp();
var that;

Page({
	data: {
		avatar_path: "",
		avatar_url: "",
	},

	onLoad(options) {
		that = this;

	},

	onShareAppMessage() {

	},

	onShareTimeline() {

	},

	// 上传头像
	to_cha: function (e) {
		app.f_up(e.detail.avatarUrl, {}, function (params) {
			that.setData({
				avatar_path: params.path,
				avatar_url: params.url,
			});
		});
	},

	// 提交
	subs: function (e) {
		var data = e.detail.value;
		app.request_api("/index/my/m_up", data, function (params) {
			if (params.code == 0) {
				wx.showToast({
					title: "登录成功",
				});
				setTimeout(() => {
					wx.redirectTo({
						url: "/pages/cert_sel/cert_sel",
					});
				}, 1500);
			} else {
				wx.showToast({
					title: params.msg,
					icon: "none",
				});
			}
		});
	},
});

部分app.js

javascript 复制代码
var app;
var that;

App({
  // 文件上传
  f_up: function (file_path, data = {}, callback = false) {
    wx.showLoading({
      title: "上传中...",
      mask: true,
    });
    var usf = wx.uploadFile({
      url: app.globalData.api_url + "/index/base/upload",
      filePath: file_path,
      name: "file",
      formData: data,
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        "Cookie": "session_id",
        "X-Requested-With": "XMLHttpRequest",
      },
      success: function (params) {
        var params1 = JSON.parse(params.data);
        if (params1.code == 0) {
          if (callback != false) {
            callback(params1);
          }
        } else {
          wx.showModal({
            title: "上传失败",
            content: params1.msg,
            showCancel: false,
          });
        }

        setTimeout(() => {
          wx.hideLoading();
        }, 100);
      },
    });
    usf.onProgressUpdate(function (params) {
      wx.showLoading({
        title: "上传中..." + params.progress + "%",
        mask: true,
      });
    });
  },

  // 图片选择
  m_sel: function (types, len = 1, callback = false) {
    wx.chooseMedia({
      count: len, //可选择最大文件数 (最多100)
      type: types, //文件类型,all是全部文件类型
      success: function (params) {
        wx.showLoading({
          title: "上传中...",
          mask: true,
        });
        var isup = 0;
        var res = [];
        params.tempFiles.forEach(function (vo, key) {
          app.f_up(vo.tempFilePath, {}, function (params1) {
            res.push(params1);
            setTimeout(() => {
              isup++;
              if (isup == params.tempFiles.length) {
                wx.hideLoading();
                callback(res);
              }
            }, 100);
          });
        });
      },
    });
  },

  globalData: {
    api_url: "http://test.cc",
    openid: "",
    wid: 0,
  },
});

上传接口

php 复制代码
// 上传
function upload()
{
	set_time_limit(0);
	ini_set("max_execution_time", 0);
	ini_set("memory_limit", -1);

	$name = trim(input("name")); //获取文件名
	$file = request()->file("file"); //获取上传的文件
	if (!$file) {
		$res["msg"] = "文件大小超限";
		$res["code"] = 1;
		return json($res);
	}

	$path_ul = "./Uploads/file/"; //移动到框架应用根目录/public/Uploads/目录下
	$infos = $file->move($path_ul);
	if (!$infos) {
		$res["msg"] = $file->getError();
		$res["code"] = 1;
		return json($res);
	}

	if (!$name) {
		$name = $infos->getInfo("name"); //获取原始文件名
	}

	$path = $infos->getSaveName();
	$path = str_replace("\\", "/", $path); //\(反斜杠)替换为/(斜杠)
	$path = ltrim($path_ul, ".") . ltrim($path, "/");
	$urls = request()->domain() . $path;

	$res = [];
	$res["name"] = $name;
	$res["path"] = $path;
	$res["url"] = $urls;
	$res["field"] = $path . "|" . $name;
	$res["code"] = 0;
	return json($res);
}
相关推荐
拖孩16 小时前
流量主门槛从 500 降到 100,我的小程序上线 18 小时够到了线
前端·后端·微信小程序
云-缩影17 小时前
做微信小程序,自建服务器还是用云托管:技术选型对比
服务器·微信小程序·小程序
Frag0ut20 小时前
Notepad++ 正则表达式完全手册:文本处理的终极武器(2026实战版)
正则表达式·notepad++·数据清洗·日志分析·正则替换·提取ip·正则表达式实战
极限挑战哥1 天前
共享电单车联营平台分账架构实践:二清合规与自动分账零人工方案
java·大数据·人工智能·微信小程序·架构·微信公众平台
超级新零售2 天前
服装订货会数字化:商品版本、订单状态与后续配发数据如何衔接?
大数据·小程序·企业微信
Frag0ut2 天前
Notepad++ 零基础入门:从记事本到代码编辑器的平滑过渡(2026最新版)
python·编辑器·html·json·notepad++·markdown·记事本
云-缩影2 天前
拆解微信小程序后端架构:ECS、RDS、SSL 和 VPC 怎么配合
微信小程序·架构·ssl
拖孩2 天前
AI 帮我写的小程序上线 44 天:36 个人进了收银台,8 个人付钱
前端·后端·微信小程序
xiezhr2 天前
我用workBuddy开发了自己的第一个小程序并成功上线了
ai·小程序·ai agent·workbuddy