老规矩,先上demo。
额。。。。
忘了,这次情况特殊没有demo,看预览图吧:
获取用户信息,openId
,用作生成用户专属图片文件夹
code
从而得到用户信息:encryptedData
,iv
,把这三个参数传给后台wx.checkSession
激活session_key
和 appid
WXBizDataCrypt(appid, session_key)
openId
return decrypt
为return jsonify(decrypted)
,把dict转为jsonpip install pycryptodome
就OK了wx.chooseImage
选择图片
wx.uploadFile
上传图片
openId
,携带openId
以wx.chooseImage
生成的图片路径传给服务器App({
onLaunch: function() {
// 展示本地存储能力
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
// 登录
let that = this
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
if (res.code) {
wx.getUserInfo({
success: e => {
let info = {
encryptedData: e.encryptedData,
iv: e.iv,
code: res.code
}
wx.request({
url: 'https://xxxxxx.xx/api/v/userinfo',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data: {
info: JSON.stringify(info) //把object转化为json数据
},
method: 'POST',
success: function(u) {
const encryptedData = u.data
getApp().globalData.openid = encryptedData.openId
},
fail: function(f) {
console.log(f)
},
})
},
fail: function() {
console.log('fail,login_fail')
}
})
}
}
})
wx.checkSession({
success() {
// session_key 未过期,并且在本生命周期一直有效
},
fail() {
// session_key 已经失效,需要重新执行登录流程
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,
openid: ''
}
})
const app = getApp()
Page({
data: {
motto: 'Hello World',
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo'),
src: '',
},
//事件处理函数
bindViewTap: function() {
wx.navigateTo({
url: '../logs/logs'
})
},
onLoad: function() {
if (app.globalData.userInfo) {
this.setData({
userInfo: app.globalData.userInfo,
hasUserInfo: true
})
} else if (this.data.canIUse) {
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
app.userInfoReadyCallback = res => {
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
} else {
// 在没有 open-type=getUserInfo 版本的兼容处理
wx.getUserInfo({
success: res => {
app.globalData.userInfo = res.userInfo
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
})
}
},
getUserInfo: function(e) {
console.log(e)
app.globalData.userInfo = e.detail.userInfo
this.setData({
userInfo: e.detail.userInfo,
hasUserInfo: true
})
},
choose: function() {
let that = this;
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'], //原图或压缩后
sourceType: ['album', 'camera'], //相册,相机
success: res => {
const img = res.tempFilePaths
that.setData({
src: img
})
},
})
},
upload: function() {
const user = app.globalData.openid
let that = this;
let pic = that.data.src;
wx.uploadFile({
url: 'https://xxxx.xx/xxxx/xxx/xxxxxx/' + user,
filePath: String(pic),
name: 'test',
success: function(e) {
console.log(e)
}
})
}
})
配置路由
接受传过来的文件,及openId
,hash加密后,创建文件夹
create_folder
函数,用来判断是否需要生成新的文件夹def creat_folder(folder_path):
if not os.path.exists(folder_path):
os.mkdir(folder_path)
os.chmod(folder_path,os.O_RDWR)
md5
,加密openid
,以免暴漏真实id def md5(data):
md = hashlib.md5()
md.update(data.encode('utf-8'))
data = md.hexdigest()
return data
older_path
接受参数os.path.join(app.config['UPLOADS_FOLDER'], md5(openId)
,使用pillow对图片进行存储
返回图片的网络地址
img_dir= photosSet.url(hash_openid) + '/' + fn
@app.route('/api/v/upload/' ,methods=['POST','GET'])
def mp_img(openid):
fn = time.strftime('%Y%m%d%H%M%S') + '_%d' % random.randint(0, 100) + '.png'
avata = request.files.get('test')
hash_openid = md5(openid)
new = compression_img(avata)
creat_folder(os.path.join(app.config['UPLOADS_FOLDER'], hash_openid))
pic_dir = os.path.join(app.config['UPLOADS_FOLDER'], hash_openid, fn)
new.save(pic_dir)
folder = photosSet.url(hash_openid)
img_dir= folder + '/' + fn
return img_dir
@app.route('/api/v/userinfo',methods=['POST'])
def userinfo():
info = request.values.get('info')
appid = 'wx000000000032332' //这里填你的appid
secret = 'jd82hhewh808b3sddada915e0b3' //你的secret_key
user_info = json.loads(info)
code = user_info['code']
url = 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code' % (
appid, secret, code)
data = requests.get(url).text
session_ = json.loads(data)
session_key = session_['session_key']
encryptedData = user_info['encryptedData']
iv = user_info['iv']
pc = WXBizDataCrypt(appid, session_key)
return pc.decrypt(encryptedData, iv)