index.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. const https = require("../../api/serverData");
  2. const pinyin = require('js-pinyin');
  3. import { convertTime } from "../../utils/util"
  4. import {
  5. storeBindingsBehavior
  6. } from 'mobx-miniprogram-bindings'
  7. import { store } from "../../store/index";
  8. Component({
  9. behaviors: [storeBindingsBehavior],
  10. properties: {
  11. popupType: {
  12. type: String,
  13. value: ''
  14. },
  15. popupKey: {
  16. type: String,
  17. value: ''
  18. },
  19. popupTitle: {
  20. type: String,
  21. value: ''
  22. },
  23. popupValue: {
  24. type: null,
  25. value: ''
  26. },
  27. popupApi: {
  28. type: String,
  29. value: ''
  30. },
  31. show: {
  32. type: Boolean,
  33. value: false
  34. },
  35. },
  36. data: {
  37. popupOptions: [],
  38. loading: false,
  39. value: "",
  40. label: "",
  41. mapData: [],
  42. indexList: [],
  43. cityList: []
  44. },
  45. storeBindings: {
  46. store: store,
  47. fields: {
  48. token: () => store.token,
  49. },
  50. actions: {
  51. setStore: 'setStore',
  52. }
  53. },
  54. observers: { // 设置组件的侦听属性
  55. show(val) {
  56. if (val) {
  57. const {
  58. popupType,
  59. popupValue
  60. } = this.data
  61. if (popupType === "radio" || popupType === 'city') {
  62. this.setData({
  63. value: String(popupValue)
  64. })
  65. }
  66. if (popupType === 'checkbox' || popupType === "radio") {
  67. this.getDateList()
  68. } else if (popupType === 'city') {
  69. this.getMapAreaList()
  70. } else if (popupType === 'time') {
  71. this.setData({
  72. popupOptions: [],
  73. value: popupValue
  74. })
  75. }
  76. }
  77. },
  78. },
  79. methods: {
  80. async getMapAreaList() {
  81. const {
  82. popupValue,
  83. } = this.data
  84. this.setData({
  85. loading: true
  86. })
  87. let mapData = await this.getAreaList()
  88. this.setData({
  89. mapData
  90. })
  91. mapData.map(item => {
  92. item.pinyin = pinyin.getFullChars(item.label);
  93. });
  94. let provice = {};
  95. mapData.map((item) => {
  96. const Initials = item.pinyin[0].toUpperCase();
  97. // 如果对象里有当前字母项则直接 push 一个对象,如果没有则创建一个新的键并赋值;
  98. if (provice[Initials]) {
  99. provice[Initials].push(item);
  100. } else {
  101. provice[Initials] = [item];
  102. }
  103. });
  104. // 将数据转为数组,并按字母顺利排列
  105. let filterData = [];
  106. for (let key in provice) {
  107. const obj = {
  108. letter: key,
  109. list: provice[key]
  110. };
  111. filterData.push(obj)
  112. }
  113. filterData.sort((a, b) => {
  114. return a.letter.localeCompare(b.letter)
  115. });
  116. // 为索引字符数组赋值
  117. let indexList = [],
  118. arr = [];
  119. filterData.forEach((item) => {
  120. indexList.push(item.letter)
  121. let model = {
  122. letter: item.letter,
  123. list: []
  124. }
  125. item.list.forEach((si, sii) => {
  126. const modelb = {
  127. ...si,
  128. order: Math.floor(item.list.length / 3) > Math.floor(sii / 3) ? false : true
  129. }
  130. model.list.push(modelb);
  131. })
  132. arr.push(model);
  133. }
  134. );
  135. let name = "";
  136. let index = mapData.findIndex((s) => String(s.id) === String(popupValue))
  137. if (index !== -1) {
  138. name = this.data.mapData[index].label
  139. }
  140. this.setData({
  141. indexList,
  142. cityList: arr,
  143. value: popupValue,
  144. label: name,
  145. loading: false
  146. })
  147. },
  148. async getDateList() {
  149. let resList = []
  150. const {
  151. popupType,
  152. popupValue
  153. } = this.data
  154. const {
  155. code,
  156. data,
  157. msg
  158. } = await https[this.data.popupApi]({
  159. page: 1,
  160. limit: 999999,
  161. status: '1'
  162. })
  163. if (code === 1) {
  164. const {
  165. list
  166. } = data
  167. list.forEach(si => {
  168. if (popupType === 'checkbox') {
  169. let index = popupValue.findIndex((s) => s + '' === si.id + '')
  170. resList.push({
  171. ...si,
  172. selected: index !== -1
  173. })
  174. } else {
  175. resList.push({
  176. ...si,
  177. name: si.name ? si.name : String(si.max) !== '0' ? `${si.min}~${si.max}人` : `${si.min}+人`
  178. })
  179. }
  180. })
  181. } else {
  182. await this.setStore("", "");
  183. }
  184. this.setData({
  185. popupOptions: resList,
  186. loading: false
  187. })
  188. },
  189. radioChange(e) {
  190. const index = Number(e.currentTarget.dataset.index)
  191. const options = this.data.popupOptions
  192. const value = index === "" ? "" : options[index].id
  193. const label = index === "" ? "" : options[index].name
  194. this.returnItem({
  195. value,
  196. label
  197. })
  198. },
  199. timeConfirm(e) {
  200. let str = "",
  201. time1 = "",
  202. time2 = "",
  203. num = 0;
  204. if (e.detail.length === 2) {
  205. time1 = convertTime(e.detail[0]);
  206. time2 = convertTime(e.detail[1]);
  207. let timeStamp1 = new Date(e.detail[0]).valueOf(),
  208. timeStamp2 = new Date(e.detail[1]).valueOf(),
  209. timeType1 = convertTime(e.detail[0], 'MM.DD'),
  210. timeType2 = convertTime(e.detail[1], 'MM.DD'),
  211. conver = 1000 * 60 * 60 * 24;
  212. num = ((timeStamp2 - timeStamp1) / conver) + 1
  213. str = `${timeType1}-${timeType2},${num}天`
  214. }
  215. this.returnItem({
  216. value: time1,
  217. label: str,
  218. count: num,
  219. end: time2
  220. })
  221. },
  222. bindViewTap(e) {
  223. const {
  224. pindex,
  225. index
  226. } = e.currentTarget.dataset
  227. this.returnItem({
  228. value: this.data.cityList[pindex].list[index].value,
  229. label: this.data.cityList[pindex].list[index].label,
  230. })
  231. },
  232. //复选框确定
  233. checkboxSelected() {
  234. const options = this.data.popupOptions
  235. let list = [],
  236. str = "";
  237. options.forEach((s) => {
  238. if (s.selected) {
  239. list.push(s.id)
  240. str += str === '' ? s.name : `+${s.name}`
  241. }
  242. });
  243. this.returnItem({
  244. value: list,
  245. label: str,
  246. })
  247. },
  248. //复选框选择
  249. checkboxChange(e) {
  250. const index = Number(e.currentTarget.dataset.index)
  251. const options = this.data.popupOptions
  252. options[Number(index)].selected = !options[Number(index)].selected
  253. this.setData({
  254. popupOptions: options
  255. });
  256. },
  257. onItemClick(e) {
  258. const {
  259. value,
  260. label
  261. } = e.detail
  262. this.returnItem({
  263. value,
  264. label,
  265. })
  266. },
  267. onClickLeft() {
  268. this.triggerEvent("leftClick")
  269. },
  270. returnItem(model) {
  271. let modal = {
  272. popupType: this.data.popupType,
  273. popupKey: this.data.popupKey,
  274. ...model
  275. }
  276. this.triggerEvent("itemClick", modal)
  277. },
  278. async getAreaList() {
  279. const {
  280. code,
  281. data
  282. } = await https.getArea({
  283. level: 2
  284. })
  285. let arr = code === 1 ? data.list : [];
  286. arr.map((s) => {
  287. return s.value = s.id, s.label = s.name
  288. })
  289. return arr
  290. },
  291. showToastMsg(msg: string) {
  292. wx.showToast({
  293. title: msg,
  294. icon: 'none',
  295. duration: 2000
  296. })
  297. },
  298. }
  299. })