业务交流通
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

login.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // pages/login/login.js
  2. const app = getApp()
  3. const $request = require('../../utils/request.js');
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. userInfo: null,
  10. // 是否获取到了用户信息
  11. hasUserInfo: false,
  12. },
  13. /**
  14. * 生命周期函数--监听页面加载
  15. */
  16. onLoad: function (options) {
  17. const userInfo = wx.getStorageSync('userInfo') || null;
  18. if (app.globalData.userInfo || userInfo) {
  19. this.setData({
  20. userInfo
  21. })
  22. }
  23. },
  24. // 获取手机号授权
  25. getPhoneNumber(e) {
  26. console.log(e);
  27. if (!!e.detail.iv && !!e.detail.encryptedData) {
  28. const iv = e.detail.iv
  29. const encryptedData = e.detail.encryptedData
  30. this.appletLogin(iv, encryptedData)
  31. } else {
  32. wx.showToast({
  33. title: '请允许授权',
  34. icon: "none",
  35. duration: 3000
  36. })
  37. }
  38. },
  39. // 保存用户信息
  40. appletLogin(iv, encryptedData) {
  41. let { nickName, gender, country, city, province, avatarUrl } = this.data.userInfo;
  42. $request.get('/com/appletLogin.action', {
  43. // openid: app.globalData.openid,
  44. iv: iv,
  45. encryptedData: encryptedData,
  46. sessionKey: app.globalData.sessionKey,
  47. loginState: "业务交流通",
  48. avatarUrl,
  49. gender,
  50. nickName,
  51. country,
  52. province,
  53. city
  54. }).then(res => {
  55. // console.log(res,'保存用户信息返回的Res')
  56. this.getUserSession()
  57. }).catch(error => {
  58. console.log(error, 'error appletLogin')
  59. })
  60. },
  61. // 获取用户信息弹框
  62. onCheckboxChange() {
  63. if (!this.data.hasUserInfo) {
  64. if(!this.data.userInfo){
  65. this.getUserProfile()
  66. }else{
  67. this.setData({
  68. hasUserInfo: true
  69. })
  70. }
  71. } else {
  72. this.setData({
  73. hasUserInfo: false
  74. })
  75. }
  76. },
  77. // 获取用户信息
  78. getUserProfile(e) {
  79. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
  80. // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  81. wx.getUserProfile({
  82. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  83. success: (res) => {
  84. this.setData({
  85. userInfo: res.userInfo,
  86. hasUserInfo: true
  87. })
  88. wx.setStorageSync('userInfo', res.userInfo)
  89. },
  90. fail: () => {
  91. wx.showToast({
  92. title: '请允许授权',
  93. icon: "none",
  94. duration: 3000
  95. })
  96. }
  97. })
  98. },
  99. // 保存个人信息后拿到新数据
  100. getUserSession() {
  101. let that = this
  102. wx.showLoading({
  103. title: '加载中',
  104. mask: true
  105. })
  106. $request.post('/script/getSession.action', {}).then(res => {
  107. console.log(res, 'getUserSession---res')
  108. wx.hideLoading()
  109. app.globalData.customerId = res.data.customer.customerId
  110. // wx.navigateTo({
  111. // url: "/pages/mine/mine", //跳转页面的路径,可带参数 ?隔开,不同参数用 & 分隔;相对路径,不需要.wxml后缀
  112. // success: function () {}, //成功后的回调;
  113. // fail: function () {}, //失败后的回调;
  114. // complete: function () {} //结束后的回调(成功,失败都会执行)
  115. // })
  116. }).catch(error => {
  117. console.log(error, "获取error")
  118. })
  119. },
  120. /**
  121. * 用户点击右上角分享
  122. */
  123. onShareAppMessage: function () {
  124. }
  125. })