index.js 2.9 KB

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