1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- const request = require('./WXHttpRequest.js');
- const url = require('./const.js');
- const util = require('./util.js');
- const httpRequestApi = require('./APIClient');
- const HOST = url.baseApi;
- function genAPIUrl(action) {
- return HOST + action;
- }
- // 登录
- function wxLogin(successcallback, failcallback) {
- // wx.showToast({
- // title: '加载中...',
- // icon: 'loading',
- // duration: 1500,
- // mask: true
- // })
- wx.login({
- success: function (res) {
- if (res.code) {
- // 获取openid
- console.log('登录登录登录登录code', res.code)
- let url = genAPIUrl('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)
- }
- }
- })
- }
- // 获取原来的 openid 和 session_key
- /* 新增参数grade 需要给游客添加grade参数 */
- function getOpenidNoLogin(successcallback, failcallback, grade) {
- // if (wx.getStorageSync('user')) {
- // successcallback(
- // wx.getStorageSync('user'),
- // //分享小程序按钮
- // wx.showShareMenu({
- // withShareTicket: true
- // })
- // );
- // return;
- // }
- wxLogin(function (res) {
- console.log('wx.login', res)
- if (res.data.code == 200) {
- let sessionKey = res.data.data.session_key;
- let openId = res.data.data.openid;
- // successcallback()
- // wx.setStorageSync('uid', '111')
- let url = genAPIUrl('wx/user/register');
- request.getInstance().method('POST').url(url).header({
- 'content-type': 'application/json'
- }).data({
- openId,
- grade: grade ? grade : null
- }).send().success((res) => {
- if (res.data.code === 200) {
- wx.setStorageSync('uid', res.data.data.uid)
- wx.setStorageSync('grade', res.data.data.grade)
- wx.setStorageSync('user', res.data.data)
- successcallback(res)
- }
- }).fail(() => {});
- }
- }, function (res) {
- failcallback(res);
- })
- }
- module.exports = {
- getOpenidNoLogin
- }
|