资讯专栏INFORMATION COLUMN

小程序 wepy 用户取消授权以及取消获取地理位置后的处理方法

wqj97 / 3168人阅读

摘要:在里使用进行小程序页面授权,里面包含了用户点击取消的重新授权方案警告您点击了拒绝授权将无法正常显示个人信息点击确定重新获取授权。

在wepy里使用进行小程序页面授权,里面包含了用户点击取消的重新授权方案:

</>复制代码

  1. //auth.js
  2. /*
  3. * @Author: Porco_Mar
  4. * @Date: 2018-04-11 15:49:55
  5. * @Last Modified by: Porco_Mar
  6. * @Last Modified time: 2018-04-18 10:43:36
  7. */
  8. import wepy from "wepy"
  9. export const _timer = (context) => {
  10. return new Promise((resolve, reject) => {
  11. let _timer = null;
  12. clearInterval(_timer);
  13. _timer = setInterval(() =>{
  14. resolve(author(context))
  15. },500)
  16. context.data.timer = _timer;
  17. })
  18. }
  19. export const author = (context) => {
  20. return new Promise((resolve,reject) => {
  21. var that = context;
  22. wepy.getUserInfo({
  23. success: (res) =>{
  24. var userInfo = res.userInfo;
  25. that.data.userInfo = userInfo;
  26. resolve(res.userInfo)
  27. },
  28. fail: (res) =>{
  29. console.log(".......getUserInfo fail.........")
  30. clearInterval(context.data.timer)
  31. wepy.showModal({
  32. title: "警告",
  33. content: "您点击了拒绝授权,将无法正常显示个人信息,点击确定重新获取授权。",
  34. success:function(res){
  35. if (res.confirm) {
  36. wepy.openSetting({
  37. success: (res) => {
  38. if (res.authSetting["scope.userInfo"] || res.authSetting["scope.userLocation"]){////如果用户重新同意了授权登录
  39. wepy.getUserInfo({
  40. success:function(res){
  41. resolve(res.userInfo)
  42. that.$parent.globalData.userInfo = res.userInfo;
  43. }
  44. })
  45. }
  46. },fail: function(res){
  47. resolve({"avatarUrl":"","nickName":"翠花"})
  48. console.log("没有选择授权")
  49. }
  50. })
  51. }else{
  52. console.log("还是不同意授权")
  53. }
  54. }
  55. })
  56. },
  57. complete: function (res){
  58. }
  59. })
  60. })
  61. }
  62. let isBoolen = true;
  63. export const location = (context) => {
  64. return new Promise((resolve, reject) => {
  65. if(context.$parent.globalData.location != null){
  66. resolve(context.$parent.globalData.location)
  67. console.log("已获取location")
  68. }else{
  69. console.log("没有获取到location ")
  70. wepy.getSetting({
  71. success(res) {
  72. console.log(res)
  73. if(!res.authSetting["scope.userLocation"]) {
  74. wx.showModal({
  75. title: "温馨提醒",
  76. content: "需要获取您的地理位置才能使用小程序",
  77. cancelText: "不使用",
  78. confirmText: "获取位置",
  79. success: function(res) {
  80. if(res.confirm) {
  81. getLocation(context).then((res) => {
  82. // console.log(res)
  83. if (res.code == 1){
  84. if(isBoolen){ //第一次不执行
  85. isBoolen = false;
  86. }else{
  87. wepy.openSetting({ // 点击自带取消定位健会调用这个面板
  88. success: (res) => {
  89. if (res.authSetting["scope.userLocation"]){////如果用户在面板重新同意了授权地理位置
  90. console.log("--有了scope.userLocation--")
  91. resolve(getLocation(context)) //点击面板后再次调用getLocation返回参数
  92. }
  93. },fail: function(res){
  94. console.log("--没有scope.userLocation--")
  95. }
  96. })
  97. }
  98. }else{
  99. resolve(getLocation(context))
  100. }
  101. })
  102. } else if(res.cancel) {
  103. //resolve(getLocation(context))
  104. //不做任何操作
  105. }
  106. }
  107. })
  108. }
  109. }
  110. })
  111. }
  112. })
  113. }
  114. export const getLocation = (context) => {
  115. return new Promise((resolve, reject) => {
  116. wx.getLocation({
  117. type: "wgs84",
  118. success: function(res) {
  119. var latitude = res.latitude
  120. var longitude = res.longitude
  121. var speed = res.speed
  122. var accuracy = res.accuracy
  123. context.$parent.globalData.location = {"code": 0, "latitude":latitude, "longitude":longitude, "speed":speed, "accuracy":accuracy}
  124. resolve(context.$parent.globalData.location)
  125. },
  126. fail: function(res){
  127. resolve({"code": 1, "latitude":"", "longitude":"", "speed":"", "accuracy":""})
  128. }
  129. })
  130. })
  131. }
  132. // index.wepy
  133. import wepy from "wepy"
  134. import {_timer, author, location} from "../utils/auth"
  135. onShow() {
  136. let globalDt = this.$parent.globalData
  137. if(globalDt.userInfo.nickName && globalDt.userInfo.avatarUrl){
  138. this.userInfo = globalDt.userInfo;
  139. }else{
  140. this.getValue(); // 获取userInfo
  141. }
  142. if(globalDt.location === null){
  143. this.getLt(this); // 获取地理位置
  144. }else{
  145. console.log("当前页面获取过location了")
  146. //console.log(globalDt.location)
  147. this.location = globalDt.location;
  148. }
  149. }
  150. async getValue () {
  151. const datam = await _timer(this)
  152. console.log(datam)
  153. this.userInfo = datam;
  154. this.$apply();
  155. }
  156. async getLt (context) {
  157. const local = await location(context)
  158. console.log(local)
  159. this.location = local;
  160. this.$apply()
  161. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/94362.html

相关文章

  • 使用wepy 程序授权点击取消授权失败的方案

    摘要:在里使用进行小程序页面授权,里面包含了用户点击取消的重新授权方案警告您点击了拒绝授权将无法正常显示个人信息点击确定重新获取授权。 在wepy里使用进行小程序页面授权,里面包含了用户点击取消的重新授权方案: //auth.js /* * @Author: Porco_Mar * @Date: 2018-04-11 15:49:55 * @Last Modified by: Por...

    baihe 评论0 收藏0
  • 记录微信程序的坑

    摘要:除官方外的参考文章微信小程序实例创建下发模板消息实例手把手教你开发微信小程序之模版消息开发教你突破小程序模板消息的推送限制获取用户信息接口的废弃问题接口是获取用户信息昵称,头像等的接口,在官方文档上写是即将废弃。 ----------------更新-------------- 2018年10月10日官网3个接口废弃的通知: 1、分享监听接口分享消息给好友时,开发者将无法从callba...

    EastWoodYang 评论0 收藏0
  • 做完程序项目、老板给我加了6k薪资~

    摘要:大家好,我是平头哥联盟的首席填坑官苏南,今天要给大家分享的是最近公司做的一个小程序项目,过程中的一些好的总结和遇到的坑,希望能给其他攻城狮带来些许便利,更希望能做完之后老板给你加薪今天是中秋节的第一天,假日的清晨莫名的醒的特别早,不知道为什 showImg(https://segmentfault.com/img/bVbhAYf?w=1278&h=722);   大家好,我是@IT·平...

    z2xy 评论0 收藏0
  • 滴滴一下,程序专车来了

    摘要:功能三滴滴费用计算古人云细节决定成败,一个良好的微信小程序往往就是一些细节打动人心,居然是模仿,虽做不到百分百,至少还是很希望一模一样。 最近时常感叹道:时间总是那么的快,转瞬即逝。对于像我这种刚入门的小生来讲,技术每天都在更新,框架也层出不穷,有时候还没弄懂这个知识大牛们又推出了更好的技术。当然学习好的技术也是十分重要的。但是在学习之后怎样才能够得到自己想要的呢,一个好的建议便是静...

    SwordFly 评论0 收藏0

发表评论

0条评论

wqj97

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<