demo.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // pages/demo/demo.js
  2. Page({
  3. data: {
  4. isHidden: true,
  5. },
  6. onShow() {
  7. let userInfo = wx.getAccountInfoSync('userinfo')
  8. if (userInfo && userInfo.uickName) {
  9. this.setData({
  10. userInfo: userInfo
  11. })
  12. } else {
  13. }
  14. },
  15. //获取用户头像
  16. getAvatar(e) {
  17. console.log(e);
  18. this.setData({
  19. avatarUrl: e.detail.avatarUrl
  20. })
  21. },
  22. //获取用户名称
  23. getName(e) {
  24. console.log("昵称获取:", e.detail.value);
  25. this.setData({
  26. nickName: e.detail.value
  27. })
  28. },
  29. gologin() {
  30. this.setData({
  31. isHidden: false
  32. })
  33. },
  34. //退出登录
  35. tuichu() {
  36. this.setData({
  37. userInfo:null,
  38. })
  39. wx.setStorageSync('userInfo', null)
  40. },
  41. potNo() {
  42. this.setData({
  43. isHidden: true
  44. })
  45. },
  46. popYes() {
  47. console.log("头像:", this.data.avatarUrl);
  48. console.log("昵称:", this.data.nickName);
  49. let avatarUrl=this.data.avatarUrl
  50. let nickName =this.data.nickName
  51. if (!avatarUrl){
  52. wx .showToast({
  53. icon:'error',
  54. title:'请获取头像',
  55. })
  56. return
  57. }
  58. if (!nickName){
  59. wx .showToast({
  60. icon:'error',
  61. title:'请获取昵称',
  62. })
  63. return
  64. }
  65. let userInfo ={}
  66. userInfo.avatarUrl = avatarUrl
  67. userInfo.nickName = nickName
  68. wx.setStorageSync('userinfo', userInfo)
  69. this.setData({
  70. isHidden: true,
  71. userInfo: userInfo
  72. })
  73. }
  74. })