微信平台服务器开启流程:
参考:https://developers.weixin.qq.com/miniprogram/dev/framework/ability/network.html
首先小程序要想正常使用的话必须要进行服务器域名配置
进入微信公众号平台:https://mp.weixin.qq.com/
点击开发->开发设置->服务器域名
在request合法中中 添加自己的服务器域名
前面会自动有https://
添加完后,即相当于可以通过微信公众号接口访问这个域名
域名前面一定要加https://
关于enroll.js的代码(主要实现了注册的逻辑)
// pages/enroll/enroll.js
Page({
/**
* 页面的初始数据
*/
data: {
username: "",
phonenumber: "",
password: "",
passwordack: "",
},
signin: function (e) {
wx.navigateBack({
delta: 1
})
},
regist: function (e) {
var that = this
var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
if (that.data.username == '') {
wx.showModal({
title: '提示!',
content: '请输入用户名!',
showCancel: false,
success(res) { }
})
} else if (that.data.phonenumber == '') {
wx.showModal({
title: '提示!',
content: '请输入手机号!',
showCancel: false,
success(res) { }
})
} else if (that.data.phonenumber.length != 11) {
wx.showModal({
title: '提示!',
content: '手机号长度有误,请重新输入!',
showCancel: false,
success(res) { }
})
} else if (!myreg.test(that.data.phonenumber)) {
wx.showModal({
title: '提示!',
content: '请输入正确的手机号码!',
showCancel: false,
success(res) { }
})
} else if (that.data.password == '') {
wx.showModal({
title: '提示!',
content: '请输入密码!',
showCancel: false,
success(res) { }
})
} else if (that.data.passwordack == '') {
wx.showModal({
title: '提示!',
content: '请输入确认密码!',
showCancel: false,
success(res) { }
})
} else if (that.data.password != that.data.passwordack) {
wx.showModal({
title: '提示!',
content: '两次输入密码不一致!',
showCancel: false,
success(res) { }
})
} else {
console.log(getApp().globalData.server + '/index.php/Home/User/Sign')
wx.request({
url: getApp().globalData.server + '/index.php/Home/User/Sign', //仅为示例,并非真实的接口地址
data: {
username: that.data.username,
phone: that.data.phonenumber,
password: that.data.password,
password_again: that.data.passwordack,
face_url: getApp().globalData.userInfo.avatarUrl,
},
method: "POST",
header: {
'content-type': "application/x-www-form-urlencoded" // 默认值
},
success(res) {
console.log(res.data)
if (res.data.error_code == 1) {
wx.showModal({
title: '提示!',
content: res.data.msg,
showCancel: false,
success(res) { }
})
} else if (res.data.error_code == 2) {
wx.showModal({
title: '提示!',
content: '两次输入密码不一致!',
showCancel: false,
success(res) { }
})
} else if (res.data.error_code == 3) {
wx.showModal({
title: '提示!',
content: '手机号已被注册!',
showCancel: false,
success(res) { }
})
} else if (res.data.error_code != 0) {
wx.showModal({
title: '哎呀!',
content: '出错了呢!' + res.data.msg,
success: function (res) {
if (res.confirm) {
console.log('用户点击确定')
} else if (res.cancel) {
console.log('用户点击取消')
}
}
})
} else if (res.data.error_code == 0) {
getApp().globalData.user = res.data.data
console.log(getApp().globalData.user)
wx.showModal({
title: '恭喜!',
content: '注册成功!',
showCancel: false,
success(res) { },
complete: function (res) {
wx.reLaunch({
url: "/pages/square/square"
})
}
})
}
},
fail: function (res) {
wx.showModal({
title: '哎呀!',
content: '网络不在状态呢!',
success: function (res) {
if (res.confirm) {
console.log('用户点击确定')
} else if (res.cancel) {
console.log('用户点击取消')
}
}
})
}
})
}
},
usernameInput: function (e) {
this.data.username = e.detail.value
},
phonenumberInput: function (e) {
this.data.phonenumber = e.detail.value
},
passwordInput: function (e) {
this.data.password = e.detail.value
},
passwordInputAck: function (e) {
this.data.passwordack = e.detail.value
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})
如果出现url地址无效问题,则一般是全局变量globalData中的https没加或者写成了http,改成https
如果还是无法访问,则是因为服务器端不支持https,可以拿浏览器访问或者postman直接试验一下是否用https能联通。
ssl加密方式是需要ssl证书服务的
关于宝塔面板开启https
参考:https://blog.csdn.net/msllws/article/details/82255078?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase
步骤如下:
申请
生成证书
之后的效果
出现如下图的密钥和证书,说明证书已经申请好了,可以使用https访问了
成功访问
之后拿微信小程序代码就也可以直接访问了
另外一种方法:
在开发者工具中,打开详情->本地设置->勾选不校验合法域名
打完勾以后就可以。
这样小程序也可以直接用http命令访问。
但是后面要发布时,还是要用https的。
本地用localhost无法连通的时候:
请把地址请求命令换成http
并且勾选不校验合法域名、web-view
如果要在手机上开http服务,要在手机上开调试