index.js 4.6 KB

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