开发环境搭建

使用自己的AppID新建小程序项目,后端服务选择小程序·云开发,点击新建,完成项目新建。

新建成功后跳转到开发者工具界面

新建后,微信端为我们提供了一个参考的模板程序,这里我们自己来创建各个所需的文件与代码,所以删除所有不需要的文件,删除cloudfunctions、miniprogram/images、miniprogram/pages文件下所有文件,同时也删除style文件和删除app.json中原始的页面配置。

此时编译下方控制台会报“VM8100:5 appJSON[“pages”] 需至少存在一项”错误,因为app.json中未配置任何页面路径,下面我们来对app.json进行配置。

  1. {
  2. "cloud": true,
  3. "pages": [
  4. "pages/index/index",
  5. "pages/detonation/detonation",
  6. "pages/user/user"
  7. ],

“cloud”:

true表示让云能力可以在所有基础库中使用,在页面路径列表pages下加入三个Tab页面路径,在window中设置全局的默认窗口样式,通过tabBar设置底部tab栏的样式,配置完成后点击编译,开发工具会自动生成三个页面的文件夹以及相关文件。

  1. "window": {
  2. "backgroundTextStyle": "light",
  3. "navigationBarBackgroundColor": "#FF3333",
  4. "navigationBarTitleText": "我要点爆",
  5. "navigationBarTextStyle": "white",
  6. "backgroundColor": "#FF3333"
  7. },
  8. "tabBar": {
  9. "backgroundColor": "#F2F2F2",
  10. "color": "#6B6B6B",
  11. "selectedColor": "#FF0000",
  12. "list": [
  13. {
  14. "pagePath": "pages/index/index",
  15. "text": "世界",
  16. "iconPath": "/images/shi.png",
  17. "selectedIconPath": "/images/shi1.png"
  18. },
  19. {
  20. "pagePath": "pages/detonation/detonation",
  21. "text": "点爆",
  22. "iconPath": "/images/bao2.png",
  23. "selectedIconPath": "/images/bao1.png"
  24. },
  25. {
  26. "pagePath": "pages/user/user",
  27. "text": "我的",
  28. "iconPath": "/images/wo1.png",
  29. "selectedIconPath": "/images/wo.png"
  30. }
  31. ]
  32. },
  33. "sitemapLocation": "sitemap.json"
  34. }

 

配置成功后页面结构与效果

创建数据库环境

设置环境名称,环境名称可以根据自己需求设置,这里设置与项目名相同dbx,下方的环境ID会自动生成,无需修改,点击确定完成创建。

创建成功后跳转云开发控制台页面

配置app.js文件,在调用云开发各 API 前,需先调用初始化方法 init 一次(全局只需一次),在wx.cloud.init中设置程序所读环境的数据库位置,刚才创建的数据库环境的ID

    

实现我的页面布局制作与用户授权登录功能

首先对页面进行布局,头部使用一个button按钮来进行授权登录获取用户信息的操作,设置button的open-type为getUserInfo,使得按钮可以从bindgetuserinfo回调中获取到用户信息,设置回调方法为getUserInfoHandler。为了让用户授权后实时更新用户头像与用户名,这里使用数据绑定与判断的方法。

 

  1. <!-- pages/user/user.wxml -->
  2. <view class="user_header">
  3. <view class="header_box">
  4. <image src="{{userTx || defaultUrl}}"></image>
  5. <button class="{{username == \'点击登录\' ? \'usernameDe\' : \'username\'}}" open-type="getUserInfo" bindgetuserinfo="getUserInfoHandler">{{username}}</button>
  6. <view class="qiandao">
  7. <text>糖果</text>
  8. </view>
  9. </view>
  10. </view>
  11. <view class="user_main">
  12. <view class="main_box">
  13. <view class="box_item">
  14. <image src="/images/jilu.png"></image>
  15. <text>点爆记录</text>
  16. </view>
  17. <view class="box_item">
  18. <image src="/images/zhudian.png"></image>
  19. <text>最近助点</text>
  20. </view>
  21. </view>
  22. <view class="main_box">
  23. <view class="box_item">
  24. <image src="/images/fengcun.png"></image>
  25. <text>我的封存</text>
  26. </view>
  27. <view class="box_item">
  28. <image src="/images/usercang.png"></image>
  29. <text>我的收藏</text>
  30. </view>
  31. </view>
  32. </view>

页面布局完成后进行user.js的编写,data中设置页面初始数据,username用于控制授权按钮用户名变换,defaultUrl设置默认头像,userTx记录用户头像,userInfo记录用户授权后所获取的信息,gender用与用户性别判断,province用于记录地区信息。

  1. // pages/user/user.js
  2. Page({
  3. data: {
  4. username: \'点击登录\',
  5. defaultUrl: \'/images/yuyin5.png\',
  6. userTx: \'\',
  7. userInfo: {},
  8. gender: 1,
  9. province: \'\',
  10. },

在onLoad中对页面进行初始化设置和用户是否登录的初始化设置,在用户授权登录后直接使用本地的用户信息,如果本地信息不存在则通过wx.getSetting获取用户设置,看用户是否授权过,如果授权过,则wx.getUserInfo直接获取用户信息。

  1. onLoad: function () {
  2. wx.setNavigationBarTitle({
  3. title: \'我的\'
  4. })
  5. //当重新加载这个页面时,查看是否有已经登录的信息
  6. let username = wx.getStorageSync(\'username\'),
  7. avater = wx.getStorageSync(\'avatar\');
  8. if (username) {
  9. this.setData({
  10. username: username,
  11. userTx: avater
  12. })
  13. }
  14. wx.getSetting({
  15. success: res => {
  16. if (res.authSetting[\'scope.userInfo\']) {
  17. wx.getUserInfo({
  18. success: res => {
  19. this.setData({
  20. userTx: res.userInfo.avatarUrl,
  21. userInfo: res.userInfo
  22. })
  23. }
  24. })
  25. }
  26. }
  27. })
  28. },

getUserInfoHandler方法保存系统常用的用户信息到本地和完成用户信息数据库注册,**button组件中bindgetuserinfo方法回调的detail数据与wx.getUserInfo返回的一致**,通过detail将所需的用户信息提取出来,将性别gender替换为‘男’和‘女’,将头像、用户名、性别、地区保存在本地。然后使用云数据库API进行数据库操作。

  1. getUserInfoHandler: function (e) {
  2. let d = e.detail.userInfo
  3. var gen = d.gender == 1 ? \'\' : \'\'
  4. this.setData({
  5. userTx: d.avatarUrl,
  6. username: d.nickName
  7. })
  8. wx.setStorageSync(\'avater\', d.avatarUrl)
  9. wx.setStorageSync(\'username\', d.nickName)
  10. wx.setStorageSync(\'gender\', gen)
  11. wx.setStorageSync(\'province\', d.province)
  12. //获取数据库引用
  13. const db = wx.cloud.database()
  14. const _ = db.command
  15. //查看是否已有登录,无,则获取id
  16. var userId = wx.getStorageSync(\'userId\')
  17. if (!userId) {
  18. userId = this.getUserId()
  19. }
  20. //查找数据库
  21. db.collection(\'users\').where({
  22. _openid: d.openid
  23. }).get({
  24. success(res) {
  25. // res.data 是包含以上定义的记录的数组
  26. //如果查询到数据,将数据记录,否则去数据库注册
  27. if (res.data && res.data.length > 0) {
  28. wx.setStorageSync(\'openId\', res.data[0]._openid)
  29. } else {
  30. //定时器
  31. setTimeout(() => {
  32. //写入数据库
  33. db.collection(\'users\').add({
  34. data: {
  35. userId: userId,
  36. userSweet: 10,
  37. voice: 0,
  38. baovoice: 0,
  39. iv: d.iv
  40. },
  41. success: function () {
  42. console.log(\'用户id新增成功\')
  43. db.collection(\'users\').where({
  44. userId: userId
  45. }).get({
  46. success: res => {
  47. wx.setStorageSync(\'openId\', res.data[0]._openid)
  48. },
  49. fail: err => {
  50. console.log(\'用户_openId设置失败\')
  51. }
  52. })
  53. },
  54. fail: function (e) {
  55. console.log(\'用户id新增失败\')
  56. }
  57. })
  58. }, 100)
  59. }
  60. },
  61. fail: err => {
  62. }
  63. })
  64. },
  65. getUserId: function () {
  66. //生产唯一id,采用一个字母或数字+1970年到现在的毫秒数+10w的一个随机数组成
  67. var w = "abcdefghijklmnopqrstuvwxyz0123456789",
  68. firstW = w[parseInt(Math.random() * (w.length))];
  69. var userId = firstW + (Date.now()) + (Math.random() * 100000).toFixed(0)
  70. wx.setStorageSync(\'userId\', userId)
  71. return userId;
  72. },
  73. })

 

在云开发控制台中创建数据库集合,我们新建一个users集合,我们只需新建集合,通过js中使用云开发API可自动创建集合中的属性和数据。

 

该users集合为用户信息表,记录用户信息,表users的结构如下:

集合创建成功后,点击将出现进行编译,此时页面效果如下:

我们点击“点击登录”按钮,然后对程序进行授权,授权后可以看到我们的头像和用户名都显示出来了,同时,打开云开发控制台,查看users集合,可以看到我们信息已经成功保存在了集合中。

  

 

至此,我们就完成了
1、云端控制台数据库的创建
2、我的页面的样式制作
3、用户授权登录功能制作
4、云数据库的用户数据存储的实现

项目源码:https://github.com/xiedong2016/dbx

版权声明:本文为xiedong2016原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/xiedong2016/p/10925126.html