123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- //app.js
- const util = require("util/util.js")
- App({
- onLaunch: function() {
- // 初始化配置
- this.autoUpgrade()
- 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;
- }
- this.globalData.CustomRate = e.screenHeight / e.screenWidth;
- }
- })
- },
- autoUpgrade: function(){
- if (wx.canIUse('getUpdateManager')) {
- const updateManager = wx.getUpdateManager()
- updateManager.onCheckForUpdate(function (res) {
- // 请求完新版本信息的回调
- if (res.hasUpdate) {
- console.log('res.hasUpdate====')
- updateManager.onUpdateReady(function () {
- wx.showModal({
- title: '更新提示',
- content: '新版本已经准备好,是否重启应用?',
- success: function (res) {
- console.log('success====', res)
- if (res.confirm) {
- updateManager.applyUpdate()
- }
- }
- })
- })
- updateManager.onUpdateFailed(function () {
- // 新的版本下载失败
- wx.showModal({
- title: '已经有新版本了哟~',
- content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~'
- })
- })
- }
- })
- }
- },
- // 微信登入
- doLogin: function( cb, info ={} ){
- let that = this
- wx.login({
- success: res => {
- if (res.code) {
- let param = Object.assign({ code: res.code }, info);
- //发起网络请求
- util.http("/base/wxLogin", param, (errCode, data) => {
- if( errCode == 0){
- let {user, token,expiresAt} = data
- user.token = token;
- user.expiresAt = expiresAt;
- this.globalData.userInfo = user
- console.log("login_ok_u", user)
- wx.setStorageSync("@yunyuanqu", user)
- this.getMakerInfo(cb, true);
- }else{
- util.showMsg( data )
- cb&&cb( {} )
- }
- });
- } else {
- console.log('登录失败!' + res.errMsg)
- }
- }
- })
- },
- checkLogin:function( cb ){
- let info = this.globalData.userInfo;
- if (!info.token){
- let cacheUser = wx.getStorageSync("@yunyuanqu");
- console.log("cacheUser", cacheUser)
- if( !cacheUser || cacheUser.expiresAt < Date.now() ){
- wx.navigateTo({
- url: '/pages/login/login',
- })
- }else{
- this.globalData.userInfo = cacheUser
- cb&&cb( cacheUser )
- }
- }else{
- cb&&cb( info )
- }
- },
- getSysInfo: function (cb) {
- wx.getSystemInfo({
- success: function (res) {
- typeof cb == "function" && cb(res);
- }
- })
- },
- goHome() {
- wx.navigateTo({
- url: '/pages/index/index',
- })
- },
- getLocation: function (cb) {
- var that = this;
- wx.getLocation({
- type: 'wgs84',
- success: function (res) {
- console.log(res)
- typeof cb == "function" && cb(res);
- }
- })
- },
- checkPermission( cb ){
- wx.getSetting({
- success: function (res) {
- if (!res.authSetting['scope.userLocation']) {
- //申请授权
- wx.authorize({
- scope: 'scope.userLocation',
- success() {
- console.log("authorize success")
- cb&&cb()
- },
- fail(){
- console.log("authorize fail")
- }
- })
- }else{
- cb&&cb()
- }
- },
- fail: function(res){
- console.log("fail", res)
- }
- })
- },
- getLocation(){
- return this.globalData.location;
- },
- setLocaltion( location ){
- this.globalData.location = location;
- },
- getUserInfo( ){
- return this.globalData.userInfo
- },
- setUserInfo( userInfo ){
- userInfo = Object.assign(this.globalData.userInfo, userInfo )
- this.globalData.userInfo = userInfo;
- },
- setMakerInfo( maker ){
- maker = Object.assign(this.globalData.maker, maker )
- this.globalData.maker = maker;
- console.log( "setMakerInfo", maker)
- },
- identifyUser(){
- this.globalData.userInfo.identify=1
- },
- getMakerInfo( cb, noCache ){
- if( !noCache && this.globalData.maker.id ){
- cb&&cb( this.globalData.maker);
- }else{
- util.http( '/wx/getMakerInfo', {}, (err,res)=>{
- if( err == 0 ) {
- this.globalData.maker = res.info
- cb&&cb( res.info );
- }else{
- cb&&cb( {} );
- }
- })
- }
- },
- setTaskTab(val){
- this.globalData.taskTab = val||0;
- },
- setAction(val){
- this.globalData.action = val;
- },
- getAction(){
- return this.globalData.action;
- },
- globalData: {
- taskTab:0,
- signUrl:"https://yyq2.oss-cn-shanghai.aliyuncs.com/pdf/sign.jpg",
- action: false,
- userInfo: {},
- location: {},
- maker: {},
- Custom:0,
- CustomBar:0,
- CustomRate:0,
- StatusBar:0
- }
- })
|