login.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // pages/login/login.js
  2. import {
  3. getOpenidSessionKey
  4. } from '../../utils/httpUtil';
  5. import url from '../../utils/const';
  6. import request from '../../utils/WXHttpRequest';
  7. const HOST = url.baseApi;
  8. function getAPIUrl(action) {
  9. return HOST + action;
  10. }
  11. Page({
  12. /**
  13. * 页面的初始数据
  14. */
  15. data: {
  16. index: 0,
  17. canIUseGetUserProfile: false
  18. },
  19. /**
  20. * 生命周期函数--监听页面加载
  21. */
  22. onLoad: function(options) {
  23. if (options.index) {
  24. this.setData({
  25. index: options.index
  26. })
  27. }
  28. if (wx.getUserProfile) {
  29. this.setData({
  30. canIUseGetUserProfile: true
  31. })
  32. }
  33. },
  34. userLoginRecord: function(uid) {
  35. if (wx.getStorageSync('uid')) {
  36. console.log('调用方法')
  37. let url = getAPIUrl('wx/loginLog');
  38. return request.getInstance().header({
  39. uid: wx.getStorageSync('uid')
  40. }).method('POST').url(url).send().success(() => {}).fail(() => {});
  41. }
  42. },
  43. getUserProfile: function(e) {
  44. wx.getUserProfile({
  45. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  46. success: (userProfile) => {
  47. console.log('getUserProfile', userProfile);
  48. const userInfo = userProfile.userInfo;
  49. this.getOpenId((openidData) => {
  50. console.log('openid', openidData)
  51. let url = getAPIUrl('wx/user');
  52. const userData = {
  53. openId: openidData.data.data.openid,
  54. unionId: openidData.data.data.unionid,
  55. nickName: userInfo.nickName,
  56. gender: userInfo.gender,
  57. avatar: userInfo.avatarUrl
  58. }
  59. request.getInstance().url(url).header({}).data(userData).method('POST').send().success(res => {
  60. console.log('登陆成功', res)
  61. wx.hideToast()
  62. wx.showToast({
  63. title: '登录成功',
  64. icon: 'success',
  65. duration: 1500,
  66. mask: true
  67. })
  68. wx.setStorageSync('uid', res.data.data.data.uid)
  69. wx.setStorageSync('user', res.data.data.data)
  70. this.userLoginRecord();
  71. const pages = getCurrentPages();
  72. const prevPage = pages[pages.length - 2];
  73. prevPage.setData({
  74. fromLoginIndex: this.data.index, // 有id就塞到第一位
  75. }, () => {
  76. wx.navigateBack({
  77. delta: 1
  78. })
  79. })
  80. });
  81. })
  82. // getOpenidSessionKey((res) => {
  83. // console.log('getUserProfilegetUserProfile', res)
  84. // wx.showToast({
  85. // title: '登录成功',
  86. // icon: 'fail',
  87. // duration: 1000,
  88. // success: () => {
  89. // const pages = getCurrentPages();
  90. // const prevPage = pages[pages.length - 2];
  91. // prevPage.setData({
  92. // fromLoginIndex: this.data.index, // 有id就塞到第一位
  93. // }, () => {
  94. // wx.navigateBack({
  95. // delta: 1
  96. // })
  97. // })
  98. // }
  99. // })
  100. // }, (error) => {
  101. // wx.showToast({
  102. // title: '登录失败',
  103. // icon: 'fail',
  104. // duration: 1000,
  105. // success: () => {
  106. // wx.navigateBack()
  107. // }
  108. // })
  109. // });
  110. }
  111. })
  112. },
  113. getOpenId: function(successcallback, failcallback) {
  114. wx.login({
  115. success: function(res) {
  116. if (res.code) {
  117. // 获取openid
  118. console.log('openId', res.code)
  119. let url = getAPIUrl('wx/user/openId')
  120. let data = {
  121. code: res.code
  122. }
  123. return request.getInstance().url(url).data(data).send().success(successcallback).fail(failcallback);
  124. } else {
  125. console.log('获取用户登录态失败!' + res.errMsg)
  126. }
  127. }
  128. })
  129. },
  130. impower: function(e) {
  131. console.log(e)
  132. var myEventDetail = {} // detail对象,提供给事件监听函数
  133. var myEventOption = {} // 触发事件的选项
  134. getOpenidSessionKey((res) => {
  135. console.log(res)
  136. wx.showToast({
  137. title: '登录成功',
  138. icon: 'fail',
  139. duration: 1000,
  140. success: () => {
  141. const pages = getCurrentPages();
  142. const prevPage = pages[pages.length - 2];
  143. prevPage.setData({
  144. fromLoginIndex: this.data.index, // 有id就塞到第一位
  145. }, () => {
  146. wx.navigateBack({
  147. delta: 1
  148. })
  149. })
  150. }
  151. })
  152. }, (error) => {
  153. wx.showToast({
  154. title: '登录失败',
  155. icon: 'fail',
  156. duration: 1000,
  157. success: () => {
  158. wx.navigateBack()
  159. }
  160. })
  161. });
  162. },
  163. touchMove: function() {
  164. return false
  165. },
  166. /**
  167. * 生命周期函数--监听页面初次渲染完成
  168. */
  169. onReady: function() {
  170. },
  171. /**
  172. * 生命周期函数--监听页面显示
  173. */
  174. onShow: function() {
  175. },
  176. /**
  177. * 生命周期函数--监听页面隐藏
  178. */
  179. onHide: function() {
  180. },
  181. /**
  182. * 生命周期函数--监听页面卸载
  183. */
  184. onUnload: function() {
  185. },
  186. /**
  187. * 页面相关事件处理函数--监听用户下拉动作
  188. */
  189. onPullDownRefresh: function() {
  190. },
  191. /**
  192. * 页面上拉触底事件的处理函数
  193. */
  194. onReachBottom: function() {
  195. },
  196. /**
  197. * 用户点击右上角分享
  198. */
  199. onShareAppMessage: function() {
  200. }
  201. })