index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. var app = getApp();
  2. Page({
  3. data: {
  4. isWxapp: true,
  5. isLogin: false,
  6. userInfo: {
  7. id: 0,
  8. avatar: '/assets/images/avatar.png',
  9. nickname: '游客',
  10. balance: 0,
  11. score: 0,
  12. level: 0
  13. }
  14. },
  15. onLoad: function () {
  16. var that = this;
  17. },
  18. onShow: function () {
  19. var that = this;
  20. if (app.globalData.userInfo) {
  21. that.setData({ userInfo: app.globalData.userInfo, isWxapp: that.isWxapp(), isLogin:true });
  22. }
  23. },
  24. login: function () {
  25. var that = this;
  26. wx.getUserProfile({
  27. lang:'zh',
  28. desc:'授权用户信息',
  29. success:function(res){
  30. app.login(res.userInfo,function () {
  31. that.setData({ userInfo: app.globalData.userInfo, isWxapp: that.isWxapp(), isLogin:true });
  32. });
  33. },
  34. fail:function(e){
  35. app.info(JSON.stringify(e));
  36. }
  37. })
  38. },
  39. logout: function () {
  40. var that = this;
  41. app.logout(function () {
  42. that.setData({ userInfo: {
  43. id: 0,
  44. avatar: '/assets/images/avatar.png',
  45. nickname: '游客',
  46. balance: 0,
  47. score: 0,
  48. level: 0
  49. }, isWxapp: that.isWxapp(), isLogin: false });
  50. });
  51. },
  52. isWxapp: function () {
  53. return app.globalData.userInfo ? app.globalData.userInfo.username.match(/^u\d+$/) : true;
  54. },
  55. showTips: function (event) {
  56. var tips = {
  57. balance: '余额',
  58. score: '积分',
  59. level: '等级',
  60. };
  61. var type = event.currentTarget.dataset.type;
  62. var content = tips[type];
  63. wx.showModal({
  64. title: '温馨提示',
  65. content: content,
  66. showCancel: false
  67. });
  68. },
  69. //点击头像上传
  70. uploadAvatar: function () {
  71. if (!app.globalData.userInfo) {
  72. app.error("请登录后再操作");
  73. return false;
  74. }
  75. var that = this;
  76. wx.chooseImage({
  77. success: function (res) {
  78. var tempFilePaths = res.tempFilePaths;
  79. var formData = app.globalData.config.upload.multipart;
  80. formData.token = app.globalData.userInfo.token;
  81. wx.uploadFile({
  82. url: app.globalData.config.upload.uploadurl,
  83. filePath: tempFilePaths[0],
  84. name: 'file',
  85. formData: formData,
  86. success: function (res) {
  87. if(res.statusCode != 200){
  88. app.error(res.errMsg);
  89. return;
  90. }
  91. var row = JSON.parse(res.data);
  92. if (row.code == 1) {
  93. app.request("/addons/cms/wxapp.user/avatar", { avatar: row.data.url }, function (data, ret) {
  94. app.success('头像上传成功!');
  95. app.globalData.userInfo = data.userInfo;
  96. that.setData({ userInfo: data.userInfo, isWxapp: that.isWxapp()});
  97. }, function (data, ret) {
  98. app.error(ret.msg);
  99. });
  100. }
  101. }, error: function (res) {
  102. app.error("上传头像失败!");
  103. }
  104. });
  105. }, error: function (res) {
  106. app.error("上传头像失败!");
  107. }
  108. });
  109. },
  110. goVip(e){
  111. if(!this.data.userInfo.is_install_vip){
  112. app.error('请安装VIP会员插件或启用此插件');
  113. return;
  114. }
  115. if(!this.data.isLogin){
  116. app.error('请登录后再操作');
  117. return;
  118. }
  119. wx.navigateTo({
  120. url: e.currentTarget.dataset.url,
  121. })
  122. },
  123. goPage(e){
  124. if(!this.data.isLogin){
  125. app.error('请登录后再操作');
  126. return;
  127. }
  128. wx.navigateTo({
  129. url: e.currentTarget.dataset.url,
  130. });
  131. },
  132. checkLogin(e){
  133. return;
  134. console.log(e);
  135. return false;
  136. }
  137. })