微信小程序:打开设置(权限)页面

想标题的时候冷却了一下,想了想,还是跟着大家叫设置好玩点

很多情况下,我们是需要用户的个人信息的,但是如果用户不小心在授权弹窗中点击了false,那么可能就会打断我们的任何臆想,所以我们在需要的时候就需要设置用户去重新授权,但是测试过后,我们会发现:

授权弹窗只会出现一次

????什么意思?意思是说,当我们点击了拒绝授权后,很抱歉,你每次去点击getuserinfo的时候,返回的是一个false,那么,这就很是尴尬了,在这种情况下,我们需要用到一个api:opensetting:

// pages/T-setting/T-setting.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasuserinfo: "未获取",
    pageNo: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    wx.getUserInfo({
      success: function (getuserinfo) {
        that.data.hasuserinfo = true;
        that.setData(that.data);
      },
      fail: function (fres) {
        that.data.hasuserinfo = false;
        that.setData(that.data);
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  clickme: function () {
    let that=this;
    wx.openSetting({
      success: function (osrs) {
        // 出发条件是返回的时候
console.log("Aaaa");
        wx.getUserInfo({
          success: function (getuserinfo) {
            that.data.hasuserinfo = true;
            that.setData(that.data);
          },
          fail: function (fres) {
            that.data.hasuserinfo = false;
            that.setData(that.data);
          }
        })
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

})
实验过上面的代码后。很轻松的就发现。当我们被用户拒绝,。但是有不得不需要某些数据的时候,我们需要的,就是打开用户的setting,然后让用户去勾选某项权限,开放给我们,如果用户实在不给。。。。。感觉一直打开setting也是个不错的选择呢。。。。

好的,您想了解关于如何编写微信小程序页面打开微信客服接口的相关内容吗?以下是具体步骤: 1. 打开微信小程序开发者工具,创建一个新的小程序项目; 2. 在项目的 app.json 文件中,添加以下代码: ``` { "pages": [ "index" ], "window": { "backgroundTextStyle": "light", "navigationBarBackgroundColor": "#fff", "navigationBarTitleText": "打开微信客服页面", "navigationBarTextStyle": "black" }, "tabBar": { "color": "#999999", "selectedColor": "#4e4e4e", "borderStyle": "white", "backgroundColor": "#ffffff", "list": [{ "pagePath": "pages/index/index", "iconPath": "image/icon_home.png", "selectedIconPath": "image/icon_home_HL.png", "text": "首页" }] }, "permission": { "scope.userLocation": { "desc": "使用您的位置信息" } } } ``` 这段代码中,我们定义了小程序的页面路由和导航栏样式,以及添加了一个“首页”标签页。同时,为了打开微信客服页面需要获取用户的位置信息,在 permission 中声明了相关权限。 3. 在 pages 目录下,创建一个名为 index 的页面,在 index.js 文件中,添加以下代码: ``` Page({ data: { longitude: "", latitude: "" }, onLoad: function (options) { wx.getLocation({ type: "wgs84", success: (res) => { console.log("获取位置信息成功", res); this.setData({ longitude: res.longitude, latitude: res.latitude }); }, fail: () => { console.log("获取位置信息失败"); } }); }, toService: function () { wx.openCustomerService({ corpId: "", // 企业号的corpid,非必填项 onSuccess: function (res) { console.log('成功打开客服页面', res); }, onFail: function (res) { console.log('打开客服页面失败', res); } }); } }) ``` 在这段代码中,我们在 onLoad 生命周期中调用 wx.getLocation API 获取用户的位置信息,然后在 toService 方法中调用 wx.openCustomerService API 打开微信客服页面。其中,corpId 是企业号的 corpid,如果不是企业号,该参数不需要填写。在 onSuccess 和 onFail 回调函数中,我们可以处理打开客服页面成功或失败的逻辑。 4. 在 index.wxml 文件中,添加以下代码: ``` <view class="container" bindtap="toService"> <text class="title">点击这里联系客服</text> </view> ``` 这段代码中,我们在小程序页面中添加了一个触发打开微信客服页面的按钮。 5. 在 index.wxss 文件中,添加以下代码: ``` .container { display: flex; flex-direction: column; justify-content: center; align-items: center; height: 100%; } .title { font-size: 16px; font-weight: bold; color: #333; margin-bottom: 20px; } ``` 这段代码中,我们定义了小程序页面的样式,使其居中显示,并设置了按钮的样式。 到此为止,一个简单的微信小程序页面打开微信客服接口的示例就完成了。现在,您可以点击按钮尝试打开微信客服页面了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值