Skip to content

Commit

Permalink
fix: fit action notify
Browse files Browse the repository at this point in the history
  • Loading branch information
Qiu-Jun committed Apr 8, 2023
1 parent a33f703 commit 878932a
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 46 deletions.
11 changes: 6 additions & 5 deletions app/controller/wx.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

const Controller = require('egg').Controller;
const amapData = require('../../utils/amap.js')
const fs = require('fs')
const jsonPath = './utils/usercity.json'
// const fs = require('fs')
// const jsonPath = './utils/usercity.json'

class HomeController extends Controller {
async verify() {
Expand Down Expand Up @@ -77,14 +77,15 @@ class HomeController extends Controller {
}

async setCity() {
const { ctx, service } = this
const { ctx, app, service } = this
try {
const { city } = ctx.query
if(!city) return ctx.fail({msg: '设置失败'})
const findCity = amapData.find(i => i.adname === city)
if(!findCity) return ctx.fail({msg: '没有找到城市'})
fs.writeFileSync(jsonPath, JSON.stringify(findCity), 'utf8')
await service.redisModule.del('cacheWether')
// fs.writeFileSync(jsonPath, JSON.stringify(findCity), 'utf8')
// await service.redisModule.del('cacheWether')
app.config.userCity = findCity
ctx.ok({msg: '设置成功'})
} catch (error) {
ctx.fail({msg: error})
Expand Down
41 changes: 18 additions & 23 deletions app/service/notifyUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,27 @@ class NotifyUtils extends Service {
async getWether() {
try {
const { app, service } = this
const cacheWether = await service.redisModule.get('cacheWether')
if(cacheWether) {
return cacheWether
} else {
let cityData = fs.readFileSync('./utils/usercity.json', 'utf8')
cityData = cityData ? JSON.parse(cityData) : null
if(!cityData) return null
const res = await app.curl(`${apiUrl.gdWether}`, {
method: 'GET',
dataType: 'json',
data: {
key: app.config.apiConfig.amap.appKey,
city: app.config.userData.city,
city: cityData.adcode
}
})

if(res.status === 200 && res.data.status === '1') {
const wether = res.data.lives[0]
await service.redisModule.set('cacheWether', wether, 10 * 60) // 10分钟
return wether
} else {
return null
// let cityData = fs.readFileSync('./utils/usercity.json', 'utf8')
let cityData = app.config.userCity || null
if(!cityData) return null
const res = await app.curl(`${apiUrl.gdWether}`, {
method: 'GET',
dataType: 'json',
data: {
key: app.config.apiConfig.amap.appKey,
city: app.config.userData.city,
city: cityData.adcode
}
})

if(res.status === 200 && res.data.status === '1') {
const wether = res.data.lives[0]
return wether
} else {
return null
}


} catch (error) {
console.log(error)
return null
Expand Down
5 changes: 3 additions & 2 deletions app/service/wx.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Wechat extends Service {
*/
async getAccessToken() {
const { app, service } = this;
const hasAccessToken = await service.redisModule.get('accessToken');
const hasAccessToken = app.wxToken || '';
if (hasAccessToken) {
return hasAccessToken;
}
Expand All @@ -40,8 +40,9 @@ class Wechat extends Service {
dataType: 'json',
});
const token = getAccessToken.data.access_token;
app.wxToken = token
// 7200秒过期,提前
await service.redisModule.set('accessToken', token, 7080);
// await service.redisModule.set('accessToken', token, 7080);
return token;

}
Expand Down
1 change: 1 addition & 0 deletions config/config.ac.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @param {Egg.EggAppInfo} appInfo app info
*/

console.log(process)
module.exports = () => {
const config = exports = {};

Expand Down
28 changes: 17 additions & 11 deletions config/config.default.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/* eslint valid-jsdoc: "off" */

'use strict';
Expand All @@ -23,19 +24,19 @@ module.exports = appInfo => {
// myAppName: 'egg',
};

config.redis = {
client: {
port: 6379, // Redis port
host: '127.0.0.1', // Redis host
password: '',
db: 0,
},
};
// config.redis = {
// client: {
// port: 6379, // Redis port
// host: '127.0.0.1', // Redis host
// password: '',
// db: 0,
// },
// };

// 正式项目要开启
config.security = {
csrf: {
enable: false,
enable: false,
},
};

Expand All @@ -45,7 +46,7 @@ module.exports = appInfo => {
appSecret: '76d09eb43886a44cf2bb993c99206934',
token: 'junetext',
template_id: 'z864JohHWpBSFdsoz-6lVx8r0kS_v0LIYYJ7TEzrupg', // 推送的模板id
user: 'wx6a908ab36533ea40', // 并非推送用户, 填appid就行了
user: '', // 并非推送用户, 填appid就行了
};

config.userData = {
Expand All @@ -55,6 +56,11 @@ module.exports = appInfo => {
weatherCity: '茂名市' // 需要获取天气的城市,必须时xx市,xx县,xx自治区, 详细可以去utils/amap.js搜索到就可以,比如广州市,不能是广州
}

config.userCity = {
"adname":"高州市",
"adcode":"440981"
}

// 第三方
config.apiConfig = {
tianxingKey: 'bcaddf1605dd53c3115c5a709082ac6f',
Expand All @@ -78,4 +84,4 @@ module.exports = appInfo => {
...config,
...userConfig,
};
};
};
8 changes: 4 additions & 4 deletions config/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

/** @type Egg.EggPlugin */
module.exports = {
redis: {
enable: true,
package: 'egg-redis',
},
// redis: {
// enable: true,
// package: 'egg-redis',
// },
};
2 changes: 1 addition & 1 deletion utils/usercity.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"adname":"茂名市","adcode":"440900"}
{"adname":"高州市","adcode":"440981"}

0 comments on commit 878932a

Please sign in to comment.