123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- //app.js
- App({
- globalData: {
- title: '晋爱人才',
- // http_host: 'http://bd.jarc.com/',
- http_host: 'https://jarc.jucai.gov.cn/',
- // api_host: 'http://bd.jarc.com/api/',
- api_host: 'https://jarc.jucai.gov.cn/api/',
- },
- onLaunch: function() {
- var self = this;
- // 登录
- if (!this.checkLogin()) {
- wx.login({
- success: res => {
- // 发送 res.code 到后台换取 openId, sessionKey, unionId
- self.post('applet/applet/login',{code:res.code},function(res){
- wx.setStorageSync('token', res.token);
- wx.setStorageSync('token_exp', res.expires_time);
- self.get('user/profile/userInfo',function(res){
- self.globalData.userInfo = res;
- },false)
- },false);
- }
- })
- } else {
- self.get('user/profile/userInfo',function(res){
- self.globalData.userInfo = res;
- },false)
- }
- // 获取系统状态栏信息
- wx.getSystemInfo({
- success: e => {
- this.globalData.StatusBar = e.statusBarHeight;
- let capsule = wx.getMenuButtonBoundingClientRect();
- if (capsule) {
- this.globalData.Custom = capsule;
- this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
- } else {
- this.globalData.CustomBar = e.statusBarHeight + 50;
- }
- }
- });
- },
- /**
- * 弹出框
- */
- msg(title, url = '', time = 2000, mask = false) {
- wx.showToast({
- title: title,
- icon: 'none',
- mask: mask,
- duration: time
- });
- if (url) {
- if (typeof url == 'function') {
- setTimeout(url,time);
- } else {
- setTimeout(function(){
- wx.navigateTo({url: url});
- },time);
- }
- }
- },
- loading(title) {
- wx.showLoading({
- title: title,
- icon: 'none',
- mask: true
- })
- },
- //服务器请求
- post: function (domain, param, success, is_load=true, error='') {
- if (is_load) {
- wx.showLoading({
- title: '加载中',
- mask: true,
- });
- }
- var that = this;
- var token = wx.getStorageSync('token');
- wx.request({
- url: that.globalData.api_host + domain,
- data : param,
- header: { 'XX-Device-Type': 'wxapp' , 'XX-Token': token},
- method: 'post',
- success: function (res) {
- if (is_load) {
- wx.hideLoading();
- }
- if (res.data.code == 1) {
- if (success != undefined) {
- success.call(this, res.data.data);
- }
- } else if (res.data.code == 10001) {
- wx.clearStorageSync();
- wx.showToast({
- title: '小程序异常,请退出后重新进入',
- icon: 'none',
- duration: 2000
- })
- } else {
- if (typeof error === 'function') {
- error(res.data);
- } else {
- wx.showToast({
- title: res.data.msg,
- icon: 'none',
- duration: 2000
- })
- }
- }
- },
- error : function () {
- if (is_load) {
- wx.hideLoading();
- }
- wx.showToast({
- title: '网络异常,请退出小程序重新登录',
- icon: 'loading'
- })
- }
- })
- },
- //服务器请求
- get: function (domain, success, is_load=true, error='') {
- if (is_load) {
- wx.showLoading({
- title: '加载中',
- mask: true,
- });
- }
- var that = this;
- var token = wx.getStorageSync('token');
- wx.request({
- url: that.globalData.api_host + domain,
- header: { 'XX-Device-Type': 'wxapp' , 'XX-Token': token},
- success: function (res) {
- if (is_load) {
- wx.hideLoading();
- }
- if (res.data.code == 1) {
- if (success != undefined) {
- success.call(this, res.data.data);
- }
- } else if (res.data.code == 10001) {
- wx.clearStorageSync();
- wx.showToast({
- title: '小程序异常,请退出后重新进入',
- icon: 'none',
- duration: 2000
- })
- } else {
- if (typeof error === 'function') {
- error(res.data);
- } else {
- wx.showToast({
- title: res.data.msg,
- icon: 'none',
- duration: 2000
- })
- }
- }
- },
- error : function () {
- if (is_load) {
- wx.hideLoading();
- }
- wx.showToast({
- title: '网络异常,请退出小程序重新登录',
- icon: 'loading'
- })
- }
- })
- },
- /**
- * 登录验证
- */
- checkLogin: function () {
- let token = wx.getStorageSync('token');
- if (!token) {
- return false;
- }
- let token_exp = wx.getStorageSync('token_exp');
- let now = Date.parse(new Date())/1000
- if (token_exp < now) {
- return false;
- }
- return true;
- },
- loading: function (title) {
- wx.showLoading({
- title: title,
- icon: 'none',
- mask: true
- })
- },
- confirm: function (content,success) {
- wx.showModal({
- title: '提示',
- content: content,
- success(res) {
- if (res.confirm) {
- wx.hideToast();
- success(res);
- } else {
- wx.hideToast();
- }
- }
- })
- }
- })
|