|
- //app.js
- App({
- onLaunch: function () {
- // 展示本地存储能力
- // var logs = wx.getStorageSync('logs') || []
- // logs.unshift(Date.now())
- // wx.setStorageSync('logs', logs)
- var openid = wx.getStorageSync('openid') || "";
- console.log(this.globalData.present_id)
- // 登录
- if (!openid){
- wx.login({
- success: res => {
- // 发送 res.code 到后台换取 openId, sessionKey, unionId
- // console.log(res)
- if (res.code){
- wx.request({
- url: this.globalData.httpUrl+'openid',
- header: {
- code: res.code
- },
- success: e => {
- // console.log(e)
- if (e.data.code == 200) {
- this.globalData.openId = e.data.data.openid;
- this.globalData.session_key = e.data.data.session_key;
- this.globalData.token = e.data.data.token;
- wx.setStorageSync('openid', e.data.data.openid)
- wx.setStorageSync('session_key', e.data.data.session_key)
- wx.setStorageSync('token', e.data.data.token)
-
- }
- }
- })
- } else {
- console.log("登陆失败" + res.errMsg)
- }
- }
- })
- }else{
- this.globalData.openid = openid;
- }
- this.getUserType();
- // 获取用户信息
- // wx.getSetting({
- // success: res => {
- // console.log(res)
- // if (res.authSetting['scope.userInfo']) {
- // // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
- // wx.getUserInfo({
- // success: e => {
- // // 可以将 res 发送给后台解码出 unionId
- // this.globalData.userInfo = e.userInfo
- // // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
- // // 所以此处加入 callback 以防止这种情况
- // if (this.userInfoReadyCallback) {
- // this.userInfoReadyCallback(res)
- // }
- // }
- // })
- // }else{
- // wx.authorize({
- // scope: 'scope.record',
- // success() {
- // wx.getUserInfo({
- // success: e => {
- // console.log(e)
- // }
- // })
- // }
- // })
- // }
- // }
- // })
- },
- getUserType:function(){//获取用户注册状态
-
- this.wxRequest(this.globalData.httpUrl + 'getinfo',{},e=>{
- // console.log(e)
- if(e.code==201){//未注册跳到注册页面
- wx.reLaunch({
- url: '/pages/register/register'
- })
- } else if (e.code == 202){//已注册跳到首页购券大厅
- wx.switchTab({
- url: '/pages/index/index'
- })
- }
- },this)
- },
- globalData: {
- userInfo: null,
- present_id:"",//判断用户是通过领取优惠券进来的还是直接进的
- number:"",//赠送优惠券的数量
- openId:"",
- session_key:"",
- token:"",
- userInfo:"",
- httpUrl:"https://laomenkuang.jiyou-tech.com/apiWx/"
- },
- wxRequest: function (url, params, callback, thisArg, methods, openid) {
- let that = this;
- // console.log(this.globalData.openid)
- var httpUrl = url;
- var str = "";
- var count = 0;
- for (let key in params) {
- if (count) {
- str += "&" + key + "=" + params[key];
- } else {
- str += key + "=" + params[key];
- }
- count++;
- }
- if (str) {
- httpUrl += "?" + str;
- }
- if (!methods) {
- methods = "GET";
- }
- if (methods == "POST") {
- wx.request({
- url: url,
- data: params,
- method: methods,
- header: {
- "content-type": "application/x-www-form-urlencoded",
- "openid": this.globalData.openid
- },
- success: function (re) {
- // if (re.data.code == -1002) {
- // wx.removeStorageSync('openid');
- // that.wxLogin();
- // // that.wxRequest(url, params, callback, thisArg, methods, openid);
- // }
- if (callback && thisArg) {
- callback.call(thisArg, re.data);
- }
- },
- fail: function (re) {
- wx.hideLoading();
- }
- })
- } else {
- wx.request({
- url: httpUrl,
- method: methods,
- header: {
- "openid": this.globalData.openid,
- },
- success: function (re) {
- // if (re.data.code == -1002) {
- // wx.removeStorageSync('openid');
- // that.wxLogin();
- // // that.wxRequest(url, params, callback, thisArg, methods, openid);
- // }
- if (callback && thisArg) {
- callback.call(thisArg, re.data);
- }
- },
- fail: function (res) {
- wx.hideLoading();
- }
- })
- }
- },
- // requestGet:function(url,data,callback){//get接口调用
- // wx.request({
- // url: url,
- // header: {
- // "openid": this.globalData.openId
- // },
- // data: data,
- // success:res=>{
- // if (callback) {
-
- // }
- // }
-
-
- // })
- // },
- // requestPost: function (url, data) {//post接口调用
- // wx.request({
- // url: url,
- // header: {
- // "Content-Type":"application/x-www-form-urlencoded",
- // openid: this.globalData.openId
- // },
- // data: data,
- // success(res) {
- // console.log(res.data)
- // }
- // })
- // }
- })
|