• 微擎小程序获取不到头像和昵称解决方案


    这是一个使用微擎小程序的代码示例,其中包含了获取用户头像和昵称的功能。以下是解决方案:

    1. 首先,在

    2. 在Page对象中定义onChooseAvatar函数来处理选择头像的事件:

      1. onChooseAvatar: function(event) {
      2. var that = this;
      3. wx.chooseImage({
      4. count: 1,
      5. success:function(res) {
      6. that.setData({
      7. avatarUrl: res.tempFilePaths[0]
      8. });
      9. }
      10. });
      11. }
    3. 标签中加入name="nickname"属性,设置输入框的类型为昵称:

      1. <view class="cu-form-group">
      2. <view class="title">昵 称:</view>
      3. <input placeholder="请输入昵称" type="nickname" name="nickname" maxlength="32"></input>
      4. </view>
    4. 提交表单时,通过form标签的bindsubmit属性绑定getUserName事件处理函数。在事件处理函数中,可以使用event.detail.value获取用户输入的昵称和手机号:

      1. <form bindsubmit="getUserName">
      2. ...
      3. </form>
      1. getUserName: function (event) {
      2. var nickname = event.detail.value.nickname;
      3. var mobile = event.detail.value.mobile;
      4. // 进行相应的逻辑处理
      5. }

    完整代码login.wxml 

    1. <view>
    2. <form bindsubmit="getUserName">
    3. <button class="avatar-wrapper" open-type="chooseAvatar" bindchooseavatar="onChooseAvatar">
    4. <image class="avatar" src="{{avatarUrl?avatarUrl:'../../resource/icon/user.png'}}" mode="aspectFill"></image>
    5. </button>
    6. <view class="cu-form-group">
    7. <view class="title">昵 称:</view>
    8. <input placeholder="请输入昵称" type="nickname" name="nickname" maxlength="32"></input>
    9. </view>
    10. <view class="cu-form-group">
    11. <view class="title">手机号:</view>
    12. <input placeholder="请输入手机号" type="text" name="mobile" maxlength="12"></input>
    13. </view>
    14. <view class="up-bt">
    15. <button form-type="submit" role="button" aria-disabled="false" class="save-bt">登录</button>
    16. </view>
    17. </form>
    18. </view>

    css部分login.wxss

    1. .avatar{
    2. width: 70px;
    3. height: 70px;
    4. text-align: center;
    5. border-radius: 50%;
    6. }
    7. .avatar-wrapper{
    8. background: #c9c9c9;
    9. border: 1px solid #ffffff;
    10. border-radius: 50%;
    11. width: 70px;
    12. height: 70px;
    13. text-align: center;
    14. line-height: 70px;
    15. padding: 0;
    16. margin-top: 10px;
    17. }
    18. .cu-form-group{
    19. background-color: #ffffff;
    20. width: 98%;
    21. margin: 0 auto;
    22. border-radius: 10rpx;
    23. padding: 10px 0px;
    24. margin-bottom: 10px;
    25. margin-top: 10px;
    26. }
    27. .cu-form-group .title{
    28. float: left;
    29. }
    30. .save-bt{
    31. background-color: #ffffff;
    32. width: 85%;
    33. margin: 0 auto;
    34. }

     

    微擎修改登录部分util.js

    1. var _typeof3 = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(e) {
    2. return typeof e;
    3. } : function(e) {
    4. return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e;
    5. }, _typeof2 = "function" == typeof Symbol && "symbol" == _typeof3(Symbol.iterator) ? function(e) {
    6. return void 0 === e ? "undefined" : _typeof3(e);
    7. } : function(e) {
    8. return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : void 0 === e ? "undefined" : _typeof3(e);
    9. }, _typeof = "function" == typeof Symbol && "symbol" == _typeof2(Symbol.iterator) ? function(e) {
    10. return void 0 === e ? "undefined" : _typeof2(e);
    11. } : function(e) {
    12. return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : void 0 === e ? "undefined" : _typeof2(e);
    13. }, _base = require("base64"), _md = require("md5"), _md2 = _interopRequireDefault(_md);
    14. function _interopRequireDefault(e) {
    15. return e && e.__esModule ? e : {
    16. default: e
    17. };
    18. }
    19. function _defineProperty(e, t, n) {
    20. return t in e ? Object.defineProperty(e, t, {
    21. value: n,
    22. enumerable: !0,
    23. configurable: !0,
    24. writable: !0
    25. }) : e[t] = n, e;
    26. }
    27. var util = {};
    28. function getQuery(e) {
    29. var t = [];
    30. if (-1 != e.indexOf("?")) for (var n = e.split("?")[1].split("&"), o = 0; o < n.length; o++) n[o].split("=")[0] && unescape(n[o].split("=")[1]) && (t[o] = {
    31. name: n[o].split("=")[0],
    32. value: unescape(n[o].split("=")[1])
    33. });
    34. return t;
    35. }
    36. function getUrlParam(e, t) {
    37. return t = new RegExp("(^|&)" + t + "=([^&]*)(&|$)"), null != (t = e.split("?")[1].match(t)) ? unescape(t[2]) : null;
    38. }
    39. function getSign(e, t, n) {
    40. var o = require("underscore.js"), r = require("md5.js"), a = "";
    41. if (getUrlParam(e, "sign") || t && t.sign) return !1;
    42. if (e && (a = getQuery(e)), t) {
    43. var i, s = [];
    44. for (i in t) i && t[i] && (s = s.concat({
    45. name: i,
    46. value: t[i]
    47. }));
    48. a = a.concat(s);
    49. }
    50. a = o.sortBy(a, "name"), a = o.uniq(a, !0, "name");
    51. for (var u = "", c = 0; c < a.length; c++) a[c] && a[c].name && a[c].value && (u += a[c].name + "=" + a[c].value,
    52. c < a.length - 1 && (u += "&"));
    53. return r(u + (n = n || getApp().siteInfo.token));
    54. }
    55. util.base64Encode = function(e) {
    56. return (0, _base.base64_encode)(e);
    57. }, util.base64Decode = function(e) {
    58. return (0, _base.base64_decode)(e);
    59. }, util.md5 = function(e) {
    60. return (0, _md2.default)(e);
    61. }, util.url = function(e, t) {
    62. var n = getApp(), o = n.siteInfo.siteroot + "?i=" + n.siteInfo.uniacid + "&t=" + n.siteInfo.multiid + "&v=" + n.siteInfo.version + "&from=wxapp&";
    63. if (e && ((e = e.split("/"))[0] && (o += "c=" + e[0] + "&"), e[1] && (o += "a=" + e[1] + "&"),
    64. e[2] && (o += "do=" + e[2] + "&")), t && "object" === (void 0 === t ? "undefined" : _typeof(t))) for (var r in t) r && t.hasOwnProperty(r) && t[r] && (o += r + "=" + t[r] + "&");
    65. return o;
    66. }, util.getSign = function(e, t, n) {
    67. return getSign(e, t, n);
    68. }, util.request = function(n) {
    69. require("underscore.js");
    70. var e = require("md5.js"), o = getApp();
    71. (n = n || {}).cachetime = n.cachetime || 0, n.showLoading = void 0 === n.showLoading || n.showLoading;
    72. var t = wx.getStorageSync("userInfo").sessionid, r = n.url;
    73. -1 == r.indexOf("http://") && -1 == r.indexOf("https://") && (r = util.url(r)),
    74. getUrlParam(r, "state") || n.data && n.data.state || !t || (r = r + "&state=we7sid-" + t),
    75. n.data && n.data.m || (a = getCurrentPages()).length && (a = a[getCurrentPages().length - 1]) && a.__route__ && (r = r + "&m=" + a.__route__.split("/")[0]);
    76. var a = getSign(r, n.data);
    77. if (a && (r = r + "&sign=" + a), !r) return !1;
    78. if (wx.showNavigationBarLoading(), n.showLoading && util.showLoading(), n.cachetime) {
    79. var i = e(r), s = wx.getStorageSync(i), u = Date.parse(new Date());
    80. if (s && s.data) {
    81. if (s.expire > u) return n.complete && "function" == typeof n.complete && n.complete(s),
    82. n.success && "function" == typeof n.success && n.success(s), console.log("cache:" + r),
    83. wx.hideLoading(), wx.hideNavigationBarLoading(), !0;
    84. wx.removeStorageSync(i);
    85. }
    86. }
    87. wx.request((_defineProperty(s = {
    88. url: r,
    89. data: n.data || {},
    90. header: n.header || {},
    91. method: n.method || "GET"
    92. }, "header", {
    93. "content-type": "application/x-www-form-urlencoded"
    94. }), _defineProperty(s, "success", function(e) {
    95. wx.hideNavigationBarLoading(), wx.hideLoading();
    96. var t = wx.getStorageSync("wxInfo");
    97. console.log(t), e.data.errno ? "41009" == e.data.errno ? (t || wx.setStorageSync("userInfo", ""),
    98. util.getUserInfo(function() {
    99. util.request(n);
    100. }, t)) : n.fail && "function" == typeof n.fail ? n.fail(e) : e.data.message && (t = null != e.data.data && e.data.data.redirect ? e.data.data.redirect : "",
    101. o.util.message(e.data.message, t, "error")) : (n.success && "function" == typeof n.success && n.success(e),
    102. n.cachetime && (e = {
    103. data: e.data,
    104. expire: u + 1e3 * n.cachetime
    105. }, wx.setStorageSync(i, e)));
    106. }), _defineProperty(s, "fail", function(e) {
    107. wx.hideNavigationBarLoading(), wx.hideLoading();
    108. var t = require("md5.js")(r);
    109. if ((t = wx.getStorageSync(t)) && t.data) return n.success && "function" == typeof n.success && n.success(t),
    110. console.log("failreadcache:" + r), !0;
    111. n.fail && "function" == typeof n.fail && n.fail(e);
    112. }), _defineProperty(s, "complete", function(e) {
    113. n.complete && "function" == typeof n.complete && n.complete(e);
    114. }), s));
    115. }, util.getWe7User = function(t, e) {
    116. var n = wx.getStorageSync("userInfo") || {};
    117. util.request({
    118. url: "auth/session/openid",
    119. data: {
    120. code: e || ""
    121. },
    122. cachetime: 0,
    123. showLoading: !1,
    124. success: function(e) {
    125. e.data.errno || (n.sessionid = e.data.data.sessionid, n.memberInfo = e.data.data.userinfo,
    126. wx.setStorageSync("userInfo", n)), "function" == typeof t && t(n);
    127. }
    128. });
    129. }, util.upadteUser = function(e, t) {
    130. console.log(e);
    131. var n = wx.getStorageSync("userInfo");
    132. if (!e) return "function" == typeof t && t(n);
    133. n.wxInfo = e.userInfo, wx.setStorageSync("wxInfo", e), util.request({
    134. url: "auth/session/userinfo",
    135. data: {
    136. userInfo: JSON.stringify(e.userInfo)
    137. },
    138. method: "POST",
    139. cachetime: 0,
    140. success: function(e) {
    141. console.log(e), e.data.errno || (n.memberInfo = e.data.data, wx.setStorageSync("userInfo", n)),
    142. "function" == typeof t && t(n);
    143. }
    144. });
    145. }, util.checkSession = function(t) {
    146. util.request({
    147. url: "auth/session/check",
    148. method: "POST",
    149. cachetime: 0,
    150. showLoading: !1,
    151. success: function(e) {
    152. e.data.errno ? "function" == typeof t.fail && t.fail() : "function" == typeof t.success && t.success();
    153. },
    154. fail: function() {
    155. "function" == typeof t.fail && t.fail();
    156. }
    157. });
    158. }, util.getUserInfo = function(n, o) {
    159. function e() {
    160. console.log("start login"), wx.login({
    161. success: function(e) {
    162. console.log(o), util.getWe7User(function(t) {
    163. o ? util.upadteUser(o, function(e) {
    164. "function" == typeof n && n(e);
    165. }) : wx.canIUse("getUserProfile") ? wx.showModal({
    166. title: "获取用户信息",
    167. content: "请允许授权以便为您提供服务",
    168. success: function(e) {
    169. if(e.confirm){
    170. // util.upadteUser(e, function(e) {
    171. // "function" == typeof n && n(e);
    172. // });
    173. //新增跳转到login
    174. wx.navigateTo({
    175. url: '/we/pages/user/login',
    176. })
    177. }
    178. }
    179. }) : "function" == typeof n && n(t);
    180. }, e.code);
    181. },
    182. fail: function() {
    183. wx.showModal({
    184. title: "获取信息失败",
    185. content: "请允许授权以便为您提供给服务",
    186. success: function(e) {
    187. e.confirm && util.getUserInfo();
    188. }
    189. });
    190. }
    191. });
    192. }
    193. var t = wx.getStorageSync("userInfo") || {};
    194. t.sessionid ? util.checkSession({
    195. success: function() {
    196. o ? util.upadteUser(o, function(e) {
    197. "function" == typeof n && n(e);
    198. }) : "function" == typeof n && n(t);
    199. },
    200. fail: function() {
    201. t.sessionid = "", console.log("relogin"), wx.removeStorageSync("userInfo"), e();
    202. }
    203. }) : e();
    204. }, util.navigateBack = function(t) {
    205. var e, n = t.delta || 1;
    206. t.data && ((e = (e = getCurrentPages())[e.length - (n + 1)]).pageForResult ? e.pageForResult(t.data) : e.setData(t.data)),
    207. wx.navigateBack({
    208. delta: n,
    209. success: function(e) {
    210. "function" == typeof t.success && t.success(e);
    211. },
    212. fail: function(e) {
    213. "function" == typeof t.fail && t.fail(e);
    214. },
    215. complete: function() {
    216. "function" == typeof t.complete && t.complete();
    217. }
    218. });
    219. }, util.footer = function(e) {
    220. var t, e = e, n = getApp().tabBar;
    221. for (t in n.list) n.list[t].pageUrl = n.list[t].pagePath.replace(/(\?|#)[^"]*/g, "");
    222. e.setData({
    223. tabBar: n,
    224. "tabBar.thisurl": e.__route__
    225. });
    226. }, util.message = function(e, t, n) {
    227. if (!e) return !0;
    228. var o, r, a;
    229. "object" == (void 0 === e ? "undefined" : _typeof(e)) && (t = e.redirect, n = e.type,
    230. e = e.title), t && (o = t.substring(0, 9), a = r = "", "navigate:" == o ? (a = "navigateTo",
    231. r = t.substring(9)) : "redirect:" == o ? (a = "redirectTo", r = t.substring(9)) : (r = t,
    232. a = "redirectTo")), console.log(r), "success" == (n = n || "success") ? wx.showToast({
    233. title: e,
    234. icon: "success",
    235. duration: 2e3,
    236. mask: !!r,
    237. complete: function() {
    238. r && setTimeout(function() {
    239. wx[a]({
    240. url: r
    241. });
    242. }, 1800);
    243. }
    244. }) : "error" == n && wx.showModal({
    245. title: "系统信息",
    246. content: e,
    247. showCancel: !1,
    248. complete: function() {
    249. r && wx[a]({
    250. url: r
    251. });
    252. }
    253. });
    254. }, util.user = util.getUserInfo, util.showLoading = function() {
    255. wx.getStorageSync("isShowLoading") && (wx.hideLoading(), wx.setStorageSync("isShowLoading", !1)),
    256. wx.showLoading({
    257. title: "加载中",
    258. complete: function() {
    259. wx.setStorageSync("isShowLoading", !0);
    260. },
    261. fail: function() {
    262. wx.setStorageSync("isShowLoading", !1);
    263. }
    264. });
    265. }, util.showImage = function(e) {
    266. if (!(e = e ? e.currentTarget.dataset.preview : "")) return !1;
    267. wx.previewImage({
    268. urls: [ e ]
    269. });
    270. }, util.parseContent = function(e) {
    271. if (!e) return e;
    272. var t = e.match(new RegExp([ "\ud83c[\udf00-\udfff]", "\ud83d[\udc00-\ude4f]", "\ud83d[\ude80-\udeff]" ].join("|"), "g"));
    273. if (t) for (var n in t) e = e.replace(t[n], "[U+" + t[n].codePointAt(0).toString(16).toUpperCase() + "]");
    274. return e;
    275. }, util.date = function() {
    276. this.isLeapYear = function(e) {
    277. return 0 == e.getYear() % 4 && (e.getYear() % 100 != 0 || e.getYear() % 400 == 0);
    278. }, this.dateToStr = function(e, t) {
    279. return e = arguments[0] || "yyyy-MM-dd HH:mm:ss", t = arguments[1] || new Date(),
    280. (e = (e = (e = (e = (e = (e = (e = (e = (e = (e = (e = (e = e.replace(/yyyy|YYYY/, t.getFullYear())).replace(/yy|YY/, 9 < t.getYear() % 100 ? (t.getYear() % 100).toString() : "0" + t.getYear() % 100)).replace(/MM/, 9 < t.getMonth() ? t.getMonth() + 1 : "0" + (t.getMonth() + 1))).replace(/M/g, t.getMonth())).replace(/w|W/g, [ "", "", "", "", "", "", "" ][t.getDay()])).replace(/dd|DD/, 9 < t.getDate() ? t.getDate().toString() : "0" + t.getDate())).replace(/d|D/g, t.getDate())).replace(/hh|HH/, 9 < t.getHours() ? t.getHours().toString() : "0" + t.getHours())).replace(/h|H/g, t.getHours())).replace(/mm/, 9 < t.getMinutes() ? t.getMinutes().toString() : "0" + t.getMinutes())).replace(/m/g, t.getMinutes())).replace(/ss|SS/, 9 < t.getSeconds() ? t.getSeconds().toString() : "0" + t.getSeconds())).replace(/s|S/g, t.getSeconds());
    281. }, this.dateAdd = function(e, t, n) {
    282. switch (n = arguments[2] || new Date(), e) {
    283. case "s":
    284. return new Date(n.getTime() + 1e3 * t);
    285. case "n":
    286. return new Date(n.getTime() + 6e4 * t);
    287. case "h":
    288. return new Date(n.getTime() + 36e5 * t);
    289. case "d":
    290. return new Date(n.getTime() + 864e5 * t);
    291. case "w":
    292. return new Date(n.getTime() + 6048e5 * t);
    293. case "m":
    294. return new Date(n.getFullYear(), n.getMonth() + t, n.getDate(), n.getHours(), n.getMinutes(), n.getSeconds());
    295. case "y":
    296. return new Date(n.getFullYear() + t, n.getMonth(), n.getDate(), n.getHours(), n.getMinutes(), n.getSeconds());
    297. }
    298. }, this.dateDiff = function(e, t, n) {
    299. switch (e) {
    300. case "s":
    301. return parseInt((n - t) / 1e3);
    302. case "n":
    303. return parseInt((n - t) / 6e4);
    304. case "h":
    305. return parseInt((n - t) / 36e5);
    306. case "d":
    307. return parseInt((n - t) / 864e5);
    308. case "w":
    309. return parseInt((n - t) / 6048e5);
    310. case "m":
    311. return n.getMonth() + 1 + 12 * (n.getFullYear() - t.getFullYear()) - (t.getMonth() + 1);
    312. case "y":
    313. return n.getFullYear() - t.getFullYear();
    314. }
    315. }, this.strToDate = function(dateStr) {
    316. var data = dateStr, reCat = /(\d{1,4})/gm, t = data.match(reCat);
    317. return t[1] = t[1] - 1, eval("var d = new Date(" + t.join(",") + ");"), d;
    318. }, this.strFormatToDate = function(e, t) {
    319. var n = 0, o = -1, r = t.length;
    320. -1 < (o = e.indexOf("yyyy")) && o < r && (n = t.substr(o, 4));
    321. var a = 0;
    322. -1 < (o = e.indexOf("MM")) && o < r && (a = parseInt(t.substr(o, 2)) - 1);
    323. var i = 0;
    324. -1 < (o = e.indexOf("dd")) && o < r && (i = parseInt(t.substr(o, 2)));
    325. var s = 0;
    326. (-1 < (o = e.indexOf("HH")) || 1 < (o = e.indexOf("hh"))) && o < r && (s = parseInt(t.substr(o, 2)));
    327. var u = 0;
    328. -1 < (o = e.indexOf("mm")) && o < r && (u = t.substr(o, 2));
    329. var c = 0;
    330. return -1 < (o = e.indexOf("ss")) && o < r && (c = t.substr(o, 2)), new Date(n, a, i, s, u, c);
    331. }, this.dateToLong = function(e) {
    332. return e.getTime();
    333. }, this.longToDate = function(e) {
    334. return new Date(e);
    335. }, this.isDate = function(e, t) {
    336. null == t && (t = "yyyyMMdd");
    337. var n = t.indexOf("yyyy");
    338. if (-1 == n) return !1;
    339. var o = e.substring(n, n + 4);
    340. return -1 != (n = t.indexOf("MM")) && (n = e.substring(n, n + 2), -1 != (t = t.indexOf("dd")) && (t = e.substring(t, t + 2),
    341. !(!isNumber(o) || "2100" < o || o < "1900" || !isNumber(n) || "12" < n || n < "01" || t > getMaxDay(o, n) || t < "01")));
    342. }, this.getMaxDay = function(e, t) {
    343. return 4 == t || 6 == t || 9 == t || 11 == t ? "30" : 2 == t ? e % 4 == 0 && e % 100 != 0 || e % 400 == 0 ? "29" : "28" : "31";
    344. }, this.isNumber = function(e) {
    345. return /^\d+$/g.test(e);
    346. }, this.toArray = function(e) {
    347. e = arguments[0] || new Date();
    348. var t = Array();
    349. return t[0] = e.getFullYear(), t[1] = e.getMonth(), t[2] = e.getDate(), t[3] = e.getHours(),
    350. t[4] = e.getMinutes(), t[5] = e.getSeconds(), t;
    351. }, this.datePart = function(e, t) {
    352. t = arguments[1] || new Date();
    353. var n = "";
    354. switch (e) {
    355. case "y":
    356. n = t.getFullYear();
    357. break;
    358. case "M":
    359. n = t.getMonth() + 1;
    360. break;
    361. case "d":
    362. n = t.getDate();
    363. break;
    364. case "w":
    365. n = [ "", "", "", "", "", "", "" ][t.getDay()];
    366. break;
    367. case "ww":
    368. n = t.WeekNumOfYear();
    369. break;
    370. case "h":
    371. n = t.getHours();
    372. break;
    373. case "m":
    374. n = t.getMinutes();
    375. break;
    376. case "s":
    377. n = t.getSeconds();
    378. }
    379. return n;
    380. }, this.maxDayOfDate = function(e) {
    381. return (e = arguments[0] || new Date()).setDate(1), e.setMonth(e.getMonth() + 1),
    382. e = e.getTime() - 864e5, new Date(e).getDate();
    383. };
    384. }, module.exports = util;

  • 相关阅读:
    Cisco.Packet.Tracer思科模拟器浮动路由讲解(含实例步骤)
    Android平台GB28181设备接入模块相关博客概览
    Swift-19-基础入门
    【实用 Python 库】使用 XPath 与 lxml 模块在 Python 中高效解析 XML 与 HTML
    idea Springboot闲置物品交易平台VS开发mysql数据库web结构java编程计算机网页源码maven项目
    【国庆活动】掌握这些SQL核心理念,一起轻松玩转国庆假期
    Docker搭建FRP内网穿透服务器
    4.3.4-Testing_for_Account_Enumeration_and_Guessable_User_Account
    UG NX二次开发(C++)-UIStyler-如何获取树中节点的子节点
    【Mac】破解死循环,成功安装 Homebrew、curl、wget,快速配置 zsh
  • 原文地址:https://blog.csdn.net/u014692298/article/details/133621316