<repo_name>pop0030/vue-webview-based-player<file_sep>/src/js/fbSDK.js
import axios from 'axios'
import auth from './auth'

const fields = {
    "fields": "name,id,picture,email,cover,age_range,link,gender,locale,timezone,verified"
}

window.fbAsyncInit = ()=>{ 
    fbAsyncInit()
}

function fbAsyncInit() {
    console.log('[facebook]=>FB套件建立')
    FB.init({
        appId: '1925894757644354',
        cookie: true, // enable cookies to allow the server to access 
        // the session
        xfbml: true, // parse social plugins on this page
        version: 'v2.8' // use graph api version 2.8
    });
    // Now that we've initialized the JavaScript SDK, we call 
    // FB.getLoginStatus().  This function gets the state of the
    // person visiting this page and can return one of three states to
    // the callback you provide.  They can be:
    //
    // 1. Logged into your app ('connected')
    // 2. Logged into Facebook, but not your app ('not_authorized')
    // 3. Not logged into Facebook and can't tell if they are logged into
    //    your app or not.
    //
    // These three cases are handled in the callback function.
    auth.stateCheck()
};

// This is called with the results from from FB.getLoginStatus().
function statusChangeCallback(response) {
    console.log('[狀態檢查]=>檢查中...')
    // The response object is returned with a status field that lets the
    // app know the current login status of the person.
    // Full docs on the response object can be found in the documentation
    // for FB.getLoginStatus().
    if (response.status === 'connected') {
        console.log('[狀態檢查]=>已連結')
        // Logged into your app and Facebook.
    } else if (response.status === 'not_authorized') {
        // The person is logged into Facebook, but not your app.
        console.log('[狀態檢查]=>未授權')
    } else {
        // The person is not logged into Facebook, so we're not sure if
        // they are logged into this app or not.
        console.log('[狀態檢查]=>未登入')
    }
}

function getData() {
    return new Promise(function(resolve, reject) {
        console.log('[資料連線]=>連線中...')
        let str = new Date()
        FB.getLoginStatus(function(response){
            statusChangeCallback(response);
            if (response.status === 'connected') {
                FB.api('/me', 'GET', fields, function(res) {
                    console.log('[資料連線]=>取得資料')
                    let end = new Date()
                    console.log('[資料連線]=>時間:'+(end-str)+'ms')
                    resolve(res)
                })
            } else {
                resolve('unconnected')
            }
        })
    })
}

// This function is called when someone finishes with the Login
// Button.  See the onlogin handler attached to it in the sample
// code below.
function checkLoginState() {
    FB.getLoginStatus(function(response) {
        statusChangeCallback(response);
    });
}

// Here we run a very simple test of the Graph API after login is
// successful.  See statusChangeCallback() for when this call is made.

function login() {
    console.log('[登入]連線中...')
    FB.login(async function(res){
        console.log('[登入]連線完成')
        statusChangeCallback(res)
        auth.stateCheck()
    }, {scope: 'public_profile,email'})
    //checkLoginState()
}

function logout() {
    return new Promise((resolve,reject)=>{
        console.log('[登出]連線中...')
        FB.api('/me/permissions', 'DELETE', function(res) {
            console.log('[登出]連線完成')
            statusChangeCallback(res)
            resolve()
        })
    })
}

export default {
    fbAsyncInit,
    logout,
    login,
    getData
}<file_sep>/src/js/contentSDK.js
import axios from 'axios'

const contentSDK = {}
const nav = navigator.userAgent
let canDownload = true

const os = function() {
	var os = ""
	if (/(Android)/i.test(nav)) {
		//console.log('android')
		os = "android"
	} else if (/(iPhone|iPad|iPod|iOS)/i.test(nav)) {
		//console.log('ios')
		os = "ios"
	} else {
		os = "pc"
	}
	return os
}

contentSDK.getDeviceId = function() {
	if (os == 'android') {
		//console.log("android")
		window.JSInterface.getDeviceId()
	} else if (os == 'ios') {
		//console.log("ios")
		location.href = 'app:$getDeviceId'
	} else {
		//console.log("pc")
	}
}

contentSDK.genToken = async function(deviceId, userId, getJWTTokenURL) {
	let jsonData = JSON.stringify({ "iss": "ubn.com", "company": "ubn", "awesome": true, "account": "u7481722", "deviceId": deviceId, "userId": userId })
	let strTime = new Date()
	let res = await axios({
		url: getJWTTokenURL,
		method: 'POST',
		responseType: "json",
		header: {
			'Content-Type': 'application/json',
		},
		data: jsonData
	})
	let endTime = new Date()
	let transTime = endTime - strTime
	//console.log(transTime+ 'ms')
	contentSDK.saveToken(res.returnToken)
	return res.data
}



contentSDK.sendAllContentId = function(strIds) {
	console.log(strIds)

	if (os == 'android') {
		//console.log("android")
		window.JSInterface.checkBookDownload(strIds)
	} else if (os == 'ios') {
		//console.log("ios")
		location.href = 'app:$checkBookDownload:$strIds=' + strIds
	} else {
		//console.log("pc")
	}

}

contentSDK.saveToken = function(token) {
	//console.log("os=" + os + "saveToken=" + token)

	if (os == 'android') {
		window.JSInterface.saveToken(token)
	} else if (os == 'ios') {
		location.href = 'app:$saveToken:$token=' + token
	}

}



contentSDK.checkBookDownload = function(strIds) {

}



contentSDK.downloadBookFile = function(contentId, token, cdn_url, imagePath) {
	//console.log('contentId==>' + contentId)
	//console.log('token==>' + token)
	//console.log('cdn_url==>' + cdn_url)
	//console.log('imagePath==>' + imagePath)
	if (canDownload) {
		if (os == 'android') {
			window.JSInterface.downloadBookFile(contentId, token, cdn_url, imagePath)
		} else if (os == 'ios') {
			location.href = 'app:$downloadBookFile:$contentId=' + contentId + '&token=' + token + '&cdn_url=' + encodeURI(cdn_url) + '&image_url=' + encodeURI(imagePath)
		}
		canDownload = false
	} else {
		alert('檔案尚在下載中，請稍後')
		return
	}
}



contentSDK.bindDownloadChanged = function(contentId, percent) {
	//console.log("percent=" + percent)
	if (percent == 100) {
		canDownload = true
	}
	bindDownloadChanged(contentId, percent)
}



contentSDK.openEPUB = function(contentId) {
	//console.log('open epub contentId=' + contentId)
	if (os == 'android') {
		window.JSInterface.openEPUB(contentId)
	} else if (os == 'ios') {
		location.href = 'app:$openEPUB:$contentId=' + contentId
	}

}



contentSDK.deleteBookFile = function(contentId) {

	if (os == 'android') {
		window.JSInterface.deleteBookFile(contentId)
	} else if (os == 'ios') {
		location.href = 'app:$deleteBookFile:$contentId=' + contentId
	}

}

contentSDK.deletedContentId = function(contentId) {

}

contentSDK.playMovie = function(contentPath) {
	if (os == 'android') {
		window.JSInterface.playMovie(contentPath)
	} else if (os == 'ios') {
		location.href = 'app:$playMovie:$contentPath=' + contentPath
	}
}

contentSDK.errorCallback = function(errCode) {
	//console.log('JS errorCallback:' + errCode)
	canDownload = true
	errorCallback(errCode)
}

contentSDK.openQRCode = function() {
	//console.log('JS openQRCode.....')
	if (os == 'android') {
		window.JSInterface.openQRCode()
	} else if (os == 'ios') {

	}
}

export default contentSDK
<file_sep>/src/js/auth.js
import axios from 'axios'
import fb from './fbSDK'
import store from './store'

const stateCheck = async function(){
	console.log('[檢查登入]開始檢查...')
	let apiId = '<KEY>'
	let apiUrl = 'https://script.google.com/macros/s/'+ apiId +'/exec'
	let fbDataObj = await fb.getData()
	if ( fbDataObj === 'unconnected' ) {
		console.log('[檢查登入]FB未登入或其他問題')
	} else {
		console.log('[檢查登入]FB已授權連線')
		let str = new Date()
		let fbDataText = JSON.stringify(fbDataObj)
		console.log('[檢查登入]確認會員資料...')
		axios({
			method: 'POST',
			url: apiUrl,
			params : {
				action: 'loginFromFacbook'
			},
			data: fbDataText
		}).then((res)=>{
			let userData = res.data[fbDataObj.id]
			console.log('[檢查登入]完成確認')
			console.log('歡迎!'+userData.name)
			let end = new Date()
			console.log('[檢查登入]=>時間:'+(end - str)+'ms')
			store.commit('updateUser', userData)
		}).catch((err)=>{
			console.log(err)
		})
	}
}

const login = function(){
	fb.login()
}

const logout = async function(){
	let logout = await fb.logout()
	store.commit('clearUser')
}


export default {
	stateCheck,
	login,
	logout
}