多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 用户注册 注意用户注册需要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); } ```