# 用戶注冊
注意用戶注冊需要core_user和core_identity表配合,注意使用事務。
## 手機號注冊示例
其他比如郵箱注冊、微信注冊參考修改
```
use?app\core\model\Useras?coreUserModel;
use?app\core\model\Identityas?coreIdentityModel;
// 判斷已注冊
$identityInfo= [];
$identityInfo['cloudAlias'] = 0
$identityInfo['identityType'] = 'mobile';
// 取值emial/mobile/wxmp/wxapp等
$identityInfo['verified'] = 1;
$identityInfo['identifier'] = input('post.mobile');
// 如果是微信登錄此字段存儲微信openid
$identityInfo['identityGroup'] = '+86'; // 如果是微信登錄此字段存儲微信公眾號的appid
if (coreIdentityModel::where($identityInfo)->count()) {
throw new \think\Exception('該手機號已經被注冊', 0);
}
try?{
Db::startTrans();
// 注冊新用戶
$time = micro_time();
$dataDb = [];
$dataDb['cloudId'] = 0;
$dataDb['cloudAlias'] = 0;
$dataDb['userKey'] = \think\helper\Str::random(64); // 秘鑰
$dataDb['nickname'] = 'm_' . $time;
$dataDb['username'] = 'm_' . $time;
$dataDb['password'] = user_md5(input('post.password'), $dataDb['userKey']); // 密碼不能明文需要加密存儲
$dataDb['avatar'] = '';
$dataDb['status'] = 1;
$dataDb['registerTime'] = time();
$userInfo = coreUserModel::create($dataDb);
if (!$userInfo) {
throw new \Exception("注冊失敗", 0);
}
// 綁定手機號
、綁定郵箱、綁定微信openid或者其他OAuth2登錄的openid
$dataIdentity = [];
$dataIdentity['uid'] = $userInfo->id;
$dataIdentity['cloudAlias'] =0;
$dataIdentity['identityType'] = $identityInfo['identityType'];
$dataIdentity['identityGroup'] = $identityInfo['identityGroup'];
$dataIdentity['identifier'] = $identityInfo['identifier'];
$dataIdentity['verified'] = 1;
$dataIdentity['createTime'] = time();
$userIdentityInfo = coreIdentityModel::create($dataIdentity);
if (!$userIdentityInfo->id) {
throw new \Exception("注冊失敗", 0);
}
Db::commit();
return$userInfo;
}catch(\Exception$e)?{
Db::rollback();
throw new\Exception($e->getMessage(),?0);
}
```
- 說明
- 簡介
- 系統安裝
- 后端注意
- 目錄結構
- 數據表
- 用戶注冊
- 前端注意
- 后端接口開發
- 新建模塊
- 創建數據表
- 創建模型
- 創建后臺控制器
- 添加后臺接口
- 創建前臺控制器
- 添加前臺接口
- 常用接口
- 檢查用戶登錄
- 內置接口
- Builder動態頁面
- Builder列表
- addTopButton
- addRightButton
- addColumn
- setDataList
- setDataPage
- getData
- Builder表單
- setFormMethod
- addFormItem
- 單圖image
- 多圖images
- addFormRule
- setFormValues
- getData
- 自定義組件
- 自定義頁面組件
- 自定義Form組件
- 加載第三方js插件
- 常見問題
- 模塊開發者
- 升級指南
- 圖標
- 擴展
- Composer
- ThinkPHP5.1
- GuzzleHttp
- phpspreadsheet
- QueryList
- phpseclib
- 云后臺接口