index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // index.js
  2. // 获取应用实例
  3. const app = getApp()
  4. //正式环境
  5. let API_BASE_URL = 'wss://uspace.4dage.com/'
  6. let PAGE_URL = 'https://uspace.4dage.com/'
  7. //测试环境,上传发布版本要注释以下代码
  8. //API_BASE_URL = 'ws://test-xspace.4dage.com/'
  9. //PAGE_URL = 'https://test-xspace.4dage.com/'
  10. Page({
  11. data: {
  12. motto: 'Hello World',
  13. userInfo: {},
  14. url: '',
  15. hasUserInfo: false,
  16. PageOrientation: 'portrait',
  17. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  18. sIdAndrIdQuery: '',
  19. loadCompele: false,
  20. inputNickName: '',
  21. phone: ''
  22. },
  23. onShareAppMessage() {
  24. // const promise = new Promise(resolve => {
  25. // setTimeout(() => {
  26. // resolve({
  27. // title: '四维元宇宙'
  28. // })
  29. // }, 1000)
  30. // })
  31. return {
  32. title: '四维元宇宙',
  33. //path: '/page/user?id=123',
  34. //promise
  35. }
  36. },
  37. onShareTimeline() {
  38. },
  39. bindKeyInput(e) {
  40. this.setData({
  41. inputNickName: e.detail.value
  42. })
  43. },
  44. onLoad(options) {
  45. // 1、获取二维码传参
  46. if (options.scene) {
  47. this.setData({
  48. sIdAndrIdQuery: decodeURIComponent(options.scene),
  49. })
  50. }
  51. if (wx.getStorageSync('phone')) {
  52. this.setData({
  53. phone: wx.getStorageSync('phone')
  54. })
  55. }
  56. if (wx.getStorageSync('nickName')) {
  57. console.log(wx.getStorageSync('nickName'))
  58. this.setData({
  59. inputNickName: wx.getStorageSync('nickName')
  60. })
  61. }
  62. },
  63. loadcompele() {
  64. this.setData({
  65. loadCompele: true
  66. })
  67. },
  68. enterUrl() {
  69. this.setData({
  70. url: `${PAGE_URL}?avatarUrl=${encodeURIComponent(this.data.avatarUrl)}&nickName=${encodeURIComponent(this.data.nickName)}${this.data.sIdAndrIdQuery?('&'+this.data.sIdAndrIdQuery):''}`,
  71. })
  72. wx.setPageOrientation({
  73. orientation: 'landscape',
  74. })
  75. },
  76. saveNumber(num) {
  77. let userinfo = wx.getStorageSync('userInfo')
  78. wx.request({
  79. // url: 'http://192.168.20.55:8040/' + `api/wx/save/wxUser`,
  80. url: PAGE_URL + `api/wx/save/wxUser`,
  81. method: 'post',
  82. data: {
  83. ...userinfo,
  84. phone: num
  85. },
  86. dataType: 'json',
  87. success: res => {
  88. },
  89. fail: err => {
  90. console.log(err)
  91. },
  92. complete: complete => {
  93. }
  94. })
  95. },
  96. getPhoneNumber(e) {
  97. console.log(e.detail.code)
  98. if (e.detail.code) {
  99. wx.request({
  100. // url: 'http://192.168.20.55:8040/' + `api/wx/getPhone/${e.detail.code}`,
  101. url: PAGE_URL + `api/wx/getPhone/${e.detail.code}`,
  102. method: 'get',
  103. dataType: 'json',
  104. success: res => {
  105. console.log(res)
  106. if (res.data.code > -1) {
  107. wx.setStorageSync('phone', res.data.data.phoneNumber)
  108. this.saveNumber(res.data.data.phoneNumber)
  109. }
  110. },
  111. fail: err => {
  112. console.log(err)
  113. },
  114. complete: complete => {
  115. }
  116. })
  117. }
  118. },
  119. getUserProfile(e) {
  120. if (wx.getStorageSync('avatarUrl') && wx.getStorageSync('nickName')) {
  121. wx.setStorageSync('nickName', this.data.inputNickName)
  122. this.setData({
  123. avatarUrl: wx.getStorageSync('avatarUrl'),
  124. nickName: wx.getStorageSync('nickName')
  125. })
  126. this.enterUrl()
  127. } else {
  128. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  129. wx.getUserProfile({
  130. desc: '使用您的昵称,作为数字人的昵称', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  131. success: (res) => {
  132. wx.setStorageSync('userInfo', res.userInfo)
  133. let {
  134. nickName,
  135. avatarUrl
  136. } = res.userInfo
  137. this.setData({
  138. avatarUrl: avatarUrl,
  139. nickName: this.data.inputNickName.trim() || nickName
  140. })
  141. wx.setStorageSync('avatarUrl', this.data.avatarUrl)
  142. wx.setStorageSync('nickName', this.data.nickName)
  143. this.enterUrl()
  144. },
  145. fail: (error) => {
  146. console.log(error);
  147. //如果用户拒绝,使用默认昵称
  148. this.setData({
  149. avatarUrl: null,
  150. nickName: this.data.inputNickName.trim() || '微信用户'
  151. })
  152. this.enterUrl()
  153. }
  154. })
  155. }
  156. }
  157. })