|
本帖最后由 杨永安 于 2021-8-20 16:39 编辑
在线查看地址:文档:仿小红书云函数.md
链接:file:///C:\Users\i\AppData\Roaming\Tencent\QQTempSys\%W@GJ$ACOF(TYDYECOKVDYB.pnghttp://note**.**/noteshare?id=45a40e1f7227c6d1638607a6348cdaa1&sub=56B5FA5DA5C9425793BEFD7EC229CDBB
[size=2.25em]Globalconst G = { wx:{ APPID : '你自己的APPID', SECRET : '你自己的秘钥' }, successmsg,data,code=0)=>{ return {msg,data,code}; }, errormsg,data=null,code=1)=>{ return {msg,data,code}; }};Initfunction Init(Models){ Models.install(['axios']);}MiddleWarepost远程函数publish[td]函数类型 | 请求类型 | 函数名称 | 方法名 | 远程函数 | post | 发布图文 | publish | post.publish = async()=> { const postData =req.body; const USER = req.session.user; if(!USER){ return G.error("请登录"); } postData.user_id = USER.id; postData.topic_id = postData.topic.id; delete postData.topic; postData.creat_at = new Date(); try{ return G.success('发布成功',await post.create(postData)); }catch(e){ return G.error('发布失败',e); } };recommend[td]函数类型 | 请求类型 | 函数名称 | 方法名 | 远程函数 | get | 推荐列表 | recommend | [td]post.recommend = async(page)=> { const limit = 10; const skip =(page-1)*limit; const include = [ {relation:'topic'}, {relation:'user',scope:{ fields:['username','id','avatar'] }}, ]; return G.success("推荐列表",await post.find({limit,skip,include})); };upload[td]函数类型 | 请求类型 | 函数名称 | 方法名 | 远程函数 | post | 上传 | upload | post.upload = async()=> { try { const info = await Models.api.qiniuFile.upload(req); return G.success("上传成功",info); } catch (err) { return G.error("上传失败",err); }};topic远程函数list[td]函数类型 | 请求类型 | 函数名称 | 方法名 | 远程函数 | get | 话题列表 | list | topic.list = async()=> { return G.success("话题列表",await topic.find());};user远程函数info[td]函数类型 | 请求类型 | 函数名称 | 方法名 | 远程函数 | get | 用户信息 | info | user.info = async()=> {const userData = req.session.user; if(userData){ return G.success("用户信息",req.session.user); }else{ return G.error("请登录"); }};wx_code_login[td]函数类型 | 请求类型 | 函数名称 | 方法名 | 远程函数 | get | code登录 | wx_code_login | user.wx_code_login = async(code)=> { const {APPID,SECRET} = G.wx; const {data:loginData,data:{openid,access_token}} = await Models.api.axios.get(`https://api.weixin**.**/sns/oauth2/access_token?appid=${APPID}&secret=${SECRET}&code=${code}&grant_type=authorization_code`); const hasUser = await user.findOne({where:{passwordpenid}}); if(hasUser){ req.session.user = hasUser; return G.success('登录成功',hasUser); }else{ const {data:infoData} = await Models.api.axios.get(`https://api.weixin**.**/sns/userinfo?access_token=${access_token}&openid=${openid}`); try { const obj = { username:infoData.nickname, avatar:infoData.headimgurl||'https://www.apicloud.com/img/logo_icon.png', passwordpenid, gender:infoData.sex }; const regUser = await user.create(obj); req.session.user = regUser; return G.success('注册成功',regUser); } catch (err) { return G.error('注册失败',err); } } };
|
|