Component({ /** * 组件的属性列表 */ properties: { shopDetail: Object }, /** * 组件的初始数据 */ data: { booShow: false, actions: [ { name: '呼叫', value: 'makePhoneCall' }, { name: '复制号码', value: 'setClipboardData' }, { name: '添加到手机通讯录', value: 'addPhoneContact' } ] }, /** * 组件的方法列表 */ methods: { showActionSheet() { this.setData({ booShow: true }) }, hideActionSheet() { this.setData({ booShow: false }) }, async onSelect(event) { const { value } = event.detail const { shopDetail } = this.data this.hideActionSheet() switch (value) { case 'makePhoneCall': try { await wx.makePhoneCall({ phoneNumber: shopDetail.shop_phone }) } catch (err) {} break case 'setClipboardData': try { await wx.setClipboardData({ data: shopDetail.shop_phone }) } catch (err) {} break case 'addPhoneContact': this.handleGetSetting() break default: } }, // 获取添加手机通讯录联系人权限 async handleGetSetting() { const that = this try { const { errMsg, authSetting } = await wx.getSetting() if (errMsg === 'getSetting:ok') { if (authSetting['scope.addPhoneContact']) { await that.addPhoneContactBridge() return } } } catch (err) {} try { await wx.authorize({ scope: 'scope.addPhoneContact' }) await that.addPhoneContactBridge() } catch (err) { wx.showModal({ title: '提示', content: '未开启添加手机通讯录联系人权限,去设置中打开', success(res) { if (res.confirm) { that.openSetting() } } }) } }, // 去小程序自带设置页:返回 async openSetting() { try { const openSettingData = await wx.openSetting() if (openSettingData.authSetting['scope.addPhoneContact']) { await this.addPhoneContactBridge() } } catch (err) {} }, async addPhoneContactBridge() { const { shopDetail } = this.data try { await wx.addPhoneContact({ firstName: shopDetail.user_name, mobilePhoneNumber: shopDetail.shop_phone }) } catch (err) {} }, async openLocationBridge() { const { shop_address } = this.data.shopDetail try { await wx.openLocation({ latitude: shop_address.latitude * 1, longitude: shop_address.longitude * 1, name: shop_address.address_name }) } catch (err) { console.log(err) } } } })