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

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

相关推荐
ON.LIN1 天前
云边去水印小程序详细搭建教程(附源码)
小程序
2501_915909061 天前
iOS test 测试怎么做?功能、性能、兼容、稳定与安全五类测试指南
android·ios·小程序·https·uni-app·iphone·webview
deng1hao1 天前
2026小微企业企业邮箱选择指南:低成本高性价比方案
网络·科技·小程序
Ai-_Man1 天前
从AI公式到可编辑文档:MathType格式转换技术分析教程
人工智能·ai·小程序
show4332 天前
2026视频处理小程序技术选型指南:链接解析+OCR+ASR+AI配音多引擎对比
小程序·ocr·音视频
凡科网小帆2 天前
2026小程序商城平台哪家强,小程序商城平台选型怎么做
大数据·小程序·小程序商城平台
游戏开发爱好者82 天前
App Store 上传 IPA 自动化,.p8 密钥认证与 CI/CD 接入实战
android·运维·ci/cd·小程序·uni-app·自动化·iphone
Greg_Zhong2 天前
微信小程序 + 腾讯云人体分析:从 0 到 1 实现 AI 抠图打卡合照(细节待更新~)
人工智能·微信小程序·腾讯云·ai抠图-ai打卡拍照
游戏开发爱好者82 天前
iOS 推送怎么配置,APNs 推送证书、设备库与群发
android·ios·小程序·https·uni-app·iphone·webview
snow@li2 天前
小程序:200知识点/速查手册
小程序