赞
踩
1.在utils文件夹新建request.js,内容如下:
const app = getApp() const host = '自己项目的线上接口地址' /* * POST请求 * URL: 接口地址 * postData: 参数 * doSuccess: 成功的回调函数 * doFail: 失败的回调函数 * 请求头,参数类型,回调事件逻辑等根据自己的项目而定啦 * */ function request(url, postData, doSuccess, doFail) { wx.showLoading({ title: '加载中' }) wx.request({ url: host + url, header: { "content-type": "application/x-www-form-urlencoded" }, data: { postData }, method: 'POST', success: function (res) { wx.hideLoading() doSuccess(res.data) }, fail: function (error) { wx.hideLoading() wx.showModal({ title: '提示', content: '请求失败' }) doFail() } }) } export { request }
2.在页面中使用
wxml:
<view bindtap="searchOrder" >查询</view>
js:
import { request } from '../../utils/request' Page({ searchOrder(e) { const searchParams = { // 参数根据后端接口而定哦 "companyName": this.data.companyName, "searchName": this.data.orderNum, } request('getXWGJDeliveryList.do',searchParams , this.success, this.fail) }, success: function (res) { const _this = this; _this.setData({ listData: res.data, }) }, fail: function () { }, })
1.在utils文件夹新建request.js,内容如下:
const app = getApp() const host = '自己项目的线上接口地址' const fetch = (params = {}) => { params.header = { 'content-type': 'application/x-www-form-urlencoded', // 'token': app.globalData.token || '' } if (params.url.startsWith('/')) { params.url = host + params.url } return wx.request({...params}).then(({res: {code, message, data}}) =>{ if (res.code === 200) { return Promise.resolve(data) } return Promise.reject(message) }) } export { fetch }
新建一个api文件夹,与utils文件夹同级,根据自己模块新建对应的js文件,内容如下:
import { fetch } from '../utils/request'
export function postSearch(data) {
return fetch({
method: 'POST',
url: '',
data
})
}
页面使用:
js:
import { postSearch } from '../../api/search'
Page({
postSearch()
.then(({ dataList }) => this.upData({ sysParamList: dataList }))
.then(() => {
this.upData({
keyList,
formData: { keys: keyList }
})
})
<view class="order_content" bindtap="jumpOder">
jumpOder(e){
wx.navigateTo({
url: '../order/order'
})
},
1.字符串类型:
<view class="order_content" bindtap="jumpOder" data-id="{{id}}">
jumpOder(e){
wx.navigateTo({
url: '../order/order?id='+ e.currentTarget.dataset.id
})
},
另一个页面接收数据:
onLoad: function (options) {
const _this = this
_this.setData({
id: options.id
})
},
2.对象,数组类型:
<view class="order_content" bindtap="jumpOder" data-item="{{item}}">
需要先把对象或者数组转成json字符串
jumpOder(e){
const item = JSON.stringify(e.currentTarget.dataset.item)
wx.navigateTo({
url: '../order/order?item ='+ item
})
},
另一个页面接收数据:
onLoad: function (options) {
const _this = this
_this.setData({
item: JSON.parse(options.item)
})
},
这里重要的事情说三遍:一定要认真看文档!一定要认真看文档!!一定要认真看文档!!!
文档:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html
小程序首页 => 设置 => 基本设置 => 微信认证 详情
前端只能获取到加密的数据,需要后端配合解密:
逻辑流程图(侵删):
实际使用:
wxml:
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">获取用户手机号</button>
js:
getPhoneNumber (e) { console.log('errMsg',e.detail.errMsg) console.log('iv',e.detail.iv) console.log('encryptedData',e.detail.encryptedData) /*登录*/ wx.login({ success: res => { console.log('code',res.code) const params = { code: res.code, encryptedData: e.detail.encryptedData, iv: e.detail.iv } request('接口地址', params, this.then, this.catch ) }, then (res) { // wx.setStorage({ // key: 'openid', // data: res.data.openid // }) // wx.setStorage({ // key: 'sessionKey', // data: res.data.sessionKey // }) // wx.setStorageSync( 'sessionKey', res.data.sessionKey) console.log('手机号:' ,res.data.phoneNumber) }, catch () {} }) } },
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。