info.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. this._getPhoneNumber(e)
  29. },
  30. async _getPhoneNumber(e) {
  31. let res
  32. const extConfigSync = wx.getExtConfigSync()
  33. if (extConfigSync.subDomain) {
  34. // 服务商模式
  35. res = await WXAPI.wxappServiceBindMobile({
  36. token: wx.getStorageSync('token'),
  37. code: this.data.code,
  38. encryptedData: e.detail.encryptedData,
  39. iv: e.detail.iv,
  40. })
  41. } else {
  42. res = await WXAPI.bindMobileWxapp(wx.getStorageSync('token'), this.data.code, e.detail.encryptedData, e.detail.iv)
  43. }
  44. AUTH.wxaCode().then(code => {
  45. this.data.code = code
  46. })
  47. if (res.code === 10002) {
  48. AUTH.login(this)
  49. return
  50. }
  51. if (res.code == 0) {
  52. wx.showToast({
  53. title: '绑定成功',
  54. icon: 'success',
  55. duration: 2000
  56. })
  57. this.getUserApiInfo();
  58. } else {
  59. wx.showModal({
  60. title: '提示',
  61. content: res.msg,
  62. showCancel: false
  63. })
  64. }
  65. },
  66. async getUserApiInfo() {
  67. const res = await WXAPI.userDetail(wx.getStorageSync('token'))
  68. if (res.code == 0) {
  69. let _data = {}
  70. _data.apiUserInfoMap = res.data
  71. _data.nick = res.data.base.nick
  72. _data.avatarUrl = res.data.base.avatarUrl
  73. if (!res.data.base.gender) {
  74. _data.gender = '未知'
  75. }
  76. if (res.data.base.gender == 1) {
  77. _data.gender = '男性'
  78. }
  79. if (res.data.base.gender == 2) {
  80. _data.gender = '女性'
  81. }
  82. this.setData(_data)
  83. }
  84. },
  85. async formSubmit(e) {
  86. console.log(e);
  87. const postData = {
  88. token: wx.getStorageSync('token'),
  89. nick: this.data.nick
  90. }
  91. if (this.data.avatarUrlTmpFile) {
  92. const res = await WXAPI.uploadFile(wx.getStorageSync('token'), this.data.avatarUrlTmpFile)
  93. if (res.code == 0) {
  94. postData.avatarUrl = res.data.url
  95. }
  96. }
  97. if (this.data.genderIndex != -1) {
  98. postData.gender = this.data.genderIndex*1 + 1
  99. }
  100. postData.extJsonStr = JSON.stringify(e.detail.value)
  101. console.log(postData);
  102. const res = await WXAPI.modifyUserInfo(postData)
  103. if (res.code != 0) {
  104. wx.showToast({
  105. title: res.msg,
  106. icon: 'none'
  107. })
  108. }
  109. wx.showToast({
  110. title: '编辑成功',
  111. })
  112. setTimeout(() => {
  113. wx.navigateBack({
  114. delta: 0,
  115. })
  116. }, 1000);
  117. },
  118. chooseImage() {
  119. const _this = this
  120. wx.chooseImage({
  121. count: 1,
  122. success (res) {
  123. _this.setData({
  124. avatarUrl: res.tempFilePaths[0],
  125. avatarUrlTmpFile: res.tempFilePaths[0]
  126. })
  127. }
  128. })
  129. },
  130. bindPickerChange: function(e) {
  131. this.setData({
  132. genderIndex: e.detail.value,
  133. gender: this.data.genderArray[e.detail.value]
  134. })
  135. },
  136. })