emergencyContact.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <template>
  2. <view class="container">
  3. <uni-nav-bar fixed :border="false" backgroundColor="#fff" title="常用联系人" :statusBar="true" left-icon="left"
  4. @clickLeft="$navBack()" @clickRight="clickRight">
  5. <!-- #ifndef MP-WEIXIN -->
  6. <template v-slot:right>
  7. <uni-icons type="plusempty" size="20"></uni-icons>
  8. </template>
  9. <!-- #endif -->
  10. </uni-nav-bar>
  11. <scroll-view scroll-y="true" :style="{height: `calc(100vh - ${statusBarHeight} - 228rpx)`}">
  12. <view class="container-body">
  13. <myEmpty text="暂无联系人" v-show="$isEmpty(phoneList) || phoneList.length == 0"></myEmpty>
  14. <view class="list">
  15. <uni-swipe-action ref="swipeAction">
  16. <uni-swipe-action-item v-for="(item,idx) in phoneList" :key="idx" :right-options="options"
  17. @click="onClick(idx)">
  18. <view class="list-item border-line">
  19. <view class="list-item-left">
  20. <view class="item-name">{{item.name}}</view>
  21. <view class="item-phone">{{item.number}}</view>
  22. </view>
  23. <view :class="item.sos ? 'list-item-right active':'list-item-right'"
  24. @click="handleSOS(item)">紧急联系人</view>
  25. </view>
  26. </uni-swipe-action-item>
  27. </uni-swipe-action>
  28. </view>
  29. <view class="tipsbox" v-if="phoneList&&phoneList.length>0">
  30. <view class="tipsbox-title border-line">
  31. <text>禁止常用联系人以外的号码呼入</text>
  32. <mySwitch :value="forbid" inactiveColor="#f7f7f7" @change="onSwitchChange"></mySwitch>
  33. </view>
  34. <view class="tipsbox-desc">设备未开通来电显示的用户禁止开启</view>
  35. </view>
  36. </view>
  37. </scroll-view>
  38. <view class="footer">
  39. <button class="savebtn" :loading="btnLoading" :disabled="btnLoading" @click="handleSend">
  40. 发送到手表
  41. </button>
  42. <!-- #ifdef MP-WEIXIN -->
  43. <view class="footer-add" @click="clickRight">新增联系人</view>
  44. <!-- #endif -->
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. // #ifdef APP-PLUS
  50. import { nativeContact } from '@/utils/nativeContact';
  51. // #endif
  52. import { getSetUpInfoByDeviceId, syncPhonebook,clearPhonebook } from "@/api/pages_watch/index.js";
  53. import mySwitch from "/pages_watch/components/mySwitch/mySwitch.vue";
  54. import myEmpty from "@/pages_watch/components/myEmpty/myEmpty.vue";
  55. export default {
  56. components:{
  57. mySwitch,
  58. myEmpty
  59. },
  60. data() {
  61. return {
  62. statusBarHeight: uni.getSystemInfoSync().statusBarHeight + 'px',
  63. deviceId: "",
  64. btnLoading: false,
  65. phoneList: [],
  66. // 1 true拦截陌生来电, 2 false不拦截陌生来电。不传的时候不设置陌生来电拦截
  67. forbid: false,
  68. options: [{
  69. text: '删除',
  70. style: {
  71. backgroundColor: '#dd524d'
  72. }
  73. }],
  74. }
  75. },
  76. onLoad(option) {
  77. uni.$on('getSaveList', (data) => {
  78. if (this.$isLogin() && this.deviceId && data && data.length > 0) {
  79. this.phoneList = this.phoneList.concat(data)
  80. }
  81. })
  82. this.deviceId = uni.getStorageSync("deviceId") || ""
  83. if (this.$isLogin()) {
  84. if (this.deviceId) {
  85. this.getSetUpInfo()
  86. } else {
  87. console.log("set.vue ---- deviceId is undefined")
  88. // uni.showToast({
  89. // title: "deviceId不存在",
  90. // icon: "none"
  91. // })
  92. }
  93. } else {
  94. this.$showLoginPage()
  95. }
  96. },
  97. onShow() {
  98. if(this.$refs.swipeAction){
  99. this.$refs.swipeAction.closeAll()
  100. }
  101. },
  102. onUnload() {
  103. uni.$off("getSaveList")
  104. },
  105. methods: {
  106. getSetUpInfo() {
  107. getSetUpInfoByDeviceId({
  108. deviceId: this.deviceId
  109. }).then(res => {
  110. if (res.code == 200 && res.data) {
  111. this.forbid = res.data.phonebooks.forbid == 1 ? true : false
  112. this.phoneList = res.data.phonebooks.phoneList && res.data.phonebooks.phoneList.length >
  113. 0 ? res.data.phonebooks.phoneList : []
  114. } else {
  115. uni.showToast({
  116. title: res.msg,
  117. icon: "none"
  118. })
  119. }
  120. })
  121. },
  122. clickRight() {
  123. uni.showActionSheet({
  124. // #ifdef H5
  125. itemList: ['创建联系人'],
  126. // #endif
  127. // #ifndef H5
  128. itemList: ['创建联系人','手机通讯录导入'],
  129. // #endif
  130. success: (res) => {
  131. if (res.tapIndex == 0) {
  132. const count = 8 - this.phoneList.length
  133. uni.navigateTo({
  134. url: "/pages_watch/index/equipment/editEmergencyContact?count=" + count
  135. })
  136. } else {
  137. // #ifdef MP-WEIXIN
  138. wx.chooseContact({
  139. success: res => {
  140. if(!this.phoneList.some(item=>item.number == res.phoneNumber)) {
  141. this.phoneList.push({
  142. name: res.displayName,
  143. number: res.phoneNumber,
  144. sos: false
  145. })
  146. }
  147. },
  148. fail: error => {
  149. console.log('weixin chooseContact error:',error);
  150. },
  151. });
  152. // #endif
  153. // #ifdef APP-PLUS
  154. nativeContact.contacts.getContact((name, number) => {
  155. if(!this.phoneList.some(item=>item.number == number)) {
  156. this.phoneList.push({
  157. name,
  158. number,
  159. sos: false
  160. })
  161. }
  162. });
  163. // #endif
  164. }
  165. },
  166. fail: function(res) {
  167. console.log(res.errMsg);
  168. }
  169. });
  170. },
  171. handleSOS(item) {
  172. item.sos = !item.sos
  173. },
  174. onClick(index) {
  175. uni.showModal({
  176. content: '是否删除选中的联系人',
  177. success: (res) => {
  178. if (res.confirm) {
  179. this.phoneList.splice(index, 1)
  180. this.$refs.swipeAction.closeAll()
  181. }
  182. }
  183. });
  184. },
  185. onSwitchChange(e) {
  186. this.forbid = e
  187. },
  188. handleSend() {
  189. if (this.phoneList && this.phoneList.length > 0 && this.phoneList.every(item => !item.sos)) {
  190. uni.showModal({
  191. content: '请先设置紧急联系人,否则无法使用手表SOS功能',
  192. showCancel: false,
  193. });
  194. return
  195. }
  196. uni.showLoading({
  197. title: ''
  198. });
  199. if(this.phoneList && this.phoneList.length > 0) {
  200. const param = {
  201. deviceid: this.deviceId,
  202. phoneList: this.phoneList,
  203. forbid: this.phoneList && this.phoneList.length == 0 ? 2 : this.forbid ? 1 : 2
  204. }
  205. syncPhonebook(param).then(res => {
  206. uni.hideLoading()
  207. if (res.code == 200) {
  208. uni.navigateBack()
  209. } else {
  210. uni.showToast({
  211. title: res.msg,
  212. icon: "none"
  213. })
  214. }
  215. }).catch(() => {
  216. uni.hideLoading()
  217. })
  218. } else {
  219. clearPhonebook({deviceId: this.deviceId}).then(res => {
  220. uni.hideLoading()
  221. if (res.code == 200) {
  222. uni.navigateBack()
  223. } else {
  224. uni.showToast({
  225. title: res.msg,
  226. icon: "none"
  227. })
  228. }
  229. }).catch(() => {
  230. uni.hideLoading()
  231. })
  232. }
  233. }
  234. }
  235. }
  236. </script>
  237. <style lang="scss" scoped>
  238. .container-body {
  239. .list {
  240. background-color: #fff;
  241. &-item {
  242. height: 130rpx;
  243. padding: 24rpx;
  244. box-sizing: border-box;
  245. display: flex;
  246. align-items: center;
  247. justify-content: space-between;
  248. font-family: PingFang SC, PingFang SC;
  249. font-weight: 400;
  250. font-size: 28rpx;
  251. color: #333;
  252. &-right {
  253. height: 70rpx;
  254. padding: 0 30rpx;
  255. line-height: 70rpx;
  256. text-align: center;
  257. background-color: #f5f5f5;
  258. border-radius: 80rpx;
  259. }
  260. }
  261. .item-name {
  262. font-weight: 500;
  263. font-size: 32rpx;
  264. }
  265. .item-phone {
  266. margin-top: 10rpx;
  267. color: #999;
  268. }
  269. .active {
  270. background-color: #dd524d;
  271. color: #FFFFFF;
  272. }
  273. }
  274. .tipsbox {
  275. padding: 0 24rpx;
  276. margin: 32rpx 24rpx;
  277. border-radius: 20rpx;
  278. background-color: #e9e9eb;
  279. font-family: PingFang SC, PingFang SC;
  280. font-weight: 400;
  281. font-size: 28rpx;
  282. color: #333;
  283. &-title {
  284. padding: 24rpx 0;
  285. font-size: 32rpx;
  286. display: flex;
  287. align-items: center;
  288. justify-content: space-between;
  289. text {
  290. flex: 1;
  291. margin-right: 24rpx;
  292. }
  293. }
  294. .tipsbox-desc {
  295. padding: 24rpx 0;
  296. }
  297. }
  298. }
  299. .footer {
  300. width: 100%;
  301. height: 140rpx;
  302. padding: 0 24rpx;
  303. box-sizing: border-box;
  304. display: flex;
  305. flex-direction: row;
  306. align-items: center;
  307. justify-content: center;
  308. box-sizing: border-box;
  309. position: fixed;
  310. z-index: 99;
  311. bottom: var(--window-bottom);
  312. left: 0;
  313. .savebtn {
  314. flex: 1;
  315. max-width: 622rpx;
  316. height: 88rpx;
  317. line-height: 88rpx;
  318. border-radius: 44rpx 44rpx 44rpx 44rpx;
  319. background-color: #FF7700;
  320. font-family: PingFang SC, PingFang SC;
  321. font-weight: 500;
  322. font-size: 32rpx;
  323. color: #FFFFFF;
  324. margin: 0;
  325. &::after {
  326. border: none;
  327. }
  328. }
  329. .footer-add {
  330. flex: 1;
  331. height: 88rpx;
  332. margin-left: 20rpx;
  333. box-sizing: border-box;
  334. line-height: 88rpx;
  335. border-radius: 44rpx 44rpx 44rpx 44rpx;
  336. border: 2rpx solid #ECECEC;
  337. display: flex;
  338. flex-direction: row;
  339. align-items: center;
  340. justify-content: center;
  341. font-family: PingFang SC, PingFang SC;
  342. font-weight: 500;
  343. font-size: 32rpx;
  344. color: #FF7700;
  345. background-color: #fff;
  346. }
  347. }
  348. </style>