总目录
目录
第一步,下载微信开发工具
链接: 微信开发工具下载. 我是下载这个
第二步,安装
第三步,扫码进入开发工具
第四步,新建项目
注册小程序账号,获取AppID
链接: 小程序注册. 注册完成进入
如何找到appid
在设置—>最下面
后台选java开发,我没选云开发
第五步,开发代码
获取用户信息代码
// login.wxml
<view class="box">
<button class="btn" catchtap="GetUserInfo">获取用户信息</button>
</view>
// login.js
// pages/login/login.js
Page({
/**
* 页面的初始数据
*/
data: {
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
},
GetUserInfo(e) {
wx.getUserProfile({
desc: '正在获取',//不写不弹提示框
success: function (res) {
console.log('获取成功: ', res)
},
fail: function (err) {
console.log("获取失败: ", err)
}
})
},
})
展示图
点击允许,就会获取到用户得基本信息
获取用户得openid代码
小程序代码
// login.wxml
<view class="box">
<button class="btn" catchtap="Getopenid">获取用户openid</button>
</view>
// login.js
//获取应用实例
const app = getApp()
// pages/login/login.js
Page({
/**
* 页面的初始数据
*/
data: {
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
},
Getopenid(e) {
wx.login({
success: function (res) {
wx.request({
// app.globalData.url这个是app.js里面写的全局值,在下图,这是为了后期上线改路径用的,这个app.在这段代码的最上面
//这个res.code每次调用wx.Login的时候都是不一样的,所以保存code是没有用的
url: app.globalData.url + '/Login/testopenid?code=' + res.code,
method: 'post',
header: {
'content-type': 'application/SelectNews' // 默认值
},
success: res => {
// 获取到用户的 openid
// console.log("用户的openid:" + res.data);
console.log(res)
}
})
}
})
},
})
// app.js
App({
onLaunch() {
// 展示本地存储能力
const logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
// 登录
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
}
})
},
globalData: {
// url: 'https://sale.sss.com',//这是上线后的地址
url: 'http://localhost:9528/',
userInfo: null
}
})
openid Java代码
package com.example.zzy.Controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* \*
* \* User: zzy
* \* Date: 2021/1/25
* \* Time: 11:21
* \* Description:
* \
*/
@RestController
@RequestMapping("/Login")
public class UserController {
@ResponseBody
@RequestMapping("testopenid")
public String getUserInfo(@RequestParam(name = "code") String code) throws Exception {
System.out.println("code" + code);
String url = "https://api.weixin.qq.com/sns/jscode2session";
url += "?appid=wx*******";//自己的appid
url += "&secret=ae*******";//自己的appSecret
url += "&js_code=" + code;
url += "&grant_type=authorization_code";
url += "&connect_redirect=1";
String res = null;
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// DefaultHttpClient();
HttpGet httpget = new HttpGet(url); //GET方式
CloseableHttpResponse response = null;
// 配置信息
RequestConfig requestConfig = RequestConfig.custom() // 设置连接超时时间(单位毫秒)
.setConnectTimeout(5000) // 设置请求超时时间(单位毫秒)
.setConnectionRequestTimeout(5000) // socket读写超时时间(单位毫秒)
.setSocketTimeout(5000) // 设置是否允许重定向(默认为true)
.setRedirectsEnabled(false).build(); // 将上面的配置信息 运用到这个Get请求里
httpget.setConfig(requestConfig); // 由客户端执行(发送)Get请求
response = httpClient.execute(httpget); // 从响应模型中获取响应实体
HttpEntity responseEntity = response.getEntity();
System.out.println("响应状态为:" + response.getStatusLine());
if (responseEntity != null) {
res = EntityUtils.toString(responseEntity);
System.out.println("响应内容长度为:" + responseEntity.getContentLength());
System.out.println("响应内容为:" + res);
}
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
JSONObject jo = JSON.parseObject(res);
String openid = jo.getString("openid");
System.out.println("openid==" + openid);
return openid;
}
}
如何获取你自己小程序的appid,appSecret
链接: 小程序注册. 注册完成进入
点击重置就可以获取到APPsecret了
展示图
code的问题
这个res.code每次调用wx.Login的时候都是不一样的,所以保存code是没有用的,code就是用来查询openid用的,只能用一次
openid的坑
openid就和每个人的身份证号一样,但是但是,他是根据小程序的appid和APPsecret来获取的,如果你用其他的小程序获取同一个用户的openid,就会获取另一个openid
第六步,上传
提交就行了
这个测试备注就是填一些你小程序的问题,就是备注一下,比如你有一个模块没写好但是要先上传,你就可以备注一下,要不然你有模块不能点击的话是不会通过审核的(过不过就看测试员的心情了。。。。)
通过审核后就可以点击上线了
其他
小程序上线跨域问题
开发管理-开发设置-下面