123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- // app.js
- var http = require('utils/util.js');
- App({
- data: {
- num: 0,
- link: ''
- },
- onLaunch() {
- this.getSetting()
- },
- getSetting(){
- var _this = this;
- wx.getSetting({
- success: function(res){
- if (res.authSetting['scope.userInfo']) {
- // 已经授权,可以直接调用 getUserInfo 获取头像昵称
- wx.getUserInfo({
- success:function (e){
- wx.setStorageSync('encryptedData',e.encryptedData)
- wx.setStorageSync('iv',e.iv)
- _this.checkSession()
- }
- })
- }else{//未授权
- wx.clearStorage()
- console.log("未授权--verified")
- }
- }
- })
- },
- checkSession (cb) {//判断微信登录是否过期
- var _this = this;
- wx.checkSession({
- success (cb) {
- //session_key 未过期,并且在本生命周期一直有效
- if(!wx.getStorageSync('isLogin')){
- _this.login(_this.data.link)
- }
- },
- fail() {
- // session_key 已经失效,需要重新执行登录流程
- _this.login(_this.data.link)
- }
- })
- },
- login(cb,errcb){
- var _this = this;
- wx.login({
- success (res) {
- console.log(res)
- if (res.code) {
- if(!wx.getStorageSync('encryptedData')||!wx.getStorageSync('iv')) return;
- wx.showLoading({
- title: '登录中',
- })
- // 发起网络请求 发送 res.code 到后台换取 openId, sessionKey, unionId
- http.req('/api/user/login', 'get', {
- code: res.code,
- encrypted_data:wx.getStorageSync('encryptedData'),
- iv: wx.getStorageSync('iv'),
- }, (res) => {
- if(res && res.errno == 0){
- wx.hideLoading()
- //成功
- wx.setStorageSync('session_key',res.rst.session_key)
- wx.setStorageSync('userInfo',res.rst)
- wx.setStorageSync('isLogin',true)
- cb?cb():''
- _this.data.num = 0;
- }else{
- wx.clearStorage()
- // errcb?errcb(res.err):'';
- if(res.errno=='1004'&&_this.data.num<5) {
- _this.data.num = Number(_this.data.num)+1
- _this.data.link = cb;//保存设置的回调地址
- _this.getSetting();
- }
- }
- },()=>{
- errcb?errcb():'';
- wx.hideLoading()
- })
- } else {
- console.log('登录失败!' + res.errMsg)
- }
- }
- })
- },
- shareEvent(res,self_page){//分享
- var pages = getCurrentPages() //获取加载的页面
- var currentPage = pages[pages.length-1] //获取当前页面的对象
- var url = currentPage.route //当前页面url
- var options = currentPage.options
- 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/app-gw/linkHelp.png?v='+(wx.getStorageSync('version')?wx.getStorageSync('version'):'1.0.2'),
- path: self_page?self_page:path2
- }
- },
- func: {
- req: http.req,
- },
- })
|