methods.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import Apis from '@/common/apis/index'
  2. const mobileReg = /^1[3456789]\d{9}$/
  3. export default {
  4. async initData() {
  5. try {
  6. uni.showLoading({
  7. title: '加载中...'
  8. })
  9. const res = await Apis.GetAssociation()
  10. this.users = res.data
  11. uni.hideLoading()
  12. } catch (e) {
  13. //TODO handle the exception
  14. }
  15. },
  16. // 获取code
  17. login() {
  18. uni.showLoading({
  19. title: '',
  20. mask: true
  21. });
  22. let me = this
  23. uni.login({
  24. async success(res) {
  25. if (res.code) {
  26. try {
  27. await Apis.wechatUserLogin({
  28. code: res.code
  29. })
  30. uni.hideLoading()
  31. } catch (err) {
  32. console.error(err)
  33. me.login()
  34. }
  35. } else {
  36. console.error(res.errMsg)
  37. me.login()
  38. }
  39. },
  40. fail(res) {
  41. console.error(res)
  42. me.login()
  43. }
  44. })
  45. },
  46. // 手机号授权登录
  47. async getPhoneNumber(event) {
  48. try {
  49. if (!uni.getStorageSync('userInfo') || !uni.getStorageSync('token')) {
  50. await this.login()
  51. }
  52. let me = this
  53. if (event.detail.errMsg === 'getPhoneNumber:ok') {
  54. uni.showLoading({
  55. title: '正在登录...',
  56. mask: true
  57. });
  58. let params = {
  59. refer_code: uni.getStorageSync('referCode') || '',
  60. iv: event.detail.iv,
  61. encryptedData: event.detail.encryptedData
  62. }
  63. await Apis.wechatMobileLogin(params)
  64. this.onGetUserInfo()
  65. }
  66. } catch (error) {
  67. console.error(error)
  68. }
  69. },
  70. async onGetUserInfo() {
  71. const res = await Apis.getWechatUserInfo();
  72. // 登录IM
  73. this.$imLogin.onImLogin(res.data)
  74. // 初始化Linkflow
  75. let userProfile = uni.getStorageSync('userProfile') || ''
  76. // 更新用户基本信息
  77. let userInfo = res.data
  78. if (userProfile) {
  79. await Apis.updateWechatInfo(userProfile)
  80. userInfo = {
  81. ...res.data,
  82. ...userProfile
  83. }
  84. }
  85. uni.setStorageSync('userInfo', userInfo);
  86. this.$store.dispatch('LinkflowUserInfo');
  87. this.$store.dispatch('LinkflowSendEvent', {
  88. event: 'UDE_357UY5NKYLE',
  89. attrs: {
  90. attr1: res.data.name,
  91. attr2: res.data.mobile,
  92. attr3: res.data.gender
  93. }
  94. })
  95. var pages = getCurrentPages();
  96. if (pages.length <= 1) {
  97. // 页面栈的实例小于一个页面
  98. uni.reLaunch({
  99. url: '../index/index'
  100. })
  101. } else {
  102. this.onBackPage()
  103. }
  104. uni.hideLoading()
  105. },
  106. onBackPage() {
  107. uni.navigateBack({
  108. delta: 1
  109. })
  110. },
  111. onEnterPage(page) {
  112. if (!this.userInfo) {
  113. return uni.navigateTo({
  114. url: '../login/index'
  115. })
  116. }
  117. switch (page) {
  118. case 'addPatient':
  119. uni.navigateTo({
  120. url: '../addPatient/index'
  121. })
  122. break
  123. case 'setting':
  124. uni.navigateTo({
  125. url: '../setting/index'
  126. })
  127. break
  128. }
  129. },
  130. bindUnbundle(){
  131. uni.showModal({
  132. title: '提示',
  133. content: '确定解绑此学生信息吗?',
  134. success (res) {
  135. if (res.confirm) {
  136. console.log('用户点击确定');
  137. } else if (res.cancel) {
  138. console.log('用户点击取消');
  139. }
  140. }
  141. });
  142. },
  143. }