ContactImport.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <template>
  2. <view class="overlay" v-if="visible" @touchmove.stop.prevent>
  3. <view class="modal">
  4. <text class="modal-title">添加联系人</text>
  5. <!-- 从微信通讯录导入 -->
  6. <view class="wechat-import-btn" @click="chooseFromWeChat">
  7. <text class="wechat-import-text">从微信通讯录导入</text>
  8. </view>
  9. <view class="form">
  10. <view class="form-row">
  11. <text class="form-label">姓名</text>
  12. <input class="form-input" v-model="form.name" placeholder="请输入姓名" />
  13. </view>
  14. <view class="form-row">
  15. <text class="form-label">手机号</text>
  16. <input class="form-input" v-model="form.phone" placeholder="请输入手机号" type="number" />
  17. </view>
  18. <view class="form-row">
  19. <text class="form-label">关系类型</text>
  20. <picker mode="selector" :range="typeLabels" @change="onTypeChange">
  21. <view class="form-picker">{{ form.relationshipTypeLabel || '请选择关系类型' }}</view>
  22. </picker>
  23. </view>
  24. <view class="form-row">
  25. <text class="form-label">认识时间</text>
  26. <picker mode="date" @change="onDateChange">
  27. <view class="form-picker">{{ form.knownSince || '请选择认识时间' }}</view>
  28. </picker>
  29. </view>
  30. <view class="form-row">
  31. <text class="form-label">生日</text>
  32. <picker mode="date" @change="onBirthdayChange">
  33. <view class="form-picker">{{ form.birthday || '请选择生日' }}</view>
  34. </picker>
  35. </view>
  36. <view class="form-row">
  37. <text class="form-label">个人介绍</text>
  38. <textarea class="form-textarea" v-model="form.bio" placeholder="选填" />
  39. </view>
  40. </view>
  41. <view class="modal-actions">
  42. <button class="btn-cancel" @click="onCancel">取消</button>
  43. <button class="btn-confirm" @click="onConfirm">确认添加</button>
  44. </view>
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. import { importPhoneContact } from '../utils/api.js'
  50. var DEFAULT_RELATIONSHIP_TYPES = [
  51. { value: 'family', label: '家人' },
  52. { value: 'friend', label: '朋友' },
  53. { value: 'partner', label: '伴侣' },
  54. { value: 'colleague', label: '同事' },
  55. { value: 'other', label: '其他' }
  56. ]
  57. export default {
  58. props: {
  59. visible: { type: Boolean, default: false },
  60. name: { type: String, default: '' },
  61. phone: { type: String, default: '' }
  62. },
  63. data: function() {
  64. var defaultLabels = []
  65. for (var i = 0; i < DEFAULT_RELATIONSHIP_TYPES.length; i++) {
  66. defaultLabels.push(DEFAULT_RELATIONSHIP_TYPES[i].label)
  67. }
  68. return {
  69. form: {
  70. name: '',
  71. phone: '',
  72. relationshipType: '',
  73. relationshipTypeLabel: '',
  74. knownSince: '',
  75. birthday: '',
  76. bio: ''
  77. },
  78. relationshipTypes: DEFAULT_RELATIONSHIP_TYPES,
  79. typeLabels: defaultLabels
  80. }
  81. },
  82. watch: {
  83. visible: function(val) {
  84. if (val) {
  85. if (!this.form.name && this.name) {
  86. this.form.name = this.name
  87. }
  88. if (!this.form.phone && this.phone) {
  89. this.form.phone = this.phone
  90. }
  91. }
  92. },
  93. name: function(val) {
  94. if (val && !this.form.name) {
  95. this.form.name = val
  96. }
  97. },
  98. phone: function(val) {
  99. if (val && !this.form.phone) {
  100. this.form.phone = val
  101. }
  102. }
  103. },
  104. methods: {
  105. chooseFromWeChat: function() {
  106. var self = this
  107. if (typeof wx !== 'undefined' && wx.chooseContact) {
  108. wx.chooseContact({
  109. success: function(res) {
  110. self.form.name = res.name || ''
  111. self.form.phone = res.phone || ''
  112. },
  113. fail: function() {
  114. uni.showToast({ title: '获取通讯录失败', icon: 'none' })
  115. }
  116. })
  117. } else {
  118. uni.showToast({ title: '当前环境不支持', icon: 'none' })
  119. }
  120. },
  121. onTypeChange: function(e) {
  122. var idx = e.detail.value
  123. var typeObj
  124. this.form.relationshipTypeLabel = this.typeLabels[idx]
  125. typeObj = this.relationshipTypes && this.relationshipTypes[idx]
  126. if (typeObj) {
  127. this.form.relationshipType = typeof typeObj === 'string' ? typeObj : (typeObj.typeKey || typeObj.value || typeObj.code || typeObj.name || typeObj.label || '')
  128. } else {
  129. this.form.relationshipType = ''
  130. }
  131. },
  132. onDateChange: function(e) {
  133. this.form.knownSince = e.detail.value
  134. },
  135. onBirthdayChange: function(e) {
  136. this.form.birthday = e.detail.value
  137. },
  138. onConfirm: function() {
  139. var self = this
  140. if (!this.form.name || !this.form.phone) {
  141. uni.showToast({ title: '请填写姓名和手机号', icon: 'none' })
  142. return
  143. }
  144. var extra = {}
  145. if (this.form.relationshipType) extra.relationshipType = this.form.relationshipType
  146. if (this.form.knownSince) extra.knownSince = this.form.knownSince
  147. if (this.form.birthday) extra.birthday = this.form.birthday
  148. if (this.form.bio) extra.bio = this.form.bio
  149. importPhoneContact({
  150. name: this.form.name,
  151. phone: this.form.phone,
  152. extra: extra
  153. }).then(function(res) {
  154. if (res.code === 200) {
  155. uni.showToast({ title: '添加成功', icon: 'success' })
  156. self.$emit('success')
  157. self.reset()
  158. } else {
  159. uni.showToast({ title: res.message || '添加失败', icon: 'none' })
  160. }
  161. }).catch(function() {
  162. uni.showToast({ title: '网络错误', icon: 'none' })
  163. })
  164. },
  165. onCancel: function() {
  166. this.reset()
  167. this.$emit('close')
  168. },
  169. reset: function() {
  170. this.form = {
  171. name: '',
  172. phone: '',
  173. relationshipType: '',
  174. relationshipTypeLabel: '',
  175. knownSince: '',
  176. birthday: '',
  177. bio: ''
  178. }
  179. }
  180. }
  181. }
  182. </script>
  183. <style scoped>
  184. .overlay {
  185. position: fixed;
  186. top: 0; left: 0; right: 0; bottom: 0;
  187. background: rgba(0,0,0,0.5);
  188. z-index: 999;
  189. display: flex;
  190. align-items: flex-end;
  191. justify-content: center;
  192. }
  193. .modal {
  194. background: #fff;
  195. border-radius: 24rpx 24rpx 0 0;
  196. padding: 40rpx 30rpx 60rpx;
  197. width: 100%;
  198. box-sizing: border-box;
  199. max-height: 80vh;
  200. overflow-y: auto;
  201. }
  202. .modal-title {
  203. font-size: 32rpx;
  204. font-weight: bold;
  205. color: #333;
  206. margin-bottom: 24rpx;
  207. display: block;
  208. text-align: center;
  209. }
  210. .wechat-import-btn {
  211. background: #F0F9FF;
  212. border: 1rpx solid #BAE6FD;
  213. border-radius: 16rpx;
  214. padding: 24rpx 20rpx;
  215. display: flex;
  216. align-items: center;
  217. justify-content: center;
  218. margin-bottom: 24rpx;
  219. }
  220. .wechat-import-btn:active {
  221. opacity: 0.7;
  222. }
  223. .wechat-import-text {
  224. font-size: 28rpx;
  225. color: #0EA5E9;
  226. font-weight: 500;
  227. }
  228. .form-row {
  229. margin-bottom: 24rpx;
  230. }
  231. .form-label {
  232. font-size: 26rpx;
  233. color: #666;
  234. display: block;
  235. margin-bottom: 8rpx;
  236. }
  237. .form-input {
  238. border: 1rpx solid #ddd;
  239. border-radius: 12rpx;
  240. padding: 24rpx 20rpx;
  241. font-size: 28rpx;
  242. width: 100%;
  243. box-sizing: border-box;
  244. color: #333;
  245. min-height: 80rpx;
  246. background: #fff;
  247. }
  248. .form-textarea {
  249. border: 1rpx solid #ddd;
  250. border-radius: 12rpx;
  251. padding: 24rpx 20rpx;
  252. font-size: 28rpx;
  253. width: 100%;
  254. box-sizing: border-box;
  255. color: #333;
  256. min-height: 160rpx;
  257. }
  258. .form-picker {
  259. border: 1rpx solid #ddd;
  260. border-radius: 12rpx;
  261. padding: 24rpx 20rpx;
  262. font-size: 28rpx;
  263. color: #333;
  264. min-height: 80rpx;
  265. display: flex;
  266. align-items: center;
  267. }
  268. .modal-actions {
  269. display: flex;
  270. gap: 20rpx;
  271. margin-top: 40rpx;
  272. }
  273. .btn-cancel {
  274. flex: 1;
  275. padding: 20rpx;
  276. border-radius: 12rpx;
  277. border: 1rpx solid #ddd;
  278. background: #fff;
  279. font-size: 28rpx;
  280. color: #666;
  281. text-align: center;
  282. }
  283. .btn-confirm {
  284. flex: 2;
  285. padding: 20rpx;
  286. border-radius: 12rpx;
  287. background: linear-gradient(135deg, #FF6B35, #F97316);
  288. font-size: 28rpx;
  289. color: #fff;
  290. text-align: center;
  291. border: none;
  292. }
  293. </style>