java对接微信小程序(登录&获取用户信息)
需求说明:
用户通过小程序登录,进入到平台系统,进行各功能操作;
解决方案:
相关代码:
首先我们需要用到 http工具类 方便后续的接口调用: import org.apache.http.NameValuePair; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; public class HttpClientUtils { final static int TIMEOUT = 1000; final static int TIMEOUT_MSEC = 5 * 1000; public static String doPost(String url, Map<String, String> paramMap) throws IOException { // 创建Httpclient对象 CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null; String resultString = ""; try { // 创建Http Post请求 HttpPost httpPost = new HttpPost(url); // 创建参数列表 if (paramMap != null) { List<NameValuePair> paramList = new ArrayList<>(); for (Map.Entry<String, String> param : paramMap.entrySet()) { paramList.add(new BasicNameValuePair(param.getKey(), param.getValue())); } // 模拟表单 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList); httpPost.setEntity(entity); } httpPost.setConfig(builderRequestConfig()); // 执行http请求 response = httpClient.execute(httpPost); resultString = EntityUtils.toString(response.getEntity(), "UTF-8"); } catch (Exception e) { throw e; } finally { try { response.close(); } catch (IOException e) { throw e; } } return resultString; } private static RequestConfig builderRequestConfig() { return RequestConfig.custom() .setConnectTimeout(TIMEOUT_MSEC) .setConnectionRequestTimeout(TIMEOUT_MSEC) .setSocketTimeout(TIMEOUT_MSEC).build(); } }
小程序用户表
CREATE TABLE `wechat_user` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `nickname` varchar(100) DEFAULT NULL COMMENT 用户昵称, `avatar_url` varchar(500) DEFAULT NULL COMMENT 用户头像, `gender` int(11) DEFAULT NULL COMMENT 性别 0-未知、1-男性、2-女性, `country` varchar(100) DEFAULT NULL COMMENT 所在国家, `province` varchar(100) DEFAULT NULL COMMENT 省份, `city` varchar(100) DEFAULT NULL COMMENT 城市, `mobile` varchar(100) DEFAULT NULL COMMENT 手机号码, `open_id` varchar(100) NOT NULL COMMENT 小程序openId, `union_id` varchar(100) DEFAULT COMMENT 小程序unionId, `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 插入时间, `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 更新时间, PRIMARY KEY (`id`), KEY `idx_open_id` (`open_id`), KEY `idx_union_id` (`union_id`), KEY `idx_mobile` (`mobile`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT=小程序用户表;
dto
主要代码:
controller
mapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.mmc.aircraftsystemserver.api.wechet.pojo.WechatUser; import org.apache.ibatis.annotations.Mapper; @Mapper public interface WechatMapper extends BaseMapper<WechatUser> { }
impl
注意:
上一篇:
uniapp开发微信小程序-2.页面制作
下一篇:
前端如何使用微信支付