diff --git a/src/core/helpers/protocol/device/add-phone-contact.js b/src/core/helpers/protocol/device/add-phone-contact.js new file mode 100644 index 0000000000000000000000000000000000000000..39425037b70a94bec2315c4f0e6a2656edfae8a1 --- /dev/null +++ b/src/core/helpers/protocol/device/add-phone-contact.js @@ -0,0 +1,11 @@ +export const addPhoneContact = { + firstName: { + type: String, + required: true, + validator (firstName) { + if (!firstName) { + return 'addPhoneContact:fail parameter error: parameter.firstName should not be empty String;' + } + } + } +} diff --git a/src/platforms/app-plus/service/api/device/add-phone-contact.js b/src/platforms/app-plus/service/api/device/add-phone-contact.js index 4fdada5ae894f596ebf45050e6251472e9717b38..0f55b9eed588e5b8bfcc488bb83d071ee9c30105 100644 --- a/src/platforms/app-plus/service/api/device/add-phone-contact.js +++ b/src/platforms/app-plus/service/api/device/add-phone-contact.js @@ -2,217 +2,136 @@ import { invoke } from '../../bridge' -export function addPhoneContact ({ - photoFilePath = '', - nickName, - lastName, - middleName, - firstName, - remark, - mobilePhoneNumber, - 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', - value: photoFilePath - }] - } - - if (remark) { - contact.note = remark - } - - const mobilePhone = { - type: 'mobile' - } - - const workPhone = { - type: 'work' - } - - const companyPhone = { - type: 'company' - } - - const homeFax = { - type: 'home fax' - } - - const workFax = { - type: 'work fax' - } - - 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', - value: email - }] - } - - if (url) { - contact.urls = [{ - type: 'other', - value: url - }] - } - - const org = { - type: 'company' - } - - if (organization) { - org.name = organization - } - if (title) { - org.title = title - } - - if (weChatNumber) { - contact.ims = [{ - type: 'other', - value: weChatNumber - }] - } - - const defaultAddress = { +const schema = { + name: { + givenName: 'firstName', + middleName: 'middleName', + familyName: 'lastName', + }, + nickname: 'nickName', + photos: { + type: 'url', + value: 'photoFilePath' + }, + note: 'remark', + phoneNumbers: [ + { + type: 'mobile', + value: 'mobilePhoneNumber' + }, + { + type: 'work', + value: 'workPhoneNumber' + }, + { + type: 'company', + value: 'hostNumber' + }, + { + type: 'home fax', + value: 'homeFaxNumber' + }, + { + type: 'work fax', + value: 'workFaxNumber' + } + ], + emails: [{ + type: 'home', + value: 'email' + }], + urls: [{ + type: 'other', + value: 'url' + }], + organizations: [{ + type: 'company', + name: 'organization', + title: 'title' + }], + ims: [{ + type: 'other', + value: 'weChatNumber' + }], + addresses: [ + { type: 'other', - preferred: true - } - - const homeAddress = { - type: 'home' - } - const companyAddress = { - type: 'company' - } - - if (addressCountry) { - defaultAddress.country = addressCountry - } - - if (addressState) { - defaultAddress.region = addressState - } - - if (addressCity) { - defaultAddress.locality = addressCity - } - - 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) { - companyAddress.country = workAddressCountry - } - - if (workAddressState) { - companyAddress.region = workAddressState - } - - if (workAddressCity) { - companyAddress.locality = workAddressCity - } - - if (workAddressStreet) { - companyAddress.streetAddress = workAddressStreet - } + preferred: true, + country: 'addressCountry', + region: 'addressState', + locality: 'addressCity', + streetAddress: 'addressStreet', + postalCode: 'addressPostalCode' + }, + { + type: 'home', + country: 'homeAddressCountry', + region: 'homeAddressState', + locality: 'homeAddressCity', + streetAddress: 'homeAddressStreet', + postalCode: 'homeAddressPostalCode' + }, + { + type: 'company', + country: 'workAddressCountry', + region: 'workAddressState', + locality: 'workAddressCity', + streetAddress: 'workAddressStreet', + postalCode: 'workAddressPostalCode' + } + ] +} - if (workAddressPostalCode) { - companyAddress.postalCode = workAddressPostalCode +const keepFields = ['type', 'preferred'] + +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 (!contact[contactKey].length) { + delete contact[contactKey] + } else { + hasValue++ + } + } else { + contact[contactKey] = {} + if (buildContact(contact[contactKey], data, dataKey)) { + hasValue++ + } else { + delete contact[contactKey] + } + } } + }) + 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(() => { invoke(callbackId, { errMsg: 'addPhoneContact:ok'