大数据平台的小程序版本

app.js 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // app.js
  2. var http = require('utils/util.js');
  3. var md5 = require('utils/md5.js');
  4. var commonJs = require('utils/common.js')
  5. App({
  6. data:{
  7. showHome: false,//记录是否应该请求首页的接口
  8. num: 0,
  9. safeAreaTop:30,//距离上面的安全距离
  10. statusNavBarHeight:0,
  11. cyRank_type:1,
  12. link: '',
  13. systemInfo: {},
  14. isOnshow:false,
  15. isIOS:false,
  16. rootDocment:'https://sph.wenxingshuju.com/wx_sph'
  17. // rootDocment:'http://60.205.169.65:8011'
  18. },
  19. onLaunch(options) {
  20. if(options.query.source){
  21. //source:h5从其他浏览器跳到小程序
  22. wx.setStorageSync('source', options.query.source)
  23. }
  24. if(options.query.key){
  25. wx.setStorageSync('key', options.query.key)
  26. }
  27. var _this = this;
  28. _this.data.showHome = false;
  29. this.data.isOnshow = true
  30. this.verified();//版本控制(过审用)
  31. var info_platform = wx.getSystemInfoSync()
  32. if (info_platform.platform === 'android') {
  33. this.data.isIOS = false
  34. } else {
  35. this.data.isIOS = true
  36. }
  37. wx.getSystemInfo({
  38. success (res) {
  39. _this.data.systemInfo = res
  40. _this.data.windowWidth = res.windowWidth
  41. _this.data.windowHeight = res.windowHeight
  42. if(_this.data.systemInfo.platform == "ios"){
  43. _this.data.safeAreaTop = _this.data.systemInfo.statusBarHeight + 10
  44. }else if(_this.data.systemInfo.platform == "android"){
  45. _this.data.safeAreaTop = _this.data.systemInfo.statusBarHeight + 10
  46. }else{
  47. _this.data.safeAreaTop = _this.data.systemInfo.statusBarHeight + 10
  48. }
  49. let modelmes = res.model; //手机品牌
  50. //其实后面关于XS、XR、XS MAX的判断都可以去掉,因为他们里面都包含了'iPhone X'这个字符;
  51. if (modelmes.indexOf('iPhone X') != -1) {
  52. _this.data.systemInfo.isIpx = true
  53. } else if (modelmes.indexOf('iPhone XS') != -1) {
  54. _this.data.systemInfo.isIpx = true
  55. } else if (modelmes.indexOf('iPhone XR') != -1) {
  56. _this.data.systemInfo.isIpx = true
  57. } else if (modelmes.indexOf('iPhone XS Max') != -1) {
  58. _this.data.systemInfo.isIpx = true
  59. } else if (modelmes.indexOf('iPhone 11') != -1) {
  60. _this.data.systemInfo.isIpx = true
  61. } else if (modelmes.indexOf('iPhone 11 Pro') != -1) {
  62. _this.data.systemInfo.isIpx = true
  63. } else if (modelmes.indexOf('iPhone 11 Pro Max') != -1) {
  64. _this.data.systemInfo.isIpx = true
  65. }
  66. }
  67. })
  68. this.getNavBarHeight()
  69. },
  70. onShow:function(options){
  71. if(!wx.getStorageSync('isLogin')){//解决未登录情况下,点击左上角home
  72. if(this.data.isOnshow){
  73. this.data.isOnshow = false
  74. }else{
  75. this.verified();//版本控制(过审用)
  76. }
  77. }
  78. // scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
  79. if(options.scene) {
  80. const scene = decodeURIComponent(options.query.scene)
  81. this.data.loadScene = options.scene;//进入的公众号的场景
  82. if(scene.indexOf('invite')!=-1&&scene.indexOf('qun_id')!=-1){
  83. wx.setStorageSync('friend_invite_code', this.getQueryVariable(scene,'invite'))
  84. wx.setStorageSync('sph_praise_id', this.getQueryVariable(scene,'qun_id'))
  85. }
  86. }
  87. if (options.scene == 1007 || options.scene == 1008 || options.scene == 1001) {
  88. // 1007通过单人聊天会话分享进入 1008 通过群聊会话分享进入
  89. if(options.query.invite){
  90. wx.setStorageSync('friend_invite_code', options.query.invite)
  91. }
  92. if(options.query.qun_id){
  93. wx.setStorageSync('sph_praise_id', options.query.qun_id)
  94. }
  95. }
  96. },
  97. // 获取二维码参数
  98. getQueryVariable(query,variable) {
  99. var vars = query.split("&");
  100. for (var i=0;i<vars.length;i++) {
  101. var pair = vars[i].split("=");
  102. if(pair[0] == variable){return pair[1];}
  103. }
  104. return(false);
  105. },
  106. getNavBarHeight(){
  107. // 获取状态栏高度
  108. const { statusBarHeight } = wx.getSystemInfoSync();
  109. // 得到右上角菜单的位置尺寸
  110. const menuButtonObject = wx.getMenuButtonBoundingClientRect();
  111. const { top, height } = menuButtonObject;
  112. // 计算导航栏的高度
  113. // 此高度基于右上角菜单在导航栏位置垂直居中计算得到
  114. const navBarHeight = height + (top - statusBarHeight) * 2;
  115. // 计算状态栏与导航栏的总高度
  116. this.data.statusNavBarHeight = statusBarHeight + navBarHeight
  117. },
  118. verified(cb){//版本控制
  119. var _this = this;
  120. http.req('/api/wx/verified', 'get', {
  121. version: 'v4.7'
  122. }, function (res) {
  123. if(res && res.errno == 0){
  124. _this.data.showHome = true;
  125. wx.setStorageSync('isverfiled',true)
  126. wx.setStorageSync('isLogin',false)
  127. wx.setStorageSync('admin_id',res.rst.data.id)
  128. if(!wx.getStorageSync('ttl')||wx.setStorageSync('ttl')!=res.rst.data.ttl) {
  129. wx.setStorageSync('ttl',res.rst.data.ttl)
  130. var sign=md5.hex_md5("random="+res.rst.data.random+"&ttl="+res.rst.data.ttl+"&user_id="+res.rst.data.id);
  131. wx.setStorageSync('sign',sign)
  132. }
  133. wx.setStorageSync('doneLogin', false)
  134. wx.switchTab({
  135. url: '/pages/index/index',
  136. })
  137. var pages = getCurrentPages(); //页面对象
  138. var currentPage = pages[0];
  139. currentPage.onLoad();
  140. cb?cb('verified'):''
  141. }else if(res.errno == 9014){
  142. wx.setStorageSync('isverfiled',false)
  143. cb?cb():''
  144. _this.login()
  145. }
  146. },(err)=>{
  147. cb?cb():''
  148. console.log("err",err)
  149. })
  150. },
  151. // 登录
  152. login(link){
  153. // wx.redirectTo({
  154. // url: '/pages/welcome/welcome',
  155. // })
  156. var _this = this;
  157. wx.login({
  158. success (res) {
  159. if (res.code) {
  160. // 发起网络请求 发送 res.code 到后台换取 openId, sessionKey, unionId
  161. http.req('/api/wx/loginByUnionId', 'get', {
  162. code: res.code,
  163. invite_code: wx.getStorageSync('friend_invite_code'),
  164. sph_praise_id:wx.getStorageSync('sph_praise_id'),
  165. source:wx.getStorageSync('source')?wx.getStorageSync('source'):'',
  166. key:wx.getStorageSync('key')?wx.getStorageSync('key'):''
  167. }, function (res) {
  168. _this.data.showHome = true;
  169. wx.hideLoading()
  170. wx.setStorageSync('isRequest', false)
  171. if(res && res.errno == 0){
  172. wx.setStorageSync('source', '')
  173. wx.setStorageSync('key', '')
  174. //成功
  175. var session_key = res.rst.session_key;
  176. wx.setStorageSync('session_key',session_key)
  177. res.rst.info.avatarUrl = res.rst.avatarUrl;
  178. wx.setStorageSync('userInfo',res.rst.info)
  179. wx.setStorageSync('admin_id',res.rst.info.id)
  180. wx.setStorageSync('wx_subscribe',res.rst.info.wx_subscribe)
  181. wx.setStorageSync('ttl',res.rst.ttl)
  182. var sign=md5.hex_md5("random="+res.rst.random+"&ttl="+res.rst.ttl+"&user_id="+res.rst.info.id);
  183. wx.setStorageSync('sign',sign)
  184. wx.setStorageSync('isLogin',true)
  185. let pages = getCurrentPages(); //页面对象
  186. let currentPage = pages[0];
  187. if(currentPage.route=='pages/index/index'){
  188. currentPage.onLoad();
  189. }else{
  190. if(Object.keys(currentPage.options).length>0) {
  191. currentPage.onShow();
  192. }else {
  193. currentPage.onLoad();
  194. }
  195. }
  196. }else{
  197. if(res.errno=='9012'&&_this.data.num<5) {
  198. _this.data.num = Number(_this.data.num)+1
  199. _this.data.link = cb;//保存设置的回调地址
  200. }else {
  201. wx.redirectTo({
  202. url: '/pages/welcome/welcome',
  203. })
  204. }
  205. }
  206. },()=>{
  207. wx.hideLoading()
  208. })
  209. } else {
  210. console.log('登录失败!' + res.errMsg)
  211. }
  212. }
  213. })
  214. },
  215. //判断微信登录是否过期
  216. checkSession (cb) {
  217. var _this = this;
  218. wx.checkSession({
  219. success (cb) {
  220. //session_key 未过期,并且在本生命周期一直有效
  221. wx.setStorageSync('hasWxLogin',true);//微信登录是否过期
  222. if(!wx.getStorageSync('isLogin') && !wx.getStorageSync('isRequest')){
  223. _this.login()
  224. }else{//已在登录状态
  225. console.log("已在登录状态")
  226. }
  227. },
  228. fail() {
  229. console.log('过期,重新登录')
  230. // session_key 已经失效,需要重新执行登录流程
  231. _this.login()
  232. }
  233. })
  234. },
  235. shareEvent(res,self_page){//分享
  236. var pages = getCurrentPages() //获取加载的页面
  237. var currentPage = pages[pages.length-1] //获取当前页面的对象
  238. var url = currentPage.route //当前页面url
  239. var options = currentPage.options
  240. options = Object.assign(options,{'platform':this.data.platform})
  241. var path = '';
  242. for(var i in options){
  243. path += '&' + i + '=' + options[i]
  244. }
  245. var path2 = url + "?" + path.substr(1)
  246. if(res.from === 'button'){
  247. //来自页面内转发按钮
  248. }
  249. return{
  250. title:'问星数据【视频号版】最新的视频号分析工具',
  251. imageUrl:'https://kx-gw.oss-cn-beijing.aliyuncs.com/wxVideoBack.png',
  252. path: self_page?self_page:path2
  253. }
  254. },
  255. extensionShare(res,qun_id,share_title,share_img_url){//邀请活动的分享
  256. var title = share_title?share_title:'我做了个很棒的视频号,快来看看啊~'
  257. var name = wx.getStorageSync('userInfo').wx_nickname?wx.getStorageSync('userInfo').wx_nickname:wx.getStorageSync('userInfo').name;
  258. return{
  259. title: title,
  260. imageUrl:share_img_url,
  261. path: '/pages/index/index?invite='+wx.getStorageSync('userInfo').invite_code+'&qun_id='+qun_id
  262. }
  263. },
  264. func: {
  265. rootDocment: http.rootDocment,
  266. req: http.req,
  267. md5: md5.hex_md5,
  268. getDay: commonJs.getDay,
  269. NumberHandle: commonJs.NumberHandle
  270. }
  271. })