当前位置:   article > 正文

uniCloud作业 开发留言板 uniCloud实战项目开发_uniapp留言板

uniapp留言板

uniCloud云开发留言板【uniCloud作业】

实现了用户登录、用户注册、留言、删除留言、实现了增删改等

下面三个tabbar栏可切换

注册页面代码

  1. <template>
  2. <view class="content">
  3. <view class="input-group">
  4. <view class="input-row border">
  5. <text class="title">账号:</text>
  6. <m-input type="text" focus clearable v-model="username" placeholder="请输入账号"></m-input>
  7. </view>
  8. <view class="input-row border">
  9. <text class="title">密码:</text>
  10. <m-input type="password" displayable v-model="password" placeholder="请输入密码"></m-input>
  11. </view>
  12. <view class="input-row">
  13. <text class="title">确认密码:</text>
  14. <m-input type="password" displayable v-model="confirmPassword" placeholder="请确认密码"></m-input>
  15. </view>
  16. </view>
  17. <view class="btn-row">
  18. <button type="primary" class="primary" @tap="register">注册并登录</button>
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. import mInput from '../../components/m-input.vue';
  24. export default {
  25. components: {
  26. mInput
  27. },
  28. data() {
  29. return {
  30. username: '',
  31. password: '',
  32. confirmPassword: ''
  33. }
  34. },
  35. methods: {
  36. register() {
  37. /**
  38. * 客户端对账号信息进行一些必要的校验。
  39. * 实际开发中,根据业务需要进行处理,这里仅做示例。
  40. */
  41. if (this.username.length < 3) {
  42. uni.showToast({
  43. icon: 'none',
  44. title: '账号最短为 3 个字符'
  45. });
  46. return;
  47. }
  48. if (this.password.length < 6) {
  49. uni.showToast({
  50. icon: 'none',
  51. title: '密码最短为 6 个字符'
  52. });
  53. return;
  54. }
  55. if (this.password !== this.confirmPassword) {
  56. uni.showToast({
  57. icon: 'none',
  58. title: '两次密码输入不一致'
  59. });
  60. return;
  61. }
  62. const data = {
  63. username: this.username,
  64. password: this.password
  65. }
  66. uniCloud.callFunction({
  67. name: 'user-center',
  68. data: {
  69. action: 'register',
  70. params: data
  71. },
  72. success(e) {
  73. console.log("注册", e);
  74. if (e.result.code === 0) {
  75. uni.showToast({
  76. title: '注册成功'
  77. });
  78. uni.setStorageSync('uni_id_token', e.result.token)
  79. uni.setStorageSync('username', e.result.username)
  80. uni.reLaunch({
  81. url: '../index/index',
  82. });
  83. } else {
  84. uni.showModal({
  85. content: JSON.stringify(e.result),
  86. showCancel: false
  87. })
  88. }
  89. },
  90. fail(e) {
  91. uni.showModal({
  92. content: JSON.stringify(e),
  93. showCancel: false
  94. })
  95. }
  96. })
  97. }
  98. }
  99. }
  100. </script>
  101. <style>
  102. .content {
  103. display: flex;
  104. flex: 1;
  105. flex-direction: column;
  106. background-color: #efeff4;
  107. padding: 10px;
  108. }
  109. </style>

留言页面代码

  1. <template>
  2. <view class="content">
  3. <view class="input_box">
  4. <textarea placeholder="请输入要留言的内容" v-model="list.content" class="textarea border" />
  5. <!-- <input type="text" placeholder="请输入用户名:" v-model="list.name" class="input border" /> -->
  6. <button class="submit" type="primary" @click="Add">提交</button>
  7. </view>
  8. </view>
  9. </template>
  10. <script>
  11. import { mapState, mapMutations } from 'vuex'
  12. import { univerifyLogin } from '@/common/univerify.js'
  13. export default {
  14. computed: mapState(['forcedLogin', 'hasLogin', 'userName']),
  15. data () {
  16. return {
  17. list: {
  18. time: "",
  19. name:"",
  20. content:""
  21. }
  22. }
  23. },
  24. onLoad() {
  25. let uniIdToken = uni.getStorageSync('uni_id_token')
  26. if (uniIdToken) {
  27. let uniName = uni.getStorageSync('username')
  28. this.list.name = uniName
  29. this.login(uniName)
  30. uniCloud.callFunction({
  31. name: 'user-center',
  32. data: {
  33. action: 'checkToken',
  34. },
  35. success: (e) => {
  36. console.log('checkToken success', e);
  37. if (e.result.code > 0) {
  38. if (this.forcedLogin) {
  39. uni.reLaunch({
  40. url: '../login/index'
  41. });
  42. } else {
  43. uni.navigateTo({
  44. url: '../login/index'
  45. });
  46. }
  47. }
  48. },
  49. fail(e) {
  50. uni.showModal({
  51. content: JSON.stringify(e),
  52. showCancel: false
  53. })
  54. }
  55. })
  56. } else {
  57. this.guideToLogin()
  58. }
  59. },
  60. methods: {
  61. ...mapMutations(['login']),
  62. guideToLogin() {
  63. uni.showModal({
  64. title: '未登录',
  65. content: '您未登录,需要登录后才能继续',
  66. confirmColor: "#37C2BC",
  67. showCancel: !this.forcedLogin,
  68. success: (res) => {
  69. if (res.confirm) {
  70. univerifyLogin().catch((err) => {
  71. if (err === false) return;
  72. if (this.forcedLogin) {
  73. uni.reLaunch({
  74. url: '../login/index'
  75. });
  76. } else {
  77. uni.navigateTo({
  78. url: '../login/index'
  79. });
  80. }
  81. })
  82. }
  83. }
  84. });
  85. },
  86. Add() {
  87. if (!this.list.content) {
  88. uni.showToast({
  89. icon: 'error',
  90. title: "请输入内容"
  91. })
  92. return
  93. }
  94. if (!uni.getStorageSync('username')) {
  95. this.guideToLogin()
  96. return
  97. }
  98. uni.showLoading({
  99. title: "提交中"
  100. })
  101. this.list.time = +new Date()
  102. let data = this.list;
  103. uniCloud.callFunction({
  104. name:'add',
  105. data:data
  106. }).then((res)=>{
  107. uni.hideLoading()
  108. uni.showModal({
  109. title: '提示',
  110. content: '提交成功',
  111. confirmColor: "#37C2BC",
  112. showCancel: false,
  113. success: res => {
  114. uni.reLaunch({
  115. url: '../index/index'
  116. });
  117. },
  118. })
  119. })
  120. }
  121. }
  122. }
  123. </script>
  124. <style lang="scss">
  125. .border {
  126. border-style: solid;
  127. border-width: 1px;
  128. border-color: #eeeeee;
  129. }
  130. .input {
  131. padding: 6rpx 8rpx;
  132. height: 42px;
  133. margin: 10rpx;
  134. font-size: 30rpx;
  135. border-radius: 6px;
  136. }
  137. .submit {
  138. width: 600upx;
  139. margin-top: 20px;
  140. margin-bottom: 20px;
  141. background-color: #37C2BC;
  142. }
  143. .list {
  144. width: 750rpx;
  145. }
  146. .input_box {
  147. width: 750rpx;
  148. background-color: #ffffff;
  149. margin-bottom: 6px;
  150. }
  151. .textarea {
  152. padding: 10rpx;
  153. margin: 10rpx;
  154. width: initial;
  155. height: 800rpx;
  156. font-size: 30rpx;
  157. border-radius: 6px;
  158. }
  159. .item {
  160. @extend .border;
  161. border-radius: 16px;
  162. border-color: #eeeeee;
  163. padding: 4px 8px;
  164. margin: 5px;
  165. display: flex;
  166. flex-wrap: nowrap;
  167. flex-direction: row;
  168. align-items: center;
  169. }
  170. .word {
  171. padding: 20rpx;
  172. flex: 1;
  173. justify-content: space-around;
  174. }
  175. .user_name {
  176. margin-bottom: 10rpx;
  177. font-size: 26upx;
  178. color: #007aff;
  179. }
  180. .content {
  181. font-size: 32upx;
  182. color: #666666;
  183. }
  184. </style>

部署教程

使用hbuildx打开项目

登录自己的开发者账号并绑定云服务

同步云函数及云数据库

然后运行到浏览器

项目截图

登录-注册功能

首页-留言-我的

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/469941
推荐阅读
相关标签
  

闽ICP备14008679号