index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { getUserInfo, userEdit} from '../../api/user.js';
  2. import { switchH5Login } from '../../api/api.js';
  3. import authLogin from '../../utils/autuLogin.js';
  4. import util from '../../utils/util.js';
  5. const app = getApp();
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. parameter: {
  12. 'navbar': '1',
  13. 'return': '1',
  14. 'title': '个人资料',
  15. 'color': true,
  16. 'class': '0'
  17. },
  18. userInfo:{},
  19. loginType: 'h5',//app.globalData.loginType
  20. userIndex: 0,
  21. switchUserInfo:[],
  22. },
  23. /**
  24. * 小程序设置
  25. */
  26. Setting: function () {
  27. wx.openSetting({
  28. success: function (res) {
  29. console.log(res.authSetting)
  30. }
  31. });
  32. },
  33. switchAccounts: function (e) {
  34. let index = e.currentTarget.dataset.index, userInfo = this.data.switchUserInfo[index] , that = this;
  35. that.setData({ userIndex: index });
  36. if (that.data.switchUserInfo.length <= 1) return true;
  37. if (userInfo === undefined) return app.Tips({title:'切换的账号不存在'});
  38. if (userInfo.user_type === 'h5'){
  39. wx.showLoading({ title: '正在切换中' });
  40. switchH5Login().then(res => {
  41. wx.hideLoading();
  42. app.globalData.token = res.data.token;
  43. app.globalData.expires_time = res.data.time;
  44. app.globalData.loginType = 'h5';
  45. app.globalData.userInfo = res.data.userInfo;
  46. that.getUserInfo();
  47. }).catch(err => {
  48. wx.hideLoading();
  49. return app.Tips({ title: err });
  50. })
  51. }else{
  52. wx.showLoading({ title: '正在切换中' });
  53. authLogin('routine').then(res => {
  54. that.getUserInfo();
  55. wx.hideLoading();
  56. }).catch(err=>{
  57. wx.hideLoading();
  58. return app.Tips({ title: err });
  59. });
  60. }
  61. },
  62. /**
  63. * 授权回调
  64. */
  65. onLoadFun:function(){
  66. this.getUserInfo();
  67. },
  68. /**
  69. * 退出登录
  70. *
  71. */
  72. outLogin:function(){
  73. if (this.data.loginType == 'h5'){
  74. app.globalData.token = '';
  75. app.globalData.isLog = false;
  76. app.globalData.userInfo = {};
  77. app.globalData.expiresTime = 0;
  78. wx.showLoading({
  79. title: '正在退出登录',
  80. });
  81. return wx.switchTab({
  82. url: '/pages/index/index',
  83. success: function () {
  84. wx.hideLoading();
  85. }
  86. });
  87. }
  88. },
  89. /**
  90. * 生命周期函数--监听页面加载
  91. */
  92. onLoad: function (options) {
  93. },
  94. getPhoneNumber:function(e){
  95. var detail = e.detail, cache_key = wx.getStorageSync('cache_key'),that=this;
  96. if (detail.errMsg =='getPhoneNumber:ok'){
  97. if (!cache_key){
  98. app.globalData.token='';
  99. app.globalData.isLog=false;
  100. return false;
  101. }
  102. }else{
  103. app.Tips({ title:'取消授权'});
  104. }
  105. },
  106. /**
  107. * 获取用户详情
  108. */
  109. getUserInfo:function(){
  110. var that=this;
  111. getUserInfo().then(res=>{
  112. that.setData({ userInfo: res.data, switchUserInfo: res.data.switchUserInfo || [] });
  113. for(let i=0;i<that.data.switchUserInfo.length;i++){
  114. if (that.data.switchUserInfo[i].uid === that.data.userInfo.uid){
  115. that.setData({userIndex:i});
  116. }
  117. }
  118. });
  119. },
  120. /**
  121. * 上传文件
  122. *
  123. */
  124. uploadpic: function () {
  125. var that = this;
  126. util.uploadImageOne('upload/image', function (res){
  127. var userInfo = that.data.switchUserInfo[that.data.userIndex];
  128. if (userInfo !== undefined){
  129. userInfo.avatar = res.data.url;
  130. }
  131. that.data.switchUserInfo[that.data.userIndex] = userInfo;
  132. that.setData({ switchUserInfo: that.data.switchUserInfo });
  133. });
  134. },
  135. /**
  136. * 提交修改
  137. */
  138. formSubmit:function(e){
  139. var that = this, value = e.detail.value,userInfo = that.data.switchUserInfo[that.data.userIndex];
  140. if (!value.nickname) return app.Tips({title:'用户姓名不能为空'});
  141. value.avatar = userInfo.avatar;
  142. userEdit(value).then(res=>{
  143. return app.Tips({ title: res.msg, icon: 'success' }, { tab: 3, url: 1 });
  144. }).catch(msg=>{
  145. return app.Tips({ title: msg || '保存失败,您并没有修改' }, { tab: 3, url: 1 });
  146. });
  147. },
  148. })