Last active
November 4, 2018 14:37
-
-
Save 1c7/3a0b9182ec1c1a07506354f653c8b4c2 to your computer and use it in GitHub Desktop.
微信内容安全,参考代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 微信小程序,内容安全示例代码 | |
const qiniuUploader = require("../../utils/qiniuUploader.js"); // https://github.com/gpake/qiniu-wxapp-sdk | |
const wittcism = require('../../utils/wittcism.js') // API | |
Page({ | |
data: {}, | |
onShow() {}, | |
change_avatar() { | |
qiniuUploader.upload(filePath, (res) => { | |
var original_qiniu_link = res.qiniu_url | |
var link = 'https://img.wittcism.com/' + res.qiniu_url + '?imageView2/2/w/740/h/1324'; | |
var params = { | |
url: link | |
} | |
wittcism.imgSecCheckByURL(params).then(data => { | |
if (data['errmsg'] == 'ok') { | |
// 图片没问题 | |
wx.hideLoading(); | |
that.update_user_avatar(original_qiniu_link); | |
wx.showToast({ | |
title: '修改头像成功', | |
icon: 'success', | |
}) | |
} else { | |
// 提示图片有问题 | |
wx.hideLoading(); | |
if (data['errmsg'] == 'risky') { | |
wx.showToast({ | |
title: '微信内容安全接口认为您上传的图片含有违法违规内容, 修改头像失败', | |
icon: 'none', | |
}) | |
} else { | |
wx.showToast({ | |
title: data['errmsg'], | |
icon: 'none', | |
}) | |
} | |
} | |
}).catch(e => { | |
console.log(e); | |
wx.showToast({ | |
title: '出错了,请稍后再试', | |
icon: 'none', | |
}) | |
}) | |
}, (error) => { | |
wx.hideLoading(); | |
wx.showToast({ | |
title: '上传头像文件失败', | |
icon: 'none', | |
}) | |
console.log('error: ' + error); | |
}, { | |
region: 'SCN', | |
domain: 'https://img.wittcism.com', | |
uptokenURL: 'https://wittcism.com/backend/api/v1/upload_token_for_wechat', | |
}, (progress) => {}) | |
}, | |
}) |
qiniuUploader.upload 纯粹只是上传图片到七牛
后端代码:Ruby on Rails 5.2
class Api::V1::MiniappController < Api::V1::ApplicationController
# 接收图片地址
# 从图片地址下载图片,并传给微信内容安全接口,原样返回结果
def img_sec_check_url
url = params[:url]
unless url
render json: {status: 2, message: '需要图片的 URL'}
return
end
download = open(url)
file_path = "/tmp/#{download.base_uri.to_s.split('/')[-1]}"
file = IO.copy_stream(download, file_path)
access_token = return_miniapp_access_token()
url = "https://api.weixin.qq.com/wxa/img_sec_check?access_token=#{access_token}"
r = RestClient::Request.execute(
method: :post,
url: url,
:payload => {
:multipart => true,
:file => File.new(file_path, 'rb')
}
)
r = JSON.parse(r)
render json: r
return
end
# 返回 access_token (用了 redis 做缓存)
def return_miniapp_access_token
redis = $redis
if redis.get("wechat_miniapp_access_token")
return redis.get("wechat_miniapp_access_token")
end
# if not exist in redis
# now we have to send http request to get a access_token from weixin
appid = Setting.wechat_mini_program_app_id
appsecret = Setting.wechat_mini_program_app_secret
url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=#{appid}&secret=#{appsecret}"
r = RestClient::Request.execute({method: :get, url: url})
r = JSON.parse(r)
unless r['access_token']
return false
end
redis.set("wechat_miniapp_access_token", r['access_token'])
redis.expire("wechat_miniapp_access_token", r['expires_in'].to_i - 60) # 提早 60 秒过期
return r['access_token']
end
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
wittcism.imgSecCheckByURL 做的事情纯粹是把七牛 URL 传给后端
七牛图片 URL 比如:
https://img.wittcism.com/tmp/wx3e98618e26700e1b.o6zAJs6rSSMeG3ZYAlQjcxSTQuz0.BhMwMs4CuyEYec61c87a1767c8e5877e396fdc30a19b.jpg
我这里发的后端地址类似这样:https://example.com/api/v1/miniapp/img_sec_check_url
(仅供参考)