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

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

相关推荐
Uyker8 小时前
微信小程序动态效果实战指南:从悬浮云朵到丝滑列表加载
前端·微信小程序·小程序
happyCoder12 小时前
uniapp 微信小程序实现定时消息订阅提醒(前后端)
微信小程序
Uyker1 天前
从零开始制作小程序简单概述
前端·微信小程序·小程序
打小就很皮...1 天前
HBuilder 发行Android(apk包)全流程指南
前端·javascript·微信小程序
说私域1 天前
定制开发开源AI智能名片驱动下的海报工厂S2B2C商城小程序运营策略——基于社群口碑传播与子市场细分的实证研究
人工智能·小程序·开源·零售
说私域2 天前
内容力重塑品牌增长:开源AI大模型驱动下的智能名片与S2B2C商城赋能抖音生态种草范式
人工智能·小程序·开源·零售
前端缘梦2 天前
微信小程序登录方案实践-从账号体系到用户信息存储
前端·微信小程序
coding随想2 天前
2025年小程序开发全解析:技术储备、行业趋势与实战案例
微信小程序
Nueuis2 天前
微信小程序前端面经
前端·微信小程序·小程序
轩1152 天前
实现仿中国婚博会微信小程序
微信小程序·小程序