No Description

app.js 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. num: 0,
  8. safeAreaTop:30,//距离上面的安全距离
  9. statusNavBarHeight:0,
  10. cyRank_type:1,
  11. link: '',
  12. isOnshow:false,
  13. isIOS:false,
  14. rootDocment:'https://sph.wenxingshuju.com/wx_sph'
  15. // rootDocment:'http://60.205.169.65:8025'
  16. },
  17. onLaunch() {
  18. var _this = this;
  19. this.data.isOnshow = true
  20. var info_platform = swan.getSystemInfoSync()
  21. if (info_platform.platform === 'android') {
  22. this.data.isIOS = false
  23. } else {
  24. this.data.isIOS = true
  25. }
  26. swan.getSystemInfo({
  27. success (res) {
  28. _this.data.safeAreaTop = res.statusBarHeight + 10
  29. }
  30. })
  31. this.getNavBarHeight()
  32. this.verified();//版本控制(过审用)
  33. },
  34. onShow:function(options){
  35. if(!swan.getStorageSync('isLogin')){//解决未登录情况下,点击左上角home
  36. if(this.data.isOnshow){
  37. this.data.isOnshow = false
  38. }else{
  39. this.verified();//版本控制(过审用)
  40. }
  41. }
  42. },
  43. getNavBarHeight(){
  44. // 获取状态栏高度
  45. const { statusBarHeight } = swan.getSystemInfoSync();
  46. // 得到右上角菜单的位置尺寸
  47. const menuButtonObject = swan.getMenuButtonBoundingClientRect();
  48. const { top, height } = menuButtonObject;
  49. // 计算导航栏的高度
  50. // 此高度基于右上角菜单在导航栏位置垂直居中计算得到
  51. const navBarHeight = height + (top - statusBarHeight) * 2;
  52. // 计算状态栏与导航栏的总高度
  53. this.data.statusNavBarHeight = statusBarHeight + navBarHeight
  54. },
  55. verified(cb){//版本控制
  56. var _this = this;
  57. http.req('/api/wx/verified', 'get', {
  58. version: 'v2.3'
  59. }, function (res) {
  60. if(res && res.errno == 0){
  61. swan.setStorageSync('isLogin',false)
  62. swan.setStorageSync('admin_id',res.rst.data.id)
  63. if(!swan.getStorageSync('ttl')||swan.setStorageSync('ttl')!=res.rst.data.ttl) {
  64. swan.setStorageSync('ttl',res.rst.data.ttl)
  65. var sign=md5.hex_md5("random="+res.rst.data.random+"&ttl="+res.rst.data.ttl+"&user_id="+res.rst.data.id);
  66. swan.setStorageSync('sign',sign)
  67. }
  68. swan.setStorageSync('doneLogin', false)
  69. swan.switchTab({
  70. url: '/pages/index/index',
  71. })
  72. cb?cb('verified'):''
  73. }else if(res.errno == 9014){
  74. cb?cb():''
  75. _this.login()
  76. }
  77. },(err)=>{
  78. cb?cb():''
  79. console.log("err",err)
  80. })
  81. },
  82. // 登录
  83. login(link){
  84. console.log(link)
  85. var _this = this;
  86. swan.login({
  87. success (res) {
  88. if (res.code) {
  89. // 发起网络请求 发送 res.code 到后台换取 openId, sessionKey, unionId
  90. http.req('/api/wx/loginByUnionId', 'get', {
  91. code: res.code
  92. }, function (res) {
  93. swan.hideLoading()
  94. swan.setStorageSync('isRequest', false)
  95. if(res && res.errno == 0){
  96. //成功
  97. var session_key = res.rst.session_key;
  98. swan.setStorageSync('session_key',session_key)
  99. res.rst.info.avatarUrl = res.rst.avatarUrl;
  100. swan.setStorageSync('userInfo',res.rst.info)
  101. swan.setStorageSync('admin_id',res.rst.info.id)
  102. swan.setStorageSync('wx_subscribe',res.rst.info.wx_subscribe)
  103. swan.setStorageSync('ttl',res.rst.ttl)
  104. var sign=md5.hex_md5("random="+res.rst.random+"&ttl="+res.rst.ttl+"&user_id="+res.rst.info.id);
  105. swan.setStorageSync('sign',sign)
  106. swan.setStorageSync('isLogin',true)
  107. let pages = getCurrentPages(); //页面对象
  108. let currentPage = pages[0];
  109. if(Object.keys(currentPage.options).length>0) {
  110. currentPage.onShow();
  111. }else {
  112. currentPage.onLoad();
  113. }
  114. }else{
  115. if(res.errno=='9012'&&_this.data.num<5) {
  116. _this.data.num = Number(_this.data.num)+1
  117. _this.data.link = cb;//保存设置的回调地址
  118. }else {
  119. // swan.redirectTo({
  120. // url: '/pages/welcome/welcome',
  121. // })
  122. }
  123. }
  124. },()=>{
  125. swan.hideLoading()
  126. })
  127. } else {
  128. console.log('登录失败!' + res.errMsg)
  129. }
  130. }
  131. })
  132. },
  133. //判断微信登录是否过期
  134. checkSession (cb) {
  135. var _this = this;
  136. swan.checkSession({
  137. success (cb) {
  138. //session_key 未过期,并且在本生命周期一直有效
  139. swan.setStorageSync('hasWxLogin',true);//微信登录是否过期
  140. if(!swan.getStorageSync('isLogin') && !swan.getStorageSync('isRequest')){
  141. _this.login()
  142. }else{//已在登录状态
  143. console.log("已在登录状态")
  144. }
  145. },
  146. fail() {
  147. console.log('过期,重新登录')
  148. // session_key 已经失效,需要重新执行登录流程
  149. _this.login()
  150. }
  151. })
  152. },
  153. shareEvent(res,self_page){//分享
  154. var pages = getCurrentPages() //获取加载的页面
  155. var currentPage = pages[pages.length-1] //获取当前页面的对象
  156. var url = currentPage.route //当前页面url
  157. var options = currentPage.options
  158. options = Object.assign(options,{'platform':this.data.platform})
  159. var path = '';
  160. for(var i in options){
  161. path += '&' + i + '=' + options[i]
  162. }
  163. var path2 = url + "?" + path.substr(1)
  164. if(res.from === 'button'){
  165. //来自页面内转发按钮
  166. }
  167. return{
  168. title:'问星数据【视频号版】最新的视频号分析工具',
  169. imageUrl:'https://kx-gw.oss-cn-beijing.aliyuncs.com/wxVideoBack.png',
  170. path: self_page?self_page:path2
  171. }
  172. },
  173. func: {
  174. req: http.req,
  175. md5: md5.hex_md5,
  176. getDay: commonJs.getDay,
  177. NumberHandle: commonJs.NumberHandle
  178. }
  179. })