/** * @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'); } }
/** * 记录日志 * @param string $message */ public static function log($message = '', $class = __CLASS__) { $traceMessage = ''; $traces = debug_backtrace(); foreach ($traces as $trace) { if (isset($trace['file'], $trace['line']) && strpos($trace['file'], YII_PATH) !== 0) { $traceMessage .= "\nin " . $trace['file'] . ' (' . $trace['line'] . ')'; } } if (!is_string($message)) { $message = print_r($message, 1); } self::$memoryTime = microtime(true); Yii::getLogger()->autoFlush = 1; Yii::getLogger()->autoDump = true; $openid = ''; if (!Yii::app()->user->isGuest) { $openid = Yii::app()->user->id; } Yii::getLogger()->log(CommonTool::getClientIp() . "[" . $openid . "]:" . $_SERVER['REQUEST_URI'] . "\n" . $message . "\n---------------\n" . $traceMessage . "\n" . print_r($_POST, 1) . "\n" . print_r($_GET, 1) . "\n" . print_r($_REQUEST, 1)); }
/** * @param $openId * @param $content * @return mixed */ public function sendText($openId, $content) { $data = json_encode(array('touser' => $openId, 'msgtype' => 'text', 'text' => array('content' => $content))); $data = CommonTool::decodeUnicode($data); return $this->send($openId, $data); }