1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| //先定义下这2个模块都是需要登录才能操作 public function behaviors() { return [ 'access' => [ 'class' => AccessControl::className(), 'only' => ['user1', 'user2'], 'rules' => [ [ 'actions' => ['user1', 'user2'], 'allow' => true, 'roles' => ['@'], ], ], ] ]; }
//第一个用户 public function actionUser1() { $data = []; $user = Yii::$app->user->identity; $data = array( 'uid' => $user->getId(), // 用户id 'avatar' => 'http://www.missxiaolin.com/8ad4b31c8701a18bbef9f231982f07082838feba.jpg',// 头像 'username' => $user->username,// 用户名 'rong_key' => Yii::$app->params['RONG_DEV_APP_KEY'],// 融云key 'rong_token' => $this->token()//获取融云token ); return $this->renderPartial('user1', $data); }
//第二个用户 public function actionUser2() { $data = []; $user = Yii::$app->user->identity; $data = array( 'uid' => $user->getId(), // 用户id 'avatar' => 'http://www.missxiaolin.com/8ad4b31c8701a18bbef9f231982f07082838feba.jpg',// 头像 'username' => $user->username,// 用户名 'rong_key' => Yii::$app->params['RONG_DEV_APP_KEY'],// 融云key 'rong_token' => $this->token()//获取融云token ); return $this->renderPartial('user2', $data); }
//获取token public function token() { //获取融云key $user = Yii::$app->user->identity; $token = $user->password_reset_token; // 如果有token就返回 if ($token) { return $token; } $key = Yii::$app->params['RONG_DEV_APP_KEY']; $secret = Yii::$app->params['RONG_DEV_APP_SECRET']; $rong_cloud = new RongCloud($key, $secret); $token_json = $rong_cloud->getToken($user->getId(), $user->username, 'http://www.missxiaolin.com/8ad4b31c8701a18bbef9f231982f07082838feba.jpg'); $token_array = json_decode($token_json, true); $token = $token_array['token']; $user_model = User::findOne($user->getId()); $user_model->password_reset_token = $token; $user_model->save(); return $token; }
|