代码拉取完成,页面将自动刷新
//app.js
const innerAudioContext = wx.createInnerAudioContext()
App({
onLaunch: function () {
// 初始化微信云开发
wx.cloud.init({
env: "hanzirobort-3gbrut3o3db70186"
});
//初始化语音识别
this.initAudio();
// 给字符串添加是否包含子串的方法
String.prototype.contains = function (str) {
return this.indexOf(str) >= 0;
}
// 展示本地存储能力
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
//
// 登录授权
// wx.login({
// })
// 获取用户信息,判断是否授权
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
// 可以将 res 发送给后台解码出 unionId
this.globalData.userInfo = res.userInfo
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
if (this.userInfoReadyCallback) {
this.userInfoReadyCallback(res)
}
}
})
}
}
})
},
globalData: {
userInfo: null,
isLogin:0,
status: 0,
header: {},
isExperience:0,
host:"https://megalight.top/shizirobot-0.0.1",
xiaobingHost:"https://megalight.top/xiaobing",
// xiaobingobsHeader:"https://zhaoguang2020-1304322828.cos.ap-chongqing.myqcloud.com/hanzixiaobing",//小兵obs图片位置
// xiaobingHost:"http://localhost:9090",
// host:"http://localhost:8080",
// host:"http://192.168.20.106:8080",
// host:"https://wx.megalight.cn:9000",
isPayFor: false,
manager: null,
plugin: null,
innerAudioContext: null,
imgpathHeader: "https://zhaoguang2020-1304322828.cos.ap-chongqing.myqcloud.com/shizixiaobing/shiziRobotImg",
previousImg: "https://zhaoguang2020-1304322828.cos.ap-chongqing.myqcloud.com/shizixiaobing/shiziRobotImg/img/prePage.png",
contentinfoGroupAndPart:[],
isgotopay:true,
wordgulitimers:null
},
startIsPayForListenner: function () {
let _this = this;
console.log(this.globalData.header)
if (this.globalData.isPayFor == false) {
let timeSet = setInterval(() => {
wx.request({
url: this.globalData.host + '/updateUseTimeAndPayFor',
method: 'POST',
header: this.globalData.header,
data: { status: _this.globalData.status },
success: res => {
// console.log(this.globalData.header);
if (res.data == 1) {
//提示去登录
clearInterval(timeSet);
wx.showModal({
title: "",
content: '您的游客试用时长已到期,请先登录',
showCancel: false,
confirmColor: "#0f0",
success: res => {
_this.globalData.status=1;
//调用登录
wx.redirectTo({
url: '/pages/login/login',
})
}
})
}
else if (res.data == 2) {
// 提示去支付
clearInterval(timeSet);
wx.showModal({
title: "",
content: '您的试用时长已到期,请先支付10元!',
showCancel: true,
cancelText: "取消",
confirmText: "去支付",
confirmColor: "#0f0",
success: function (res) {
if (res.confirm) {
wx.cloud.callFunction({
name: 'pay',
data: {
goodName: "汉字小兵会员卡", //商品名
totalFee: 1000 //商品价格,1为1分钱
},
success: res => {
console.log("获取支付参数成功", res);
const payment = res.result.payment
//调起支付
wx.requestPayment({
...payment,
success: res => {
console.log('pay success', res)
wx.request({
url: _this.globalData.host + '/updatePayForStatus',
method: 'GET',
header: getApp().globalData.header,
data: {},
success: res => {
wx.showModal({
cancelColor: 'cancelColor',
title: "支付成功,您可以永久使用",
showCancel: false,
success(res) {
}
})
},
fail: err => {
console.log('支付失败')
}
})
},
fail: err => {
wx.redirectTo({
url: '/pages/login/login',
})
console.error('pay fail', err)
}
})
},
fail: res => {
console.log("支付错误", res);
}
})
} else {
wx.redirectTo({
url: '/pages/login/login',
})
}
},
})
}
}
})
}, 5000)
}
else {
console.log('用户已支付')
}
},
initAudio: function () {
//调用腾讯云语音识别插件
var appid = 1304322828
var secretid = 'AKIDzvdvBE1c09RWN1bqvvkV7ZolaRUOoVnr'
var secretkey = '53pLDBwTnsktuWAZiphKkywnRpzwJRRE'
var openConsole = false
this.globalData.plugin = requirePlugin('QCloudAIVoice')
this.globalData.plugin.setQCloudSecret(appid, secretid, secretkey, openConsole)
this.globalData.manager = this.globalData.plugin.getRecordRecognitionManager();
this.globalData.innerAudioContext = wx.createInnerAudioContext();
this.globalData.manager.onStart((res) => {
console.log('开始语音识别', res.msg);
})
this.globalData.manager.onStop((res) => {
console.log('停止语音识别0', res.tempFilePath);
//跳到下一个词
// this.gotoNext();
})
this.globalData.manager.onError((res) => {
console.log('recorder error', res.errMsg);
})
},
//读出text函数
audioTTS: function (text) {
var appid = 1304322828
var secretid = 'AKIDzvdvBE1c09RWN1bqvvkV7ZolaRUOoVnr'
var secretkey = '53pLDBwTnsktuWAZiphKkywnRpzwJRRE'
var openConsole = true
this.globalData.plugin.setQCloudSecret(appid, secretid, secretkey, openConsole)
console.log("textSpeech:"+this.globalData.plugin);
//语音合成
this.globalData.plugin.textToSpeech({
content: text,
speed: 0,
volume: 0,
voiceType: 0,
language: 1,
projectId: 0,
sampleRate: 16000,
success: (data) => {
let url = data.result.filePath;
if (url && url.length > 0) {
this.globalData.innerAudioContext.autoplay = true;
this.globalData.innerAudioContext.src = url;
this.globalData.innerAudioContext.onPlay(() => {
});
this.globalData.innerAudioContext.onError((res) => {
console.log(res.errMsg)
});
}
},
fail: function (error) {
console.log(error);
}
})
}
,
ispay:function(){
var that=this;
return new Promise( function ( resolve, reject ) {
console.log("判断是否需要付款")
wx.request({
url: that.globalData.host + '/isPayForStatusinfo',
method: 'GET',
header: that.globalData.header,
data: {},
success: res => {
console.log(res)
console.log("判断是否需要付款")
if(res.data){
console.log("需要付款")
// wx.navigateTo({
// url: '../paypage/paypage',
// })
resolve(true);
}else{
resolve(false);
console.log("不需要付款")
}
},
fail: err => {
console.log('判断是否需要付款失败')
reject( "异步执行失败" );
}
})
} );
}
,
findword:function(contentGroup,contentPart){
var that=this;
return new Promise( function ( resolve, reject ) {
wx.request({
url: that.globalData.host+'/startStudy',
method:"POST",
data:{contentGroup:contentGroup,
contentPart:contentPart},
success:(contentinfoGroupAndPart)=>{
// console.log("startStudy:"+this.data.contentGroup)
// console.log("startStudy:"+this.data.contentPart)
// this.setData({
// contentinfoGroupAndPart:contentinfoGroupAndPart
// })
//加载词语数组后将pagenum重新设置为0,
// console.log("pagenum0:"+ this.data.pageNum)
// this.data.pageNum=0;
// console.log("pagenum1:"+ this.data.pageNum)
// this.setContentImg(this.data.contentinfoGroupAndPart.data[this.data.pageNum].imgpath);
// this.setStudyRecord();
resolve(contentinfoGroupAndPart.data );
},
fail:(error)=>{
console.log(error)
reject( "异步执行失败" );
}
})
} );
},
//汉字小兵说话接口
getHanziAudio:function(id){
var that=this;
return new Promise( function ( resolve, reject ) {
wx.request({
url: that.globalData.xiaobingHost+'/demo/getAudio',
method:"GET",
data:{
// Subjects:"语文",
// Levels:0.0,
// Options:"首页",
// Types:"欢快",
// Subjects:Subjects,
// Levels:Levels,
// Options:Options,
// Types:Types,
id:id
},
success:(res)=>{
// console.log("Texts:"+res.data[0].Texts)
// console.log("AudioURL:"+res.data[0].AudioURL)
// this.setData({
// // backimg:getApp().globalData.xiaobingobsHeader+res.data.imageURL
// hanzitexts:res.data[0].Texts,
// textsAudio:getApp().globalData.imgpathHeader+res.data[0].AudioURL,
// })
// this.displayTexts();
// console.log(this.data.textsAudio);
// this.audioPlay();
resolve(res.data);
return res.data;
},
fail:(error)=>{
console.log(error)
}
})
})
},
//汉字小兵动作图片接口
getHanziImage:function(id){
var that=this;
return new Promise( function ( resolve, reject ) {
wx.request({
url: that.globalData.xiaobingHost+'/demo/selectOne',
method:"GET",
data:{
id:id,
},
success:(res)=>{
// console.log("res:"+res.data)
// console.log("imageURL:"+res.data.imageURL)
// let img=res.data.imageURL;
// console.log(img);
// this.setData({
// backimg:getApp().globalData.imgpathHeader+res.data.imageURL,
// })
resolve(res.data);
return res.data;
},
fail:(error)=>{
console.log(error)
}
})
})
},
//获取汉字小兵语音接口在页面上调用的模板
getHanziTextAndAudioJieKou:function(id){
var jiekou= getApp().getHanziAudio(id);
jiekou.then( function ( value ) {
console.log("调用的语音文字接口的路径"+value.audioURL);
}, function ( error ) {
console.log( "异步执行失败,输出执行结果:" + error );
} );
},
//获取汉字小兵图片接口在页面上调用的模板
getHanziImageJieKou:function(id){
var jiekou= getApp().getHanziImage(id);
jiekou.then( function ( value ) {
console.log("调用的图片接口的路径"+value.imageURL);
}, function ( error ) {
console.log( "异步执行失败,输出执行结果:" + error );
} );
},
//播放音频
playAudio:function(textsAudio){
innerAudioContext.autoplay = true
innerAudioContext.src = this.globalData.imgpathHeader+textsAudio
innerAudioContext.onPlay(() => {
console.log('开始播放')
})
innerAudioContext.onError((res) => {
console.log(res.errMsg)
console.log(res.errCode)
})
},
//停止播放音频,退出当前页面时,暂停声音
stopAudio:function(){
innerAudioContext.stop();
},
onEndedAudio:function(){
// innerAudioContext.onEnded((res)=>{
// wx.redirectTo({
// url: '/pages/main/main',
// })
// });
},
//监听语音播放完的事件
//显示动图
// //播放音频
// showhanziImg:function(textsAudio){
// const innerAudioContext = wx.createInnerAudioContext()
// innerAudioContext.autoplay = true
// innerAudioContext.src = this.globalData.imgpathHeader+textsAudio
// innerAudioContext.onPlay(() => {
// console.log('开始播放')
// })
// innerAudioContext.onError((res) => {
// console.log(res.errMsg)
// console.log(res.errCode)
// })
// },
//返回到map页面
back:function(){
getApp().stopAudio();
wx.redirectTo({
url: '../../../map/map',
})
},
//切换小岛,返回到选小地图页面
changeXiaodao:function(){
getApp().stopAudio();
wx.navigateBack({
delta: 1,
})
},
//登录方法
denglu:function(){
let _this=this
wx.getSetting({
success: (res) => {
if (res.authSetting["scope.userInfo"]) {
wx.getUserInfo({
success: function (res) {
console.log(res);
var avatarUrl = 'userInfo.avatarUrl';
var nickName = 'userInfo.nickName';
that.setData({
[avatarUrl]: res.userInfo.avatarUrl,
[nickName]: res.userInfo.nickName,
})
if(getApp().globalData.isLogin==0){
wx.showLoading({
title: '登录中...',
mask:true
})
wx.login({
// timeout: 0,
success: (res) => {
//res.code只存在5分钟
console.log(res)
//获取登录的临时凭证
let code = res.code;
//获取后端的session_key,secret
console.log(new Date())
if (code) {
wx.request({
url: app.globalData.host+'/wxLogin',
method: 'POST',
data: { code: code },
success: (res) => {
if (res.statusCode >= 200 && res.statusCode < 300) {
wx.hideLoading({
success: (res) => {
wx.showToast({
title: '登录成功',
})
},
})
}else{
wx.hideLoading({
success: (res) => {
wx.showToast({
title: '登录失败',
})
},
})
}
console.log("sessionid:"+JSON.stringify(res.data))
// wx.setStorageSync('sessionid', res.data)
console.log(typeof res.data)
// wx.redirectTo({
// url: '../main/main',
// })
if(res.statusCode>=200&&res.statusCode<300){
//将登录状态设置为1,进入计时支付状态
getApp().globalData.status=1;
getApp().globalData.header={'Cookie':'JSESSIONID='+res.data};
console.log(new Date())
_this.isPayFor();
console.log(new Date()+"uuuu")
getApp().globalData.isLogin=1
}else{
console.log("登录异常")
wx.showModal({
title: "",
content: '登录异常!',
showCancel: false,
confirmText: "确定",
confirmColor: "#0f0",
success: function (res) {
_this.setData({
disabledLogin:false
})
}
})
}
},fail:err=>{
wx.hideLoading({
success: (res) => {},
})
console.log(err)
// _this.setData({
// disabledLogin:false
// })
}
})
}
}
})
}
},
fail: err => {
console.log('以游客身份登录!')
var avatarUrl = 'userInfo.avatarUrl';
var nickName = 'userInfo.nickName';
that.setData({
[avatarUrl]: getApp().globalData.imgpathHeader + '/testHead.jpg',
[nickName]: '兆光科技'
})
}
})
}
else {
console.log('以游客身份登录!')
var avatarUrl = 'userInfo.avatarUrl';
var nickName = 'userInfo.nickName';
that.setData({
[avatarUrl]: getApp().globalData.imgpathHeader + '/touxiang.jpg',
[nickName]: '游客'
})
if (getApp().globalData.isExperience == 0) {
this.testLogin();
}
}
}
})
}
})
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。