setting.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //index.js
  2. //获取应用实例
  3. const app = getApp()
  4. Page({
  5. data: {
  6. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  7. userInfo: {},
  8. hasUserInfo: false,
  9. // 模板列表
  10. templateList: [
  11. {
  12. name: '模板一',
  13. url: '/pages/images/template/1.jpg'
  14. },
  15. {
  16. name: '模板二',
  17. url: '/pages/images/template/2.jpg'
  18. },
  19. {
  20. name: '模板三',
  21. url: '/pages/images/template/3.jpg'
  22. },
  23. {
  24. name: '模板四',
  25. url: '/pages/images/template/4.jpg'
  26. },
  27. {
  28. name: '模板五',
  29. url: '/pages/images/template/5.jpg'
  30. },
  31. ],
  32. form: {
  33. phone: '17319450324'
  34. },
  35. // 二维码
  36. qrcodeUrl:'/pages/images/qrcode.jpg'
  37. },
  38. // 事件处理函数
  39. imageChange: function (e) {
  40. if (e) {
  41. this.setData({ "form.template": this.data.templateList[e.detail.value].name })
  42. this.setData({ "form.imgurl": this.data.templateList[e.detail.value].url })
  43. } else {
  44. this.setData({ "form.template": this.data.templateList[0].name })
  45. this.setData({ "form.imgurl": this.data.templateList[0].url })
  46. }
  47. this.setData({ "form.imgIndex": e ? e.detail.value : 0 })
  48. },
  49. onLoad: function () {
  50. // 模板默认选择
  51. this.imageChange();
  52. if (app.globalData.userInfo) {
  53. this.setData({
  54. userInfo: app.globalData.userInfo,
  55. hasUserInfo: true
  56. })
  57. } else if (this.data.canIUse) {
  58. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  59. // 所以此处加入 callback 以防止这种情况
  60. app.userInfoReadyCallback = res => {
  61. console.log(app.globalData.userInfo);
  62. this.setData({
  63. userInfo: res.userInfo,
  64. hasUserInfo: true
  65. })
  66. }
  67. } else {
  68. // 在没有 open-type=getUserInfo 版本的兼容处理
  69. wx.getUserInfo({
  70. success: res => {
  71. app.globalData.userInfo = res.userInfo
  72. this.setData({
  73. userInfo: res.userInfo,
  74. hasUserInfo: true
  75. })
  76. }
  77. })
  78. }
  79. },
  80. getUserInfo: function (e) {
  81. console.log(e)
  82. app.globalData.userInfo = e.detail.userInfo
  83. this.setData({
  84. userInfo: e.detail.userInfo,
  85. hasUserInfo: true
  86. })
  87. }
  88. })