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

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、如何切换小程序编译的页面

相关推荐
h_65432102 小时前
微信小程序点击按钮跳转链接并显示
微信小程序·小程序
liyinchi19885 小时前
原生微信小程序 textarea组件placeholder无法换行的问题解决办法
微信小程序·小程序
说私域7 小时前
基于开源链动2+1模式AI智能名片S2B2C商城小程序的低集中度市场运营策略研究
人工智能·小程序·开源·零售
少恭写代码10 小时前
duxapp 2025-03-29 更新 编译结束的复制逻辑等
react native·小程序·taro
suncentwl11 小时前
答题pk小程序道具卡的获取与应用
小程序·答题小程序·知识竞赛
bysjlwdx11 小时前
uniapp婚纱预约小程序
小程序·uni-app
ALLSectorSorft11 小时前
代驾小程序订单系统框架搭建
小程序·代驾小程序
qq_124987075311 小时前
原生小程序+springboot+vue+协同过滤算法的音乐推荐系统(源码+论文+讲解+安装+部署+调试)
java·spring boot·后端·小程序·毕业设计·课程设计·协同过滤
前端极客探险家20 小时前
微信小程序全解析:从入门到实战
微信小程序·小程序