ertonglianxibiao/app.js

220 lines
6.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { injectApp, waitLogin, gatewayHttpClient } from '@jdmini/api'
App(injectApp()({
globalData: {
userInfo: null,
openid: null,
wxUserInfo: null,
inviterId: null // 邀请人ID
},
async onLaunch(options) {
// 保存邀请人ID
if (options && options.query && options.query.inviter) {
this.globalData.inviterId = options.query.inviter
wx.setStorageSync('inviterId', options.query.inviter)
}
if (wx.canIUse('getUpdateManager')) {
const updateManager = wx.getUpdateManager();
updateManager.onCheckForUpdate(function (res) {
if (res.hasUpdate) {
updateManager.onUpdateReady(function () {
wx.showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启应用?',
success(res) {
if (res.confirm) {
updateManager.applyUpdate();
}
}
});
});
}
});
}
const accountInfo = wx.getExtConfigSync()
//console.log('#1',accountInfo)
const appId = accountInfo.appId;
// 等待登陆完成以后请求自动携带token
await waitLogin()
// 三方登录本地缓存
console.log(wx.getStorageSync('jdwx-userinfo'))
const wxUserInfo = wx.getStorageSync('jdwx-userinfo');
wx.setStorageSync('sfUserId', wxUserInfo.id)
wx.setStorageSync('appId',appId)
// 保存到全局
this.globalData.openid = wxUserInfo.openId;
this.globalData.wxUserInfo = wxUserInfo;
const userId = wx.getStorageSync('userId');
if (userId) {
console.log('用户已登录', userId);
} else {
console.log('用户未设置个人信息,需要跳转登录页');
}
//this.getqrcode('logo=1')
},
// 生成小程序二维码
async getqrcode(value) {
try {
// 构建场景值仓库ID和仓库code
const scene = value;
console.log('生成二维码请求参数:', {
scene,
page: "pages/index/index"
});
const result = await gatewayHttpClient.request('/wx/v1/api/app/qrcode', 'POST', {
"scene": scene,
"page": "pages/index/index", // 跳转到index页面
"check_path": true,
"env_version": "release"
},{
responseType: 'arraybuffer'
});
console.log('二维码接口返回:', result);
console.log('返回数据类型:', typeof result);
console.log('是否为ArrayBuffer:', result instanceof ArrayBuffer);
// 当设置 responseType: 'arraybuffer' 时,返回的直接就是 ArrayBuffer
if (result instanceof ArrayBuffer) {
console.log('检测到 ArrayBuffer转换为 base64');
const base64Data = wx.arrayBufferToBase64(result);
console.log('转换后的 base64 数据长度:', base64Data?.length);
console.log('base64 数据前50个字符:', base64Data?.substring(0, 50));
return {
success: true,
data: base64Data // 二维码图片数据(base64字符串)
};
} else {
console.error('二维码生成失败返回数据不是ArrayBuffer:', result);
return {
success: false,
message: '生成二维码失败:数据格式错误'
};
}
} catch (error) {
console.error('生成二维码异常:', error);
return {
success: false,
message: error.message || '生成二维码失败'
};
}
},
//内容安全
async checkdata(txt = '', checkType, mediaUrl = '') {
try {
if (!checkType || (checkType !== 2 && checkType !== 3)) {
throw new Error('checkType必须为2(图片检测)或3(文本检测)')
}
if (checkType === 3 && !txt) {
throw new Error('文本检测时content不能为空')
}
if (checkType === 2 && !mediaUrl) {
throw new Error('图片检测时mediaUrl不能为空')
}
const postdata = {
content: txt,
checkType: checkType,
mediaUrl: mediaUrl
}
const data = await gatewayHttpClient.request('/wx/v1/api/app/content/check', 'post', postdata)
if (data.code === 200) {
if (checkType == 3) {
return data.data.suggest === 'pass' ? 1 : 2
}
if (checkType == 2) {
return data.data.id || null
}
} else {
console.error('内容检测API调用失败:', data)
return 2
}
} catch (error) {
console.error('checkdata函数执行错误:', error)
return 2
}
},
// 轮询检查图片是否合规
//imgurl 为本地图片地址
async checkimage(imgurl){
wx.showLoading({
title: '正在检查图片...',
mask: true
});
try{
const upfileData = await gatewayHttpClient.uploadFile(imgurl, 'image');
const checkid = await this.checkdata('',2,upfileData.data.url)
let retryCount = 0;
const maxRetries = 5;
while (retryCount < maxRetries) {
await new Promise(resolve => setTimeout(resolve, 1000)); // 间隔1秒
const passcode = await this.checkSafetyResults(checkid);
// 检查具体的错误码
switch (passcode) {
case 100:
wx.hideLoading();
return upfileData.data;
case 20001:
wx.hideLoading();
await gatewayHttpClient.deleteFile(upfileData.data.id);
this.showwarning('图片时政内容,请重新选择');
return null;
case 20002:
wx.hideLoading();
await gatewayHttpClient.deleteFile(upfileData.data.id);
this.showwarning('图片含有色情内容,请重新选择');
return null;
case 20006:
wx.hideLoading();
await gatewayHttpClient.deleteFile(upfileData.data.id);
this.showwarning('图片含有违法犯罪内容,请重新选择');
return null;
case 21000:
wx.hideLoading();
await gatewayHttpClient.deleteFile(upfileData.data.id);
this.showwarning('图片非法内容,请重新选择');
return null;
default:
break
}
retryCount++;
}
// 5次超时返回失败
wx.hideLoading();
await gatewayHttpClient.deleteFile(upfileData.data.id)//删除图片
wx.showToast({
title: '图片检查超时,请重试',
icon: 'none'
});
return null;
} catch (error) {
wx.hideLoading();
console.error('图片检查失败:', error);
wx.showToast({
title: '检查失败,请重试',
icon: 'none'
});
return null;
}
},
showwarning(txt){
wx.showModal({
title: '提示',
content: txt,
showCancel: false,
success: () => {
}
});
}
}))