index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. import url from '../../url'
  2. import net from '../../net'
  3. import math from '../../math'
  4. Page({
  5. data: {
  6. // nav: [
  7. // { i: '../../imgs/shuiwu.png', t: '节税筹划' },
  8. // { i: '../../imgs/yanglao.png', t: '社保公积金计算' },
  9. // { i: '../../imgs/wuxian.png', t: '社保代缴' }
  10. // ],
  11. pageStatus: false,
  12. listStatus: false,
  13. hiddenList: true,
  14. hiddenGjj: true,
  15. cantap: true,
  16. mask: false,
  17. shebao: '',
  18. fund: '',
  19. pickerIndex: [0, 0],
  20. typeData: [{ name: '二档', index: 0 }, { name: '一档', index: 1 }],
  21. hukouData: [{ name: '本地城镇', index: 0 }, { name: '本地农村', index: 1 }, { name: '外地城镇', index: 2 }, { name: '外地农村', index: 3 }],
  22. },
  23. // navigation(e) {
  24. // let i = e.currentTarget.dataset.index
  25. // my.navigateTo({ url: `../web-view/web-view?i=${i}` })
  26. // },
  27. // goMiniProgram() {
  28. // my.navigateToMiniProgram({
  29. // appId: '2019031663552331',
  30. // success: (res) => {
  31. // console.log(JSON.stringify(res))
  32. // },
  33. // fail: (res) => {
  34. // console.log(JSON.stringify(res))
  35. // }
  36. // });
  37. // },
  38. show(e) {
  39. var className = e.target.dataset.class
  40. if (!this.data.cantap) return
  41. this.data.cantap = false
  42. // if (className === '.sbList' && this.data.hiddenList) {
  43. // this.scroll('.main')
  44. // let timer = setTimeout(() => {
  45. // this.scroll(className)
  46. // this.data.cantap = true
  47. // clearTimeout(timer)
  48. // }, 500);
  49. if (className === '.sbList') {
  50. if (this.data.hiddenList && this.data.hiddenGjj) {
  51. this.scroll('.main')
  52. } else if (this.data.hiddenList && !this.data.hiddenGjj) {
  53. this.scroll('.main')
  54. let timer = setTimeout(() => {
  55. this.scroll(className)
  56. }, 500);
  57. } else if (!this.data.hiddenList && this.data.hiddenGjj) {
  58. this.data.cantap = true
  59. return
  60. } else if (!this.data.hiddenList && !this.data.hiddenGjj) {
  61. this.scroll(className)
  62. }
  63. } else if (className === '.gjjList') {
  64. if (this.data.hiddenList && this.data.hiddenGjj) {
  65. this.scroll('.main')
  66. let timer = setTimeout(() => {
  67. this.scroll(".sbList")
  68. }, 500);
  69. } else if (this.data.hiddenList && !this.data.hiddenGjj) {
  70. this.scroll('.main')
  71. } else if (!this.data.hiddenList && this.data.hiddenGjj) {
  72. this.scroll(".sbList")
  73. } else if (!this.data.hiddenList && !this.data.hiddenGjj) {
  74. this.data.cantap = true
  75. return
  76. }
  77. } else {
  78. this.scroll(className)
  79. }
  80. let timer = setTimeout(() => {
  81. this.data.cantap = true
  82. clearTimeout(timer)
  83. }, 500);
  84. },
  85. scroll(className) {
  86. const obj1 = { h: 'mainHeight', oh: 'mainOriginHeight', hidden: 'hiddenList', status: 'pageStatus' },
  87. obj2 = { h: 'sbHeight', oh: 'sbOriginHeight', hidden: 'hiddenGjj', status: 'listStatus' }
  88. var obj = className === '.main' ? obj1 : obj2
  89. if (this.data[obj.hidden]) {
  90. var el = my.createSelectorQuery().select(className);
  91. el.boundingClientRect().exec((data) => {
  92. var h = data[0].height + 'px'
  93. this.setData({ [obj.h]: h, [obj.oh]: h, [obj.hidden]: false, [obj.status]: true })
  94. let timer = setTimeout(() => {
  95. this.setData({ [obj.h]: 0 })
  96. clearTimeout(timer)
  97. }, 0);
  98. })
  99. } else {
  100. this.setData({ [obj.h]: this.data[obj.oh], [obj.status]: false })
  101. let timer = setTimeout(() => {
  102. this.setData({ [obj.hidden]: true, [obj.h]: '' })
  103. clearTimeout(timer)
  104. }, 500);
  105. }
  106. },
  107. showPicker(e) {
  108. var pickerType = e.target.dataset.pickerType, pickerIndex = this.getPickerIndex(pickerType)
  109. this.setData({ bottom: 0, mask: true, pickerType, pickerIndex })
  110. this.changePickerData()
  111. },
  112. hiddePicker() {
  113. this.setData({ bottom: -this.data.pickerHeight, mask: false, currentValue: '' })
  114. },
  115. changePickerData() {
  116. switch (this.data.pickerType) {
  117. case '0':
  118. var p = this.data.cityData.p,
  119. c = this.getCity(p[this.data.pickerIndex[0]].id)
  120. this.setData({ pickerData: [p, c] })
  121. this.data.currentValue = c[this.data.pickerIndex[1]]
  122. break;
  123. case '1':
  124. this.setData({ pickerData: [this.data.typeData] })
  125. this.data.currentValue = this.data.pickerData[0][this.data.pickerIndex[0]]
  126. break;
  127. case '2':
  128. this.setData({ pickerData: [this.data.hukouData] })
  129. this.data.currentValue = this.data.pickerData[0][this.data.pickerIndex[0]]
  130. break;
  131. case '3':
  132. this.setData({ pickerData: [this.data.scaleData] })
  133. this.data.currentValue = this.data.pickerData[0][this.data.pickerIndex[0]]
  134. break;
  135. default:
  136. break;
  137. }
  138. },
  139. scrollPicker(e) {
  140. let index = e.detail.value[0]
  141. if (this.data.pickerType != '0') {
  142. this.data.currentValue = this.data.pickerData[0][index]
  143. } else {
  144. let index2 = e.detail.value[1], p = this.data.cityData.p,
  145. c = this.getCity(p[index].id)
  146. if (e.detail.value[0] != this.data.pickerIndex[0]) {
  147. this.data.pickerIndex = [e.detail.value[0], 0]
  148. } else {
  149. this.data.pickerIndex = e.detail.value
  150. }
  151. this.setData({ pickerData: [p, c], pickerIndex: this.data.pickerIndex })
  152. this.data.currentValue = this.data.pickerData[1][this.data.pickerIndex[1]]
  153. }
  154. },
  155. sureAction() {
  156. switch (this.data.pickerType) {
  157. case '0':
  158. this.data.city = this.data.currentValue
  159. this.setData({ city: this.data.city, shebao: '', fund: '', type: this.data.typeData[0] })
  160. this.getData(this.data.currentValue.id)
  161. break;
  162. case '1':
  163. this.data.type = this.data.currentValue
  164. this.setData({ type: this.data.type, wxyj: this.data.wxyjData[this.data.currentValue.index] })
  165. break;
  166. case '2':
  167. this.data.hukou = this.data.currentValue
  168. this.setData({ hukou: this.data.hukou })
  169. break;
  170. case '3':
  171. this.data.scale = this.data.currentValue
  172. this.setData({ scale: this.data.scale })
  173. break;
  174. default:
  175. break;
  176. }
  177. this.calc()
  178. this.hiddePicker()
  179. },
  180. getPickerIndex(pickerType) {
  181. var pickerIndex
  182. switch (pickerType) {
  183. case '0':
  184. const value = this.data.city,
  185. cityData = this.data.cityData
  186. let index1, index2
  187. cityData.p.map((item, index) => {
  188. if (item.id == value.pid) index1 = index
  189. })
  190. var city = this.getCity(cityData.p[index1].id)
  191. city.map((item, index) => {
  192. if (item.id == value.id) index2 = index
  193. })
  194. pickerIndex = [index1, index2]
  195. this.setData({ pickerData: [cityData.p, city] })
  196. break;
  197. case '1':
  198. pickerIndex = [this.data.type.index]
  199. break;
  200. case '2':
  201. pickerIndex = [this.data.hukou.index]
  202. break;
  203. case '3':
  204. pickerIndex = [this.data.scale.index]
  205. break;
  206. default:
  207. break;
  208. }
  209. return pickerIndex
  210. },
  211. getCity(id) {
  212. return this.data.cityData.c.filter((item) => {
  213. if (item.pid == id)
  214. return item
  215. })
  216. },
  217. getData(id) {
  218. const type = this.data.type.index
  219. net.request(
  220. url.baseUrl + url.getData,
  221. 'GET',
  222. { city_code: id }
  223. ).then((res) => {
  224. this.setData({ wxyjData: res.data, wxyj: res.data[type] })
  225. this.calc()
  226. })
  227. },
  228. inputAction(e) {
  229. let value = Number(e.detail.value) || ''
  230. if (e.target.dataset.input == 'shebao') {
  231. this.setData({ shebao: value })
  232. } else {
  233. this.setData({ fund: value })
  234. }
  235. this.calc()
  236. },
  237. calc() {
  238. this.calcShebao()
  239. this.calcFund()
  240. this.setData({ totalMoney: math.Addition(this.data.shebaoMoney, this.data.fundMoney) })
  241. },
  242. calcShebao() {
  243. const calcData = this.data.wxyj.hukouItemList[this.data.hukou.index].itemDataList
  244. var shebaoList = [], shebaoListTotal = [], fundList = [], arr, jishu, pm, em, pPrice = 0, ePrice = 0, itemTotal, total = 0, first_total = 0
  245. if (!this.data.shebao) {
  246. this.setData({ shebaoMoney: 0, shebaoList: '' })
  247. return
  248. }
  249. calcData.map(item => {
  250. pm = em = 0
  251. if (this.data.shebao < item.min) {
  252. jishu = item.min
  253. } else if (this.data.shebao > item.max) {
  254. jishu = item.max
  255. } else {
  256. jishu = this.data.shebao
  257. }
  258. if (!item.ep && !item.pp && !item.max) {
  259. jishu = '--'
  260. em = itemTotal = item.min
  261. pm = '--'
  262. ePrice = math.Addition(em, ePrice)
  263. arr = [[item.n], [jishu], [em], [pm], [itemTotal]]
  264. } else {
  265. if (item.ep > 0) {
  266. em = math.Multiplication(jishu, math.Division(item.ep, 100))
  267. }
  268. if (item.pp > 0) {
  269. pm = math.Multiplication(jishu, math.Division(item.pp, 100))
  270. }
  271. ePrice = math.Addition(em, ePrice)
  272. pPrice = math.Addition(pm, pPrice)
  273. itemTotal = math.Addition(em, pm)
  274. arr = [[item.n], [jishu], [em, `${item.ep}%`], [pm, `${item.pp}%`], [itemTotal]]
  275. }
  276. total = math.Addition(total, itemTotal)
  277. shebaoList.push(arr)
  278. if (item.yearPay) {
  279. first_total = math.Subtraction(total, itemTotal)
  280. shebaoListTotal = [[ePrice, pPrice, total], [math.Subtraction(first_total, pPrice), pPrice, first_total]]
  281. } else {
  282. shebaoListTotal = [[ePrice, pPrice, total]]
  283. }
  284. })
  285. this.setData({ shebaoMoney: total, shebaoListTotal, shebaoList })
  286. },
  287. calcFund() {
  288. const calcData = this.data.wxyj
  289. let jishu = this.data.fund, avgFund = 0, fundMoney = 0
  290. if (jishu == '') {
  291. jishu = 0
  292. } else if (jishu < calcData.fund_min) {
  293. jishu = calcData.fund_min
  294. } else if (jishu > calcData.fund_max) {
  295. jishu = calcData.fund_max
  296. }
  297. if (jishu) {
  298. avgFund = math.Multiplication(jishu, math.Division(this.data.scale.value, 100))
  299. fundMoney = math.Multiplication(avgFund, 2)
  300. }
  301. this.setData({ fundForm: jishu, avgFund, fundMoney })
  302. },
  303. lowest() {
  304. this.setData({ shebao: this.data.wxyj.shebao_min })
  305. this.calc()
  306. },
  307. sameShebao() {
  308. let fund
  309. this.setData({ fund: this.data.shebao })
  310. if (this.data.shebao > this.data.wxyj.fund_max) {
  311. fund = this.data.wxyj.fund_max
  312. } else if (this.data.shebao <= this.data.wxyj.fund_max && this.data.shebao >= this.data.wxyj.fund_min) {
  313. fund = this.data.shebao
  314. } else {
  315. fund = this.data.wxyj.fund_min
  316. }
  317. this.calc()
  318. },
  319. init() {
  320. my.createSelectorQuery().select('.picker').boundingClientRect().exec((data) => {
  321. this.setData({ pickerHeight: data[0].height, bottom: -data[0].height })
  322. })
  323. net.request(url.baseUrl + url.getCity, 'POST').then((res) => {
  324. const p = res.data.filter((item) => {
  325. if (item.isCity == 0)
  326. return item
  327. })
  328. const c = res.data.filter((item) => {
  329. if (item.isCity == 1)
  330. return item
  331. })
  332. this.data.cityData = { p, c }
  333. var city = this.getCity(p[0].id)[0],
  334. type = this.data.typeData[0],
  335. hukou = this.data.hukouData[0],
  336. scale = this.data.scaleData[0]
  337. this.setData({ city, type, hukou, scale, currentValue: city })
  338. this.getData(c[0].id)
  339. })
  340. var arr = [], index = 0
  341. for (let i = 5; i < 21; i++) {
  342. arr[index] = { name: `${i}%`, index, value: i }
  343. index++
  344. }
  345. this.data.scaleData = arr
  346. },
  347. onLoad(query) {
  348. // 页面加载
  349. this.init()
  350. },
  351. onReady() {
  352. // 页面加载完成
  353. },
  354. onShow() {
  355. // 页面显示
  356. },
  357. onHide() {
  358. // 页面隐藏
  359. },
  360. onUnload() {
  361. // 页面被关闭
  362. },
  363. onTitleClick() {
  364. // 标题被点击
  365. },
  366. onPullDownRefresh() {
  367. // 页面被下拉
  368. },
  369. onReachBottom() {
  370. // 页面被拉到底部
  371. },
  372. onShareAppMessage() {
  373. // 返回自定义分享信息
  374. return {
  375. title: '社保公积金代缴计算器',
  376. desc: '社保公积金代缴计算器',
  377. path: 'pages/index/index',
  378. };
  379. },
  380. });