index.js 4.0 KB

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