Files
video_mini/utils/playerManager.js
T
2026-09-05 10:55:26 +08:00

509 lines
13 KiB
JavaScript

var playletPlugin = requirePlugin("playlet-plugin");
var serialNo = null
import configs from '@/utils/config.js';
import {
httpRequest
} from '@/utils/request'
import {
login
} from './login'
import PAY from '@/utils/pay'
// 点击按钮触发此函数跳转到播放器页面
function navigateToPlayer(obj) {
// 下面的${dramaId}变量,需要替换成小程序管理后台的媒资管理上传的剧的dramaId
// 变量${srcAppid}是提审方appid
// 变量${serialNo}第几集
// 变量${extParam}是分享参数,分享的卡片和二维码会在分享的链接上携带此参数
const {
extParam,
dramaId,
srcAppid = configs.mini_appid,
serialNo = 1
} = obj
wx.navigateTo({
url: `plugin-private://wx94a6522b1d640c3b/pages/playlet/playlet?dramaId=${dramaId}&srcAppid=${srcAppid}&serialNo=${serialNo}&extParam=${extParam || ''}`
})
}
function deepClone(obj) {
if (typeof obj !== 'object' || !obj) return obj
let newObj = obj instanceof Array ? [] : {}
for (let key in obj) {
if (typeof obj[key] === 'object') {
newObj[key] = deepClone(obj[key])
} else {
newObj[key] = obj[key]
}
}
return newObj
}
const proto = {
data: { // 可通过this.data访问
playerId: '', //播放器id
arr: [],
vid: "", //剧id
info: {},
mid: "", //剧集id
userInfo: {},
is_record: 0, //是否追剧 0未追 1已追
pay_oid: '',
},
_onPlayerLoad(info) {
let that = this,
shareInterval = null;
console.log("_onPlayerLoad", info);
if (info.extParam) {
if (info.extParam.indexOf("%") < 0) {
this.data.info = info;
} else if (info.extParam) {
info.extParam = decodeURIComponent(info.extParam)
this.data.info = info;
} else {}
this.data.vid = JSON.parse(info.extParam).vid;
if (JSON.parse(info.extParam).g_order_id) { //存储分享后的gid
uni.setStorageSync('g_order_id', JSON.parse(info.extParam).g_order_id);
uni.setStorageSync('g_order_vid', JSON.parse(info.extParam).vid);
}
}
this.data.playerId = info.playerId
const pm = playletPlugin.PlayletManager.getPageManager(info.playerId)
this.pm = pm
let user = uni.getStorageSync('userinfo');
if (user.token) {
this.data.userInfo = uni.getStorageSync('userinfo');
console.log('onPlayerLoad info', info, 'data', this.data)
that.getVideoId();
that.getEncryptData({
serialNo: info.serialNo
}).then(setRes => {
// encryptedData是后台加密后的数据,具体实现见官方文档的加密章节
let lists = [setRes.data];
pm.setCanPlaySerialList({
data: setRes.encryptedData,
freeList: lists
// freeList: [{ // 1~10集是免费剧,data里面的字段也必须至少设置1~10集可播放
// start_serial_no: setRes.data.start_serial_no,
// end_serial_no: setRes.data.end_serial_no
// }],
})
});
} else {
login.wxLogin().then(res => {
this.data.userInfo = uni.getStorageSync('userinfo');
console.log('onPlayerLoad info', info, 'data', this.data)
that.getVideoId();
that.getEncryptData({
serialNo: info.serialNo
}).then(setRes => {
// encryptedData是后台加密后的数据,具体实现见官方文档的加密章节
let lists = [setRes.data];
pm.setCanPlaySerialList({
data: setRes.encryptedData,
freeList: lists
})
});
});
}
// 需要解锁的事件
pm.onCheckIsCanPlay(this.onCheckIsCanPlay);
// 页面返回事件
pm.onBack(this.pageBack)
// 支付回调事件
pm.onCustomEvent('chargeEnd', this.chargeEnd)
pm.setVisualEffectOnCapture({
visualEffect: 'none',
})
// 参考文档章节“数据上报”
pm.onDataReport((obj) => {
if (obj.event === playletPlugin.REPORT_DATA_EVENTS.LIKE || obj.event === playletPlugin
.REPORT_DATA_EVENTS.UNLIKE) { //监听点赞、取消点赞操作
console.log("user like")
this.userLikeChange();
} else if (obj.event === playletPlugin.REPORT_DATA_EVENTS.FAV || obj.event === playletPlugin
.REPORT_DATA_EVENTS.UNFAV) { //监听在追,取消在追操作
console.log("user zhui ju")
this.changeStar()
} else if (obj.event === playletPlugin.REPORT_DATA_EVENTS.CHANGE_SERIAL) { //监听切换剧集操作
console.log("user changePlay log")
this.changePlayLog()
} else if (obj.event === playletPlugin.REPORT_DATA_EVENTS.SHARE) { //监听分享操作
console.log("user share")
} else {
// console.log("其他操作")
}
})
// 设置右侧固定运营位置跳转路径
pm.setActivityInfo({
url: ''
})
// 设置运营区域
pm.updateOpenArea({
showLeft: false,
showRight: false,
leftsideAreaList: [{
left: 16, // 类似绝对定位的样式
top: 20,
width: 72,
height: 32
}, ],
ext: 'extInfo',
})
},
_initShare() {
const pm = this.pm
// 关于分享的处理
// 开启分享以及withShareTicket
pm.setDramaFlag({
share: true,
withShareTicket: true
})
// 获取分享参数,页面栈只有短剧播放器一个页面的时候可获取到此参数
// 例如从分享卡片进入、从投流广告直接跳转到播放器页面,从二维码直接进入播放器页面等情况
playletPlugin.getShareParams().then(res => {
console.log('getLaunch options query res', res)
// 关于extParam的处理,需要先做decodeURIComponent之后才能得到原值
const extParam = decodeURIComponent(res.extParam)
console.log('getLaunch options extParam', extParam)
// 如果设置了withShareTicket为true,可通过文档的方法获取更多信息
// https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share.html
const enterOptions = wx.getEnterOptionsSync()
console.log('getLaunch options shareTicket', enterOptions.shareTicket)
}).catch(err => {
console.log('getLaunch options query err', err)
})
this.getVideoId()
// 设置分享参数
pm.setExtParam(JSON.stringify({
vid: this.data.vid,
fxpid: this.data.userInfo.id,
invite: this.data.userInfo.invite || ''
}))
},
// 检测是否可以播放
onCheckIsCanPlay(param) {
var that = this;
let intervalTime = null;
serialNo = param.serialNo
console.log('onCheckIsCanPlay param', param);
if (uni.getStorageSync('userinfo').token) {
that.getVideoId().then(video => {
that.beansPay(video.data.id, param.serialNo);
})
} else {
intervalTime = setInterval(() => {
if (uni.getStorageSync('userinfo').token) {
clearInterval(intervalTime)
that.getVideoId().then(video => {
that.beansPay(video.data.id, param.serialNo);
})
} else {
console.log('no users login')
}
}, 100)
}
},
getEncryptData(obj) {
const {
code,
serialNo
} = obj;
let that = this;
// TODO: 此接口请求后台,返回下面的setCanPlaySerialList接口需要的加密参数
const {
srcAppid,
dramaId
} = this.pm.getInfo()
console.log('getEncryptData start', srcAppid, dramaId, serialNo)
return new Promise((resolve, reject) => {
// 获取免费剧集列表(包含用户已经支付的 )
wx.login({
success(res) {
// console.log('code',res.code)
httpRequest({
url: "/api/video/videoInfoWechatV1",
method: "GET",
data: {
code: res.code,
vid: that.data.vid,
oid: that.data.pay_oid
}
}).then(res => {
console.log('videoPlayer getCanPlayList res', res)
resolve({
data: res.data.data,
encryptedData: res.data.enData
})
}).catch(err => {
reject(err)
console.log(err)
})
}
})
})
},
pageBack() {
let that = this;
if (that.data.is_record == 1) {
// that.pm.navigateBack()
that.pm.switchTab({
url: '/pages/tabs/home'
})
} else {
uni.showModal({
title: "提示",
content: "是否将此剧加入追剧列表",
success(modelRes) {
if (modelRes.confirm) {
console.log('success');
that.changeStar();
}
that.pm.switchTab({
url: '/pages/tabs/home'
})
}
})
}
},
getVideoId() {
let that = this;
let infos = this.pm.getInfo();
console.log('getVideoId start', infos);
// 获取当前需支付剧集ID(媒资库的ID不能用,需要用我们自己的剧集ID)
return new Promise((resolve, reject) => {
httpRequest({
url: "/api/video/dramaIdToVideoId",
method: "GET",
data: {
drama_id: infos.dramaId,
no: infos.serialNo
}
}).then(res => {
this.data.mid = res.data.id;
if (res.data.drama_vid != that.data.vid) {
that.data.vid = res.data.drama_vid
}
this.data.is_record = res.data.is_record
resolve(res)
}).catch(err => {
console.log(err)
reject(err)
})
})
},
// 金豆购买剧集
beansPay(mid, serialNo) {
let buyData = uni.getStorageSync("buydata");
let that = this;
console.log('beansPay begin', mid, serialNo)
let upData = {
sum: 1,
buytype: "金豆",
pricebuy: buyData.isvip == 2 ? buyData.vippriceq : buyData.priceq,
token: uni.getStorageSync("userinfo").token,
uid: uni.getStorageSync("userinfo").id,
ad_code: uni.getStorageSync('get_ad_code'),
lx: 2, //1 全章购买 2单集购买
smid: mid, //单集ID
wxlx: 'wxxcx',
id: that.data.vid, //剧ID
openid: uni.getStorageSync("openid")
}
// 支付
PAY.pays.beansPay(upData).then((res) => {
console.log('beansPay res', res)
that.getEncryptData({
serialNo: serialNo
}).then(enRes => {
that.pm.isCanPlay({
data: enRes.encryptedData,
serialNo: serialNo,
status: res.code == 1 ? 1 : 2
})
})
}).catch(err => {
if (err.msg == '金豆不足') {
that.getEncryptData({
serialNo: serialNo
}).then(enRes => {
that.pm.isCanPlay({
data: enRes.encryptedData,
serialNo: serialNo,
status: 2
})
})
}
console.log('jinDouPay fail', err)
})
},
// 更新喜欢状态
userLikeChange() {
let that = this;
that.getVideoId({
dramaId: that.data.info.dramaId,
serialNo: that.data.info.serialNo
}).then(res => {
httpRequest({
url: "/api/user/tapLove",
method: "GET",
data: {
vid: that.data.vid,
mid: res.data.id
}
}).then(res => {
console.log('userLikeChange res', res)
})
})
},
// 更新追剧状态
changeStar() {
let that = this;
let subData = {
vid: that.data.vid,
token: uni.getStorageSync("userinfo").token,
uid: uni.getStorageSync("userinfo").id,
mid: ""
}
that.getVideoId({
dramaId: that.data.info.dramaId,
serialNo: that.data.info.serialNo
}).then(res => {
httpRequest({
url: "/api/user/videorecord",
method: "GET",
data: {
vid: that.data.vid,
mid: res.data.id
}
}).then(rest => {
console.log('userLikeChange res', rest)
}).catch(errs => {
console.log(errs)
})
})
},
// 更新播放记录
changePlayLog() {
let that = this;
that.getVideoId({
dramaId: that.data.info.dramaId,
serialNo: that.data.info.serialNo
}).then(res => {
console.log('changePlayLog mid', res)
httpRequest({
url: "/api/video/bfjl",
method: "GET",
data: {
vid: that.data.vid,
mid: res.data.id
}
}).then(rest => {
console.log('changePlayLog res', rest)
}).catch(errs => {
console.log('change PlayLog fail', errs)
})
})
},
adEnd(mid) {
let buyData = uni.getStorageSync("buydata");
let that = this;
let upData = {
sum: 1,
buytype: 'adUnlock',
pricebuy: buyData.isvip == 2 ? buyData.vippriceq : buyData.priceq,
token: uni.getStorageSync("userinfo").token,
uid: uni.getStorageSync("userinfo").id,
ad_code: uni.getStorageSync('get_ad_code'),
lx: 2, //1 全章购买 2单集购买
smid: mid, //单集ID
wxlx: 'wxxcx',
id: that.data.vid, //剧ID
openid: uni.getStorageSync("openid")
}
// 支付
PAY.pays.beansPay(upData).then((res) => {
console.log('beansPay res', res)
that.getEncryptData({
serialNo: serialNo.serialNo
}).then(enRes => {
that.pm.isCanPlay({
data: enRes.encryptedData,
serialNo: serialNo.serialNo,
status: res.code == 1 ? 1 : 2
})
})
}).catch(err => {
if (err.msg == '金豆不足') {
that.getEncryptData({
serialNo: serialNo.serialNo
}).then(enRes => {
that.pm.isCanPlay({
data: enRes.encryptedData,
serialNo: serialNo.serialNo,
status: 2
})
})
}
console.log('jinDouPay fail', err)
})
},
// 支付完
chargeEnd(payRes) {
let that = this;
console.log("paySuccess", payRes)
let share_g_order_vid = uni.getStorageSync('g_order_vid');
if (payRes.status == "paySuccess") {
that.data.pay_oid = payRes.oid;
this.onCheckIsCanPlay({
dramaId: that.data.info.dramaId,
serialNo: payRes.serialNo,
oid: payRes.oid
})
that.pm.setExtParam(JSON.stringify({
vid: this.data.vid,
fxpid: this.data.userInfo.id,
invite: this.data.userInfo.invite || '',
g_order_id: payRes.gOrderId,
g_order_vid: payRes.vid
}))
that.pm.hideChargeDialog();
if (payRes.gOrderId && share_g_order_vid != payRes.vid) {
that.pm.updateOpenArea({
showLeft: true
});
}
}
},
}
function PlayerManager() {
var newProto = Object.assign({}, proto)
for (const k in newProto) {
if (typeof newProto[k] === 'function') {
this[k] = newProto[k].bind(this)
} else if (typeof newProto[k] === 'object') {
if (!newProto[k]) {
this[k] = newProto[k]
} else {
this[k] = deepClone(newProto[k])
}
}
}
}
PlayerManager.navigateToPlayer = navigateToPlayer
module.exports = PlayerManager