/**
  * 粉丝表
  */
 public function initFansTable()
 {
     $tableName = Fans::tableName();
     $this->createTable($tableName, ['id' => Schema::TYPE_PK, 'wid' => Schema::TYPE_INTEGER . " UNSIGNED NOT NULL DEFAULT '0' COMMENT '所属微信公众号ID'", 'open_id' => Schema::TYPE_STRING . "(50) NOT NULL DEFAULT '' COMMENT '微信ID'", 'status' => Schema::TYPE_BOOLEAN . " NOT NULL DEFAULT '0' COMMENT '关注状态'", 'created_at' => Schema::TYPE_INTEGER . " UNSIGNED NOT NULL DEFAULT '0' COMMENT '关注时间'", 'updated_at' => Schema::TYPE_INTEGER . " UNSIGNED NOT NULL DEFAULT '0' COMMENT '修改时间'"]);
     $this->createIndex('wid', $tableName, 'wid');
     $this->createIndex('open_id', $tableName, 'open_id');
 }
 /**
  * 获取触发微信请求的微信用户信息
  * @return Fans
  */
 public function getFans()
 {
     if ($this->_fans === false) {
         $this->_fans = Fans::findByOpenId($this->message['FromUserName']);
     }
     return $this->_fans;
 }
Example #3
0
 /**
  * Finds the Fans model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Fans the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     $query = Fans::find()->andWhere(['id' => $id, 'wid' => $this->getWechat()->id]);
     if (($model = $query->one()) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Example #4
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params, $user = false)
 {
     $query = Fans::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     if ($user) {
         $query->with('user');
     }
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'wid' => $this->wid, 'status' => $this->status, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]);
     $query->andFilterWhere(['like', 'open_id', $this->open_id]);
     return $dataProvider;
 }
Example #5
0
 /**
  * 获取粉丝数据(当前公众号唯一)
  * @param bool $oauth 是否通过微信服务器Oauth API跳转获取 通过API获取的用户信息会存入session中
  * @return mixed
  */
 public function getFans($oauth = true)
 {
     if ($this->_fans === null) {
         $wechat = $this->getWechat();
         $sessionKey = self::SESSION_MOBILE_FANS_PREFIX . '_' . $wechat->id;
         $openId = Yii::$app->session->get($sessionKey);
         if (!$openId && $oauth) {
             // API获取用户
             $data = $wechat->getSdk()->getAuthorizeUserInfo('fansInfo');
             if (isset($data['openid'])) {
                 $openId = $data['openid'];
             }
         }
         if (!$openId || ($fans = Fans::findByOpenId($openId)) === null) {
             return false;
         }
         $this->setFans($fans);
     }
     return $this->_fans;
 }
 /**
  * 数据记录
  * @return array
  * @throws \yii\base\InvalidConfigException
  */
 public function actionRecord()
 {
     $wechat = $this->getWechat();
     $fans = $this->getFans();
     if (!$fans) {
         // 存储粉丝信息
         $fans = Yii::createObject(Fans::className());
         $fans->setAttributes(['wid' => $wechat->id, 'open_id' => $this->message['FromUserName'], 'status' => Fans::STATUS_SUBSCRIBED]);
         if ($fans->save() && $wechat->status > Wechat::TYPE_SUBSCRIBE) {
             // 更新用户详细数据, 普通订阅号无权限获取
             $fans->updateUser();
         }
     } elseif ($fans->status != Fans::STATUS_SUBSCRIBED) {
         // 更新关注状态
         $fans->subscribe();
     }
     //        $history = new MessageHistory();
     //        $attributes = [
     //            'wid' => $wechat->id,
     //            'module' => $this->getModuleName($this->api->lastProcessController),
     //        ];
 }