123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- // app.js
- var http = require('./utils/util.js');
- var md5 = require('./utils/md5.js');
- var commonJs = require('./utils/common.js')
- App({
- data:{
- num: 0,
- safeAreaTop:30,//距离上面的安全距离
- statusNavBarHeight:0,
- cyRank_type:1,
- link: '',
- isOnshow:false,
- isIOS:false,
- rootDocment:'https://sph.wenxingshuju.com/wx_sph'
- // rootDocment:'http://60.205.169.65:8025'
- },
- onLaunch() {
- var _this = this;
- this.data.isOnshow = true
- var info_platform = swan.getSystemInfoSync()
- if (info_platform.platform === 'android') {
- this.data.isIOS = false
- } else {
- this.data.isIOS = true
- }
- swan.getSystemInfo({
- success (res) {
- _this.data.safeAreaTop = res.statusBarHeight + 10
- }
- })
- this.getNavBarHeight()
- this.verified();//版本控制(过审用)
- },
- onShow:function(options){
- if(!swan.getStorageSync('isLogin')){//解决未登录情况下,点击左上角home
- if(this.data.isOnshow){
- this.data.isOnshow = false
- }else{
- this.verified();//版本控制(过审用)
- }
- }
- },
- getNavBarHeight(){
- // 获取状态栏高度
- const { statusBarHeight } = swan.getSystemInfoSync();
- // 得到右上角菜单的位置尺寸
- const menuButtonObject = swan.getMenuButtonBoundingClientRect();
- const { top, height } = menuButtonObject;
- // 计算导航栏的高度
- // 此高度基于右上角菜单在导航栏位置垂直居中计算得到
- const navBarHeight = height + (top - statusBarHeight) * 2;
- // 计算状态栏与导航栏的总高度
- this.data.statusNavBarHeight = statusBarHeight + navBarHeight
- },
- verified(cb){//版本控制
- var _this = this;
- http.req('/api/wx/verified', 'get', {
- version: 'v2.3'
- }, function (res) {
- if(res && res.errno == 0){
- swan.setStorageSync('isLogin',false)
- swan.setStorageSync('admin_id',res.rst.data.id)
- if(!swan.getStorageSync('ttl')||swan.setStorageSync('ttl')!=res.rst.data.ttl) {
- swan.setStorageSync('ttl',res.rst.data.ttl)
- var sign=md5.hex_md5("random="+res.rst.data.random+"&ttl="+res.rst.data.ttl+"&user_id="+res.rst.data.id);
- swan.setStorageSync('sign',sign)
- }
- swan.setStorageSync('doneLogin', false)
- swan.switchTab({
- url: '/pages/index/index',
- })
- cb?cb('verified'):''
- }else if(res.errno == 9014){
- cb?cb():''
- _this.login()
- }
- },(err)=>{
- cb?cb():''
- console.log("err",err)
- })
- },
- // 登录
- login(link){
- console.log(link)
- var _this = this;
- swan.login({
- success (res) {
- if (res.code) {
- // 发起网络请求 发送 res.code 到后台换取 openId, sessionKey, unionId
- http.req('/api/wx/loginByUnionId', 'get', {
- code: res.code
- }, function (res) {
- swan.hideLoading()
- swan.setStorageSync('isRequest', false)
- if(res && res.errno == 0){
- //成功
- var session_key = res.rst.session_key;
- swan.setStorageSync('session_key',session_key)
- res.rst.info.avatarUrl = res.rst.avatarUrl;
- swan.setStorageSync('userInfo',res.rst.info)
- swan.setStorageSync('admin_id',res.rst.info.id)
- swan.setStorageSync('wx_subscribe',res.rst.info.wx_subscribe)
- swan.setStorageSync('ttl',res.rst.ttl)
- var sign=md5.hex_md5("random="+res.rst.random+"&ttl="+res.rst.ttl+"&user_id="+res.rst.info.id);
- swan.setStorageSync('sign',sign)
- swan.setStorageSync('isLogin',true)
- let pages = getCurrentPages(); //页面对象
- let currentPage = pages[0];
- if(Object.keys(currentPage.options).length>0) {
- currentPage.onShow();
- }else {
- currentPage.onLoad();
- }
- }else{
- if(res.errno=='9012'&&_this.data.num<5) {
- _this.data.num = Number(_this.data.num)+1
- _this.data.link = cb;//保存设置的回调地址
- }else {
- // swan.redirectTo({
- // url: '/pages/welcome/welcome',
- // })
- }
- }
- },()=>{
- swan.hideLoading()
- })
- } else {
- console.log('登录失败!' + res.errMsg)
- }
- }
- })
- },
- //判断微信登录是否过期
- checkSession (cb) {
- var _this = this;
- swan.checkSession({
- success (cb) {
- //session_key 未过期,并且在本生命周期一直有效
- swan.setStorageSync('hasWxLogin',true);//微信登录是否过期
- if(!swan.getStorageSync('isLogin') && !swan.getStorageSync('isRequest')){
- _this.login()
- }else{//已在登录状态
- console.log("已在登录状态")
- }
- },
- fail() {
- console.log('过期,重新登录')
- // session_key 已经失效,需要重新执行登录流程
- _this.login()
- }
- })
- },
- shareEvent(res,self_page){//分享
- var pages = getCurrentPages() //获取加载的页面
- var currentPage = pages[pages.length-1] //获取当前页面的对象
- var url = currentPage.route //当前页面url
- var options = currentPage.options
- options = Object.assign(options,{'platform':this.data.platform})
- var path = '';
- for(var i in options){
- path += '&' + i + '=' + options[i]
- }
- var path2 = url + "?" + path.substr(1)
- if(res.from === 'button'){
- //来自页面内转发按钮
- }
- return{
- title:'问星数据【视频号版】最新的视频号分析工具',
- imageUrl:'https://kx-gw.oss-cn-beijing.aliyuncs.com/wxVideoBack.png',
- path: self_page?self_page:path2
- }
- },
- func: {
- req: http.req,
- md5: md5.hex_md5,
- getDay: commonJs.getDay,
- NumberHandle: commonJs.NumberHandle
- }
- })
|