1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- //index.js
- //获取应用实例
- const app = getApp()
- Page({
- data: {
- canIUse: wx.canIUse('button.open-type.getUserInfo'),
- userInfo: {},
- hasUserInfo: false,
- // 模板列表
- templateList: [
- {
- name: '模板一',
- url: '/pages/images/template/1.jpg'
- },
- {
- name: '模板二',
- url: '/pages/images/template/2.jpg'
- },
- {
- name: '模板三',
- url: '/pages/images/template/3.jpg'
- },
- {
- name: '模板四',
- url: '/pages/images/template/4.jpg'
- },
- {
- name: '模板五',
- url: '/pages/images/template/5.jpg'
- },
- ],
- form: {
- phone: '17319450324'
- },
- // 二维码
- qrcodeUrl:'/pages/images/qrcode.jpg'
- },
- // 事件处理函数
- imageChange: function (e) {
- if (e) {
- this.setData({ "form.template": this.data.templateList[e.detail.value].name })
- this.setData({ "form.imgurl": this.data.templateList[e.detail.value].url })
- } else {
- this.setData({ "form.template": this.data.templateList[0].name })
- this.setData({ "form.imgurl": this.data.templateList[0].url })
- }
- this.setData({ "form.imgIndex": e ? e.detail.value : 0 })
- },
- onLoad: function () {
- // 模板默认选择
- this.imageChange();
- if (app.globalData.userInfo) {
- this.setData({
- userInfo: app.globalData.userInfo,
- hasUserInfo: true
- })
- } else if (this.data.canIUse) {
- // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
- // 所以此处加入 callback 以防止这种情况
- app.userInfoReadyCallback = res => {
- console.log(app.globalData.userInfo);
- this.setData({
- userInfo: res.userInfo,
- hasUserInfo: true
- })
- }
- } else {
- // 在没有 open-type=getUserInfo 版本的兼容处理
- wx.getUserInfo({
- success: res => {
- app.globalData.userInfo = res.userInfo
- this.setData({
- userInfo: res.userInfo,
- hasUserInfo: true
- })
- }
- })
- }
- },
- getUserInfo: function (e) {
- console.log(e)
- app.globalData.userInfo = e.detail.userInfo
- this.setData({
- userInfo: e.detail.userInfo,
- hasUserInfo: true
- })
- }
- })
|