Beispiel #1
0
  // 用微信客户端获取getopenid
  async getopenidAction() {
    const pingpp = require('pingpp')(this.config('setup.wx_AppSecret'));
    // 获取用户openid
    const code = this.get('code');
    console.log(code);
    // 获取openid
    const getopenid = () => {
      const deferred = think.defer();
      pingpp.wxPubOauth.getOpenid(this.config('setup.wx_AppID'), this.config('setup.wx_AppSecret'), code, function(err, openid) {
        // console.log(openid);
        deferred.resolve(openid);
        // ...
        // pass openid to extra['open_id'] and create a charge
        // ...
      });
      return deferred.promise;
    };
    const openid = await getopenid();
    // 9think.log(think.isEmpty(openid));
    const userinfo = await this.api.getUser(openid);
    // console.log(userinfo);
    // 如果没有关注先跳到关注页面
    if (userinfo.subscribe == 0) {
      console.log(1111111111111);
      return this.redirect('/center/weixin/follow');
    };
    userinfo.subscribe_time = userinfo.subscribe_time * 1000;

    const wx_user = await this.model('wx_user').where({openid: openid}).find();

    // 存储Openid
    await this.session('wx_openid', openid);
    if (think.isEmpty(wx_user)) {
      await this.model('wx_user').add(userinfo);
      this.redirect('/center/weixin/signin');
    } else {
      await this.model('wx_user').where({openid: openid}).update(userinfo);

      // 检查微信号是否跟网站会员绑定
      if (think.isEmpty(wx_user.uid)) {
        // 没绑定跳转绑定页面
        this.redirect('/center/weixin/signin');
      } else {
        // 更新微信头像
        const filePath = think.resource + '/upload/avatar/' + wx_user.uid;
        think.mkdir(filePath);
        await this.spiderImage(userinfo.headimgurl, filePath + '/avatar.png');
        // 绑定直接登陆
        const last_login_time = await this.model('member').where({id: wx_user.uid}).getField('last_login_time', true);

        const wx_userInfo = {
          'uid': wx_user.uid,
          'username': userinfo.nickname,
          'last_login_time': last_login_time
        };
        await this.session('webuser', wx_userInfo);
        return this.redirect(this.cookie('cmswing_wx_url'));
      }
    }
  }
Beispiel #2
0
 /**
    * 监听微信关注或取消,进行本地用户数据更新
    */
 async updateusersAction() {
   const FromUserName = 'openid';// 发送方帐号(一个OpenID)
   const Event = 'subscribe';// subscribe(订阅)、unsubscribe(取消订阅)
   const user_model = this.model('wx_user');
   const api = new API(this.config('setup.wx_AppID'), this.config('setup.wx_AppSecret'));
   if (Event == 'subscribe' && !thik.isEmpty(FromUserName)) {
     // 通过openid获取用户基本信息
     const resusers = await api.getUser(FromUserName);
     // 添加到本地库中
     await user_model.add(resusers);
   } else {
     // 修改取消订阅用户的状态
     await user_model.where({'openid': FromUserName}).update({'subscribe': 0});
   }
 }