普通页面模版
var app = getApp();
var that;
Page({
data: {
},
onLoad(options) {
that = this;
},
onReady() {
that = this;
},
onShareAppMessage() {
},
onShareTimeline() {
},
})
导航
wxml
<view class="nav">
<navigator open-type="navigateBack" hover-class="none">
<image src="/imgs/back.png" mode="" />
navigator>
详情
view>
wxss
.nav {
height: 88rpx;
line-height: 88rpx;
padding-top: 88rpx;
color: #ffffff;
background:
linear-gradient(-90deg, #fd874c 0%, #c81b1b 100%),
linear-gradient(#530b0c, #530b0c);
text-align: center;
position: relative;
}
.nav navigator {
display: flex;
position: absolute;
left: 0;
bottom: -8rpx;
}
.nav navigator image {
width: 44rpx;
height: 44rpx;
margin: 30rpx;
}
流加载JS
Page({
data: {
p: 1, //页数
flow: true, //是否完成加载
loaded: true, //是否加载中
},
onLoad(options) {
that = this;
that.to_load();
},
onReady() {
that = this;
},
onReachBottom() {
var flow = that.data.flow;
var loaded = that.data.loaded;
if (!flow || !loaded) {
return false;
}
var p = that.data.p;
p++; //页数自增1
that.setData({
p: p,
});
that.to_load(); //调用加载方法
},
// 分享
onShareAppMessage() {
},
// 分享
onShareTimeline() {
},
to_load: function () {
var p = that.data.p;
that.setData({
loaded: false,
});
app.request_api("/lists", {
p: p,
}, function (params) {
var list = that.data.lists;
var lists = params.lists;
if (p == 1) {
list = [];
}
if (!lists || typeof (lists) != "object") {
lists = [];
}
list = list.concat(lists); //concat连接数组
var flow = true;
if (list.length == params.len) {
flow = false;
}
that.setData({
lists: list,
flow: flow,
loaded: true,
});
});
},
})
onReachBottom
页面滚动到底部执行的方法
官方文档https://developers.weixin.qq.com/miniprogram/dev/reference/api/Page.html#onReachBottom
app.js
var app;
var that;
App({
onLaunch(e) {
app = that = this;
if (e.path.indexOf("/index/index") === -1) {
app.to_login();
}
app.update();
app.g_share();
},
// 自动更新
update: function () {
if (wx.canIUse && wx.canIUse("getUpdateManager")) {
try {
var udmgr = wx.getUpdateManager();
udmgr.onCheckForUpdate(function (params) {
// 请求完新版本信息的回调
if (params.hasUpdate) {
udmgr.onUpdateReady(function () {
wx.showModal({
title: "更新提示",
content: "新版本已经准备好,是否重启应用?",
success: function (params2) {
if (params2.confirm) {
// 新的版本已经下载好,调用applyUpdate应用新版本并重启
udmgr.applyUpdate();
}
},
});
});
udmgr.onUpdateFailed(function () {
// 新的版本下载失败
wx.showModal({
title: "已经有新版本了哟~",
content: "新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~",
});
});
}
});
} catch (error) {
console.warn(error);
}
} else {
// 此时微信版本太低(一般而言版本都是支持的)
wx.showModal({
title: "溫馨提示",
content: "当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。",
});
}
},
// 重写分享方法
// 需要页面JS存在对应的空方法,否则没有效果
g_share: function () {
// 间接实现全局设置分享内容
wx.onAppRoute(function (params) {
// console.log(params);
// 获取加载的页面
var pages = getCurrentPages();
// 获取当前页面的对象
var view = pages[pages.length - 1];
if (view) {
var data = view.data;
if (!data.is_over_share) {
data.is_over_share = true;
view.onShareAppMessage = app.to_share_msg;
view.onShareTimeline = app.to_share_tl;
console.log("全局分享");
}
}
});
},
// 重写分享方法
to_share_msg: function () {
// 重写分享配置
return {
title: "test",
path: "/pages/index/index",
// imageUrl: app.globalData.s_img,
};
},
// 重写分享方法
to_share_tl: function () {
// 重写分享配置
return {
title: "test",
query: "g_share=tl",
};
},
// openid
to_login: function (callback = false) {
wx.login({
success: function (params) {
if (params.code) {
app.request_api(
"/index/base/login",
{
code: params.code,
},
function (params2) {
app.globalData.openid = params2.openid;
app.globalData.wid = params2.wid;
if (callback != false) {
callback();
}
}
);
}
},
});
},
// 请求接口
request_api: function (action, data = {}, callback = false) {
var show_l = false;
var timer_l = setTimeout(() => {
show_l = true;
wx.showLoading({
title: "加载中...",
mask: true,
});
}, 1500);
var url = app.globalData.api_url + action;
url = url.replace(/\?$/isg, "");
if (typeof (data.p) != "undefined")
url += (url.indexOf("?") === -1 ? "?" : "&") + "p=" + data.p;
data.wid = app.globalData.wid;
wx.request({
url: url,
method: "POST",
data: data,
dataType: "json",
timeout: 6000000,
header: {
"Content-Type": "application/x-www-form-urlencoded",
"Cookie": "session_id",
"X-Requested-With": "XMLHttpRequest",
},
success: function (params) {
clearTimeout(timer_l);
if (show_l) {
wx.hideLoading();
}
if (callback != false) {
callback(params.data);
}
},
});
},
globalData: {
api_url: "http://test.cc",
openid: 0,
wid: 0,
uid: 0,
},
});
PHP
namespace app\index\controller;
use think\Controller;
use think\Db;
class Base extends Controller
{
public $app_id = "***";
public $app_secret = "***";
// openid
public function login($code = "")
{
$times = time();
$code = trim($code);
if (empty($code)) {
$res["msg"] = "need code";
$res["code"] = 1;
return json($res);
}
$res["msg"] = "has code";
$res["code"] = 1;
$param["appid"] = $this->app_id;
$param["secret"] = $this->app_secret;
$param["js_code"] = $code;
$param["grant_type"] = "authorization_code";
$param = http_build_query($param);
$url = "https://api.weixin.qq.com/sns/jscode2session?" . $param;
$response_raw = file_get_contents($url);
$response = json_decode($response_raw, true);
$openid = !empty($response["openid"]) ? $response["openid"] : "";
$session_key = !empty($response["session_key"]) ? $response["session_key"] : "";
$errcode = !empty($response["errcode"]) ? $response["errcode"] : "";
if (!$openid) {
$res["code"] = 1;
$res = (array)$res + $response;
return json($res);
}
if ($errcode == 40029) {
$res["msg"] = "invalid code";
$res["errcode"] = $errcode;
$res["code"] = 1;
} else if ($openid) {
$info = Db::name("wechat")
->field(["session_key"], true)
->where("open_id", $openid)
->find();
$data_db["update_time"] = $times;
// $data_db["session_key"] = $session_key;
if (!empty($info["id"])) {
Db::name("wechat")->where("open_id", $info["open_id"])->update($data_db);
$wid = Db::name("wechat")->where("open_id", $info["open_id"])->value("id");
$res["msg"] = "exist";
$res["openid"] = $openid;
$res["wid"] = $wid;
$res["code"] = 0;
} else {
$data_db["open_id"] = $openid;
$data_db["status"] = 1;
$data_db["create_time"] = $times;
$wid = Db::name("wechat")->insertGetId($data_db);
$res["msg"] = "not exist";
$res["openid"] = $openid;
$res["wid"] = $wid;
$res["code"] = 0;
}
}
return json($res);
}
// 上传
public function upload()
{
set_time_limit(0);
ini_set("max_execution_time", 0);
ini_set("memory_limit", -1);
$name = trim(input("name")); //获取文件名
$file = request()->file("file"); //获取上传的文件
if (!$file) {
$res["msg"] = "文件大小超限";
$res["code"] = 1;
return json($res);
}
$file->checkSize(20000000); //限制大小
$file->checkMime(explode("|", "xls|xlsx")); //限制后缀
$path_ul = "./Uploads/file/"; //移动到框架应用根目录/public/Uploads/目录下
$info = $file->move($path_ul);
if (!$info) {
$res["msg"] = $file->getError();
$res["code"] = 1;
return json($res);
}
if (!$name) {
$name = $info->getInfo("name"); //获取原始文件名
}
$path = $info->getSaveName();
$path = str_replace("\\", "/", $path); //\(反斜杠)替换为/(斜杠)
$path = ltrim($path_ul, ".") . ltrim($path, "/");
$urls = request()->domain() . $path;
$res = [];
$res["name"] = $name;
$res["path"] = $path;
$res["urls"] = $urls;
$res["field"] = $path . "|" . $name;
$res["code"] = 0;
return json($res);
}
}