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
<?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);
}
}
新人注意事项
1.网络请求失败
注意:正式发布时必须有HTTPS
上图中的报错很可能是因为微信开发工具的配置,当然排查微信开发者工具的前提是只有工具中无法访问,此时控制台中会有如下图的报错
解决方法:把下图中的选择框选中再刷新小程序
刷新按钮位置
2.session_key
微信小程序获取openid接口会返回一个session_key,这个一定不能通过接口返回给小程序,否则会微信小程序后台会出现提示
最后希望微信再也不改获取昵称和头像的接口