123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- // app.js
- const {login,getUserInfo} = require('./API/appraise')
- App({
- onLaunch() {
- // 展示本地存储能力
- const logs = wx.getStorageSync('logs') || []
- logs.unshift(Date.now())
- wx.setStorageSync('logs', logs)
- //开发者信息
- const wxInfo = {
- appid: 'wxc25f00d0fc026ae7',
- appSecret: '9e94011d65b0f4307b1cab14022ea060'
- }
- // const ui_w = 360;
- //把vid存到缓存
- wx.setStorageSync('vid', '')
- // 获取屏幕的宽度
- var clientWidth = wx.getSystemInfoSync().windowWidth;
- var loginOption = {
- header :{
- "Content-Type": 'application/x-www-form-urlencoded',
- },
- data:{
- vid: wx.getStorageSync('vid')
- },
- }
- var infoOption = {
- header:{
- "Authorization": wx.getStorageSync('token')
- },
- }
- //检测缓存是否有token
- // console.log(wx.getStorageSync('token'));
- if(wx.getStorageSync('token') == ''){
- // console.log('token不存在');
- // 登录
- wx.login({
- success: (res) => {
- //获取用户昵称和头像
- //和微信接口服务校验
- // console.log("code: "+res.code);
- var url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + wxInfo.appid + '&secret=' + wxInfo.appSecret + '&js_code=' + res.code + '&grant_type=authorization_code'
- wx.request({
- url:url,
- success: (res) => {
- // console.log(res);
- // console.log(res.data.openid);
- wx.setStorageSync('vid', res.data.openid);
- // console.log(wx.getStorageSync('vid'));
- loginOption.data = {
- vid: wx.getStorageSync('vid'),
- }
- // console.log(loginOption.data);
- login(loginOption).then(res => {
- // console.log(res);
- wx.setStorageSync('token', res.data.data.token);
- wx.setStorageSync('uid', res.data.data.user_info.uid);
- wx.setStorageSync('avatarUrl', res.data.data.user_info.avatar);
- wx.setStorageSync('nickName', res.data.data.user_info.name);
- }).catch(err => {
- // console.log(err);
- wx.navigateTo({
- url: '/pages/register/register',
- })
- })
- }
- })
- }
- })
- }else{
- getUserInfo(infoOption).then(res => {
- // console.log(res);
- // console.log('token有效');
- wx.setStorageSync('uid', res.data.data.uid);
- wx.setStorageSync('avatarUrl', res.data.data.avatar);
- wx.setStorageSync('nickName', res.data.data.name);
- wx.setStorageSync('personalSignatrue', res.data.data.sign);
- // console.log(wx.getStorageSync('uid'));
- }).catch(err => {
- // console.log('token已失效,请重新登录');
- wx.login({
- success: (res) => {
- //和微信接口服务校验
- // console.log("code: "+res.code);
- var url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + wxInfo.appid + '&secret=' + wxInfo.appSecret + '&js_code=' + res.code + '&grant_type=authorization_code'
- wx.request({
- url:url,
- success: (res) => {
- // console.log(res);
- // console.log(res.data.openid);
- wx.setStorageSync('vid', res.data.openid);
- // console.log(wx.getStorageSync('vid'));
- loginOption.data = {
- vid: wx.getStorageSync('vid'),
- }
- login(loginOption).then(res => {
- // console.log(res);
- wx.setStorageSync('token', res.data.data.token);
- wx.setStorageSync('uid', res.data.data.user_info.uid);
- wx.setStorageSync('avatarUrl', res.data.data.user_info.avatar);
- wx.setStorageSync('nickName', res.data.data.user_info.name);
- }).catch(err => {
- // console.log(err);
- wx.navigateTo({
- url: '/pages/register/register',
- })
- })
- }
- })
- }
- })
- })
- }
- },
- globalData: {
- userInfo: null
- }
- })
|