exchange.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. var _request = require('../../../../utils/request.js')
  2. var _handle = require('../../../../utils/handle.js')
  3. Page({
  4. data: {
  5. page: 1,
  6. per_page: 10,
  7. curItem: {},
  8. radishCount: 0,
  9. checkIntegral: {},
  10. companyList: [],
  11. more: true,
  12. showPanel: false,
  13. showPwdDialog: false,
  14. ydCodeDialog: false,
  15. ydExchangeDialog: false,
  16. clogo: '',
  17. integralTotal: 0,
  18. originInToTal: 0,
  19. countdown: 0,
  20. servicePwd: '',
  21. phoneCode: '',
  22. useIntegral: 0,
  23. panelId: '',
  24. show: false,
  25. imgUrls: [
  26. ],
  27. indicatorDots: true,
  28. autoplay: true,
  29. interval: 5000,
  30. duration: 1000,
  31. circular: true,
  32. bindTel: false,
  33. checkData: {},
  34. lock: false
  35. },
  36. onLoad: function () {
  37. // this.getBalanceInfo()
  38. this.getCompanyList()
  39. this.check()
  40. this.getBanners()
  41. },
  42. onReachBottom: function () {
  43. if (this.data.more) {
  44. var page = this.data.page + 1
  45. this.setData({
  46. page: page
  47. })
  48. this.getCompanyList()
  49. }
  50. },
  51. getCode () {
  52. var that = this
  53. var url = 'v1/platforms/yidong/send_code'
  54. var params = {
  55. }
  56. var success = function (res) {
  57. wx.setStorageSync('sms_request_time', Date.now())
  58. that.startCountdown()
  59. }
  60. _request.$get(url, params, success)
  61. },
  62. startCountdown: function () {
  63. var smsRequestTime = wx.getStorageSync('sms_request_time')
  64. if (smsRequestTime) {
  65. var count = parseInt((60000 - (new Date() - smsRequestTime)) / 1000)
  66. this.setData({
  67. countdown: count
  68. })
  69. if (this.data.countdown > 0) {
  70. var timer = setInterval(() => {
  71. var countdown = this.data.countdown - 1
  72. this.setData({
  73. countdown: countdown
  74. })
  75. if (this.data.countdown <= 0) {
  76. clearInterval(timer)
  77. }
  78. }, 1000)
  79. }
  80. }
  81. },
  82. getBalanceInfo: function () {
  83. var that = this
  84. var url = "v1/user/balance/info";
  85. var params = {}
  86. var success = function (val) {
  87. that.setData({
  88. balanceInfo: val.data,
  89. bindTel : true
  90. });
  91. }
  92. _request.$get(url, params, success)
  93. },
  94. getCompanyList: function () {
  95. var that = this
  96. var url = "v1/platforms";
  97. var params = {
  98. page: that.data.page,
  99. per_page: that.data.per_page,
  100. cache: false
  101. }
  102. var success = function (res) {
  103. var result = that.data.companyList.concat(res.data.list || [])
  104. console.log(result)
  105. that.setData({
  106. companyList: result
  107. })
  108. var listMore = res.data.list_count > that.data.companyList.length
  109. that.setData({
  110. more: listMore
  111. })
  112. }
  113. _request.$get(url, params, success)
  114. },
  115. radishCountInput: function (e) {
  116. var that = this
  117. var item = that.data.curItem
  118. if (e.detail.value) {
  119. that.data.radishCount = Number(e.detail.value)
  120. that.data.integralTotal = (that.data.originInToTal - ((e.detail.value * item.p_count) / item.labi_count)).toFixed(2)
  121. } else if (e.detail.value === '') {
  122. that.data.integralTotal = 0
  123. }
  124. if ((that.data.originInToTal - that.data.integralTotal) * 100 % 100 === 0) {
  125. that.data.useIntegral = that.data.originInToTal - that.data.integralTotal
  126. } else {
  127. that.data.useIntegral = (that.data.originInToTal - that.data.integralTotal).toFixed(2)
  128. }
  129. this.setData({
  130. integralTotal: that.data.integralTotal,
  131. useIntegral: that.data.useIntegral
  132. })
  133. },
  134. checkUserIntegral: function (tag) {
  135. var that = this
  136. var url
  137. if (tag === 'liantong' || tag === 'yidong' || tag === 'yizhifu' ) {
  138. url = "v1/platform/" + tag + "/check";
  139. } else {
  140. url = "v1/platform/" + that.data.curItem.id + "/check";
  141. }
  142. var params
  143. if (tag === 'yidong') {
  144. params = {
  145. code: that.data.phoneCode
  146. }
  147. } else {
  148. params = {}
  149. }
  150. var success = function (val) {
  151. var integral
  152. var item = that.data.curItem
  153. that.data.originInToTal = val.data.total
  154. if (val.data.total >= 0 && that.data.curItem.tag === 'yidong') {
  155. that.ydDialog()
  156. that.setData({
  157. showPanel: that.data.showPanel,
  158. panelId: that.data.curItem.id,
  159. clogo: that.data.curItem.logo
  160. })
  161. }
  162. if (val.data.total) {
  163. // integral = item.p_start_count * item.labi_count / item.p_count
  164. // that.data.integralTotal = val.data.total - ((integral * item.p_count) / item.labi_count)
  165. integral = Math.floor(val.data.total / item.p_count) * item.labi_count
  166. that.data.integralTotal = (val.data.total - (integral * item.p_count / item.labi_count)).toFixed(2)
  167. that.data.radishCount = integral
  168. } else {
  169. integral = 0
  170. that.data.integralTotal = 0
  171. }
  172. if ((that.data.originInToTal - that.data.integralTotal) * 100 % 100 === 0) {
  173. that.data.useIntegral = that.data.originInToTal - that.data.integralTotal
  174. } else {
  175. that.data.useIntegral = (that.data.originInToTal - that.data.integralTotal).toFixed(2)
  176. }
  177. that.setData({
  178. checkIntegral: val.data,
  179. integralTotal: that.data.integralTotal,
  180. useIntegral: that.data.useIntegral,
  181. radishCount: integral,
  182. originInToTal: val.data.total
  183. });
  184. }
  185. _request.$get(url, params, success)
  186. },
  187. exchange: function (e) {
  188. _handle.setFormId(e)
  189. var that = this
  190. that.data.lock = true
  191. var ratio = e.detail.target.dataset.ratio
  192. var url = "v1/platform/" + e.detail.target.dataset.id + "/exchange"
  193. var params = {
  194. count: that.data.radishCount
  195. }
  196. var success = function (val) {
  197. that.data.lock = false
  198. wx.redirectTo({
  199. url: '/packageUser/pages/user/exchangeSuccess/exchangeSuccess'
  200. })
  201. }
  202. if (that.data.radishCount) {
  203. if (e.detail.target.dataset.tag === 'yizhifu') {
  204. that.getdianxin()
  205. } else {
  206. if (that.data.integralTotal >= 0) {
  207. if (that.data.radishCount % that.data.curItem.labi_range === 0) {
  208. if (e.detail.target.dataset.tag === 'liantong') {
  209. that.showDialog()
  210. } else if (e.detail.target.dataset.tag === 'yidong') {
  211. that.data.ydExchangeDialog = !that.data.ydExchangeDialog
  212. that.setData({
  213. ydExchangeDialog: that.data.ydExchangeDialog
  214. })
  215. } else {
  216. if (that.data.lock) {
  217. _request.$post(url, params, success)
  218. }
  219. }
  220. } else {
  221. wx.showToast({
  222. title: '请以' + that.data.curItem.labi_range + '的倍数输入代金券数',
  223. icon: 'none',
  224. duration: 2000
  225. })
  226. }
  227. } else {
  228. wx.showToast({
  229. title: '代金券余额不足~',
  230. icon: 'none',
  231. duration: 2000
  232. })
  233. }
  234. }
  235. } else {
  236. wx.showToast({
  237. title: '请输入兑换的代金券~',
  238. icon: 'none',
  239. duration: 2000
  240. })
  241. }
  242. },
  243. getdianxin () {
  244. var that = this
  245. var url = 'v1/pf/yizhifu/page/exchange'
  246. var params = {
  247. count: that.data.radishCount
  248. }
  249. var success = function (val) {
  250. console.log(val)
  251. wx.navigateTo({
  252. url: '/pages/web/web?id=' + val.result_str
  253. })
  254. }
  255. _request.$post(url, params, success)
  256. },
  257. servicePwdInput (e) {
  258. var that = this
  259. that.data.servicePwd = e.detail.value
  260. },
  261. phoneCodeInput (e) {
  262. var that = this
  263. that.data.phoneCode = e.detail.value
  264. },
  265. checkYDIntegral () {
  266. var that = this
  267. if (that.data.phoneCode) {
  268. that.openPanelTwo()
  269. } else {
  270. wx.showToast({
  271. title: '验证码不能为空',
  272. icon: 'none',
  273. duration: 1000
  274. })
  275. }
  276. },
  277. sumbit () {
  278. var that = this
  279. var url = "v1/platform/liantong/exchange"
  280. var params = {
  281. count: that.data.radishCount,
  282. pwd: that.data.servicePwd
  283. }
  284. var success = function (val) {
  285. wx.redirectTo({
  286. url: '/packageUser/pages/user/exchangeSuccess/exchangeSuccess'
  287. })
  288. }
  289. _request.$post(url, params, success)
  290. },
  291. sumbitYD () {
  292. var that = this
  293. var url = "v1/platform/yidong/exchange"
  294. var params = {
  295. count: that.data.radishCount,
  296. code: that.data.phoneCode
  297. }
  298. var success = function (val) {
  299. wx.redirectTo({
  300. url: '/packageUser/pages/user/exchangeSuccess/exchangeSuccess'
  301. })
  302. }
  303. if (that.data.phoneCode) {
  304. _request.$post(url, params, success)
  305. } else {
  306. wx.showToast({
  307. title: '验证码不能为空',
  308. icon: 'none',
  309. duration: 1000
  310. })
  311. }
  312. },
  313. openPanel (e) {
  314. var that = this
  315. that.data.curItem = e.currentTarget.dataset.item
  316. if (e.currentTarget.dataset.item.tag === 'yidong') {
  317. if (that.data.curItem.state === 1) {
  318. if (!that.data.showPanel) {
  319. that.ydDialog()
  320. that.setData({
  321. clogo: that.data.curItem.logo
  322. })
  323. } else {
  324. that.data.showPanel = !that.data.showPanel
  325. that.setData({
  326. showPanel: that.data.showPanel,
  327. transferR: 90,
  328. radishCount: 0,
  329. originInToTal: 0,
  330. integralTotal: 0
  331. })
  332. }
  333. }
  334. } //else if (e.currentTarget.dataset.item.tag === 'yizhifu') {
  335. // that.data.showPanel = !that.data.showPanel
  336. // that.data.show = !that.data.show
  337. // that.setData({
  338. // showPanel: that.data.showPanel,
  339. // show : that.data.show
  340. // })
  341. // }
  342. else {
  343. that.openPanelTwo()
  344. }
  345. },
  346. openPanelTwo () {
  347. var that = this
  348. if (that.data.curItem.state === 1) {
  349. that.data.showPanel = !that.data.showPanel
  350. if (that.data.showPanel) {
  351. that.checkUserIntegral(that.data.curItem.tag)
  352. if (that.data.curItem.tag === 'yizhifu') {
  353. that.setData({
  354. show: true
  355. })
  356. }
  357. that.setData({
  358. transferR: -90,
  359. // show: false
  360. })
  361. } else {
  362. that.setData({
  363. transferR: 90,
  364. radishCount: 0,
  365. originInToTal: 0,
  366. integralTotal: 0,
  367. show: false,
  368. })
  369. }
  370. if (that.data.curItem.tag !== 'yidong') {
  371. that.setData({
  372. showPanel: that.data.showPanel,
  373. panelId: that.data.curItem.id,
  374. clogo: that.data.curItem.logo,
  375. })
  376. }
  377. // if (that.data.curItem.tag === 'yizhifu') {
  378. // console.log(4444)
  379. // that.setData({
  380. // showPanel: that.data.showPanel,
  381. // panelId: that.data.curItem.id,
  382. // clogo: that.data.curItem.logo,
  383. // })
  384. // }
  385. }
  386. },
  387. showDialog () {
  388. var that = this
  389. that.data.showPwdDialog = !that.data.showPwdDialog
  390. that.setData({
  391. showPwdDialog: that.data.showPwdDialog
  392. })
  393. },
  394. ydDialog () {
  395. var that = this
  396. that.data.ydCodeDialog = !that.data.ydCodeDialog
  397. that.setData({
  398. ydCodeDialog: that.data.ydCodeDialog
  399. })
  400. },
  401. ydExDialog () {
  402. var that = this
  403. that.data.ydExchangeDialog = !that.data.ydExchangeDialog
  404. if (!that.data.ydExchangeDialog) {
  405. that.data.showPanel = !that.data.showPanel,
  406. that.setData({
  407. showPanel: that.data.showPanel,
  408. ydExchangeDialog: that.data.ydExchangeDialog
  409. })
  410. } else {
  411. that.setData({
  412. ydExchangeDialog: that.data.ydExchangeDialog
  413. })
  414. }
  415. },
  416. minus () {
  417. var that = this
  418. var item = that.data.curItem
  419. var minRadishCount = item.p_start_count * item.labi_count / item.p_count
  420. var left = that.data.radishCount - item.labi_range
  421. if (left >= minRadishCount) {
  422. that.data.radishCount = left
  423. that.data.integralTotal = (that.data.checkIntegral.total - ((that.data.radishCount * item.p_count) / item.labi_count)).toFixed(2)
  424. if ((that.data.originInToTal - that.data.integralTotal) * 100 % 100 === 0) {
  425. that.data.useIntegral = that.data.originInToTal - that.data.integralTotal
  426. } else {
  427. that.data.useIntegral = (that.data.originInToTal - that.data.integralTotal).toFixed(2)
  428. }
  429. that.setData({
  430. radishCount: that.data.radishCount,
  431. integralTotal: that.data.integralTotal,
  432. useIntegral: that.data.useIntegral
  433. })
  434. } else {
  435. wx.showToast({
  436. title: '兑换代金券数量不对',
  437. icon: 'none',
  438. duration: 2000
  439. })
  440. }
  441. },
  442. plus () {
  443. var that = this
  444. var item = that.data.curItem
  445. that.data.radishCount = that.data.radishCount + item.labi_range
  446. that.data.integralTotal = (that.data.checkIntegral.total - ((that.data.radishCount * item.p_count) / item.labi_count)).toFixed(2)
  447. if ((that.data.originInToTal - that.data.integralTotal) * 100 % 100 === 0) {
  448. that.data.useIntegral = that.data.originInToTal - that.data.integralTotal
  449. } else {
  450. that.data.useIntegral = (that.data.originInToTal - that.data.integralTotal).toFixed(2)
  451. }
  452. that.setData({
  453. radishCount: that.data.radishCount,
  454. integralTotal: that.data.integralTotal,
  455. useIntegral: that.data.useIntegral
  456. })
  457. },
  458. toUrl () {
  459. var that = this
  460. if (that.data.curItem.get_url_type === 'internal') {
  461. wx.navigateTo({
  462. url: that.data.curItem.get_url
  463. })
  464. } else if (that.data.curItem.get_url_type === 'external') {
  465. wx.navigateTo({
  466. url: '/pages/web/web?id=' + that.data.curItem.get_url
  467. })
  468. } else {
  469. wx.showToast({
  470. title: '未设置链接',
  471. icon: 'none',
  472. duration: 2000
  473. })
  474. }
  475. },
  476. check () {
  477. var that = this
  478. var url = 'v1/user/check'
  479. var params = {
  480. }
  481. var success = function (res) {
  482. if (res.data.wx_user_id <= 0) {
  483. wx.login({
  484. success: res => {
  485. _request.login(res)
  486. }
  487. })
  488. } else {
  489. that.setData({
  490. checkData: res.data
  491. })
  492. if (that.data.checkData.merchant_id > 0) {
  493. // that.getmerchant()
  494. }
  495. if (res.data.user_id > 0 && !that.data.bindTel) {
  496. that.setData({
  497. bindTel: true
  498. })
  499. that.getBalanceInfo()
  500. }
  501. }
  502. }
  503. _request.$get(url, params, success)
  504. },
  505. getBanners () {
  506. var item = 'lol'
  507. var that = this
  508. var url = 'v1/ad/' + item + '/items'
  509. var params = {
  510. }
  511. var success = function (res) {
  512. that.setData({
  513. imgUrls: res.data
  514. })
  515. }
  516. _request.$get(url, params, success)
  517. },
  518. onShareAppMessage: function (val) {
  519. return _request.share({
  520. sc: 'xcx_user_exchange'
  521. })
  522. }
  523. })