碧桂园
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

197 linhas
4.7 KiB

  1. // index.js
  2. // 获取应用实例
  3. const app = getApp()
  4. Page({
  5. data: {
  6. avatarModal: false,
  7. phoneModal: false,
  8. phoneModal1: false,
  9. showBg: false,
  10. step: 0,// 头像授权1, 手机授权2
  11. nickNamebind: false,
  12. phonebind: false,
  13. userInfo: null,
  14. userid:'',
  15. token: ''
  16. },
  17. onLoad() {
  18. this.loginGettoken()
  19. },
  20. backhome(){
  21. wx.navigateTo({
  22. url: '/pages/index/index'
  23. })
  24. },
  25. shouquan(){
  26. let userInfo = wx.getStorageSync('userInfo')
  27. if(userInfo.phone){
  28. wx.showToast({
  29. icon: 'none',
  30. title: '您已完成授权',
  31. })
  32. }
  33. if(this.data.step==1){
  34. this.setData({
  35. avatarModal: true,
  36. showBg: true
  37. })
  38. }
  39. if(this.data.step==2){
  40. this.setData({
  41. phoneModal: true,
  42. showBg: true
  43. })
  44. }
  45. },
  46. closeModal(){
  47. this.setData({
  48. showBg: false
  49. })
  50. },
  51. leave(){
  52. wx.navigateTo({
  53. url: '/pages/out/index?index=1&userid='+ this.data.userid+'&gameid=1'+'&type=2',
  54. })
  55. },
  56. //登录-获取token
  57. loginGettoken(){
  58. let that = this
  59. wx.login({
  60. success (res) {
  61. if (res.code) {
  62. //发起网络请求
  63. wx.request({
  64. url:'https://cktest.2weisou.com/meta/auth/mobile/token/social?grant_type=mobil&mobile=MINI_C@'+res.code+'@'+'',
  65. method: "POST",
  66. data: {
  67. },
  68. header: {
  69. Authorization: "Basic YXBwbGV0OmFwcGxldA==",
  70. },
  71. success(res1) {
  72. if (res1.statusCode == 200) {
  73. let userInfo = res1.data.user_info
  74. that.setData({
  75. token: res1.data.access_token,
  76. userid: userInfo.id,
  77. userInfo: userInfo
  78. })
  79. wx.setStorageSync('token', res1.data.access_token)
  80. wx.setStorageSync('userInfo', userInfo)
  81. if(!!userInfo.userWxName&&!!userInfo.phone){
  82. that.leave()
  83. return;
  84. }
  85. if(!userInfo.userWxName||!userInfo.phone){
  86. that.setData({
  87. showBg: true
  88. })
  89. }else{
  90. that.setData({
  91. showBg: false
  92. })
  93. }
  94. that.setData({
  95. avatarModal: !!userInfo.userWxName?false:true,
  96. phoneModal1: !!userInfo.phone?false:true,
  97. step: 1
  98. })
  99. if(!that.data.avatarModal){
  100. that.setData({
  101. step: 2,
  102. phoneModal: that.data.phoneModal1
  103. })
  104. }
  105. }
  106. }
  107. })
  108. } else {
  109. console.log('登录失败!' + res.errMsg)
  110. }
  111. }
  112. })
  113. },
  114. // 绑定用户头像信息
  115. bindNickname(obj) {
  116. let that = this
  117. let params = {
  118. id: that.data.userid,
  119. userWxHeadImgUrl: obj.avatarUrl,
  120. userWxName: obj.nickName,
  121. userVxGender: obj.gender
  122. }
  123. wx.request({
  124. url: 'https://cktest.2weisou.com/meta/ar/user/bindNickname',
  125. method: "POST",
  126. data: params,
  127. header: {
  128. Authorization: 'bearer '+ that.data.token
  129. },
  130. success(res) {
  131. if (res.statusCode == 200) {
  132. that.setData({
  133. avatarModal: false,
  134. step: 2
  135. })
  136. if(that.data.phoneModal1){
  137. that.setData({
  138. phoneModal: true
  139. })
  140. }
  141. }
  142. }
  143. })
  144. },
  145. getUserInfo(){
  146. let that = this
  147. wx.getUserProfile({
  148. desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  149. success: (res) => {
  150. // console.log(res)
  151. that.bindNickname(res.userInfo)
  152. }
  153. })
  154. },
  155. // 授权手机号
  156. getPhoneNumber(e) {
  157. console.log(e);
  158. let that = this
  159. if (e.detail.errMsg == "getPhoneNumber:ok") {
  160. const {
  161. iv,
  162. encryptedData
  163. } = e.detail;
  164. wx.request({
  165. url: 'https://cktest.2weisou.com/meta/ar/user/bindMini',
  166. method: "POST",
  167. data: {
  168. encrypted: encryptedData,
  169. iv
  170. },
  171. header: {
  172. Authorization: 'bearer '+ that.data.token
  173. },
  174. success(res) {
  175. console.log(res)
  176. if (res.data.code == 0) {
  177. // console.log(res)
  178. that.setData({
  179. phoneModal: false,
  180. showBg: false,
  181. step: 0
  182. })
  183. that.leave()
  184. }
  185. }
  186. })
  187. } else {
  188. wx.showToast({
  189. icon: "none",
  190. title: "请授权手机号"
  191. })
  192. }
  193. }
  194. })