## 代码
```
/**
* 获取网站根目录
* @return string 网站根目录
*/
function cmf_get_root()
{
$request = Request::instance();
$root = $request->root();
$root = str_replace('/index.php', '', $root);
if (defined('APP_NAMESPACE') && APP_NAMESPACE == 'api') {
$root = preg_replace('/\/api$/', '', $root);
$root = rtrim($root, '/');
}
return $root;
}
/**
* 验证码检查,验证完后销毁验证码
* @param string $value
* @param string $id
* @return bool
*/
function cmf_captcha_check($value, $id = "")
{
$captcha = new \think\captcha\Captcha();
return $captcha->check($value, $id);
}
/**
* CMF密码比较方法,所有涉及密码比较的地方都用这个方法
* @param string $password 要比较的密码
* @param string $passwordInDb 数据库保存的已经加密过的密码
* @return boolean 密码相同,返回true
*/
function cmf_compare_password($password, $passwordInDb)
{
if (strpos($passwordInDb, "###") === 0) {
return cmf_password($password) == $passwordInDb;
} else {
return cmf_password_old($password) == $passwordInDb;
}
}
/**
* 生成用户 token
* @param $userId
* @param $deviceType
* @return string 用户 token
*/
function cmf_generate_user_token($userId, $deviceType)
{
$userTokenQuery = Db::name("user_token")
->where('user_id', $userId)
->where('device_type', $deviceType);
$findUserToken = $userTokenQuery->find();
$currentTime = time();
$expireTime = $currentTime + 24 * 3600 * 180;
$token = md5(uniqid()) . md5(uniqid());
if (empty($findUserToken)) {
Db::name("user_token")->insert([
'token' => $token,
'user_id' => $userId,
'expire_time' => $expireTime,
'create_time' => $currentTime,
'device_type' => $deviceType
]);
} else {
Db::name("user_token")
->where('user_id', $userId)
->where('device_type', $deviceType)
->update([
'token' => $token,
'expire_time' => $expireTime,
'create_time' => $currentTime
]);
}
return $token;
}
```