index.js 2.8 KB

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