123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- const request = require('./WXHttpRequest.js');
- const url = require('./const.js');
- const HOST = url.apiUrl;
- function genAPIUrl(action) {
- return HOST + action;
- }
- // 登录
- function wxLogin(successcallback, failcallback) {
- wx.showToast({
- title: '登录中...',
- icon: 'loading',
- duration: 1500
- })
- wx.login({
- success: function(res) {
- if (res.code) {
- // 获取openid
- let url = genAPIUrl('wx/ucenter/openId')
- let data = { code: res.code }
- return request.getInstance().url(url).data(data).send().success(successcallback).fail(failcallback);
- } else {
- console.log('获取用户登录态失败!' + res.errMsg)
- }
- }
- })
- }
- // 获取 openid 和 session_key
- function getOpenidSessionKey(successcallback, failcallback) {
- if (wx.getStorageSync('user')) {
- successcallback(
- wx.getStorageSync('user'),
- //分享小程序按钮
- wx.showShareMenu({
- withShareTicket: true
- })
- );
- return;
- }
- wxLogin(function(res) {
- //console.log('获取sessionKey等内容'+JSON.stringify(res.data));
- let sessionKey = res.data.session_key;
- //登录
- getLoginSchedule(sessionKey, successcallback, failcallback);
- },function(res) {
- failcallback(res);
- })
- }
- //登录
- function getLoginSchedule(sessionKey, successcallback, failcallback) {
- //获取个人信息
- wx.getUserInfo({
- success: function (res) {
- let userInfo = res.userInfo
- let url = genAPIUrl('wx/ucenter/user/loginRegist');
- const userData = {
- encryptedData: res.encryptedData,
- sessionKey: sessionKey,
- iv: res.iv,
- dist: '1133',
- headimgurl: userInfo.avatarUrl
- }
- request.getInstance().url(url).data(userData).method('GET').send().success(res => {
- wx.hideToast()
- wx.showToast({
- title: '登录成功',
- icon: 'success',
- duration: 1500
- })
- wx.setStorageSync('uid', res.data.data.uid)
- wx.setStorageSync('user', res)
- successcallback(res)
- });
- },
- fail: function(res) {
- failcallback(res)
- }
- })
- //
-
- }
- module.exports = {
- getOpenidSessionKey
- }
|