微信小程序的开发及问题解决

HttpClient

测试例子

java 复制代码
@SpringBootTest
public class HttpClientTest {

    /**
     * 测试通过httpclient发送get方式的请求
     */
    @Test
    public void testGET() throws IOException {
        //创建httpclient对象
        CloseableHttpClient httpClient= HttpClients.createDefault();

        //创建请求对象
        HttpGet httpGet=new HttpGet("http://localhost:8080/user/shop/status");

        //发送请求,接受响应结果
        CloseableHttpResponse response=httpClient.execute(httpGet);

        //获取服务端返回的状态码
        int statusCode=response.getStatusLine().getStatusCode();
        System.out.println("服务端返回的状态码为:"+statusCode);

        HttpEntity entity=response.getEntity();
        String body= EntityUtils.toString(entity);
        System.out.println("服务端返回的数据为:"+body);

        //关闭资源
        response.close();
        httpClient.close();
    }

    /**
     * 测试通过httpclient发送post方式的请求
     */
    @Test
    public void testPOST() throws JSONException, IOException {
        //创建httpclient对象
        CloseableHttpClient httpClient=HttpClients.createDefault();

        //创建请求对象
        HttpPost httpPost = new HttpPost("http://localhost:8080/admin/employee/login");

        JSONObject jsonObject=new JSONObject();
        jsonObject.put("username","admin");
        jsonObject.put("password","123456");

        StringEntity entity=new StringEntity(jsonObject.toString());

        //指定请求编码方式
        entity.setContentEncoding("utf-8");

        //数据格式
        entity.setContentType("application/json");
        httpPost.setEntity(entity);

        //发送请求
        CloseableHttpResponse response=httpClient.execute(httpPost);

        //解析返回结果
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println("响应码为:"+statusCode);

        HttpEntity entity1=response.getEntity();
        String body = EntityUtils.toString(entity1);
        System.out.println("响应数据为:"+body);

        //关闭资源
        response.close();
        httpClient.close();
    }
}

微信小程序开发

直接申请使用测试号,记住这两项

下载微信开发者工具下载 / 稳定版更新日志

下载后打开微信开发者工具创建小程序

入门案例

实例代码

index.js

javascript 复制代码
// index.js
Page({
  data: {
    msg: "hello world",
		nickName: '',
		code:'',
		result:'',
  },

  //获取微信用户的头像和昵称
  getUserInfo() {
    wx.getUserProfile({
      desc: "获取用户信息",
      success: (res) => {
        console.log(res.userInfo);
        //为数据赋值
        this.setData({
          nickName: res.userInfo.nickName,
          url: res.userInfo.avatarUrl,
        });
      },
    });
	},
	
	//微信登录,获取微信用户的授权码
	wxLogin(){
		wx.login({
			success: (res) => {
				console.log(res.code)
				this.setData({
					code:res.code
				})
			},
		})
	},

	//发送请求
	sendRequest(){
		wx.request({
			url: 'http://localhost:8080/user/shop/status',
			method:'GET',
			success:(res)=>{
				console.log(res.data)
				this.setData({
					result:res.data
				})
			}
		})
	}
});

index.wxml

javascript 复制代码
<!-- index.wxml -->
<navigation-bar title="Weixin" back="{{false}}" color="black" background="#FFF"></navigation-bar>
<scroll-view class="scrollarea" scroll-y type="list">
		<view class="container">
				<!-- <view>{{msg}}</view> -->
				<view>
						<button bindtap="getUserInfo" type="primary">获取用户信息</button>
						昵称:{{nickName}}
						<image style="width:100px;height:100px;" src="{{url}}" />
				</view>

				<view>
					<button bind:tap="wxLogin" type="warn">微信登录</button>
					授权码:{{code}}
				</view>

				<view>
					<button bindtap="sendRequest" type="primary">发送请求</button>
					返回数据:{{result}}
				</view>
		</view>
</scroll-view>

使用微信开发者工具可能遇到的问题

1、页面空白不显示

将微信开发者工具升级到最新版

2、微信开发者工具(微信小程序开发工具)写代码的时候没有组件提示补全也没有代码缩进(安装插件)

1、打开vscode安装插件

这个要安装2.2.2版本,2.3版本以上无法使用

2、然后打开微信开发者工具的拓展

点击

导入已安装的vscode拓展

wxml格式化功能:F1 或者 CMD + Shift + P 输入 format wxml 命令 或者右键菜单,也可以配置 wxmlConfig.onSaveFormat 开启保存后自动格式化(每次保存代码后会自动格式化)

3、微信开发者工具获取微信用户昵称与头像没有弹窗

javascript 复制代码
//获取微信用户的头像和昵称
  getUserInfo() {
    wx.getUserProfile({
      desc: "获取用户信息",
      success: (res) => {
        console.log(res.userInfo);
        //为数据赋值
        this.setData({
          nickName: res.userInfo.nickName,
          url: res.userInfo.avatarUrl,
        });
      },
    });

把调试基础库改为2.27.0以下的版本

4、微信开发者工具向后端请求后回复 http://localhost:8080 不在以下 request 合法域名列表中

javascript 复制代码
	sendRequest(){
		wx.request({
			url: 'http://localhost:8080/user/shop/status',
			method:'GET',
			success:(res)=>{
				console.log(res.data)
				this.setData({
					result:res.data
				})
			}
		})
	}

解决

再次尝试,解决

5、如何切换小程序编译的页面

相关推荐
fxss14 小时前
宝宝学识屋有小程序了
微信小程序
拖孩16 小时前
又又又用 AI 写了个小程序:我站起来猛蹬 AI,使劲薅羊毛
前端·后端·微信小程序
daguanren21 小时前
await nextTick() 之后,页面为什么还没变?读 weapp-vite / Wevu 的三段更新链路
微信小程序·vite
FX1317887KP_2 天前
KET/PET/FCE 口语答案说多长才合适?长度参考与展开方法
小程序·ket口语·pet口语·fce备考·剑桥英语
律宏阔2 天前
微信小程序 + Taro + TanStack Query 缓存与页面返回刷新踩坑实录
前端·微信小程序·taro
千寻技术帮2 天前
微信小程序+Spring Boot 烘焙商城系统(附完整源码+数据库+部署文档)50053
数据库·spring boot·微信小程序·烘焙商城
码云之上2 天前
微信小程序设备指纹实践,删了重装还能认出这台手机吗?
前端·安全·微信小程序
源码手记3 天前
基于微信小程序的相亲交友智能匹配系统-附源码
微信小程序·小程序·交友
源码手记3 天前
基于微信小程序的图书馆座位预约系统(程序+文档)
微信小程序·小程序
aimartec3 天前
企业小程序与APP定制源码买断工程实践:三层解耦架构、状态机防呆与平滑迁移清单
小程序·架构