微信小程序实用代码段(收藏版)(2)

// pages/index/index.js const app = getApp() Page({ data: { token:'' }, onGotUserInfo: function (e) { var that = this if (this.data.token != 'ok' && app.globalData.token != 'ok') { wx.getSetting({ success: (res) => { //箭头函数为了处理this的指向问题 if (res.authSetting["scope.userInfo"]) { wx.login({ success: function (data) { console.log('获取登录 Code:' + data.code) var postData = { code: data.code }; wx.request({ url: 'https://m.renyiwenzhen.com/rymember.php?mod=xcxlogin&code=' + postData.code + '&nickname=' + e.detail.userInfo.nickName, data: {}, header: { 'content-type': 'application/json' }, success: function (res) { console.log(res.data); that.data.token='ok'; wx.setStorage({ key: "unionid", data: res.data.unionid }) wx.navigateTo({ url: e.target.dataset.url }) }, fail: function () { console.log('1'); } }) }, fail: function () { console.log('登录获取Code失败!'); } }) } } }) } else{ wx.navigateTo({ url: e.target.dataset.url }) } } })

六、发送请求

wx.request({ url: 'https://m.renyiwenzhen.com/xcx_ajax.php?action=babylist', //仅为示例,并非真实的接口地址 method: 'post', data: { unionid: uni }, header: { 'content-type': 'application/x-www-form-urlencoded' // 默认值 }, success(res) { // console.log(uni) console.log(res.data) that.setData({ list: res.data.bblist }) } })

七、标题栏及底部栏

全局标题栏

"window": { "backgroundTextStyle": "light", "navigationBarBackgroundColor": "#3EC8C8", "navigationBarTitleText": "乳孕呵护", "navigationBarTextStyle": "white" }

局部标题栏

{ "usingComponents": {}, "navigationBarBackgroundColor": "#fff", "navigationBarTextStyle": "black", "navigationBarTitleText": "附近医院" }

全局底部栏

"tabBar": { "color": "#e4e4e4", "selectedColor": "#333", "list": [ { "pagePath": "pages/index/index", "text": "发现", "iconPath": "./images/find.png", "selectedIconPath": "./images/finded.png" }, { "pagePath": "pages/his/his", "text": "医院", "iconPath": "./images/his.png", "selectedIconPath": "./images/hised.png" }, { "pagePath": "pages/stu/stu", "text": "经验", "iconPath": "./images/stu.png", "selectedIconPath": "./images/stued.png" }, { "pagePath": "pages/my/my", "text": "我的", "iconPath": "./images/my.png", "selectedIconPath": "./images/myed.png" } ] }

八、navigator

1、wxml

<navigator url="/pages/hishome/hishome" open-type="navigate" hover-class="none"> 底部栏没有的路由 </navigator> <navigator open-type="switchTab" url="/pages/his/his" hover-class="none"> 底部栏有的路由 </navigator>

2、js

go: function (e) { wx.navigateTo({ url: '../eatxq/eatxq?id=' + e.currentTarget.dataset.id + "&name=" + e.currentTarget.dataset.name }) }

九、加载条

<loading hidden="{{onff}}">加载中</loading> <view>页面</view>

加载完成true

wx.request({ url: 'https://m.renyiwenzhen.com/xcx_ajax.php?action=caneatsearch', method: 'post', header: { 'content-type': 'application/x-www-form-urlencoded' // 默认值 }, data: { search: options.search }, success(res) { that.setData({ list: res.data.fllist, onff: true }) } })

十、富文本处理

<view> <rich-text nodes="{{msg}}" ></rich-text> </view>

利用正则修改收到的数据

wx.request({ url: 'https://m.renyiwenzhen.com/xcx_ajax.php?action=cjdetail', method: 'post', data: { id: options.id }, header: { 'content-type': 'application/x-www-form-urlencoded' // 默认值 }, success(res) { that.setData({ msg: res.data.cjmag.cjxq.replace(/\<p>/g, "<p>") }) } })

十一、filter过滤数据

1、在根目录下的utils文件夹里创建一个名为filter.wxs文件 2、写入自己要定义的条件

var xb=function (v) { var xingb='' if(v==1){ xingb="男宝宝" } else{ xingb="女宝宝" } return xingb } module.exports = { xb:xb }

3、在页面中引入使用

<wxs src="" module="filter" /> <view><text>{{filter.xb(isxb)}}</text></view>

十二、检测版本更新

app.js

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/ec2841f2f9c1bed507b40d5ed382932b.html