Commit 269af8d2d0f5150d7842d0555f85e040054105ba

Authored by xwenliang
1 parent 5b5872de

feat: 文本合规检查

README.md
@@ -7,8 +7,10 @@ @@ -7,8 +7,10 @@
7 - 个人类小程序不支持 web-view 7 - 个人类小程序不支持 web-view
8 - wx.shareFileMessage 可以直接分享文件 8 - wx.shareFileMessage 可以直接分享文件
9 9
10 -- [ ] 内容安全问题处理 10 +- [x] 内容安全问题处理
11 - https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.msgSecCheck.html 11 - https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.msgSecCheck.html
12 12
13 - [ ] 从列表页返回后应该关闭正在播放的列表页语音 13 - [ ] 从列表页返回后应该关闭正在播放的列表页语音
14 14
  15 +- [ ] 时间长了本地缓存文件失效后应当清除对应播放列表
  16 +
1 //app.js 1 //app.js
2 App({ 2 App({
3 - onLaunch () {  
4 - 3 + async onLaunch () {
  4 + this.login();
  5 + },
  6 + // 将登录态封装进去
  7 + // todo: 多个请求同时发起,且登录态都失效时,ssid 会相互覆盖
  8 + async request(config){
  9 + let ssid = wx.getStorageSync('ssid');
  10 + if(!ssid){
  11 + ssid = await this.login();
  12 + }
  13 + const initialSuccess = config.success;
  14 + config.data = {
  15 + ...config.data,
  16 + ssid
  17 + };
  18 + config.success = async resp => {
  19 + if(resp.data.code === 5001000){
  20 + wx.setStorageSync('ssid', '');
  21 + delete config.ssid;
  22 + this.request(config);
  23 + }
  24 + else{
  25 + initialSuccess(resp);
  26 + }
  27 + };
  28 + config = {
  29 + header: {
  30 + 'content-type': 'application/json'
  31 + },
  32 + dataType: 'json',
  33 + method: 'POST',
  34 + ...config
  35 + };
  36 + return wx.request(config);
  37 + },
  38 + login(){
  39 + let ssid = wx.getStorageSync('ssid');
  40 + if(ssid){
  41 + return ssid;
  42 + }
  43 + wx.showLoading({
  44 + title: '登录中...',
  45 + mask: true
  46 + });
  47 + return new Promise((resolve, reject) => {
  48 + wx.login({
  49 + success: res => {
  50 + wx.request({
  51 + url: `${this.globalData.domain}/open-api/login`,
  52 + data: {
  53 + code: res.code,
  54 + appid: 'wx330e54aa6000516d'
  55 + },
  56 + header: {
  57 + 'content-type': 'application/json'
  58 + },
  59 + dataType: 'json',
  60 + method: 'POST',
  61 + success: resp => {
  62 + const {
  63 + ssid,
  64 + openid
  65 + } = resp.data?.data?.user;
  66 + wx.setStorageSync('ssid', ssid);
  67 + wx.setStorageSync('openid', openid);
  68 + resolve(resp.data?.data?.user);
  69 + },
  70 + fail: resp => {
  71 + wx.showModal({
  72 + title: '提示',
  73 + content: '登录失败,点击确定重试',
  74 + showCancel: false,
  75 + success: res => {
  76 + this.login();
  77 + }
  78 + });
  79 + reject(resp.errMsg);
  80 + },
  81 + complete: () => {
  82 + wx.hideLoading();
  83 + }
  84 + });
  85 + }
  86 + });
  87 + });
5 }, 88 },
6 globalData: { 89 globalData: {
  90 + // domain: 'http://localhost:3001',
  91 + domain: 'https://jilegeji.xwenliang.cn',
7 getTypeList(){ 92 getTypeList(){
8 return [ 93 return [
9 { 94 {
pages/index/index.js
@@ -19,9 +19,41 @@ Page({ @@ -19,9 +19,41 @@ Page({
19 inputText(e){ 19 inputText(e){
20 this.data.text = e.detail.value; 20 this.data.text = e.detail.value;
21 }, 21 },
22 - play(){ 22 + async play(){
23 const text = this.data.text || this.data.placeholder; 23 const text = this.data.text || this.data.placeholder;
24 const url = this.data.type[this.data.selectedIndex].createUrl(text); 24 const url = this.data.type[this.data.selectedIndex].createUrl(text);
  25 + wx.showLoading({
  26 + title: '正在合成语音...',
  27 + mask: true
  28 + });
  29 + // 检查文本是否合规
  30 + const result = await new Promise((resolve, reject) => {
  31 + app.request({
  32 + url: `${app.globalData.domain}/open-api/wechat-msg-sec-check`,
  33 + data: {
  34 + appid: 'wx330e54aa6000516d',
  35 + content: text,
  36 + openid: wx.getStorageSync('openid')
  37 + },
  38 + success(res){
  39 + resolve(JSON.parse(res.data?.data)?.result);
  40 + },
  41 + fail(err){
  42 + reject(err);
  43 + }
  44 + });
  45 + });
  46 + wx.hideLoading();
  47 + // 不合规结束
  48 + if(result.suggest !== 'pass'){
  49 + return wx.showToast({
  50 + title: '内容不合规',
  51 + icon: 'error',
  52 + mask: true,
  53 + duration: 2000
  54 + });
  55 + }
  56 +
25 this.resetAudio(); 57 this.resetAudio();
26 audio = wx.createInnerAudioContext({useWebAudioImplement: true}); 58 audio = wx.createInnerAudioContext({useWebAudioImplement: true});
27 audio.autoplay = true; 59 audio.autoplay = true;
project.config.json
@@ -39,23 +39,16 @@ @@ -39,23 +39,16 @@
39 "useCompilerPlugins": false 39 "useCompilerPlugins": false
40 }, 40 },
41 "compileType": "miniprogram", 41 "compileType": "miniprogram",
42 - "libVersion": "1.9.93", 42 + "libVersion": "2.21.3",
43 "appid": "wx330e54aa6000516d", 43 "appid": "wx330e54aa6000516d",
44 "projectname": "%E8%AF%AD%E9%9F%B3%E5%8A%A9%E6%89%8B", 44 "projectname": "%E8%AF%AD%E9%9F%B3%E5%8A%A9%E6%89%8B",
45 - "isGameTourist": false,  
46 - "condition": {  
47 - "search": {  
48 - "list": []  
49 - },  
50 - "conversation": {  
51 - "list": []  
52 - },  
53 - "game": {  
54 - "currentL": -1,  
55 - "list": []  
56 - },  
57 - "miniprogram": {  
58 - "list": []  
59 - } 45 + "condition": {},
  46 + "packOptions": {
  47 + "ignore": [],
  48 + "include": []
  49 + },
  50 + "editorSetting": {
  51 + "tabIndent": "insertSpaces",
  52 + "tabSize": 2
60 } 53 }
61 } 54 }
62 \ No newline at end of file 55 \ No newline at end of file