index.js 7.3 KB

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