uniapp applet to obtain WeChat delivery address

introduction

In the mini program mall, it is inevitable to obtain and add the WeChat shipping address. According to the current rules of the mini program, obtain the WeChat shipping address.

Package to obtain WeChat delivery address API

// asyncWx.js

/**
 * 获取用户收货地址 uni.chooseAddress
 */
export const chooseAddress = () => {
    
    
	return new Promise((resolve, reject) => {
    
    
		uni.chooseAddress({
    
    
			success: (res) => {
    
    
				resolve(res)
			},
			fail: (err) => {
    
    
				reject(err)
			}
		})
	})
}

Specific use of API

<template>
	<view @click="getWxAddress">
		<view>添加微信地址</view>
	</view>
</template>

<script>
import {
    
     chooseAddress } from '@/utils/asyncWx.js';
export default {
    
    
	methods:{
    
    
		getWxAddress() {
    
    
			chooseAddress().then(resAddress => {
    
    
				const addressData = {
    
    
					// 收货人
					consignee: resAddress.userName,
					// 手机号
					mobile: resAddress.telNumber,
					// 省
					province: resAddress.provinceName,
					// 市
					city: resAddress.cityName,
					// 区
					region: resAddress.countyName,
					// 详细地址
					detailAddress: resAddress.detailInfo,
				};
			});
		}
	}
}
</script>

Problems that arise during use

The applet reports an error chooseAddress:fail the api need to be declared in …e requiredPrivateInfos field in app.jso

Solution

manifest.json 文件"mp-weixin" 里面加一行配置
"requiredPrivateInfos" : [ "chooseAddress" ]

Precautions

Now the mini program does not require authorization to obtain the WeChat delivery address. After completing the above configuration, you can directly call the interface.

If there are any deficiencies in this article, please feel free to point them out.

Guess you like

Origin blog.csdn.net/m0_64344940/article/details/127545389