|
- // index.js
- // 获取应用实例
- const app = getApp()
-
- Page({
- data: {
- ischecked: true,
- bindInfo: false,
- userid:'',
- token: ''
- },
- onLoad() {
- this.loginGettoken()
- },
- backGame(){
- wx.navigateTo({
- url: ''
- })
- },
- checkFun(){
- this.setData({
- ischecked: !this.data.ischecked
- })
- },
- // 协议
- toxieyi(){
-
- },
- //登录-获取token
- loginGettoken(){
- let that = this
- wx.login({
- success (res) {
- if (res.code) {
- //发起网络请求
- wx.request({
- url:'https://cktest.2weisou.com/meta/auth/mobile/token/social?grant_type=mobil&mobile=MINI_C@'+res.code+'@'+'',
- method: "POST",
- data: {
- },
- header: {
- Authorization: "Basic YXBwbGV0OmFwcGxldA==",
- },
- success(res1) {
- if (res1.statusCode == 200) {
- that.setData({
- token: res1.data.access_token,
- userid: res1.data.user_info.id
- })
- }
- }
- })
- } else {
- console.log('登录失败!' + res.errMsg)
- }
- }
- })
- },
- // 绑定用户头像信息
- bindNickname(obj) {
- let that = this
- let params = {
- id: that.data.userid,
- userWxHeadImgUrl: obj.avatarUrl,
- userWxName: obj.nickName,
- userVxGender: obj.gender
- }
- wx.request({
- url: 'https://cktest.2weisou.com/meta/ar/user/bindNickname',
- method: "POST",
- data: params,
- header: {
- Authorization: 'bearer '+ that.data.token
- },
- success(res) {
- if (res.statusCode == 200) {
- that.setData({
- bindInfo: true
- })
- console.log('绑定用户头像信息成功')
- }
- }
- })
- },
- getUserInfo(){
- if(!this.data.ischecked){
- wx.showToast({
- title: '请勾选协议',
- icon: 'none',
- duration: 2000
- })
- return;
- }
- let that = this
- wx.getUserProfile({
- desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
- success: (res) => {
- console.log(res)
- that.bindNickname(res.userInfo)
- }
- })
- },
- // 授权手机号
- getPhoneNumber(e) {
- console.log(e);
- let that = this
- if (e.detail.errMsg == "getPhoneNumber:ok") {
- const {
- iv,
- encryptedData
- } = e.detail;
- wx.request({
- url: 'https://cktest.2weisou.com/meta/ar/user/bindMini',
- method: "POST",
- data: {
- encrypted: encryptedData,
- iv
- },
- header: {
- Authorization: 'bearer '+ that.data.token
- },
- success(res) {
- console.log(res)
- if (res.statusCode == 200) {
- // console.log(res)
- }
- }
- })
- } else {
- wx.showToast({
- icon: "none",
- title: "请授权手机号"
- })
- }
- }
- })
|