index.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import {
  2. getMyInfo,
  3. androidbuyVip,
  4. buyVip,
  5. getVipInfo,
  6. getWxQrcode,
  7. getLearnCard
  8. } from '~/api/user'
  9. import event from '~/mixins/event'
  10. import {
  11. getProducts,
  12. userEvent
  13. } from '~/api/global'
  14. import {
  15. createStoreBindings
  16. } from 'mobx-miniprogram-bindings'
  17. import {
  18. store
  19. } from '~/store/index'
  20. const app = getApp()
  21. Page({
  22. behaviors: [event],
  23. data: {
  24. userInfo: {},
  25. vipTime: '',
  26. tasks: [],
  27. isIos: app.globalData.isIOS,
  28. qrCode: '',
  29. activationModal: false,
  30. activationRes: {},
  31. products: [],
  32. isPreferential: false
  33. },
  34. async onLoad() {
  35. // 手工绑定
  36. this.storeBindings = createStoreBindings(this, {
  37. store,
  38. actions: {
  39. setUser: 'setUser'
  40. }
  41. })
  42. if (this.data.isIos) {
  43. let qrCode = await getWxQrcode()
  44. this.setData({
  45. qrCode: qrCode.ticketUrl
  46. })
  47. }
  48. this.openDonutBuy({currentTarget:[]})
  49. },
  50. async onShow() {
  51. if (typeof this.getTabBar === 'function') {
  52. this.getTabBar().setData({
  53. selected: 4
  54. })
  55. }
  56. let uid = wx.getStorageSync('uid') || ''
  57. if (!uid) {
  58. getApp().callBack = (res) => {
  59. this.setUserInfo()
  60. }
  61. } else {
  62. this.setUserInfo()
  63. }
  64. },
  65. // 设置用户信息及vip状态
  66. async setUserInfo() {
  67. let userInfo = await getMyInfo()
  68. let vipTime = await getVipInfo()
  69. this.setUser(userInfo.user)
  70. this.getProducts()
  71. this.setData({
  72. userInfo,
  73. vipTime,
  74. })
  75. },
  76. async getProducts() {
  77. let {
  78. productList: products,
  79. isPreferential
  80. } = await getProducts()
  81. console.log(products);
  82. this.setData({
  83. products,
  84. isPreferential
  85. })
  86. },
  87. activationCode() {
  88. wx.showModal({
  89. title: '请输入激活码',
  90. editable: true,
  91. success: async ({
  92. confirm,
  93. content
  94. }) => {
  95. if (confirm) {
  96. let regexp = /^[a-zA-Z0-9]{4}$/
  97. if (regexp.test(content)) {
  98. let activationRes = await getLearnCard({
  99. cardNo: content
  100. })
  101. if (activationRes.code == 200) {
  102. activationRes = {
  103. code: 200,
  104. message: '快去朗读挑战吧!'
  105. }
  106. }
  107. this.setUserInfo()
  108. if (typeof this.getTabBar === 'function') {
  109. this.getTabBar().setData({
  110. mask: true
  111. })
  112. }
  113. this.setData({
  114. activationModal: true,
  115. activationRes
  116. })
  117. } else {
  118. if (typeof this.getTabBar === 'function') {
  119. this.getTabBar().setData({
  120. mask: true
  121. })
  122. }
  123. this.setData({
  124. activationModal: true,
  125. activationRes: {
  126. code: 581,
  127. message: '请检查激活码输入是否正确'
  128. }
  129. })
  130. }
  131. }
  132. }
  133. })
  134. },
  135. toGzh() {
  136. this.selectComponent('#gzh').open()
  137. },
  138. openDonutBuy({
  139. currentTarget
  140. }) {
  141. this.selectComponent('#donutBuy').open(currentTarget)
  142. },
  143. async toBuy({
  144. currentTarget
  145. }) {
  146. if (currentTarget.dataset.isclick) {
  147. return
  148. }
  149. wx.showLoading({
  150. title: '提交中',
  151. mask: true
  152. })
  153. // #if MP
  154. userEvent({
  155. action: 'ANDROID_PAY_ACTIVITY',
  156. })
  157. let res = await buyVip({
  158. productId: currentTarget.dataset.id
  159. }).finally(() => {
  160. wx.hideLoading()
  161. })
  162. let {
  163. timeStamp,
  164. nonceStr,
  165. signType,
  166. paySign
  167. } = res
  168. // package保留字
  169. wx.requestPayment({
  170. timeStamp,
  171. nonceStr,
  172. package: res.package,
  173. signType,
  174. paySign,
  175. success: async (res) => {
  176. setTimeout(() => {
  177. this.setUserInfo()
  178. this.selectComponent('#vipModal').open()
  179. }, 1500)
  180. userEvent({
  181. action: 'ANDROID_PAY_SUCCESS',
  182. })
  183. },
  184. fail(res) {
  185. wx.showToast({
  186. title: "支付失败",
  187. icon: "none",
  188. duration: 3000
  189. })
  190. }
  191. })
  192. // #elif ANDROID
  193. let res = await androidbuyVip({
  194. productId: currentTarget.dataset.id
  195. }).finally(() => {
  196. wx.hideLoading()
  197. })
  198. wx.miniapp.requestPayment({
  199. timeStamp: res.timestamp,
  200. mchId: res.partnerid,
  201. prepayId: res.prepayid,
  202. package: res.package,
  203. nonceStr: res.noncestr,
  204. sign: res.sign,
  205. success: async (res) => {
  206. setTimeout(() => {
  207. this.setUserInfo()
  208. this.selectComponent('#vipModal').open()
  209. }, 1500)
  210. userEvent({
  211. action: 'ANDROID_PAY_SUCCESS',
  212. })
  213. },
  214. fail(res) {
  215. console.log(res);
  216. wx.showToast({
  217. title: "支付失败",
  218. icon: "none",
  219. duration: 3000
  220. })
  221. },
  222. complete: (res) => {
  223. console.error('wx.miniapp.requestPayment complete:', res)
  224. }
  225. })
  226. // #endif
  227. },
  228. jump({
  229. currentTarget
  230. }) {
  231. let url = currentTarget.dataset.url
  232. console.log(url);
  233. wx.navigateTo({
  234. url: url
  235. });
  236. },
  237. clipboar() {
  238. wx.setClipboardData({
  239. data: this.data.userInfo.user.eid,
  240. success: function (res) { //成功回调函数
  241. wx.showToast({
  242. title: '已复制',
  243. icon: "none"
  244. })
  245. }
  246. })
  247. },
  248. closeModal() {
  249. this.setData({
  250. activationModal: false
  251. })
  252. if (typeof this.getTabBar === 'function') {
  253. this.getTabBar().setData({
  254. mask: false
  255. })
  256. }
  257. },
  258. // 分享配置
  259. onShareAppMessage(res) {
  260. // #if MP
  261. return {
  262. title: '自从用了它,家里朗朗书声,美妙极了!你家孩子也快来试试!',
  263. path: `/pages/index/index?uid=${wx.getStorageSync('uid')}`,
  264. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/375-300-1.jpg'
  265. }
  266. // #elif ANDROID
  267. return {
  268. title: '自从用了它,家里朗朗书声,美妙极了!你家孩子也快来试试!',
  269. userName: 'gh_50f61361ad1d',
  270. path: `/pages/index/index?uid=${wx.getStorageSync('uid')}`,
  271. imagePath: '/static/375-300-2.jpg',
  272. webpageUrl: 'http://www.efunbox.cn',
  273. withShareTicket: true,
  274. miniprogramType: 1,
  275. scene: 0,
  276. }
  277. // #endif
  278. },
  279. onShareTimeline: function () {
  280. return {
  281. title: '终于找到适合孩子的朗读神器了!动画配音,边玩边学!',
  282. query: `uid=${wx.getStorageSync('uid')}`,
  283. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/yuwen.jpg'
  284. }
  285. },
  286. })