relevance.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <template>
  2. <view class="uni-app">
  3. <view class="status-bar" />
  4. <view class="main-container">
  5. <wk-nav-bar :title="navTitle">
  6. <!-- #ifndef MP-WEIXIN -->
  7. <button class="button white-btn" @click="handleConfirm">
  8. 确定
  9. </button>
  10. <!-- #endif -->
  11. </wk-nav-bar>
  12. <view class="search-wrapper">
  13. <wk-search-box class="search-box" @search="handleSearch" />
  14. <view
  15. v-if="showCreate"
  16. class="new-btn"
  17. @click="handleAdd">
  18. 新增
  19. </view>
  20. </view>
  21. <wk-scroll-view
  22. :status="listStatus"
  23. :refresh="false"
  24. class="list-scroll"
  25. @loadmore="getList()">
  26. <wk-select-list
  27. v-if="listData.length > 0"
  28. v-model="defaultVal"
  29. :list="listData"
  30. :label-field="typeObj.labelField"
  31. :value-field="typeObj.valueField"
  32. :max="bridge.maxlength"
  33. @change="handleChange" />
  34. </wk-scroll-view>
  35. <!-- #ifdef MP-WEIXIN -->
  36. <view class="footer-btn-group">
  37. <button class="button" @click="handleConfirm">
  38. 确定
  39. </button>
  40. </view>
  41. <!-- #endif -->
  42. </view>
  43. </view>
  44. </template>
  45. <script>
  46. /**
  47. * 选择关联项
  48. * @pageEvent {Function} selected-relevance 选中的关联项发生变化后触发
  49. */
  50. import {GetList as crmCustomerIndex} from 'API/crm/customer'
  51. import {GetList as crmContactsIndex} from 'API/crm/concat'
  52. import {GetList as crmBusinessIndex} from 'API/crm/business'
  53. import {GetList as crmContractIndex} from 'API/crm/contract'
  54. export default {
  55. name: 'RelevanceChoose',
  56. data() {
  57. return {
  58. listData: [],
  59. selectedList: [],
  60. defaultVal: [],
  61. params: {
  62. page: 0,
  63. limit: 15,
  64. search: ''
  65. },
  66. listStatus: 'more',
  67. reqMap: {
  68. customer: crmCustomerIndex,
  69. contacts: crmContactsIndex,
  70. business: crmBusinessIndex,
  71. contract: crmContractIndex,
  72. },
  73. routerQuery: {},
  74. bridge: {}, // 附加参数
  75. refreshPage: false
  76. }
  77. },
  78. computed: {
  79. typeObj() {
  80. if (!this.routerQuery || !this.routerQuery.type) return {}
  81. const obj = {
  82. customer: {labelField: 'customerName', valueField: 'customerId', title: '客户'},
  83. contacts: {labelField: 'name', valueField: 'contactsId', title: '联系人'},
  84. business: {labelField: 'businessName', valueField: 'businessId', title: '商机'},
  85. contract: {labelField: 'name', valueField: 'contractId', title: '合同'},
  86. }[this.routerQuery.type] || {}
  87. if (this.bridge && this.bridge.config) {
  88. const config = this.bridge.config || {}
  89. return {
  90. labelField: config.labelField || obj.labelField,
  91. valueField: config.valueField || obj.valueField,
  92. title: this.bridge.title || obj.title,
  93. }
  94. } else {
  95. return obj
  96. }
  97. },
  98. navTitle() {
  99. if (this.bridge.title) return this.bridge.title
  100. return ('选择' + this.typeObj.title) || '选择关联项'
  101. },
  102. showCreate() {
  103. const type = this.routerQuery.type || null
  104. let authFlag = false
  105. if (type) {
  106. authFlag = this.$auth(`crm.${type}.save`)
  107. }
  108. return Boolean(this.bridge.config && this.bridge.config.showCreate && authFlag)
  109. }
  110. },
  111. onLoad(options) {
  112. this.routerQuery = options || {}
  113. this.bridge = getApp().globalData.selectedValBridge[options.type] || {}
  114. // console.log('bridge: ', bridge)
  115. // console.log('routerQuery: ', this.routerQuery)
  116. this.defaultVal = this.bridge.defaultVal || []
  117. this.handleChange(this.defaultVal)
  118. this.getList()
  119. },
  120. onShow() {
  121. if (this.refreshPage) {
  122. this.$nextTick(function() {
  123. this.refreshPage = false
  124. this.params.page = 0
  125. this.params.search = ''
  126. this.getList({}, true)
  127. })
  128. }
  129. },
  130. onUnload() {
  131. getApp().globalData.selectedValBridge = {}
  132. },
  133. methods: {
  134. /**
  135. * 获取选项列表
  136. */
  137. getList() {
  138. if (!this.$isEmpty(this.bridge.list)) {
  139. this.listData = this.bridge.list
  140. return
  141. }
  142. if (!this.routerQuery || !this.routerQuery.type) return {}
  143. const type = this.routerQuery.type // 类型
  144. const otherParams = this.bridge.params || {} // 路由附加参数
  145. this.params.page++
  146. const request = this.bridge.request || this.reqMap[type]
  147. if (!request) return
  148. this.listStatus = 'loading'
  149. const params = {
  150. ...this.params,
  151. ...otherParams
  152. }
  153. if (params.pageType === 0) {
  154. delete params.page
  155. delete params.limit
  156. }
  157. request(params).then(res => {
  158. if (this.params.page === 1) {
  159. this.listData = []
  160. }
  161. console.log(res.list)
  162. this.listData = this.listData.concat(res.list)
  163. this.listStatus = res.lastPage ? 'noMore' : 'more'
  164. }).catch(() => {
  165. this.listStatus = 'more'
  166. })
  167. },
  168. /**
  169. * 搜索
  170. * @param {String} keyword
  171. */
  172. handleSearch(keyword) {
  173. this.params.search = keyword
  174. this.params.page = 0
  175. this.getList()
  176. },
  177. /**
  178. * 保存记录选择的项
  179. * @param {Object} data
  180. */
  181. handleChange(data) {
  182. this.selectedList = data
  183. },
  184. /**
  185. * 新建
  186. */
  187. handleAdd() {
  188. const type = this.routerQuery.type || null
  189. if (!type) return
  190. this.$Router.navigateTo({
  191. url: `/pages_crm/${type}/create`
  192. })
  193. },
  194. /**
  195. * 确认选择,通知上一级页面,关联项已经选择
  196. */
  197. handleConfirm() {
  198. this.selectedList.forEach((item, index) => {
  199. item.contactsName = item.name
  200. })
  201. const data = {
  202. type: this.routerQuery.type,
  203. guid: this.bridge.guid,
  204. bridge: this.bridge,
  205. data: this.selectedList
  206. }
  207. if (this.bridge.config && this.bridge.config.checkFn) {
  208. const res = this.bridge.config.checkFn(data)
  209. if (!res) return
  210. }
  211. uni.$emit('selected-relevance', data)
  212. this.$Router.navigateBack()
  213. }
  214. }
  215. }
  216. </script>
  217. <style scoped lang="scss">
  218. .main-container {
  219. display: flex;
  220. flex-direction: column;
  221. overflow: hidden;
  222. .search-wrapper {
  223. padding: 0 3%;
  224. @include left;
  225. .search-box {
  226. flex: 1;
  227. padding-left: 0;
  228. padding-right: 0;
  229. }
  230. .new-btn {
  231. font-size: $wk-font-base;
  232. color: $theme-color;
  233. margin-left: 20rpx;
  234. }
  235. }
  236. .list-scroll {
  237. flex: 1;
  238. overflow: hidden;
  239. padding-bottom: 20rpx;
  240. }
  241. }
  242. </style>