ContactImport.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <template>
  2. <view class="overlay" v-if="visible" @touchmove.stop.prevent :style="{ '--font-scale': fontScale }">
  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. import fontScaleMixin from '@/mixins/font-scale.js'
  58. export default {
  59. mixins: [fontScaleMixin],
  60. props: {
  61. visible: { type: Boolean, default: false },
  62. name: { type: String, default: '' },
  63. phone: { type: String, default: '' }
  64. },
  65. data: function() {
  66. var defaultLabels = []
  67. for (var i = 0; i < DEFAULT_RELATIONSHIP_TYPES.length; i++) {
  68. defaultLabels.push(DEFAULT_RELATIONSHIP_TYPES[i].label)
  69. }
  70. return {
  71. form: {
  72. name: '',
  73. phone: '',
  74. relationshipType: '',
  75. relationshipTypeLabel: '',
  76. knownSince: '',
  77. birthday: '',
  78. bio: ''
  79. },
  80. relationshipTypes: DEFAULT_RELATIONSHIP_TYPES,
  81. typeLabels: defaultLabels
  82. }
  83. },
  84. watch: {
  85. visible: function(val) {
  86. if (val) {
  87. if (!this.form.name && this.name) {
  88. this.form.name = this.name
  89. }
  90. if (!this.form.phone && this.phone) {
  91. this.form.phone = this.phone
  92. }
  93. }
  94. },
  95. name: function(val) {
  96. if (val && !this.form.name) {
  97. this.form.name = val
  98. }
  99. },
  100. phone: function(val) {
  101. if (val && !this.form.phone) {
  102. this.form.phone = val
  103. }
  104. }
  105. },
  106. methods: {
  107. chooseFromWeChat: function() {
  108. var self = this
  109. if (typeof wx !== 'undefined' && wx.chooseContact) {
  110. wx.chooseContact({
  111. success: function(res) {
  112. self.form.name = res.name || ''
  113. self.form.phone = res.phone || ''
  114. },
  115. fail: function() {
  116. uni.showToast({ title: '获取通讯录失败', icon: 'none' })
  117. }
  118. })
  119. } else {
  120. uni.showToast({ title: '当前环境不支持', icon: 'none' })
  121. }
  122. },
  123. onTypeChange: function(e) {
  124. var idx = e.detail.value
  125. var typeObj
  126. this.form.relationshipTypeLabel = this.typeLabels[idx]
  127. typeObj = this.relationshipTypes && this.relationshipTypes[idx]
  128. if (typeObj) {
  129. this.form.relationshipType = typeof typeObj === 'string' ? typeObj : (typeObj.typeKey || typeObj.value || typeObj.code || typeObj.name || typeObj.label || '')
  130. } else {
  131. this.form.relationshipType = ''
  132. }
  133. },
  134. onDateChange: function(e) {
  135. this.form.knownSince = e.detail.value
  136. },
  137. onBirthdayChange: function(e) {
  138. this.form.birthday = e.detail.value
  139. },
  140. onConfirm: function() {
  141. var self = this
  142. if (!this.form.name || !this.form.phone) {
  143. uni.showToast({ title: '请填写姓名和手机号', icon: 'none' })
  144. return
  145. }
  146. var extra = {}
  147. if (this.form.relationshipType) extra.relationshipType = this.form.relationshipType
  148. if (this.form.knownSince) extra.knownSince = this.form.knownSince
  149. if (this.form.birthday) extra.birthday = this.form.birthday
  150. if (this.form.bio) extra.bio = this.form.bio
  151. importPhoneContact({
  152. name: this.form.name,
  153. phone: this.form.phone,
  154. extra: extra
  155. }).then(function(res) {
  156. if (res.code === 200) {
  157. uni.showToast({ title: '添加成功', icon: 'success' })
  158. self.$emit('success')
  159. self.reset()
  160. } else {
  161. uni.showToast({ title: res.message || '添加失败', icon: 'none' })
  162. }
  163. }).catch(function() {
  164. uni.showToast({ title: '网络错误', icon: 'none' })
  165. })
  166. },
  167. onCancel: function() {
  168. this.reset()
  169. this.$emit('close')
  170. },
  171. reset: function() {
  172. this.form = {
  173. name: '',
  174. phone: '',
  175. relationshipType: '',
  176. relationshipTypeLabel: '',
  177. knownSince: '',
  178. birthday: '',
  179. bio: ''
  180. }
  181. }
  182. }
  183. }
  184. </script>
  185. <style scoped lang="scss">
  186. .overlay {
  187. position: fixed;
  188. top: 0; left: 0; right: 0; bottom: 0;
  189. background: rgba(0,0,0,0.5);
  190. z-index: 999;
  191. display: flex;
  192. align-items: flex-end;
  193. justify-content: center;
  194. }
  195. .modal {
  196. background: $app-surface;
  197. border-radius: 24rpx 24rpx 0 0;
  198. padding: 40rpx $uni-space-page 60rpx;
  199. width: 100%;
  200. box-sizing: border-box;
  201. max-height: 80vh;
  202. overflow-y: auto;
  203. }
  204. .modal-title {
  205. font-size: calc(32rpx * var(--font-scale, 1));
  206. font-weight: bold;
  207. color: $facade-text-strong;
  208. margin-bottom: 24rpx;
  209. display: block;
  210. text-align: center;
  211. }
  212. .wechat-import-btn {
  213. background: $tint-sky-soft;
  214. border: 1rpx solid $tint-sky-line;
  215. border-radius: 16rpx;
  216. padding: 24rpx 20rpx;
  217. display: flex;
  218. align-items: center;
  219. justify-content: center;
  220. margin-bottom: 24rpx;
  221. }
  222. .wechat-import-btn:active {
  223. opacity: 0.7;
  224. }
  225. .wechat-import-text {
  226. font-size: calc(28rpx * var(--font-scale, 1));
  227. color: $uni-info;
  228. font-weight: 500;
  229. }
  230. .form-row {
  231. margin-bottom: 24rpx;
  232. }
  233. .form-label {
  234. font-size: calc(26rpx * var(--font-scale, 1));
  235. color: $facade-text-body;
  236. display: block;
  237. margin-bottom: 8rpx;
  238. }
  239. .form-input {
  240. border: 1rpx solid $facade-border-soft;
  241. border-radius: 16rpx;
  242. padding: 24rpx 20rpx;
  243. font-size: calc(28rpx * var(--font-scale, 1));
  244. width: 100%;
  245. box-sizing: border-box;
  246. color: $facade-text-strong;
  247. min-height: 80rpx;
  248. background: $app-surface;
  249. }
  250. .form-textarea {
  251. border: 1rpx solid $facade-border-soft;
  252. border-radius: 16rpx;
  253. padding: 24rpx 20rpx;
  254. font-size: calc(28rpx * var(--font-scale, 1));
  255. width: 100%;
  256. box-sizing: border-box;
  257. color: $facade-text-strong;
  258. min-height: 160rpx;
  259. }
  260. .form-picker {
  261. border: 1rpx solid $facade-border-soft;
  262. border-radius: 16rpx;
  263. padding: 24rpx 20rpx;
  264. font-size: calc(28rpx * var(--font-scale, 1));
  265. color: $facade-text-strong;
  266. min-height: 80rpx;
  267. display: flex;
  268. align-items: center;
  269. }
  270. .modal-actions {
  271. display: flex;
  272. gap: 20rpx;
  273. margin-top: 40rpx;
  274. }
  275. .btn-cancel {
  276. flex: 1;
  277. padding: 20rpx;
  278. border-radius: 16rpx;
  279. border: 1rpx solid $facade-border-soft;
  280. background: $app-surface;
  281. font-size: calc(28rpx * var(--font-scale, 1));
  282. color: $facade-text-body;
  283. text-align: center;
  284. }
  285. .btn-confirm {
  286. flex: 2;
  287. padding: 20rpx;
  288. border-radius: 16rpx;
  289. background: linear-gradient(135deg, $mind-accent, $brand-primary);
  290. font-size: calc(28rpx * var(--font-scale, 1));
  291. color: $app-surface;
  292. text-align: center;
  293. border: none;
  294. }
  295. </style>