index.js 6.1 KB

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