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, shareUid) { 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, shareUid } 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, shareUid) { wxLogin(function(res) { if (res.data.code == 200) { console.log(res); // let openId = res.data.data.openid; // 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.data.uid) wx.setStorageSync('grade', res.data.data.data.grade) wx.setStorageSync('user', res.data.data.data) successcallback(res.data) } // }).fail(() => {}); } }, function(res) { failcallback(res); }, shareUid) } module.exports = { getOpenidNoLogin }