打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
微信小程序云开发

要有遥不可及的梦想,也要有脚踏实地的本事。----------- Grapefruit.Banuit Gang(香柚帮)


这一章讲解的是用户注册的逻辑:

继上一章节开通云服务之后,首先要做的就是创建一个数据库集合,用来存放用户的注册的数据,比如,用户名、密码等

按照步骤,点击数据库,然后点击加号,输入数据库集合表名称即可

创建完成之后,我们就要写页面了,新建注册页面:

在我们写页面之前还要在app.js中做下配置,配置如下:

  1. if (!wx.cloud) {
  2. console.error('请使用 2.2.3 或以上的基础库以使用云能力')
  3. } else {
  4. wx.cloud.init({
  5. // env 参数说明:
  6. // env 参数决定接下来小程序发起的云开发调用(wx.cloud.xxx)会默认请求到哪个云环境的资源
  7. // 此处请填入环境 ID, 环境 ID 可打开云控制台查看
  8. // 如不填则使用默认环境(第一个创建的环境)
  9. // env: 'my-env-id',
  10. traceUser: true,
  11. })
  12. }

这段代码要放在onLaunch: function (){}里面,获取用户信息的下面,配置完成之后,即可开始完成注册页面代码:

下面是我自己写的一套注册页面,仅供参考:

wxml:

  1. <view class="container">
  2. <image src="https://7069-pintu-game-52d2a-1301643624.tcb.qcloud.la/pintu_icon/login_bg.jpg"></image>
  3. </view>
  4. <view class="login_box">
  5. <view class="section">
  6. <input placeholder="请输入用户名" placeholder-class="color" bindblur='username' />
  7. <image src="https://7069-pintu-game-52d2a-1301643624.tcb.qcloud.la/pintu_icon/username.png"></image>
  8. </view>
  9. <view class="section">
  10. <input type='number' maxlength='11' placeholder="请输入手机号" placeholder-class="color" bindblur='mobile' />
  11. <image src="https://7069-pintu-game-52d2a-1301643624.tcb.qcloud.la/pintu_icon/mobile.png"></image>
  12. </view>
  13. <view class="section">
  14. <input password="true" placeholder="请输入密码" placeholder-class="color" bindblur='pass1' />
  15. <image src="https://7069-pintu-game-52d2a-1301643624.tcb.qcloud.la/pintu_icon/pass.png"></image>
  16. </view>
  17. <view class="section">
  18. <input password="true" placeholder="确认密码" placeholder-class="color" bindblur='pass2' />
  19. <image src="https://7069-pintu-game-52d2a-1301643624.tcb.qcloud.la/pintu_icon/pass.png"></image>
  20. </view>
  21. <button class="register" type="warn" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">注册</button>
  22. </view>

wxss:

  1. /* pages/login/login.wxss */
  2. .container {
  3. position: absolute;
  4. width: 100%;
  5. height: 100%;
  6. }
  7. .container image {
  8. width: 100%;
  9. height: 100%;
  10. }
  11. .login_box{
  12. width: 90%;
  13. position: absolute;
  14. top: 10%;
  15. left: 5%;
  16. }
  17. .section{
  18. width: 100%;
  19. border-bottom: 4rpx solid #FFF;
  20. margin-top: 40rpx;
  21. position: relative;
  22. }
  23. .section input{
  24. height: 100rpx;
  25. color: #FFF;
  26. box-sizing: border-box;
  27. padding-left: 80rpx;
  28. font-size: 36rpx;
  29. }
  30. .section image{
  31. width: 60rpx;
  32. height: 60rpx;
  33. position: absolute;
  34. top: 20rpx;
  35. left: 10rpx;
  36. }
  37. .color{
  38. color: #FFF;
  39. }
  40. .register{
  41. margin-top: 200rpx;
  42. }
  43. .register{
  44. background: #fa5151 !important;
  45. color: #FFF !important;
  46. }

app.wxss

  1. .container {
  2. display: flex;
  3. flex-direction: column;
  4. align-items: center;
  5. box-sizing: border-box;
  6. }

 大致样式是这样的:

接下来是js逻辑代码

  1. const db = wx.cloud.database() //这一句必不可少
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. username: '', //用户名
  8. mobile: '', //手机号
  9. pass1: '', //密码
  10. pass2: '', //确认密码
  11. },
  12. // 用户名失去焦点
  13. username(e) {
  14. this.setData({
  15. username: e.detail.value
  16. })
  17. },
  18. // 手机号失去焦点
  19. mobile(e) {
  20. this.setData({
  21. mobile: e.detail.value
  22. })
  23. },
  24. // 密码失去焦点
  25. pass1(e) {
  26. var pattern = /^[\w_]{8,16}$/; //密码正则
  27. if (!pattern.test(e.detail.value)) {
  28. this.setData({
  29. pass1: ''
  30. })
  31. wx.showToast({
  32. title: '密码长度必须为8-16位,并且由字母,数字或下划线组成',
  33. icon: 'none',
  34. duration: 3000
  35. })
  36. } else {
  37. this.setData({
  38. pass1: e.detail.value
  39. })
  40. }
  41. },
  42. // 确认密码失去焦点
  43. pass2(e) {
  44. this.setData({
  45. pass2: e.detail.value
  46. })
  47. },
  48. // 点击注册按钮
  49. bindGetUserInfo: function(e){
  50. if (e.detail.userInfo) {
  51. //用户按了允许授权按钮
  52. wx.showLoading({
  53. title: '正在注册...',
  54. })
  55. if (this.data.username == '') {
  56. wx.showToast({
  57. title: '用户名不能为空',
  58. icon: 'none',
  59. duration: 2000
  60. })
  61. } else if (this.data.mobile == '') {
  62. wx.showToast({
  63. title: '手机号不能为空',
  64. icon: 'none',
  65. duration: 2000
  66. })
  67. } else if (!(/^1[3456789]\d{9}$/.test(this.data.mobile))) {
  68. wx.showToast({
  69. title: '手机号码格式有误,请重新输入!',
  70. icon: 'none',
  71. duration: 2000
  72. })
  73. } else if (this.data.pass1 == '') {
  74. wx.showToast({
  75. title: '密码不能为空',
  76. icon: 'none',
  77. duration: 2000
  78. })
  79. } else if (this.data.pass2 == '') {
  80. wx.showToast({
  81. title: '请再次输入密码',
  82. icon: 'none',
  83. duration: 2000
  84. })
  85. } else if (this.data.pass1 != this.data.pass2) {
  86. wx.showToast({
  87. title: '两次密码输入不一致,请重新输入!',
  88. icon: 'none',
  89. duration: 2000
  90. })
  91. } else {
  92. var that = this
  93. // 注册这个账户之前,我们首先要做的就是查询一下这个集合表中是否已经存在过这个用户了
  94. db.collection('login_info').where({ //查询接口
  95. username: that.data.username //传参,输入的用户名
  96. })
  97. .get({
  98. success: function(res) {
  99. if (res.data.length == 0) { //判断用户名是否被注册过,等于空说明没被查询到,就是没有注册过,
  100. db.collection('login_info').where({ //我写的数用户名和手机号都可以登录,所以同一个手机号和用户名只能算一个账号,也要验证一下手机号是否被注册过
  101. mobile: that.data.mobile
  102. })
  103. .get({
  104. success: function(res) {
  105. if (res.data.length == 0) { //判断手机号是否被注册过,等于空说明没被查询到,就是没有注册过,
  106. // 获取当前时间,写入数据库,可以知道此账号是何时创建
  107. var myDate = new Date();
  108. var y = myDate.getFullYear();
  109. var m = myDate.getMonth() + 1;
  110. var d = myDate.getDate();
  111. var h = myDate.getHours();
  112. var ms = myDate.getMinutes();
  113. var s = myDate.getSeconds();
  114. var time = y + '-' + m + '-' + d + ' ' + h + ':' + ms + ':' + s
  115. db.collection('login_info').add({ //验证完成之后,添加的接口
  116. data: {
  117. username: that.data.username, //用户名
  118. mobile: that.data.mobile, //手机号
  119. pass1: that.data.pass1, //密码
  120. time: time //创建时间
  121. },
  122. success: function(res) {
  123. if (res.errMsg == 'collection.add:ok') { //接口调取成功,也就是注册成功
  124. // 这里面你可以加一下注册成功之后的逻辑,是直接登录也好,或者是跳到登录页面页面
  125. wx.hideLoading();
  126. wx.showToast({
  127. title: '注册成功',
  128. icon: 'none'
  129. })
  130. }
  131. }
  132. })
  133. } else {
  134. wx.showToast({
  135. title: '此手机号已被别人注册,换一个试试!',
  136. icon: 'none',
  137. duration: 2000
  138. })
  139. }
  140. }
  141. })
  142. } else {
  143. wx.showToast({
  144. title: '此用户名已被别人注册,换一个试试!',
  145. icon: 'none',
  146. duration: 2000
  147. })
  148. }
  149. }
  150. })
  151. }
  152. } else {
  153. //用户按了拒绝按钮
  154. wx.showModal({
  155. title: '警告',
  156. content: '您点击了拒绝授权,将无法进行账号注册,请授权之后再注册!!!',
  157. showCancel: false,
  158. confirmText: '返回授权',
  159. success: function(res) {}
  160. })
  161. }
  162. },
  163. /**
  164. * 生命周期函数--监听页面加载
  165. */
  166. onLoad: function(options) {
  167. },
  168. /**
  169. * 生命周期函数--监听页面初次渲染完成
  170. */
  171. onReady: function() {
  172. },
  173. /**
  174. * 生命周期函数--监听页面显示
  175. */
  176. onShow: function() {
  177. },
  178. /**
  179. * 生命周期函数--监听页面隐藏
  180. */
  181. onHide: function() {
  182. },
  183. /**
  184. * 生命周期函数--监听页面卸载
  185. */
  186. onUnload: function() {
  187. },
  188. /**
  189. * 页面相关事件处理函数--监听用户下拉动作
  190. */
  191. onPullDownRefresh: function() {
  192. },
  193. /**
  194. * 页面上拉触底事件的处理函数
  195. */
  196. onReachBottom: function() {
  197. },
  198. /**
  199. * 用户点击右上角分享
  200. */
  201. onShareAppMessage: function() {
  202. }
  203. })

这里面注释讲解已经写的很清楚了,如果有看不明白的小伙伴,可以在下方留言

点击注册,提示注册成功之后,就可以看到我们的数据库已经增加了这一条数据

如此,到这一步,我们的注册逻辑已经完成,希望能帮助到一些朋友,有不明白的可以在下方留言

下一章讲解登录逻辑:https://blog.csdn.net/qq_43052274/article/details/105269351

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
微信小程序 判断是否是手机号
小程序--点击显示与隐藏-动态更改img-src 路径
微信小程序开发教程(第4弹)
微信小程序自定义tabbar(官方custom
微信小程序的简单制作开发流程,人人都可以自己开发小程序
用微信小程序写一个微信(布局练习)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服