base.js 448 B

123456789101112131415
  1. module.exports = class extends think.Controller {
  2. async __before() {
  3. // 根据token值获取用户id
  4. think.token = this.ctx.header['x-nideshop-token'] || '';
  5. const tokenSerivce = think.service('token', 'admin');
  6. think.userId = await tokenSerivce.getUserId();
  7. // 只允许登录操作
  8. if (this.ctx.controller !== 'auth') {
  9. if (think.userId <= 0) {
  10. return this.fail(401, '请先登录');
  11. }
  12. }
  13. }
  14. };