index.js 7.4 KB

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