integral.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. // pages/integral/integral.js
  2. import Integralinfo from '../../api/integralinfo'
  3. import {getMobileCache, getPhoneNumberNew as getPhoneNumberSync} from '../../utils/user'
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. lock: false,
  10. noResult: false,
  11. noMore: false,
  12. bannerList: [],
  13. couponList: [],
  14. channelid: '',
  15. shopid: '',
  16. storeid: '',
  17. bodyHidden: true,
  18. bgStatus: false,
  19. scorePhone: false,
  20. animationData: {},
  21. integralIngo:{},
  22. loading: false,
  23. setPhone: '',
  24. current: '',
  25. integralNum: 0,
  26. phone: '********',
  27. // phone: '15888282621',
  28. mobile: '********',
  29. paymentStatus: false,
  30. paymentBgStatus: false,
  31. activeNum: 0,
  32. listTrue: [],
  33. listStatus: false,
  34. total: 0,
  35. row: {},
  36. codeName: '60s后重新获取',
  37. codeTime: '',
  38. active1obj: {
  39. orderId: '',
  40. order_no: '',
  41. step: ''
  42. },
  43. active1code: '',
  44. active2code: '',
  45. tgid: '',
  46. scene: '',
  47. noClick: true,
  48. showToastObj: {
  49. icon: '',
  50. title: '',
  51. status: false
  52. }
  53. },
  54. /**
  55. * 生命周期函数--监听页面加载
  56. */
  57. onLoad: function (options) {
  58. var reg = /^(\d{3})\d{4}(\d{4})$/;
  59. this.setData({
  60. isLogin: getMobileCache() != '',
  61. phone: getMobileCache() == ''?'********':getMobileCache().replace(reg, "$1****$2"),
  62. setPhone: getMobileCache(),
  63. })
  64. },
  65. /**
  66. * 生命周期函数--监听页面初次渲染完成
  67. */
  68. onReady: function () {
  69. },
  70. /**
  71. * 生命周期函数--监听页面显示
  72. */
  73. onShow: function () {
  74. this.getBannerList()
  75. this.getCouponList()
  76. },
  77. /**
  78. * 生命周期函数--监听页面隐藏
  79. */
  80. onHide: function () {
  81. },
  82. /**
  83. * 生命周期函数--监听页面卸载
  84. */
  85. onUnload: function () {
  86. },
  87. /**
  88. * 页面相关事件处理函数--监听用户下拉动作
  89. */
  90. onPullDownRefresh: function () {
  91. },
  92. /**
  93. * 页面上拉触底事件的处理函数
  94. */
  95. onReachBottom: function () {
  96. },
  97. /**
  98. * 用户点击右上角分享
  99. */
  100. onShareAppMessage: function () {
  101. },
  102. getBannerList: function() {
  103. Integralinfo.getBannerList({
  104. channelid: this.data.channelid,
  105. shopid: this.data.shopid,
  106. storeid: this.data.storeid
  107. }).then(res => {
  108. console.log(res.data)
  109. this.setData({
  110. bannerList: res.data
  111. })
  112. }).catch(err => {
  113. console.log(err)
  114. })
  115. },
  116. getCouponList: function() {
  117. Integralinfo.getList({
  118. channelid: this.data.channelid,
  119. shopid: this.data.shopid,
  120. storeid: this.data.storeid
  121. }).then(res => {
  122. this.couponListView(res.data)
  123. this.data.lock = false
  124. }).catch(_ => {
  125. console.log(_)
  126. this.data.lock = false
  127. })
  128. },
  129. couponListView: function(data) {
  130. if (!Array.isArray(data) || data.length == 0) {
  131. console.log("优惠券列表数据为空");
  132. }
  133. data.forEach((v) => {
  134. v.quantity = 0
  135. v.integral = v.price * v.ratio
  136. })
  137. let num = this.data.integralNum > 20000 ? 19999 : this.data.integralNum
  138. const arr = []
  139. for (let i = 0; i < data.length; i++) {
  140. data[i].quantity = parseInt(num / data[i].integral)
  141. if (data[i].quantity > 0) {
  142. num -= (data[i].quantity * data[i].price) * data[i].ratio
  143. this.total += (data[i].quantity * data[i].price) * data[i].ratio
  144. arr.push(data[i])
  145. data.splice(i, 1)
  146. i--
  147. }
  148. }
  149. this.setData({
  150. couponList: [...arr, ...data]
  151. })
  152. console.log(this.data.couponList)
  153. },
  154. // 点击标识点触发
  155. detailsClick(row) {
  156. this.hideModal();
  157. this.row = row.currentTarget.dataset.info
  158. console.log(this.row)
  159. if (this.row.detail_type === 0) {
  160. this.row.detailArr = this.row.detail.split('\n')
  161. } else {
  162. this.row.detailArr = row.detail
  163. }
  164. this.setData({
  165. integralIngo: this.row,
  166. })
  167. console.log(this.data.integralIngo)
  168. setTimeout(() => {
  169. this.showDodal();
  170. }, 100)
  171. },
  172. showDodal() {
  173. var that = this;
  174. // 显示遮罩层
  175. var animation = wx.createAnimation({
  176. duration: 150,
  177. timingFunction: "linear",
  178. delay: 0
  179. })
  180. this.setData({
  181. bgStatus: true,
  182. bodyHidden: false
  183. })
  184. that.animation = animation
  185. animation.translateY(-500).step()
  186. that.setData({
  187. animationData: animation.export()
  188. })
  189. },
  190. //隐藏对话框
  191. hideModal() {
  192. var that = this;
  193. console.log(that.data)
  194. // 隐藏遮罩层
  195. var animation = wx.createAnimation({
  196. duration: 150,
  197. timingFunction: "linear",
  198. delay: 0
  199. })
  200. this.setData({
  201. bodyHidden: true,
  202. bgStatus: false,
  203. scorePhone: false,
  204. })
  205. that.animation = animation
  206. animation.translateY(0).step()
  207. that.setData({
  208. animationData: animation.export()
  209. })
  210. },
  211. /**授权手机号 */
  212. getPhoneNumber(e) {
  213. getPhoneNumberSync(e, async () => {
  214. console.log(e)
  215. if (getMobileCache() == '') {
  216. wx.showToast({
  217. icon:'error',
  218. title:'先查询移动积分'
  219. })
  220. setTimeout(()=>{
  221. that.data.setPhone = ''
  222. that.scoreClick()
  223. wx.hideLoading()
  224. },500)
  225. } else {
  226. var reg = /^(\d{3})\d{4}(\d{4})$/;
  227. this.setData({
  228. isLogin: true,
  229. setPhone: getMobileCache(),
  230. mobile: getMobileCache(),
  231. phone: getMobileCache().replace(reg, "$1****$2")
  232. })
  233. // console.log(this.data.setPhone)
  234. // console.log(this.data.mobile)
  235. // console.log(this.data.phone)
  236. // this.getphoneClick()
  237. }
  238. })
  239. },
  240. // 查询积分
  241. scoreClick() {
  242. this.setData({
  243. bgStatus: true,
  244. scorePhone: true,
  245. })
  246. },
  247. // 查询积分
  248. getphoneClick() {
  249. console.log(this.data.setPhone)
  250. if (!(/^1[3456789]\d{9}$/.test(this.data.setPhone))) {
  251. wx.showToast({
  252. title: '填写正确手机号',
  253. icon: 'error',
  254. duration: 2000
  255. });
  256. } else if (this.data.setPhone != '') {
  257. // wx.showLoading({
  258. // title: '加载中'
  259. // });
  260. Integralinfo.getBalance({
  261. mobile: this.data.setPhone,
  262. callbackUrl: '../integral/integral.wxml',
  263. fingerprint: '',
  264. sessionid: '',
  265. shopid: 2,
  266. type: 'wx',
  267. }).then(arr => {
  268. console.log(arr)
  269. wx.hideLoading();
  270. if (arr.code === '0000') {
  271. this.data.integralNum = arr.data
  272. var reg = /^(\d{3})\d{4}(\d{4})$/;
  273. this.data.mobile = this.data.setPhone
  274. this.data.setPhone = this.data.setPhone.replace(reg, "$1****$2");
  275. let response = {
  276. points: arr.data,
  277. outTokenId: this.data.setPhone + '_out'
  278. }
  279. App.globalData.AppShow = response
  280. this.hideModal()
  281. this.getCouponList()
  282. wx.pageScrollTo({
  283. scrollTop: 0
  284. });
  285. let dateNum = Math.round(new Date().getTime() / 1000).toString()
  286. if (wx.getStorageSync('orderDetails') != '' && this.data.mobile === JSON.parse(wx
  287. .getStorageSync('orderDetails')).mobile) {
  288. if (dateNum - JSON.parse(wx.getStorageSync('orderDetails')).date <= 120) {
  289. const data = JSON.parse(wx.getStorageSync('orderDetails'))
  290. if (dateNum - data.date < 60) {
  291. let time = dateNum - data.date
  292. this.codeName = 60 - time + 's后重新获取'
  293. this.codeSetInterval(60 - time)
  294. } else {
  295. this.codeName = '重新获取验证码'
  296. }
  297. this.data.paymentStatus = true
  298. this.data.paymentBgStatus = true
  299. this.data.active1code = ''
  300. this.data.active2code = ''
  301. this.data.active1obj = data
  302. if (data.step === 1) {
  303. this.data.activeNum = 1
  304. } else if (data.step === 2) {
  305. this.data.activeNum = 0
  306. }
  307. }
  308. }
  309. } else {
  310. this.data.loading = true
  311. this.data.bgStatus = true
  312. setTimeout(() => {
  313. // #ifdef H5
  314. window.location.href = arr.message;
  315. // #endif
  316. // #ifdef MP-WEIXIN
  317. const version = wx.getSystemInfoSync().SDKVersion
  318. if (this.compareVersion(version, '2.20.1') >= 0) {
  319. wx.openEmbeddedMiniProgram({
  320. appId: arr.data.appId,
  321. path: arr.data.path,
  322. extraData: arr.data.extraData,
  323. success(res) {
  324. // 打开成功
  325. },
  326. fail() {}
  327. })
  328. } else {
  329. // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示
  330. wx.navigateToMiniProgram({
  331. appId: arr.data.appId,
  332. path: arr.data.path,
  333. extraData: arr.data.extraData,
  334. success(res) {
  335. // 打开成功
  336. },
  337. fail() {
  338. }
  339. })
  340. }
  341. // #endif
  342. this.hideModal()
  343. this.loading = false
  344. }, 4000)
  345. }
  346. })
  347. }
  348. },
  349. // 商品数量减少按钮
  350. reduceClick(e) {
  351. console.log(e.currentTarget.dataset.index)
  352. let index = e.currentTarget.dataset.index
  353. this.data.couponList[index].quantity--
  354. this.data.total -= parseInt(this.data.couponList[index].integral)
  355. },
  356. // 商品数量添加按钮
  357. addClick(e) {
  358. let index = e.currentTarget.dataset.index
  359. if (this.data.phone !== '********') {
  360. if (this.data.total + parseInt(this.data.couponList[index].integral) > 20000) {
  361. wx.showToast({
  362. title: '单笔限额两万分',
  363. icon: 'error',
  364. duration: 2000
  365. });
  366. } else if ((this.data.integralNum - this.data.total) >= this.data.couponList[index].integral) {
  367. this.data.couponList[index].quantity++
  368. this.data.total += parseInt(this.data.couponList[index].integral)
  369. } else {
  370. wx.showToast({
  371. title: '积分不足',
  372. icon: 'error',
  373. duration: 2000
  374. });
  375. }
  376. } else {
  377. wx.showToast({
  378. title: '先查询移动积分',
  379. icon: 'error',
  380. duration: 1000
  381. });
  382. setTimeout(() => {
  383. this.scoreClick()
  384. }, 1000)
  385. }
  386. },
  387. /** 刷新 */
  388. shuaxinClick() {
  389. Integralinfo.getBalance({
  390. mobile: this.data.setPhone,
  391. callbackUrl: '../integral/integral.wxml',
  392. fingerprint: '',
  393. sessionid: '',
  394. shopid: 2,
  395. type: 'wx',
  396. }).then(arr => {
  397. if (arr.code === '0000') {
  398. wx.hideLoading();
  399. wx.showToast({
  400. title: '查询成功!',
  401. icon: 'success',
  402. duration: 1000
  403. });
  404. this.data.integralNum = arr.data
  405. var reg = /^(\d{3})\d{4}(\d{4})$/;
  406. this.data.setPhone = this.data.mobile
  407. this.data.phone = this.data.setPhone.replace(reg, "$1****$2");
  408. }
  409. })
  410. },
  411. })