>cmf_generate_user_token($userId, $deviceType)
作用:生成用户的token
## 应用
```
public function test(){
cmf_generate_user_token(1,'mobile');
}
```
>设备类型;mobile,android,iphone,ipad,web,pc,mac,wxapp
## 如图
![mark](http://qiniu.newthink.cc/blog/20170926-141847852.png)
## 函数代码
```
/**
* 生成用户 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;
}
```