生活难以两全,我更想让自己永远拥有探索的精神,保持好的状态出发,遇见、抵达

之前发了《如何用PHP原生实现小程序广告获取卡密--每日密码?》的文章,不出意外灵沐小程序开发卡密功能
灵沐从来不是一个人的程序,我们在文档的建议反馈可以看到作为一款开源的流量主变现的小程序,离不开所有人的建议
相反我也在努力学习,今天我们来共同探索
方案
这个方式最直接,但是要有PHP环境和mysql数据库,因为srtapi是nodejs环境
直接在后台开发,不省事,也不是很安全,还不一定适合
思考
要在strapi做卡密我们需要实现2个步骤,第一返回卡密,第二删除或移除这条卡密;那还有一个巨大的问题,要验证卡密是否给使用
综上所述我们得知一个结论目前卡密又很长的路要走,但是如果做为获取不同密码还是可以玩一玩
实践
在后台建立一个数据模型,只需要提供一个多行文本框

同时需要设置权限

访问接口他会返回所有的数据,造成了传说中的不安全
http://localhost:1337/api/cdker?populate=*
我们可以在后端目录中做API接口返回的数据btoa加密
strapi/my-project/src/api/cdker/controllers/cdker.js
async find(ctx) {
// some logic here
const response = await super.find(ctx);
// some more logic
const frkey = btoa(response.data.attributes.key);
response.data = frkey;
return response;
}
对比一下返回内容和返回接口

小程序上是无法使用atob解密,只能使用cv大法,搭配上strapi find以及updata接口,代码如下
// atob解密
atobs() {
wx.request({
url: 'http://localhost:1337/api/cdker?populate=*',
success: (res) => {
// 传入后端过来的内容
var inputs = res.data.data;
// atob解密
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
let str = inputs.replace(/=+$/, '');
let output = '';
if (str.length % 4 === 1) {
throw new Error('InvalidLengthError');
}
for (let i = 0, len = str.length; i < len; i += 4) {
const a = chars.indexOf(str.charAt(i));
const b = chars.indexOf(str.charAt(i + 1));
const c = chars.indexOf(str.charAt(i + 2));
const d = chars.indexOf(str.charAt(i + 3));
const sum = (a << 18) | (b << 12) | (c << 6) | d;
output += String.fromCharCode((sum >> 16) & 0xFF, (sum >> 8) & 0xFF, sum & 0xFF);
}
// 转化成数组
const keys = output.split(/[\n,]/g);
// 返回第一条密码
this.setData({
frkey: keys[0]
})
// 删除第一条密码
keys.shift();
// 准备body提交数据
const uptateKyes = {
"data":{
"key":keys.join("\n")
}
}
// update更新后台
wx.request({
url: 'http://localhost:1337/api/cdker?populate=*',
method:'PUT',
data:JSON.stringify(uptateKyes),
header:{
'Content-Type': 'application/json'
},
success:(sud)=>{
console.log(sud)
}
})
}
})
},
但是又有一个问题就是unpdata返回的数据是带整条整体信息的,因此也要在
strapi/my-project/src/api/cdker/controllers/cdker.js
稍微修改下返回内容
'use strict';
/**
* cdker controller
*/
const {
createCoreController
} = require('@strapi/strapi').factories;
module.exports = createCoreController('api::cdker.cdker', ({
strapi
}) => ({
async update(ctx) {
// some logic here
const response = await super.update(ctx);
const ups = [{
"code":200,
"msg":"你成功了呀!!!"
}]
response.data = ups;
return response;
},
async find(ctx) {
// some logic here
const response = await super.find(ctx);
// some more logic
const frkey = btoa(response.data.attributes.key);
response.data = frkey;
return response;
}
}));
总结
绕了一圈发现strapi无法很好的实现卡密折腾,折腾,头发多!!!
