login.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import {
  2. get,
  3. post,
  4. login,
  5. getWxLoginCode,
  6. clearWxLoginCode
  7. } from '../../utils/http'
  8. import { objToParam } from '../../utils/util'
  9. Page({
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. userInfo: null,
  15. prePage: 0,
  16. showPhoneBtn: false,
  17. agreeStatus: false,
  18. currentLoginStatus: false // 本次登录状态
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad: function (options) {
  24. console.log(options)
  25. if (options.prePage) {
  26. this.setData({
  27. prePage: options.prePage
  28. })
  29. }
  30. // 清理+更新微信code
  31. this.refreshWxCode()
  32. },
  33. /**
  34. * 生命周期函数--监听页面初次渲染完成
  35. */
  36. onReady: function () {},
  37. /**
  38. * 生命周期函数--监听页面显示
  39. */
  40. onShow: function () {
  41. // 本次已登录成功
  42. if (this.data.currentLoginStatus) {
  43. this.setData({ currentLoginStatus: false }, () => {
  44. this.loginSuccRedirect(1)
  45. })
  46. }
  47. },
  48. /**
  49. * 生命周期函数--监听页面隐藏
  50. */
  51. onHide: function () {},
  52. /**
  53. * 生命周期函数--监听页面卸载
  54. */
  55. onUnload: function () {},
  56. /**
  57. * 页面相关事件处理函数--监听用户下拉动作
  58. */
  59. onPullDownRefresh: function () {},
  60. /**
  61. * 页面上拉触底事件的处理函数
  62. */
  63. onReachBottom: function () {},
  64. /**
  65. * 用户点击右上角分享
  66. */
  67. onShareAppMessage: function () {},
  68. /**
  69. * 获取用户信息
  70. */
  71. getUserInfo(e) {
  72. let that = this
  73. if (!that.data.agreeStatus) {
  74. wx.showToast({
  75. title: '请同意用户协议和隐私政策',
  76. icon: 'none'
  77. })
  78. return
  79. }
  80. wx.getUserProfile({
  81. desc: 'desc',
  82. success: (res) => {
  83. console.log(res);
  84. that.setData(
  85. {
  86. userInfo: JSON.parse(res.rawData)
  87. },
  88. () => {
  89. login().then((data) => {
  90. let phoneStatus = data.status
  91. // 状态值,0.需要获取授权信息,1.不需再获取授权信息
  92. if (phoneStatus == 1) {
  93. that.loginSucc(data, data)
  94. }
  95. that.setData({ showPhoneBtn: !phoneStatus })
  96. })
  97. }
  98. )
  99. console.log(JSON.parse(res.rawData))
  100. },
  101. fail:(res)=>{
  102. console.log(res,111111);
  103. }
  104. })
  105. },
  106. /**
  107. * 获取用户手机号
  108. */
  109. getPhoneNumber(e) {
  110. let that = this
  111. if (!that.data.agreeStatus) {
  112. wx.showToast({
  113. title: '请同意用户协议和隐私政策',
  114. icon: 'none'
  115. })
  116. return
  117. }
  118. login().then((resLogin) => {
  119. console.log(resLogin,'resLogin')
  120. let invite = {}
  121. if (getApp().invite_id) {
  122. invite['invite_id'] = getApp().invite_id
  123. }
  124. post(
  125. 'api/user/wxData',
  126. {
  127. avatar_url: this.data.userInfo.avatarUrl,
  128. nickname: this.data.userInfo.nickName,
  129. iv: e.detail.iv,
  130. encrypted_data: e.detail.encryptedData,
  131. wx_data: JSON.stringify(this.data.userInfo),
  132. ticket: resLogin.ticket,
  133. ...invite
  134. },
  135. (res) => {
  136. that.loginSucc(resLogin, res.data)
  137. },
  138. (err) => {}
  139. )
  140. })
  141. return
  142. },
  143. /**
  144. * 登录成功逻辑
  145. */
  146. loginSucc(loginRes, wxDataRes) {
  147. console.log(wxDataRes,'wxDataReswxDataReswxDataRes')
  148. if (loginRes.status == 0 && wxDataRes.coupon&& Object.keys(wxDataRes.coupon).length>0) {
  149. getApp().globalData.couponPopup = true
  150. getApp().globalData.coupon = wxDataRes.coupon
  151. }
  152. wx.setStorageSync('userInfo', wxDataRes)
  153. wx.setStorageSync('token', loginRes.token)
  154. get('api/user', {}, (res) => {
  155. if (res.data) {
  156. getApp().globalData.userInfo = res.data
  157. wx.setStorageSync('userInfo', res.data)
  158. }
  159. })
  160. // 标记此次登录成功
  161. this.setData({
  162. currentLoginStatus: true
  163. })
  164. // 获取用户问诊信息
  165. get(
  166. 'api/user/investigation',
  167. {},
  168. (res) => {
  169. console.log(res)
  170. // 用户是否已填写,1是,0否
  171. // if (res.data.is_finished == 1) {
  172. wx.showToast({
  173. title: '登录成功',
  174. icon: 'none'
  175. })
  176. this.loginSuccRedirect()
  177. // return
  178. // }
  179. // wx.navigateTo({
  180. // url: `/pages/member/investigation/investigation`
  181. // })
  182. },
  183. () => {
  184. this.loginSuccRedirect()
  185. }
  186. )
  187. },
  188. /**
  189. * 登录成功后重定向到特定页面
  190. */
  191. loginSuccRedirect(sec = 1500) {
  192. let that = this
  193. setTimeout(() => {
  194. if (that.data.prePage == 1) {
  195. wx.navigateBack()
  196. } else {
  197. let sceneData = getApp().globalData.sceneData
  198. sceneData.path =
  199. sceneData.path == 'pages/login/login'
  200. ? 'pages/home/home'
  201. : sceneData.path
  202. wx.reLaunch({
  203. url: `/${sceneData.path}?${objToParam(sceneData.query)}`
  204. })
  205. }
  206. }, sec)
  207. },
  208. /**
  209. * 清理+更新微信code
  210. */
  211. refreshWxCode() {
  212. clearWxLoginCode()
  213. getWxLoginCode()
  214. },
  215. /**
  216. * 跳转去用户协议
  217. */
  218. goToAgree(e) {
  219. wx.navigateTo({
  220. url: '/pages/agreement/agreement?agree=' + e.currentTarget.dataset.agree
  221. })
  222. },
  223. /**
  224. * 同意隐私政策
  225. */
  226. rulesAgree() {
  227. this.setData({
  228. agreeStatus: !this.data.agreeStatus
  229. })
  230. }
  231. })