info.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. const WXAPI = require('apifm-wxapi')
  2. const AUTH = require('../../utils/auth')
  3. Page({
  4. data: {
  5. avatarUrl: undefined,
  6. avatarUrlTmpFile: undefined,
  7. gender: undefined,
  8. genderArray: [ '男性', '女性'],
  9. genderIndex: -1
  10. },
  11. onLoad: function (options) {
  12. this.getUserApiInfo()
  13. },
  14. onShow: function () {
  15. AUTH.wxaCode().then(code => {
  16. this.data.code = code
  17. })
  18. },
  19. getPhoneNumber: function(e) {
  20. if (!e.detail.errMsg || e.detail.errMsg != "getPhoneNumber:ok") {
  21. wx.showModal({
  22. title: '提示',
  23. content: e.detail.errMsg,
  24. showCancel: false
  25. })
  26. return;
  27. }
  28. WXAPI.bindMobileWxapp(wx.getStorageSync('token'), this.data.code, e.detail.encryptedData, e.detail.iv).then(res => {
  29. AUTH.wxaCode().then(code => {
  30. this.data.code = code
  31. })
  32. if (res.code === 10002) {
  33. AUTH.openLoginDialog()
  34. return
  35. }
  36. if (res.code == 0) {
  37. wx.showToast({
  38. title: '绑定成功',
  39. icon: 'success',
  40. duration: 2000
  41. })
  42. this.getUserApiInfo();
  43. } else {
  44. wx.showModal({
  45. title: '提示',
  46. content: res.msg,
  47. showCancel: false
  48. })
  49. }
  50. })
  51. },
  52. async getUserApiInfo() {
  53. const res = await WXAPI.userDetail(wx.getStorageSync('token'))
  54. if (res.code == 0) {
  55. let _data = {}
  56. _data.apiUserInfoMap = res.data
  57. _data.nick = res.data.base.nick
  58. _data.avatarUrl = res.data.base.avatarUrl
  59. if (!res.data.base.gender) {
  60. _data.gender = '未知'
  61. }
  62. if (res.data.base.gender == 1) {
  63. _data.gender = '男性'
  64. }
  65. if (res.data.base.gender == 2) {
  66. _data.gender = '女性'
  67. }
  68. this.setData(_data)
  69. }
  70. },
  71. async formSubmit(e) {
  72. console.log(e);
  73. const postData = {
  74. token: wx.getStorageSync('token'),
  75. nick: this.data.nick
  76. }
  77. if (this.data.avatarUrlTmpFile) {
  78. const res = await WXAPI.uploadFile(wx.getStorageSync('token'), this.data.avatarUrlTmpFile)
  79. if (res.code == 0) {
  80. postData.avatarUrl = res.data.url
  81. }
  82. }
  83. if (this.data.genderIndex != -1) {
  84. postData.gender = this.data.genderIndex*1 + 1
  85. }
  86. postData.extJsonStr = JSON.stringify(e.detail.value)
  87. console.log(postData);
  88. const res = await WXAPI.modifyUserInfo(postData)
  89. if (res.code != 0) {
  90. wx.showToast({
  91. title: res.msg,
  92. icon: 'none'
  93. })
  94. }
  95. wx.showToast({
  96. title: '编辑成功',
  97. })
  98. setTimeout(() => {
  99. wx.navigateBack({
  100. delta: 0,
  101. })
  102. }, 1000);
  103. },
  104. chooseImage() {
  105. const _this = this
  106. wx.chooseImage({
  107. count: 1,
  108. success (res) {
  109. _this.setData({
  110. avatarUrl: res.tempFilePaths[0],
  111. avatarUrlTmpFile: res.tempFilePaths[0]
  112. })
  113. }
  114. })
  115. },
  116. bindPickerChange: function(e) {
  117. this.setData({
  118. genderIndex: e.detail.value,
  119. gender: this.data.genderArray[e.detail.value]
  120. })
  121. },
  122. })