小程序链接助手

app.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // app.js
  2. var http = require('utils/util.js');
  3. App({
  4. data: {
  5. num: 0,
  6. link: ''
  7. },
  8. onLaunch() {
  9. this.getSetting()
  10. },
  11. getSetting(){
  12. var _this = this;
  13. wx.getSetting({
  14. success: function(res){
  15. if (res.authSetting['scope.userInfo']) {
  16. // 已经授权,可以直接调用 getUserInfo 获取头像昵称
  17. wx.getUserInfo({
  18. success:function (e){
  19. wx.setStorageSync('encryptedData',e.encryptedData)
  20. wx.setStorageSync('iv',e.iv)
  21. _this.checkSession()
  22. }
  23. })
  24. }else{//未授权
  25. wx.clearStorage()
  26. console.log("未授权--verified")
  27. }
  28. }
  29. })
  30. },
  31. checkSession (cb) {//判断微信登录是否过期
  32. var _this = this;
  33. wx.checkSession({
  34. success (cb) {
  35. //session_key 未过期,并且在本生命周期一直有效
  36. if(!wx.getStorageSync('isLogin')){
  37. _this.login(_this.data.link)
  38. }
  39. },
  40. fail() {
  41. // session_key 已经失效,需要重新执行登录流程
  42. _this.login(_this.data.link)
  43. }
  44. })
  45. },
  46. login(cb,errcb){
  47. var _this = this;
  48. wx.login({
  49. success (res) {
  50. console.log(res)
  51. if (res.code) {
  52. if(!wx.getStorageSync('encryptedData')||!wx.getStorageSync('iv')) return;
  53. wx.showLoading({
  54. title: '登录中',
  55. })
  56. // 发起网络请求 发送 res.code 到后台换取 openId, sessionKey, unionId
  57. http.req('/api/user/login', 'get', {
  58. code: res.code,
  59. encrypted_data:wx.getStorageSync('encryptedData'),
  60. iv: wx.getStorageSync('iv'),
  61. }, (res) => {
  62. if(res && res.errno == 0){
  63. wx.hideLoading()
  64. //成功
  65. wx.setStorageSync('session_key',res.rst.session_key)
  66. wx.setStorageSync('userInfo',res.rst)
  67. wx.setStorageSync('isLogin',true)
  68. cb?cb():''
  69. _this.data.num = 0;
  70. }else{
  71. wx.clearStorage()
  72. // errcb?errcb(res.err):'';
  73. if(res.errno=='1004'&&_this.data.num<5) {
  74. _this.data.num = Number(_this.data.num)+1
  75. _this.data.link = cb;//保存设置的回调地址
  76. _this.getSetting();
  77. }
  78. }
  79. },()=>{
  80. errcb?errcb():'';
  81. wx.hideLoading()
  82. })
  83. } else {
  84. console.log('登录失败!' + res.errMsg)
  85. }
  86. }
  87. })
  88. },
  89. shareEvent(res,self_page){//分享
  90. var pages = getCurrentPages() //获取加载的页面
  91. var currentPage = pages[pages.length-1] //获取当前页面的对象
  92. var url = currentPage.route //当前页面url
  93. var options = currentPage.options
  94. var path = '';
  95. for(var i in options){
  96. path += '&' + i + '=' + options[i]
  97. }
  98. var path2 = url + "?" + path.substr(1)
  99. if(res.from === 'button'){
  100. //来自页面内转发按钮
  101. }
  102. return{
  103. title:'扩展链接高佣版',
  104. imageUrl:'https://kx-gw.oss-cn-beijing.aliyuncs.com/app-gw/linkHelp.png?v='+(wx.getStorageSync('version')?wx.getStorageSync('version'):'1.0.2'),
  105. path: self_page?self_page:path2
  106. }
  107. },
  108. func: {
  109. req: http.req,
  110. },
  111. })