【工作中解决的一些问题】

工作中解决的一些问题

1、接口传参要是为空数组时

在这里插入图片描述
使用stuList.removeIf(Objects::isNull);

2、保留小数点后两位
			//保留小数点后两位 第一种
			Float attendanceRate = 3.8822f;
			BigDecimal b = new BigDecimal(attendanceRate);
			float f1 =  b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
			System.out.println(f1);
			//保留小数点后两位 第二种
			DecimalFormat hisFormat = new DecimalFormat("########.##");
			hisFormat.setRoundingMode(RoundingMode.DOWN);
			System.out.println(hisFormat.format(attendanceRate));
3、安装brew
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

选择1-5克隆源,回车,输入Y,再选择1-5镜像源
在这里插入图片描述
在这里插入图片描述

4、安装pip3
curl https://bootstrap.pypa.io/get-pip.py | python3

查看pip版本
看看是本地已经有python2还是python3

pip --version
pip3 --version

在这里插入图片描述

5、安装mysql-client
brew install mysql-client

5.1写入配置文件方法一

echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.bash_profile
export PATH="/usr/local/opt/mysql-client/bin:$PATH"

5.1写入配置文件方法二

vim ~/.bash_profile

输入i,写入

export PATH="/usr/local/opt/mysql-client/bin:$PATH"

按Esc,输入:wq
执行source ./bash_profile

5.2 输入pip3 install mysqlclient

6、Mapper.xml里面的resultType

基本类型:基本类型

Integer getCountSeaClient();
resultType="java.lang.Integer"

数组类型:数组里面的基本类型

List<String> getClientQrLabel();
resultType="java.lang.String"
7、set、list、map
@GetMapping("/test")
	public void test(){
		Set set = new HashSet();
		set.add("我是3");
		set.add("我是1");
		set.add("我是2");
		set.add(null);
		set.add(null);
		System.out.println("set排序:"+set);
		System.out.println(set.stream().iterator());
		for (Object set1:set){
			System.out.println("set遍历:"+set1);
		}
		Set tree = new TreeSet();
		tree.add(3);
		tree.add(1);
		tree.add(2);
		System.out.println("treeSet排序:"+tree);
		List list = new LinkedList();
		list.add(3);
		list.add(1);
		list.add(null);
		list.add(null);
		list.add(2);
		System.out.println("list排序:"+list);
	}
8、微信小程序消息订阅

模版根据微信开发者平台申请的去写MessageVO实体类,下面是封装好的可以根据消息订阅模版直接用的工具类,如需要添加到数据库需要另外新增一个数据库实体类,存放创建消息者、openid(用于不在创建消息时就发送消息,存放openid可以做个定时任务去发送消息)等,以下是我创建的一个存放消息的实体类。
如何获取openid,前端传code就可以获取了

	WxMaJscode2SessionResult wxResult = wxMaService.jsCode2SessionInfo(code);
	openId = wxResult.getOpenid();
package org.springblade.crm.entity;

import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Data;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.Date;
import lombok.EqualsAndHashCode;
import org.springblade.core.tenant.mp.TenantEntity;

/**
 * 任务清单表 实体类
 *
 * @author ljx
 * @since 2023-03-22
 */
@Data
@TableName("bask_task")
@ApiModel(value = "Task对象", description = "任务清单表")
@EqualsAndHashCode(callSuper = true)
public class TaskEntity extends TenantEntity {
	@JsonSerialize(using = ToStringSerializer.class)
	@ApiModelProperty("主键id")
	@TableId(value = "id", type = IdType.AUTO)
	private Long id;
	/**
	 * 销售id
	 */
	@ApiModelProperty(value = "销售id")
	private Long salesId;
	/**
	 * 客户id
	 */
	@ApiModelProperty(value = "客户id")
	private Long clientId;
	/**
	 * 任务标题
	 */
	@ApiModelProperty(value = "任务标题")
	private String taskName;
	/**
	 * 提醒时间
	 */
	@ApiModelProperty(value = "提醒时间")
	private Date taskTime;
	/**
	 * 任务状态
	 */
	@ApiModelProperty(value = "任务状态")
	private String taskType;
	/**
	 * 任务内容
	 */
	@ApiModelProperty(value = "任务内容")
	private String taskContent;
	/**
	 * 是否发送
	 */
	@ApiModelProperty(value = "是否发送")
	private Boolean isSend;
	/**
	 * 微信openid
	 */
	@ApiModelProperty(value = "微信openid")
	private String openid;
}
import lombok.Data;

import java.util.Date;
import java.util.Map;
/**
 * 消息订阅的消息模版
 */
@Data
public class MessageVO {
	private String thing1;
	private String time3;
	private String thing2;
}
import lombok.Data;
import org.springblade.yibu.user.vo.MessageVO;
/**
 * 微信开发者平台消息订阅固定需要用到的参数
 */
@Data
public class WxMsgConfig {
	/*接收者(用户)的 openid*/
	private String touser;

	/*所需下发的订阅模板id*/
	private String templateId;

	/*点击消息后跳转的页面*/
	private String page;

	/*跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版*/
	private String miniprogramState="developer";

	/*进入小程序查看”的语言类型,支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN返回值*/
	private String lang="zh_CN";

	/*模板数据,这里定义为object是希望所有的模板都能使用这个消息配置*/
	private Object data;

	/*前端生成一次性code*/
	private String code;

	/*appId*/
	private String appId;

	/*消息模版*/
	private MessageVO messageVO;
/**
 * 发送消息工具类、需根据不同模版更改
 */
@PostMapping("/wx/msg/sendMessage")
public R sendMessage(@RequestBody WxMsgConfig wxMsgConfig) {
	HttpURLConnection httpConn = null;
	InputStream is = null;
	BufferedReader rd = null;
	String accessToken = null;
	String str = null;

	try {
		wxMaService.switchover(wxMsgConfig.getAppId());
		//获取token  小程序全局唯一后台接口调用凭据
		accessToken = wxMaService.getAccessToken();
		System.out.println("1111111"+accessToken);

		JSONObject xmlData = new JSONObject();
		xmlData.put("touser", wxMsgConfig.getTouser());//接收者(用户)的 openid
		xmlData.put("template_id", wxMsgConfig.getTemplateId());//所需下发的订阅模板id
		xmlData.put("page", wxMsgConfig.getPage());//点击模板卡片后的跳转页面,仅限本小程序内的页面
		xmlData.put("miniprogram_state", wxMsgConfig.getMiniprogramState());//跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
		xmlData.put("lang", wxMsgConfig.getLang());//进入小程序查看”的语言类型,支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN返回值

		/**
		 * 订阅消息参数值内容限制说明
		 thing.DATA:20个以内字符;可汉字、数字、字母或符号组合
		 time.DATA:24小时制时间格式(支持+年月日),支持填时间段,两个时间点之间用“~”符号连接
		 */

		JSONObject data = new JSONObject();
		//业务名称
		JSONObject thing1 = new JSONObject();//character_string4必须和模板消息的字段id一致,以下同样,必须要一致,注意时间格式
		thing1.put("value", wxMsgConfig.getMessageVO().getThing1());
		data.put("thing1", thing1);
		//办理时间
		JSONObject time3 = new JSONObject();
		time3.put("value", wxMsgConfig.getMessageVO().getTime3());
		data.put("time3", time3);
		//办理备注
		JSONObject thing2 = new JSONObject();
		thing2.put("value", wxMsgConfig.getMessageVO().getThing2());
		data.put("thing2", thing2);

		xmlData.put("data", data);//小程序模板数据

		System.out.println("发送模板消息xmlData:" + xmlData);
		URL url = new URL(
			"https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="
				+ accessToken);
		httpConn = (HttpURLConnection) url.openConnection();
		httpConn.setRequestProperty("Host", "https://api.weixin.qq.com");
		// httpConn.setRequestProperty("Content-Length", String.valueOf(xmlData.));
		httpConn.setRequestProperty("Content-Type", "text/xml; charset=\"UTF-8\"");
		httpConn.setRequestMethod("POST");
		httpConn.setDoInput(true);
		httpConn.setDoOutput(true);
		OutputStream out = httpConn.getOutputStream();
		OutputStreamWriter osw = new OutputStreamWriter(out, "UTF-8");
		osw.write(xmlData.toString());
		osw.flush();
		osw.close();
		out.close();
		is = httpConn.getInputStream();
		rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));

		while ((str = rd.readLine()) != null) {
			System.out.println("返回数据:" + str);
		}

	} catch (Exception e) {
		e.printStackTrace();
		System.out.println("发送模板消息失败.." + e.getMessage());
	} finally {
		WxMaConfigHolder.remove();//清理ThreadLocal
	}
	return R.status(true);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值