关于为了避免用户取消“获取地址”之后无法再打开,我看教程说是用opensetting().
https://www.bilibili.com/video/BV1y7411z7hQ?p=85
但是代码一样还是没用。我用openSetting()怎么还打不开设置,不知道哪错了.
点取消授权,再点button, console确实输出我故意打印的“取消”了但并没有打开设置。
WXML:
获取收货地址
JS.
handleChooseAddress(){
wx.getSetting({
success: (result) => {
// console.log(result);
const scopeAddress=result.authSetting["scope.address"];
if (scopeAddress===true||scopeAddress===undefined){
//console.log(result);
wx.chooseAddress({
success: (result1) => {
console.log(result1);
}
});
}else{
console.log("取消");
wx.openSetting({
success: (result2) => {
wx.chooseAddress({
success: (result3) => {
console.log(result3);
}
});
}
});
}
}
})
}
另一种写法还是不对 :
报错:errMsg: "openSetting:fail errcode 1"}
/*
Promise形式 getSetting
*/
export const getSetting=()=>{
return new Promise((resolve,reject)=>{
wx.getSetting({
withSubscriptions: true,
success: (result)=>{
resolve(result);
},
fail:(err)=>{
reject(err);
}
});
})
}
/*
Promise形式 chooseAddress
*/
export const chooseAddress=()=>{
return new Promise((resolve,reject)=>{
wx.chooseAddress({
withSubscriptions: true,
success: (result)=>{
resolve(result);
},
fail:(err)=>{
reject(err);
}
});
})
}
/*
Promise形式 openSetting
*/
export const openSetting=()=>{
return new Promise((resolve,reject)=>{
wx.openSetting({
withSubscriptions: true,
success: (result)=>{
resolve(result);
},
fail:(err)=>{
reject(err);
}
});
})
}
// pages/cart/index.js
import{getSetting,chooseAddress,openSetting } from "../../utils/asyncWx.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({
/**
* 页面的初始数据
*/
data: {
},
async handleChooseAddress(){
try {
/*
wx.getSetting({
success: (result) => {
// console.log(result);
const scopeAddress=result.authSetting["scope.address"];
if (scopeAddress===true||scopeAddress===undefined){
//console.log(result);
wx.chooseAddress({
success: (result1) => {
console.log(result1);
}
});
}else{
console.log("取消");
wx.openSetting({
success: (result2) => {
wx.chooseAddress({
success: (result3) => {
console.log(result3);
}
});
}
});
}
},
fail:()=>{},
complete:()=>{}
});
*/
const res1=await getSetting();
const scopeAddress=res1.authSetting["scope.address"];
if (scopeAddress===false){
await openSetting();
}
const res2=await chooseAddress();
console.log(res2);
} catch (error) {
console.log(error);
}
}
})
调整后“打开小程序设置页”将支持以下两种实现方式:
button
组件来使用此功能,示例代码如下:<button open-type="openSetting" bindopensetting="callback">打开设置页</button>
wx.openSetting
接口的调用,示例代码如下:<button bindtap="openSetting">打开设置页</button> openSetting() { wx.openSetting()}
我这也遇上这个问题了,看了上面两位奆佬的解答才知道为啥,我这用的解决方法是设置button的open-type="{{weizhi}}",在fail后设置setData里的weizhi变成openSetting,感觉不是很好,权当抛砖引玉吧。
需要用户发起,使用button按钮的open-type设置触发
我在开发工具里点了呀。视频教程里在开发工具里是出来设置了,但我的没出来,我搜了一些说这个接口2018年10月改过,但也过了很久了, 应该不是官方的问题,不知道哪写些错了
必须是用户确实有点击行为才能调起设置界面