收藏
回答

openSetting()的问题,我用了打不开设置?

关于为了避免用户取消“获取地址”之后无法再打开,我看教程说是用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);
          }
    }
  


})
最后一次编辑于  2020-07-27
回答关注问题邀请回答
收藏

4 个回答

  • 卡布奇诺赛高
    卡布奇诺赛高
    2020-08-16

    我这也遇上这个问题了,看了上面两位奆佬的解答才知道为啥,我这用的解决方法是设置button的open-type="{{weizhi}}",在fail后设置setData里的weizhi变成openSetting,感觉不是很好,权当抛砖引玉吧。


    2020-08-16
    有用
    回复
  • Rua!
    Rua!
    2020-07-27

    需要用户发起,使用button按钮的open-type设置触发

    2020-07-27
    有用
    回复
  • 杨六郎
    杨六郎
    2020-07-27

    我在开发工具里点了呀。视频教程里在开发工具里是出来设置了,但我的没出来,我搜了一些说这个接口2018年10月改过,但也过了很久了, 应该不是官方的问题,不知道哪写些错了

    2020-07-27
    有用
    回复
  • 微喵网络
    微喵网络
    2020-07-27

    必须是用户确实有点击行为才能调起设置界面

    2020-07-27
    有用
    回复
登录 后发表内容
问题标签