123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- // app.js
- var http = require('utils/util.js');
- var md5 = require('utils/md5.js');
- var commonJs = require('utils/common.js')
- App({
- data:{
- showHome: false,//记录是否应该请求首页的接口
- num: 0,
- safeAreaTop:30,//距离上面的安全距离
- statusNavBarHeight:0,
- cyRank_type:1,
- link: '',
- systemInfo: {},
- isOnshow:false,
- isIOS:false,
- rootDocment:'https://sph.wenxingshuju.com/wx_sph'
- // rootDocment:'http://60.205.169.65:8011'
- },
- onLaunch(options) {
- if(options.query.source){
- //source:h5从其他浏览器跳到小程序
- wx.setStorageSync('source', options.query.source)
- }
- if(options.query.key){
- wx.setStorageSync('key', options.query.key)
- }
- var _this = this;
- _this.data.showHome = false;
- this.data.isOnshow = true
- this.verified();//版本控制(过审用)
- var info_platform = wx.getSystemInfoSync()
- if (info_platform.platform === 'android') {
- this.data.isIOS = false
- } else {
- this.data.isIOS = true
- }
- wx.getSystemInfo({
- success (res) {
- _this.data.systemInfo = res
- _this.data.windowWidth = res.windowWidth
- _this.data.windowHeight = res.windowHeight
- if(_this.data.systemInfo.platform == "ios"){
- _this.data.safeAreaTop = _this.data.systemInfo.statusBarHeight + 10
- }else if(_this.data.systemInfo.platform == "android"){
- _this.data.safeAreaTop = _this.data.systemInfo.statusBarHeight + 10
- }else{
- _this.data.safeAreaTop = _this.data.systemInfo.statusBarHeight + 10
- }
- let modelmes = res.model; //手机品牌
- //其实后面关于XS、XR、XS MAX的判断都可以去掉,因为他们里面都包含了'iPhone X'这个字符;
- if (modelmes.indexOf('iPhone X') != -1) {
- _this.data.systemInfo.isIpx = true
- } else if (modelmes.indexOf('iPhone XS') != -1) {
- _this.data.systemInfo.isIpx = true
- } else if (modelmes.indexOf('iPhone XR') != -1) {
- _this.data.systemInfo.isIpx = true
- } else if (modelmes.indexOf('iPhone XS Max') != -1) {
- _this.data.systemInfo.isIpx = true
- } else if (modelmes.indexOf('iPhone 11') != -1) {
- _this.data.systemInfo.isIpx = true
- } else if (modelmes.indexOf('iPhone 11 Pro') != -1) {
- _this.data.systemInfo.isIpx = true
- } else if (modelmes.indexOf('iPhone 11 Pro Max') != -1) {
- _this.data.systemInfo.isIpx = true
- }
- }
- })
- this.getNavBarHeight()
- },
- onShow:function(options){
- if(!wx.getStorageSync('isLogin')){//解决未登录情况下,点击左上角home
- if(this.data.isOnshow){
- this.data.isOnshow = false
- }else{
- this.verified();//版本控制(过审用)
- }
- }
- // scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
- if(options.scene) {
-
- const scene = decodeURIComponent(options.query.scene)
- this.data.loadScene = options.scene;//进入的公众号的场景
- if(scene.indexOf('invite')!=-1&&scene.indexOf('qun_id')!=-1){
- wx.setStorageSync('friend_invite_code', this.getQueryVariable(scene,'invite'))
- wx.setStorageSync('sph_praise_id', this.getQueryVariable(scene,'qun_id'))
- }
- }
- if (options.scene == 1007 || options.scene == 1008 || options.scene == 1001) {
- // 1007通过单人聊天会话分享进入 1008 通过群聊会话分享进入
- if(options.query.invite){
- wx.setStorageSync('friend_invite_code', options.query.invite)
- }
- if(options.query.qun_id){
- wx.setStorageSync('sph_praise_id', options.query.qun_id)
- }
- }
- },
- // 获取二维码参数
- getQueryVariable(query,variable) {
- var vars = query.split("&");
- for (var i=0;i<vars.length;i++) {
- var pair = vars[i].split("=");
- if(pair[0] == variable){return pair[1];}
- }
- return(false);
- },
- getNavBarHeight(){
- // 获取状态栏高度
- const { statusBarHeight } = wx.getSystemInfoSync();
- // 得到右上角菜单的位置尺寸
- const menuButtonObject = wx.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: 'v4.7'
- }, function (res) {
- if(res && res.errno == 0){
- _this.data.showHome = true;
- wx.setStorageSync('isverfiled',true)
- wx.setStorageSync('isLogin',false)
- wx.setStorageSync('admin_id',res.rst.data.id)
- if(!wx.getStorageSync('ttl')||wx.setStorageSync('ttl')!=res.rst.data.ttl) {
- wx.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);
- wx.setStorageSync('sign',sign)
- }
- wx.setStorageSync('doneLogin', false)
- wx.switchTab({
- url: '/pages/index/index',
- })
- var pages = getCurrentPages(); //页面对象
- var currentPage = pages[0];
- currentPage.onLoad();
- cb?cb('verified'):''
- }else if(res.errno == 9014){
- wx.setStorageSync('isverfiled',false)
- cb?cb():''
- _this.login()
- }
- },(err)=>{
- cb?cb():''
- console.log("err",err)
- })
- },
- // 登录
- login(link){
- // wx.redirectTo({
- // url: '/pages/welcome/welcome',
- // })
- var _this = this;
- wx.login({
- success (res) {
- if (res.code) {
- // 发起网络请求 发送 res.code 到后台换取 openId, sessionKey, unionId
- http.req('/api/wx/loginByUnionId', 'get', {
- code: res.code,
- invite_code: wx.getStorageSync('friend_invite_code'),
- sph_praise_id:wx.getStorageSync('sph_praise_id'),
- source:wx.getStorageSync('source')?wx.getStorageSync('source'):'',
- key:wx.getStorageSync('key')?wx.getStorageSync('key'):''
- }, function (res) {
- _this.data.showHome = true;
- wx.hideLoading()
- wx.setStorageSync('isRequest', false)
- if(res && res.errno == 0){
- wx.setStorageSync('source', '')
- wx.setStorageSync('key', '')
- //成功
- var session_key = res.rst.session_key;
- wx.setStorageSync('session_key',session_key)
- res.rst.info.avatarUrl = res.rst.avatarUrl;
- wx.setStorageSync('userInfo',res.rst.info)
- wx.setStorageSync('admin_id',res.rst.info.id)
- wx.setStorageSync('wx_subscribe',res.rst.info.wx_subscribe)
- wx.setStorageSync('ttl',res.rst.ttl)
- var sign=md5.hex_md5("random="+res.rst.random+"&ttl="+res.rst.ttl+"&user_id="+res.rst.info.id);
- wx.setStorageSync('sign',sign)
- wx.setStorageSync('isLogin',true)
- let pages = getCurrentPages(); //页面对象
- let currentPage = pages[0];
- if(currentPage.route=='pages/index/index'){
- currentPage.onLoad();
- }else{
- 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 {
- wx.redirectTo({
- url: '/pages/welcome/welcome',
- })
- }
- }
- },()=>{
- wx.hideLoading()
- })
- } else {
- console.log('登录失败!' + res.errMsg)
- }
- }
- })
- },
- //判断微信登录是否过期
- checkSession (cb) {
- var _this = this;
- wx.checkSession({
- success (cb) {
- //session_key 未过期,并且在本生命周期一直有效
- wx.setStorageSync('hasWxLogin',true);//微信登录是否过期
- if(!wx.getStorageSync('isLogin') && !wx.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
- }
- },
- extensionShare(res,qun_id,share_title,share_img_url){//邀请活动的分享
- var title = share_title?share_title:'我做了个很棒的视频号,快来看看啊~'
- var name = wx.getStorageSync('userInfo').wx_nickname?wx.getStorageSync('userInfo').wx_nickname:wx.getStorageSync('userInfo').name;
- return{
- title: title,
- imageUrl:share_img_url,
- path: '/pages/index/index?invite='+wx.getStorageSync('userInfo').invite_code+'&qun_id='+qun_id
- }
- },
- func: {
- rootDocment: http.rootDocment,
- req: http.req,
- md5: md5.hex_md5,
- getDay: commonJs.getDay,
- NumberHandle: commonJs.NumberHandle
- }
- })
|