zoukankan      html  css  js  c++  java
  • app.js

      1 import SiteInfo from "siteInfo.js";
      2 App({
      3   onShow: function () {
      4     // wx.checkSession({ //检测当前用户的session_key是否过期
      5     //   success: function() { //session_key 未过期,并且在本生命周期一直有效
      6 
      7     //   },
      8     //   fail: function() { //session_key 已经失效,需要重新执行登录流程
      9     //     console.warn("授权过期,重新登录小程序")
     10     //     wx.navigateTo({
     11     //       url: "/pages/openauth/index" //重新授权
     12     //     })
     13     //   }
     14     // })
     15   },
     16   onLaunch: function () {
     17     // console.log("授权未过期")
     18     let vm = this;
     19     wx.showToast({
     20       title: '欢迎来到 nunumua !',
     21       icon: "none",
     22       mask: true
     23     })
     24     //更新版本
     25     vm.CheckVersion();
     26     let promise = this.SetHeader();
     27     promise.then(res => {
     28     }).catch(errMsg => {
     29       console.error(errMsg)
     30     });
     31   },
     32   /**检查小程序版本更新 */
     33   CheckVersion: function () {
     34     const updateManager = wx.getUpdateManager()
     35 
     36     updateManager.onCheckForUpdate(function (res) {
     37       // 请求完新版本信息的回调
     38       // console.log(res.hasUpdate)
     39     })
     40 
     41     updateManager.onUpdateReady(function () {
     42       wx.showModal({
     43         title: '更新提示',
     44         content: '新版本已经准备好,是否重启应用?',
     45         success: function (res) {
     46           if (res.confirm) {
     47             // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
     48             updateManager.applyUpdate()
     49           }
     50         }
     51       })
     52     })
     53 
     54     updateManager.onUpdateFailed(function () {
     55       // 新版本下载失败
     56       console.error('新版本下载失败!')
     57       wx.showToast({
     58         title: '新版本下载失败,请检查你的网路链接是否通畅!',
     59         icon: 'none',
     60         duration: 2000,
     61         mask: true
     62       })
     63     })
     64   },
     65   /**检查是否授权 */
     66   CheckAuth: function () {
     67     var that = this;
     68     // 查看是否授权
     69     wx.getSetting({
     70       success(res) {
     71         if (res.authSetting['scope.userInfo']) {
     72           // 已经授权,可以直接调用 getUserInfo 获取头像昵称
     73           wx.getUserInfo({
     74             success: function (res) {
     75               App.globalData.userInfo = res.userInfo; //更新用户信息
     76             }
     77           });
     78           //是否授权获取地理位置
     79           if (!res.authSetting['scope.userLocation']) {
     80             wx.navigateTo({
     81               url: '/pages/openauth/index', //跳转到授权页面
     82             })
     83           }
     84         } else {
     85           wx.navigateTo({
     86             url: '/pages/openauth/index', //跳转到授权页面
     87           })
     88         }
     89       }
     90     })
     91   },
     92   /**小程序登录:调用接口获取登录凭证(code) */
     93   WxLogin: function () {
     94     return new Promise((resolve, reject) => {
     95       let vm = this;
     96       wx.showLoading({
     97         title: '登录小程序',
     98         mask: true
     99       });
    100       wx.login({
    101         success(res) {
    102           if (res.code) {
    103             //发起网络请求
    104             wx.request({
    105               url: SiteInfo["Siteroot"] + '/api/login',
    106               header: {
    107                 Version: SiteInfo["Version"]
    108               },
    109               method: 'POST',
    110               data: {
    111                 code: res.code
    112               },
    113               success(res) {
    114                 res.data && res.data.code == 200 ? resolve(res.data.data) : reject(res.data.message);
    115               },
    116               fail(res) {
    117                 wx.hideLoading();
    118                 console.log('登录失败!' + res.errMsg)
    119               },
    120               complete: function (res) {
    121                 wx.hideLoading();
    122               },
    123             });
    124           } else {
    125             wx.hideLoading();
    126             console.log('登录失败!' + res.errMsg)
    127           }
    128         },
    129         fail(res) {
    130           console.error('调用小程序登录接口失败!' + res.errMsg)
    131           wx.showToast({
    132             title: '登录小程序失败,请检查你的网路链接是否通畅',
    133             icon: 'none',
    134             duration: 2000,
    135             mask: true
    136           })
    137         }
    138       })
    139     });
    140   },
    141   /** 自定义头部:返回按钮位置 */
    142   SetHeader: function () {
    143     return new Promise((resolve, reject) => {
    144       const vm = this;
    145       wx.getSystemInfo({
    146         success: function (res) {
    147           let totalTopHeight = 82
    148           if (res.model.indexOf('iPhone X') !== -1) {
    149             totalTopHeight = 110
    150           } else if (res.model.indexOf('iPhone') !== -1) {
    151             totalTopHeight = 64
    152           }
    153           vm.globalData.statusBarHeight = res.statusBarHeight;
    154           vm.globalData.titleBarHeight = totalTopHeight - res.statusBarHeight;
    155           resolve(res);
    156         },
    157         failure() {
    158           vm.globalData.statusBarHeight = 0;
    159           vm.globalData.titleBarHeight = 0;
    160           reject("获取系统信息失败!");
    161         }
    162       })
    163     })
    164 
    165   },
    166   /** 开始选择地理位置 */
    167   ChooseLocation: function () {
    168     let vm = this;
    169     wx.showLoading({
    170       title: '获取地理位置',
    171       mask: true,
    172     });
    173     setTimeout(() => {
    174       wx.hideLoading()
    175       wx.chooseLocation({
    176         success: (res) => {
    177           // console.log(res);
    178           if (res.errMsg == "chooseLocation:ok") {
    179             if (res.address == "") {
    180               vm.CancelChooseLocation()
    181             } else {
    182               delete res.errMsg;
    183               vm.globalData.locationBool = true; //
    184               wx.setStorageSync("location", res) //将地理位置缓存
    185               vm.GetLoacationOrGoHome(); //前往首页
    186             }
    187           } else {
    188             console.error(res.errMsg);
    189           }
    190         },
    191         fail: (res) => {
    192           vm.CancelChooseLocation()
    193           // if (res.errMsg == "chooseLocation:fail cancel") {
    194           //   vm.CancelChooseLocation()
    195           // } else if (res.errMsg =="chooseLocation:fail auth deny"){
    196           //   wx.showModal({
    197           //     content: 'motiva 需要获取你的位置来定位附近的场地。',
    198           //     showCancel: false,
    199           //     success(res) {
    200           //       if (res.confirm) {
    201           //         vm.ChooseLocation()
    202           //       }
    203           //     }
    204           //   })
    205           // }
    206           console.error("chooseLocation:" + res.errMsg);
    207         },
    208         complete: () => { }
    209       });
    210     }, 500)
    211   },
    212   /**用户取消选择地理位置 */
    213   CancelChooseLocation: function () {
    214     let vm = this;
    215     let location_name = wx.getStorageSync('location').name;
    216     if (!location_name) {
    217       wx.showModal({
    218         content: '点击确定在地图下方选择您的地理位置,nunumua 需要定位你附近的场地。',
    219         showCancel: false,
    220         success(res) {
    221           if (res.confirm) {
    222             vm.ChooseLocation()
    223           }
    224         }
    225       })
    226     }
    227   },
    228   /** 完成授权和地理位置 之后返回首页 */
    229   GetLoacationOrGoHome: function () {
    230     // let location = wx.getStorageSync("location");
    231     // if (location) {
    232       var pages = getCurrentPages();
    233       if (pages.length == 1) {
    234         wx.switchTab({
    235           url: '/pages/home/index/index'
    236         })
    237       } else {
    238         wx.navigateBack({
    239           delta: 2
    240         })
    241       }
    242 
    243     // } else {
    244     //   this.ChooseLocation();
    245     // }
    246   },
    247   /**获取用户信息 */
    248   SaveUserInfo: function (userInfo) {
    249     return new Promise((resolve, reject) => {
    250       wx.request({
    251         url: SiteInfo["Siteroot"] + '/api/registered',
    252         header: {
    253           Version: SiteInfo["Version"]
    254         },
    255         method: 'POST',
    256         data: {
    257           userInfo: userInfo
    258         },
    259         success(res) {
    260           res.data && res.data.code == 200 ? resolve(res.data.data) : reject(res.data.message);
    261         },
    262         fail(res) {
    263           wx.hideLoading();
    264           console.log('用户注册失败!' + res.errMsg)
    265         },
    266         complete: function (res) {
    267           wx.hideLoading();
    268         },
    269       });
    270 
    271     })
    272   },
    273   globalData: {
    274     statusBarHeight: 0,
    275     titleBarHeight: 0,
    276     userInfo: null,//用户信息
    277     location: null,//用户地理位置
    278     userInfoBool: false,//用户授权
    279     locationBool: false,//用户定位
    280     needReauth: false //是否需要重新授权
    281   }
    282 });

    获取用户信息、获取定位

    import SiteInfo from "siteInfo.js";
    App({
    onShow: function () {
    // wx.checkSession({ //检测当前用户的session_key是否过期
    // success: function() { //session_key 未过期,并且在本生命周期一直有效

    // },
    // fail: function() { //session_key 已经失效,需要重新执行登录流程
    // console.warn("授权过期,重新登录小程序")
    // wx.navigateTo({
    // url: "/pages/openauth/index" //重新授权
    // })
    // }
    // })
    },
    onLaunch: function () {
    // console.log("授权未过期")
    let vm = this;
    wx.showToast({
    title: '欢迎来到 motiva !',
    icon: "none",
    mask: true
    })
    //更新版本
    vm.CheckVersion();
    let promise = this.SetHeader();
    promise.then(res => {
    }).catch(errMsg => {
    console.error(errMsg)
    });
    },
    /**检查小程序版本更新 */
    CheckVersion: function () {
    const updateManager = wx.getUpdateManager()

    updateManager.onCheckForUpdate(function (res) {
    // 请求完新版本信息的回调
    // console.log(res.hasUpdate)
    })

    updateManager.onUpdateReady(function () {
    wx.showModal({
    title: '更新提示',
    content: '新版本已经准备好,是否重启应用?',
    success: function (res) {
    if (res.confirm) {
    // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
    updateManager.applyUpdate()
    }
    }
    })
    })

    updateManager.onUpdateFailed(function () {
    // 新版本下载失败
    console.error('新版本下载失败!')
    wx.showToast({
    title: '新版本下载失败,请检查你的网路链接是否通畅!',
    icon: 'none',
    duration: 2000,
    mask: true
    })
    })
    },
    /**检查是否授权 */
    CheckAuth: function () {
    var that = this;
    // 查看是否授权
    wx.getSetting({
    success(res) {
    if (res.authSetting['scope.userInfo']) {
    // 已经授权,可以直接调用 getUserInfo 获取头像昵称
    wx.getUserInfo({
    success: function (res) {
    App.globalData.userInfo = res.userInfo; //更新用户信息
    }
    });
    //是否授权获取地理位置
    if (!res.authSetting['scope.userLocation']) {
    wx.navigateTo({
    url: '/pages/openauth/index', //跳转到授权页面
    })
    }
    } else {
    wx.navigateTo({
    url: '/pages/openauth/index', //跳转到授权页面
    })
    }
    }
    })
    },
    /**小程序登录:调用接口获取登录凭证(code) */
    WxLogin: function () {
    return new Promise((resolve, reject) => {
    let vm = this;
    wx.showLoading({
    title: '登录小程序',
    mask: true
    });
    wx.login({
    success(res) {
    if (res.code) {
    //发起网络请求
    wx.request({
    url: SiteInfo["Siteroot"] + '/api/login',
    header: {
    Version: SiteInfo["Version"]
    },
    method: 'POST',
    data: {
    code: res.code
    },
    success(res) {
    res.data && res.data.code == 200 ? resolve(res.data.data) : reject(res.data.message);
    },
    fail(res) {
    wx.hideLoading();
    console.log('登录失败!' + res.errMsg)
    },
    complete: function (res) {
    wx.hideLoading();
    },
    });
    } else {
    wx.hideLoading();
    console.log('登录失败!' + res.errMsg)
    }
    },
    fail(res) {
    console.error('调用小程序登录接口失败!' + res.errMsg)
    wx.showToast({
    title: '登录小程序失败,请检查你的网路链接是否通畅',
    icon: 'none',
    duration: 2000,
    mask: true
    })
    }
    })
    });
    },
    /** 自定义头部:返回按钮位置 */
    SetHeader: function () {
    return new Promise((resolve, reject) => {
    const vm = this;
    wx.getSystemInfo({
    success: function (res) {
    let totalTopHeight = 82
    if (res.model.indexOf('iPhone X') !== -1) {
    totalTopHeight = 110
    } else if (res.model.indexOf('iPhone') !== -1) {
    totalTopHeight = 64
    }
    vm.globalData.statusBarHeight = res.statusBarHeight;
    vm.globalData.titleBarHeight = totalTopHeight - res.statusBarHeight;
    resolve(res);
    },
    failure() {
    vm.globalData.statusBarHeight = 0;
    vm.globalData.titleBarHeight = 0;
    reject("获取系统信息失败!");
    }
    })
    })

    },
    /** 开始选择地理位置 */
    ChooseLocation: function () {
    let vm = this;
    wx.showLoading({
    title: '获取地理位置',
    mask: true,
    });
    setTimeout(() => {
    wx.hideLoading()
    wx.chooseLocation({
    success: (res) => {
    // console.log(res);
    if (res.errMsg == "chooseLocation:ok") {
    if (res.address == "") {
    vm.CancelChooseLocation()
    } else {
    delete res.errMsg;
    vm.globalData.locationBool = true; //
    wx.setStorageSync("location", res) //将地理位置缓存
    vm.GetLoacationOrGoHome(); //前往首页
    }
    } else {
    console.error(res.errMsg);
    }
    },
    fail: (res) => {
    vm.CancelChooseLocation()
    // if (res.errMsg == "chooseLocation:fail cancel") {
    // vm.CancelChooseLocation()
    // } else if (res.errMsg =="chooseLocation:fail auth deny"){
    // wx.showModal({
    // content: 'motiva 需要获取你的位置来定位附近的场地。',
    // showCancel: false,
    // success(res) {
    // if (res.confirm) {
    // vm.ChooseLocation()
    // }
    // }
    // })
    // }
    console.error("chooseLocation:" + res.errMsg);
    },
    complete: () => { }
    });
    }, 500)
    },
    /**用户取消选择地理位置 */
    CancelChooseLocation: function () {
    let vm = this;
    let location_name = wx.getStorageSync('location').name;
    if (!location_name) {
    wx.showModal({
    content: '点击确定在地图下方选择您的地理位置,motiva 需要定位你附近的场地。',
    showCancel: false,
    success(res) {
    if (res.confirm) {
    vm.ChooseLocation()
    }
    }
    })
    }
    },
    /** 完成授权和地理位置 之后返回首页 */
    GetLoacationOrGoHome: function () {
    // let location = wx.getStorageSync("location");
    // if (location) {
    var pages = getCurrentPages();
    if (pages.length == 1) {
    wx.switchTab({
    url: '/pages/home/index/index'
    })
    } else {
    wx.navigateBack({
    delta: 2
    })
    }

    // } else {
    // this.ChooseLocation();
    // }
    },
    /**获取用户信息 */
    SaveUserInfo: function (userInfo) {
    return new Promise((resolve, reject) => {
    wx.request({
    url: SiteInfo["Siteroot"] + '/api/registered',
    header: {
    Version: SiteInfo["Version"]
    },
    method: 'POST',
    data: {
    userInfo: userInfo
    },
    success(res) {
    res.data && res.data.code == 200 ? resolve(res.data.data) : reject(res.data.message);
    },
    fail(res) {
    wx.hideLoading();
    console.log('用户注册失败!' + res.errMsg)
    },
    complete: function (res) {
    wx.hideLoading();
    },
    });

    })
    },
    globalData: {
    statusBarHeight: 0,
    titleBarHeight: 0,
    userInfo: null,//用户信息
    location: null,//用户地理位置
    userInfoBool: false,//用户授权
    locationBool: false,//用户定位
    needReauth: false //是否需要重新授权
    }
    });
  • 相关阅读:
    PAT 1088. Rational Arithmetic
    PAT 1087. All Roads Lead to Rome
    PAT 1086. Tree Traversals Again
    PAT 1085. Perfect Sequence
    PAT 1084. Broken Keyboard
    PAT 1083. List Grades
    PAT 1082. Read Number in Chinese
    求最大公因数
    [转载]Latex文件转成pdf后的字体嵌入问题的解决
    [转载]Matlab有用的小工具小技巧
  • 原文地址:https://www.cnblogs.com/kitty-blog/p/11095451.html
Copyright © 2011-2022 走看看