vip.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. import {
  2. get,
  3. post
  4. } from '../../utils/http'
  5. import {
  6. formatActivity
  7. } from '../../utils/time'
  8. var WxParse = require('../../components/local/wxParse/wxParse.js')
  9. Page({
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. list: [],
  15. visible: false,
  16. userInfo: null,
  17. current: 0,
  18. activity: {},
  19. wxParseMemberRules: false, // 是否已解析
  20. pay_methon: 1,
  21. payNow: false,
  22. },
  23. pay_methonw() {
  24. this.setData({
  25. pay_methon: 1
  26. })
  27. },
  28. pay_methonc() {
  29. this.setData({
  30. pay_methon: 2
  31. })
  32. },
  33. onClosePay() {
  34. this.setData({
  35. payNow: false
  36. })
  37. },
  38. /**
  39. * 获取我的储值
  40. * api/user
  41. */
  42. getAmount() {
  43. get('api/user/amount', {}, (res) => {
  44. if (res.data) {
  45. this.setData({
  46. inserllAmount: res.data.amount
  47. })
  48. }
  49. })
  50. },
  51. /**
  52. * 生命周期函数--监听页面加载
  53. */
  54. onLoad: function (options) {
  55. // if(options.userInfo) {
  56. // let userInfo = decodeURIComponent((options.userInfo));
  57. // this.setData({ userInfo: JSON.parse(userInfo) })
  58. // }
  59. this.setData({
  60. userInfo: wx.getStorageSync('userInfo'),
  61. })
  62. this.getCard()
  63. this.getUserActivity()
  64. console.log(getApp().globalData)
  65. let that = this
  66. // 获取会员守则
  67. this.getmemberRules()
  68. .then((memberRules) => {
  69. WxParse.wxParse('article', 'html', memberRules, that, 5)
  70. that.setData({
  71. wxParseMemberRules: true,
  72. })
  73. })
  74. .catch(() => {})
  75. },
  76. /**
  77. * 生命周期函数--监听页面初次渲染完成
  78. */
  79. onReady: function () {},
  80. /**
  81. * 生命周期函数--监听页面显示
  82. */
  83. onShow: function () {
  84. this.getAmount()
  85. },
  86. /**
  87. * 生命周期函数--监听页面隐藏
  88. */
  89. onHide: function () {},
  90. /**
  91. * 生命周期函数--监听页面卸载
  92. */
  93. onUnload: function () {
  94. if (this.timer_) {
  95. clearInterval(this.timer_)
  96. }
  97. },
  98. /**
  99. * 页面相关事件处理函数--监听用户下拉动作
  100. */
  101. onPullDownRefresh: function () {},
  102. /**
  103. * 页面上拉触底事件的处理函数
  104. */
  105. onReachBottom: function () {},
  106. /**
  107. * 用户点击右上角分享
  108. */
  109. onShareAppMessage: function () {},
  110. onPay: function () {
  111. this.setData({
  112. payNow: true,
  113. })
  114. },
  115. /**
  116. * 去支付
  117. */
  118. payNow() {
  119. let {
  120. list,
  121. current
  122. } = this.data
  123. let that = this
  124. this.checkFirst()
  125. .then((res) => {
  126. post(
  127. 'api/card', {
  128. card_id: list[current].id,
  129. },
  130. (res) => {
  131. this.setData({
  132. order_id: res.data.order_id,
  133. })
  134. post(
  135. 'api/pay/card', {
  136. pay_way: this.data.pay_methon == 1 ? 'weixin' : 'amount',
  137. order_id: res.data.order_id,
  138. },
  139. (res) => {
  140. that.setData({
  141. payNow: false
  142. })
  143. if (that.data.pay_methon == 2) {
  144. wx.showToast({
  145. title: '支付成功',
  146. icon: 'none',
  147. })
  148. setTimeout(() => {
  149. that.setData({
  150. visible: true
  151. })
  152. }, 1000)
  153. return
  154. }
  155. wx.requestPayment({
  156. timeStamp: res.data.pay_data.timeStamp,
  157. nonceStr: res.data.pay_data.nonceStr,
  158. package: res.data.pay_data.package,
  159. signType: res.data.pay_data.signType,
  160. paySign: res.data.pay_data.paySign,
  161. success(res) {
  162. wx.showToast({
  163. title: '支付成功',
  164. icon: 'none',
  165. })
  166. setTimeout(() => {
  167. that.setData({
  168. visible: true
  169. })
  170. }, 1000)
  171. },
  172. fail(res) {
  173. wx.showToast({
  174. title: '支付失败',
  175. icon: 'error',
  176. })
  177. },
  178. })
  179. }
  180. )
  181. }
  182. )
  183. })
  184. .catch((rej) => {
  185. wx.navigateTo({
  186. url: '/pages/disclaimer/disclaimer',
  187. })
  188. })
  189. },
  190. /**
  191. * 暂不激活
  192. */
  193. onCancelUse() {
  194. this.onClose('visible')
  195. wx.redirectTo({
  196. url: '/pages/vipCard/vipCard',
  197. })
  198. },
  199. /**
  200. * 确认激活
  201. */
  202. onConfirmUse() {
  203. post(
  204. 'api/card/active', {
  205. order_id: this.data.order_id,
  206. },
  207. (res) => {
  208. console.log(res)
  209. this.onClose('visible')
  210. wx.showToast({
  211. title: '激活成功',
  212. icon: 'none',
  213. })
  214. setTimeout(() => {
  215. wx.navigateBack()
  216. }, 1500)
  217. }
  218. )
  219. },
  220. /**
  221. * 关闭弹框
  222. */
  223. onClose(key) {
  224. this.setData({
  225. [key]: false,
  226. })
  227. },
  228. /**
  229. * 选择vip
  230. */
  231. onChangeVip(e) {
  232. this.setData({
  233. current: e.currentTarget.dataset.index,
  234. })
  235. },
  236. /**
  237. * 获取会员卡列表
  238. * /api/card
  239. */
  240. getCard() {
  241. get('api/card', {}, (res) => {
  242. this.setData({
  243. list: res.data,
  244. })
  245. console.log(res)
  246. })
  247. },
  248. /**
  249. * 是否首次购买
  250. * /api/order/check_first
  251. */
  252. checkFirst() {
  253. return new Promise((resolve, reject) => {
  254. if (wx.getStorageSync('agree')) {
  255. resolve()
  256. } else {
  257. get('api/order/check_first', {}, (res) => {
  258. if (res.data.order_status == 1) {
  259. resolve()
  260. } else {
  261. reject()
  262. }
  263. })
  264. }
  265. })
  266. },
  267. /**
  268. * 获取首次开卡优惠
  269. * api/user/activity
  270. */
  271. getUserActivity() {
  272. if (this.timer) {
  273. clearInterval(this.timer_)
  274. }
  275. get('api/user/activity', {}, (res) => {
  276. if (res.data) {
  277. res.data.currentTime = formatActivity(res.data.expire_time_seconds)
  278. this.setData({
  279. activity: res.data,
  280. })
  281. this.timer_ = setInterval(() => {
  282. if (res.data.expire_time_seconds <= 0) {
  283. clearInterval(this.timer_)
  284. }
  285. res.data.expire_time_seconds--
  286. res.data.currentTime = formatActivity(res.data.expire_time_seconds)
  287. this.setData({
  288. activity: res.data,
  289. })
  290. }, 1000)
  291. }
  292. })
  293. },
  294. /**
  295. * 获取会员守则
  296. */
  297. getmemberRules() {
  298. let memberRules = getApp().globalData.memberRules
  299. return new Promise((resolve, reject) => {
  300. if (memberRules) {
  301. resolve(memberRules)
  302. } else {
  303. get(
  304. 'api/agreement', {},
  305. (res) => {
  306. memberRules = res.data.member_rules
  307. memberRules = this.removeCss(memberRules)
  308. getApp().globalData.memberRules = memberRules
  309. resolve(memberRules)
  310. },
  311. () => {
  312. reject()
  313. }
  314. )
  315. }
  316. })
  317. },
  318. /**
  319. * 去除富文本图片默认样式
  320. */
  321. removeCss(content) {
  322. let reg = /(style|class)="[^"]+"/gi
  323. let img = /<img[^>]+>/gi
  324. let res
  325. if (img.test(content)) {
  326. res = content.match(img)
  327. for (let i = 0; i < res.length; i++) {
  328. content = content.replace(res[i], res[i].replace(reg, ''))
  329. }
  330. }
  331. return content.replace(/\<img/gi, '<img class="richImg" ')
  332. },
  333. })