问星数据小程序

app.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. windowWidth:0,//屏幕的宽度
  8. windowHeight:0,//屏幕的高度
  9. loginGoPage:'pages/index/index?from=login',
  10. loginGoPageQuery:{}
  11. },
  12. onLaunch: function () {
  13. this.verified();//版本控制(过审用)
  14. },
  15. onShow:function(options){
  16. console.log("onshow",options)
  17. if (options.scene == 1007 || options.scene == 1008) {
  18. // 1007通过单人聊天会话分享进入 1008 通过群聊会话分享进入
  19. this.data.loginGoPage = options.path
  20. this.data.loginGoPageQuery = options.query
  21. }
  22. if (options.scene == 1001) {
  23. // 通过发现栏小程序进入
  24. }
  25. },
  26. verified(){//版本控制
  27. var _this = this;
  28. console.log("开始")
  29. http.req('/v2/api/wx/verified', 'get', {
  30. version: 'v1.7'
  31. }, function (res) {
  32. if(res && res.errno == 0){
  33. wx.setStorageSync('isLogin',false)
  34. wx.setStorageSync('admin_id',res.rst.data.id)
  35. if(!wx.getStorageSync('ttl')||wx.setStorageSync('ttl')!=res.rst.data.ttl) {
  36. wx.setStorageSync('ttl',res.rst.data.ttl)
  37. var sign=md5.hex_md5("random="+res.rst.data.random+"&ttl="+res.rst.data.ttl+"&user_id="+res.rst.data.id);
  38. wx.setStorageSync('sign',sign)
  39. }
  40. wx.setStorageSync('doneLogin', false)
  41. _this.loginGoPageEvent()
  42. }else if(res.errno == 9014){
  43. console.log("自己的权限")
  44. wx.getSystemInfo({
  45. success (res) {
  46. _this.data.windowWidth = res.windowWidth
  47. _this.data.windowHeight = res.windowHeight
  48. }
  49. })
  50. _this.getSetting()
  51. }
  52. },(err)=>{
  53. console.log("err",err)
  54. })
  55. },
  56. getSetting(){
  57. var _this = this;
  58. wx.getSetting({
  59. success: function(res){
  60. if (res.authSetting['scope.userInfo']) {
  61. // 已经授权,可以直接调用 getUserInfo 获取头像昵称
  62. wx.getUserInfo({
  63. success:function (e){
  64. wx.setStorageSync('encryptedData',e.encryptedData)
  65. wx.setStorageSync('iv',e.iv)
  66. _this.checkSession()
  67. }
  68. })
  69. }else{
  70. //未授权
  71. console.log("未授权--verified")
  72. wx.redirectTo({
  73. url: '/pages/welcome/welcome',
  74. })
  75. }
  76. }
  77. })
  78. },
  79. checkSession (cb) {//判断微信登录是否过期
  80. var _this = this;
  81. wx.checkSession({
  82. success (cb) {
  83. //session_key 未过期,并且在本生命周期一直有效
  84. wx.setStorageSync('hasWxLogin',true);//微信登录是否过期
  85. if(!wx.getStorageSync('isLogin') && !wx.getStorageSync('isRequest')){
  86. _this.login()
  87. }else{//已在登录状态
  88. console.log("已在登录状态")
  89. }
  90. },
  91. fail() {
  92. console.log('过期,重新登录')
  93. // session_key 已经失效,需要重新执行登录流程
  94. _this.login()
  95. }
  96. })
  97. },
  98. login(cb){
  99. console.log("登录ing")
  100. var _this = this;
  101. wx.login({
  102. success (res) {
  103. if (res.code) {
  104. // 发起网络请求 发送 res.code 到后台换取 openId, sessionKey, unionId
  105. http.req('/v2/api/wx/login', 'get', {
  106. code: res.code,
  107. encrypted_data:wx.getStorageSync('encryptedData'),
  108. iv: wx.getStorageSync('iv'),
  109. platform_id: 1
  110. }, function (res) {
  111. wx.hideLoading()
  112. console.log(res,'logiin')
  113. wx.setStorageSync('isRequest', false)
  114. if(res && res.errno == 0){
  115. //成功
  116. var session_key = res.rst.session_key;
  117. wx.setStorageSync('session_key',session_key)
  118. if(!res.rst.info || res.rst.info.length == 0){//用户未绑定手机号
  119. wx.setStorageSync('noneLogin',true);//微信登录是否过期
  120. wx.redirectTo({
  121. url: '/pages/login/login',
  122. })
  123. }else{
  124. wx.setStorageSync('userInfo',res.rst.info)
  125. wx.setStorageSync('admin_id',res.rst.info.id)
  126. wx.setStorageSync('ttl',res.rst.ttl)
  127. var sign=md5.hex_md5("random="+res.rst.random+"&ttl="+res.rst.ttl+"&user_id="+res.rst.info.id);
  128. wx.setStorageSync('sign',sign)
  129. wx.setStorageSync('isLogin',true)
  130. cb?cb():''
  131. _this.loginGoPageEvent()
  132. }
  133. }else{
  134. console.log("跳转授权- login接口 errno != 0")
  135. wx.redirectTo({
  136. url: '/pages/welcome/welcome',
  137. })
  138. }
  139. },()=>{
  140. wx.hideLoading()
  141. })
  142. } else {
  143. console.log('登录失败!' + res.errMsg)
  144. }
  145. }
  146. })
  147. },
  148. loginGoPageEvent(){//登录跳转的页面事件
  149. var queryString = '?';
  150. for(var i in this.data.loginGoPageQuery){
  151. if(queryString != '?'){
  152. queryString = '&' + queryString + i + '=' + this.data.loginGoPageQuery[i]
  153. }else{
  154. queryString = queryString + i + '=' + this.data.loginGoPageQuery[i]
  155. }
  156. }
  157. if(queryString == '?'){
  158. queryString = '';
  159. }
  160. console.log(queryString,this.data.loginGoPage,this.data.loginGoPageQuery)
  161. if(this.data.loginGoPage.indexOf('pages/index/index') != -1 || this.data.loginGoPage.indexOf('pages/live/live') != -1 || this.data.loginGoPage.indexOf('pages/originality/originality/index') != -1 || this.data.loginGoPage.indexOf('pages/follow/follow') != -1){
  162. wx.switchTab({
  163. url: '/'+this.data.loginGoPage + queryString,
  164. success: function(res){
  165. var page = getCurrentPages().pop();
  166. if (page == undefined || page == null) return;
  167. page.onLoad();
  168. },
  169. })
  170. }else{
  171. wx.navigateTo({
  172. url: '/'+this.data.loginGoPage + queryString
  173. })
  174. }
  175. },
  176. func: {
  177. req: http.req,
  178. md5: md5.hex_md5,
  179. getDay: commonJs.getDay,
  180. NumberHandle: commonJs.NumberHandle
  181. },
  182. })