|
@@ -2,13 +2,22 @@
|
|
|
import {
|
|
|
getOpenidSessionKey
|
|
|
} from '../../utils/httpUtil';
|
|
|
+import url from '../../utils/const';
|
|
|
+import request from '../../utils/WXHttpRequest';
|
|
|
+
|
|
|
+const HOST = url.baseApi;
|
|
|
+
|
|
|
+
|
|
|
+function getAPIUrl(action) {
|
|
|
+ return HOST + action;
|
|
|
+}
|
|
|
Page({
|
|
|
|
|
|
/**
|
|
|
* 页面的初始数据
|
|
|
*/
|
|
|
data: {
|
|
|
- index: undefined,
|
|
|
+ index: 0,
|
|
|
canIUseGetUserProfile: false
|
|
|
},
|
|
|
|
|
@@ -28,39 +37,100 @@ Page({
|
|
|
})
|
|
|
}
|
|
|
},
|
|
|
+ userLoginRecord: function (uid) {
|
|
|
+ if (wx.getStorageSync('uid')) {
|
|
|
+ console.log('调用方法')
|
|
|
+ let url = getAPIUrl('wx/loginLog');
|
|
|
+ return request.getInstance().header({
|
|
|
+ uid: wx.getStorageSync('uid')
|
|
|
+ }).method('POST').url(url).send().success(() => {}).fail(() => {});
|
|
|
+ }
|
|
|
+ },
|
|
|
getUserProfile: function (e) {
|
|
|
wx.getUserProfile({
|
|
|
desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
|
|
|
- success: (res) => {
|
|
|
- console.log('getUserProfile',res);
|
|
|
- getOpenidSessionKey((res) => {
|
|
|
- console.log('getUserProfilegetUserProfile',res)
|
|
|
- wx.showToast({
|
|
|
- title: '登录成功',
|
|
|
- icon: 'fail',
|
|
|
- duration: 1000,
|
|
|
- success: () => {
|
|
|
- const pages = getCurrentPages();
|
|
|
- const prevPage = pages[pages.length - 2];
|
|
|
- prevPage.setData({
|
|
|
- fromLoginIndex: this.data.index, // 有id就塞到第一位
|
|
|
- }, () => {
|
|
|
- wx.navigateBack({
|
|
|
- delta: 1
|
|
|
- })
|
|
|
+ success: (userProfile) => {
|
|
|
+ console.log('getUserProfile', userProfile);
|
|
|
+ const userInfo = userProfile.userInfo;
|
|
|
+ this.getOpenId((openidData) => {
|
|
|
+ console.log('openid', openidData)
|
|
|
+ let url = getAPIUrl('wx/user');
|
|
|
+ const userData = {
|
|
|
+ openId: openidData.data.data.openid,
|
|
|
+ unionId: openidData.data.data.unionid,
|
|
|
+ wechatName: userInfo.nickName,
|
|
|
+ gender: userInfo.gender,
|
|
|
+ avatar: userInfo.avatarUrl
|
|
|
+ }
|
|
|
+ request.getInstance().url(url).header({}).data(userData).method('POST').send().success(res => {
|
|
|
+ console.log('登陆成功', res)
|
|
|
+ wx.hideToast()
|
|
|
+ wx.showToast({
|
|
|
+ title: '登录成功',
|
|
|
+ icon: 'success',
|
|
|
+ duration: 1500,
|
|
|
+ mask: true
|
|
|
+ })
|
|
|
+ wx.setStorageSync('uid', res.data.data.data.uid)
|
|
|
+ wx.setStorageSync('user', res.data.data.data)
|
|
|
+ this.userLoginRecord();
|
|
|
+ const pages = getCurrentPages();
|
|
|
+ const prevPage = pages[pages.length - 2];
|
|
|
+ console.log(222222, prevPage)
|
|
|
+ prevPage.setData({
|
|
|
+ fromLoginIndex: this.data.index, // 有id就塞到第一位
|
|
|
+ }, () => {
|
|
|
+ wx.navigateBack({
|
|
|
+ delta: 1
|
|
|
})
|
|
|
- }
|
|
|
- })
|
|
|
- }, (error) => {
|
|
|
- wx.showToast({
|
|
|
- title: '登录失败',
|
|
|
- icon: 'fail',
|
|
|
- duration: 1000,
|
|
|
- success: () => {
|
|
|
- wx.navigateBack()
|
|
|
- }
|
|
|
- })
|
|
|
- });
|
|
|
+ })
|
|
|
+ });
|
|
|
+ })
|
|
|
+ // getOpenidSessionKey((res) => {
|
|
|
+ // console.log('getUserProfilegetUserProfile', res)
|
|
|
+ // wx.showToast({
|
|
|
+ // title: '登录成功',
|
|
|
+ // icon: 'fail',
|
|
|
+ // duration: 1000,
|
|
|
+ // success: () => {
|
|
|
+ // const pages = getCurrentPages();
|
|
|
+ // const prevPage = pages[pages.length - 2];
|
|
|
+ // prevPage.setData({
|
|
|
+ // fromLoginIndex: this.data.index, // 有id就塞到第一位
|
|
|
+ // }, () => {
|
|
|
+ // wx.navigateBack({
|
|
|
+ // delta: 1
|
|
|
+ // })
|
|
|
+ // })
|
|
|
+ // }
|
|
|
+ // })
|
|
|
+ // }, (error) => {
|
|
|
+ // wx.showToast({
|
|
|
+ // title: '登录失败',
|
|
|
+ // icon: 'fail',
|
|
|
+ // duration: 1000,
|
|
|
+ // success: () => {
|
|
|
+ // wx.navigateBack()
|
|
|
+ // }
|
|
|
+ // })
|
|
|
+ // });
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+ getOpenId: function (successcallback, failcallback) {
|
|
|
+ wx.login({
|
|
|
+ success: function (res) {
|
|
|
+ if (res.code) {
|
|
|
+ // 获取openid
|
|
|
+ console.log('openId', res.code)
|
|
|
+ let url = getAPIUrl('wx/user/openId')
|
|
|
+ let data = {
|
|
|
+ code: res.code
|
|
|
+ }
|
|
|
+ return request.getInstance().url(url).data(data).send().success(successcallback).fail(failcallback);
|
|
|
+ } else {
|
|
|
+ console.log('获取用户登录态失败!' + res.errMsg)
|
|
|
+ }
|
|
|
}
|
|
|
})
|
|
|
},
|