index.js 5.4 KB

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