/** * @param $openid * @param null $userData * @return User|static * @throws Exception */ public function save($openid, $userData = null) { $user = User::model()->findByPk($openid); if (!empty($user)) { if (!empty($userData['nickname'])) { $user->nickname = $userData['nickname']; } if (!empty($userData['headimgurl'])) { $user->avatar = $userData['headimgurl']; } } else { $ip = YII_DEBUG ? '121.32.52.217' : CommonTool::getClientIp(); $info = CommonTool::getCity($ip); $count = User::model()->count(); $user = new User(); $user->attributes = array('user_id' => $openid, 'nickname' => empty($userData['nickname']) ? '微信用户' . (25874 + $count) : CommonTool::emojiFilter($userData['nickname']), 'avatar' => !empty($userData['headimgurl']) ? $userData['headimgurl'] : 'http://appmcdn.m0.hk/avatar.jpg', 'ip' => $ip, 'city' => $info['city'], 'created_at' => time()); } if (!$user->save()) { CommonTool::log($user->getErrors()); throw new Exception('创建用户失败!'); } return $user; }
public function receiveData(array $sendData) { $data_struct = ['data', 'key', 'signature']; if (array_diff($data_struct, array_keys($sendData))) { throw new \Exception('Data Structure Error'); } try { $AES_secret = $this->RSA->privDecrypt($sendData['key']); \CommonTool::log('AES_secret', $AES_secret); } catch (\Exception $e) { throw new \Exception('Failed To Decode AES Secret'); } if ($this->RSA->verify($sendData['data'], $sendData['signature']) === false) { throw new \Exception('Check Signature Failed'); } $this->AES_secret = $AES_secret; $this->AES->setSecretKey($this->AES_secret); $data = $this->AES->decrypt($sendData['data']); \CommonTool::log('decrypted_data', $data); $data = json_decode($data, true); if (json_last_error() == JSON_ERROR_NONE) { if (\CommonTool::checkSummary($data)) { return $data; } throw new \Exception('CheckSignature Failed'); } else { throw new \Exception('Received Data json_decode Failed'); } }