提交 5679e5ba 编写于 作者: D DCloud_LXH

fix(app): 修复 重复添加联系人的Bug https://gitee.com/dcloud/uni-app/issues/I4NY6C

上级 2ff036e3
export const addPhoneContact = {
firstName: {
type: String,
required: true,
validator (firstName) {
if (!firstName) {
return 'addPhoneContact:fail parameter error: parameter.firstName should not be empty String;'
}
}
}
}
...@@ -2,217 +2,136 @@ import { ...@@ -2,217 +2,136 @@ import {
invoke invoke
} from '../../bridge' } from '../../bridge'
export function addPhoneContact ({ const schema = {
photoFilePath = '', name: {
nickName, givenName: 'firstName',
lastName, middleName: 'middleName',
middleName, familyName: 'lastName',
firstName, },
remark, nickname: 'nickName',
mobilePhoneNumber, photos: {
weChatNumber,
addressCountry,
addressState,
addressCity,
addressStreet,
addressPostalCode,
organization,
title,
workFaxNumber,
workPhoneNumber,
hostNumber,
email,
url,
workAddressCountry,
workAddressState,
workAddressCity,
workAddressStreet,
workAddressPostalCode,
homeFaxNumber,
homePhoneNumber,
homeAddressCountry,
homeAddressState,
homeAddressCity,
homeAddressStreet,
homeAddressPostalCode
} = {}, callbackId) {
plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, (addressbook) => {
const contact = addressbook.create()
const name = {}
if (lastName) {
name.familyName = lastName
}
if (firstName) {
name.givenName = firstName
}
if (middleName) {
name.middleName = middleName
}
contact.name = name
if (nickName) {
contact.nickname = nickName
}
if (photoFilePath) {
contact.photos = [{
type: 'url', type: 'url',
value: photoFilePath value: 'photoFilePath'
}] },
} note: 'remark',
phoneNumbers: [
if (remark) { {
contact.note = remark type: 'mobile',
} value: 'mobilePhoneNumber'
},
const mobilePhone = { {
type: 'mobile' type: 'work',
} value: 'workPhoneNumber'
},
const workPhone = { {
type: 'work' type: 'company',
} value: 'hostNumber'
},
const companyPhone = { {
type: 'company' type: 'home fax',
} value: 'homeFaxNumber'
},
const homeFax = { {
type: 'home fax' type: 'work fax',
} value: 'workFaxNumber'
}
const workFax = { ],
type: 'work fax' emails: [{
}
if (mobilePhoneNumber) {
mobilePhone.value = mobilePhoneNumber
}
if (workPhoneNumber) {
workPhone.value = workPhoneNumber
}
if (hostNumber) {
companyPhone.value = hostNumber
}
if (homeFaxNumber) {
homeFax.value = homeFaxNumber
}
if (workFaxNumber) {
workFax.value = workFaxNumber
}
contact.phoneNumbers = [mobilePhone, workPhone, companyPhone, homeFax, workFax]
if (email) {
contact.emails = [{
type: 'home', type: 'home',
value: email value: 'email'
}] }],
} urls: [{
if (url) {
contact.urls = [{
type: 'other', type: 'other',
value: url value: 'url'
}] }],
} organizations: [{
type: 'company',
const org = { name: 'organization',
type: 'company' title: 'title'
} }],
ims: [{
if (organization) {
org.name = organization
}
if (title) {
org.title = title
}
if (weChatNumber) {
contact.ims = [{
type: 'other', type: 'other',
value: weChatNumber value: 'weChatNumber'
}] }],
} addresses: [
{
const defaultAddress = {
type: 'other', type: 'other',
preferred: true preferred: true,
} country: 'addressCountry',
region: 'addressState',
const homeAddress = { locality: 'addressCity',
type: 'home' streetAddress: 'addressStreet',
} postalCode: 'addressPostalCode'
const companyAddress = { },
type: 'company' {
} type: 'home',
country: 'homeAddressCountry',
if (addressCountry) { region: 'homeAddressState',
defaultAddress.country = addressCountry locality: 'homeAddressCity',
} streetAddress: 'homeAddressStreet',
postalCode: 'homeAddressPostalCode'
if (addressState) { },
defaultAddress.region = addressState {
} type: 'company',
country: 'workAddressCountry',
if (addressCity) { region: 'workAddressState',
defaultAddress.locality = addressCity locality: 'workAddressCity',
} streetAddress: 'workAddressStreet',
postalCode: 'workAddressPostalCode'
if (addressStreet) { }
defaultAddress.streetAddress = addressStreet ]
} }
if (addressPostalCode) {
defaultAddress.postalCode = addressPostalCode
}
if (homeAddressCountry) {
homeAddress.country = homeAddressCountry
}
if (homeAddressState) {
homeAddress.region = homeAddressState
}
if (homeAddressCity) {
homeAddress.locality = homeAddressCity
}
if (homeAddressStreet) {
homeAddress.streetAddress = homeAddressStreet
}
if (homeAddressPostalCode) {
homeAddress.postalCode = homeAddressPostalCode
}
if (workAddressCountry) { const keepFields = ['type', 'preferred']
companyAddress.country = workAddressCountry
function buildContact(contact, data, schema) {
let hasValue = 0
Object.keys(schema).forEach(contactKey => {
const dataKey = schema[contactKey]
const typed = typeof dataKey
if (typed !== 'object') {
if (keepFields.indexOf(contactKey) !== -1) {
contact[contactKey] = schema[contactKey]
} else {
if (typeof data[dataKey] !== 'undefined') {
hasValue++
contact[contactKey] = data[dataKey]
} else {
delete contact[contactKey]
}
}
} else {
if (dataKey instanceof Array) {
contact[contactKey] = []
dataKey.forEach(item => {
const obj = {}
if (buildContact(obj, data, item)) {
contact[contactKey].push(obj)
} }
})
if (workAddressState) { if (!contact[contactKey].length) {
companyAddress.region = workAddressState delete contact[contactKey]
} else {
hasValue++
} }
} else {
if (workAddressCity) { contact[contactKey] = {}
companyAddress.locality = workAddressCity if (buildContact(contact[contactKey], data, dataKey)) {
hasValue++
} else {
delete contact[contactKey]
} }
if (workAddressStreet) {
companyAddress.streetAddress = workAddressStreet
} }
if (workAddressPostalCode) {
companyAddress.postalCode = workAddressPostalCode
} }
})
return hasValue
}
contact.addresses = [defaultAddress, homeAddress, companyAddress] export function addPhoneContact(data, callbackId) {
plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, (addressbook) => {
!data.photoFilePath && (data.photoFilePath = '')
const contact = addressbook.create()
buildContact(contact, data, schema)
contact.save(() => { contact.save(() => {
invoke(callbackId, { invoke(callbackId, {
errMsg: 'addPhoneContact:ok' errMsg: 'addPhoneContact:ok'
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册