index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. Component({
  2. /**
  3. * 组件的属性列表
  4. */
  5. properties: {},
  6. /**
  7. * 组件的初始数据
  8. */
  9. data: {
  10. booShow: false,
  11. actions: [
  12. {
  13. name: '呼叫',
  14. value: 'makePhoneCall'
  15. },
  16. {
  17. name: '复制号码',
  18. value: 'setClipboardData'
  19. },
  20. {
  21. name: '添加到手机通讯录',
  22. value: 'addPhoneContact'
  23. }
  24. ],
  25. businessInfo: {
  26. phone: '13429176706'
  27. }
  28. },
  29. /**
  30. * 组件的方法列表
  31. */
  32. methods: {
  33. showActionSheet() {
  34. this.setData({ booShow: true })
  35. },
  36. hideActionSheet() {
  37. this.setData({ booShow: false })
  38. },
  39. async onSelect(event) {
  40. const { value } = event.detail
  41. const { businessInfo } = this.data
  42. this.hideActionSheet()
  43. switch (value) {
  44. case 'makePhoneCall':
  45. try {
  46. await wx.makePhoneCall({ phoneNumber: businessInfo.phone })
  47. } catch (err) {}
  48. break
  49. case 'setClipboardData':
  50. try {
  51. await wx.setClipboardData({ data: businessInfo.phone })
  52. } catch (err) {}
  53. break
  54. case 'addPhoneContact':
  55. this.handleGetSetting()
  56. break
  57. default:
  58. }
  59. },
  60. // 获取添加手机通讯录联系人权限
  61. async handleGetSetting() {
  62. const that = this
  63. try {
  64. const { errMsg, authSetting } = await wx.getSetting()
  65. if (errMsg === 'getSetting:ok') {
  66. if (authSetting['scope.addPhoneContact']) {
  67. await that.addPhoneContactBridge()
  68. return
  69. }
  70. }
  71. } catch (err) {}
  72. try {
  73. await wx.authorize({ scope: 'scope.addPhoneContact' })
  74. await that.addPhoneContactBridge()
  75. } catch (err) {
  76. wx.showModal({
  77. title: '提示',
  78. content: '未开启添加手机通讯录联系人权限,去设置中打开',
  79. success(res) {
  80. if (res.confirm) {
  81. that.openSetting()
  82. }
  83. }
  84. })
  85. }
  86. },
  87. // 去小程序自带设置页:返回
  88. async openSetting() {
  89. try {
  90. const openSettingData = await wx.openSetting()
  91. if (openSettingData.authSetting['scope.addPhoneContact']) {
  92. await this.addPhoneContactBridge()
  93. }
  94. } catch (err) {}
  95. },
  96. async addPhoneContactBridge() {
  97. const { businessInfo } = this.data
  98. try {
  99. await wx.addPhoneContact({
  100. firstName: 'businessInfo.phone',
  101. mobilePhoneNumber: businessInfo.phone
  102. })
  103. } catch (err) {}
  104. },
  105. async openLocationBridge() {
  106. try {
  107. // 测试 latitude: 30.25727 longitude: 120.20523
  108. await wx.openLocation({
  109. latitude: 30.25727,
  110. longitude: 120.20523,
  111. name: '浙江省杭州市余杭区'
  112. })
  113. } catch (err) {}
  114. }
  115. }
  116. })