index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // pages/auth/index.js
  2. const app = getApp();
  3. const AUTH = require('../../utils/auth');
  4. import {userSave,getuserPhone} from "../../requests/api";
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  11. phonePop:false,
  12. wxqcode:'',
  13. locationFalg:false,
  14. },
  15. getUserInfoFn(res){
  16. if(res.detail.errMsg==='getUserInfo:ok'){
  17. app.globalData.userInfo=res.detail.userInfo;
  18. this.setData({phonePop:true});
  19. }else{
  20. this.setData({phonePop:true});
  21. }
  22. },
  23. bindGetPhoneNumber(e){
  24. if (e.detail.errMsg == "getPhoneNumber:ok"){
  25. AUTH.checkHasLogined().then(isLogined=>{
  26. console.log(isLogined,"isLogined")
  27. if(isLogined){
  28. var data={};
  29. data.encryptedData=e.detail.encryptedData;
  30. data.iv=e.detail.iv;
  31. data.openid=wx.getStorageSync('openId') || app.globalData.openId;
  32. getuserPhone(data).then(async result=>{
  33. console.log(result,"result")
  34. if(result.result==="success"){
  35. app.globalData.phoneNumber=result.phoneNumber;
  36. await this.creatUser();
  37. this.goHome();
  38. }
  39. })
  40. }
  41. })
  42. }else{
  43. this.goHome()
  44. }
  45. },
  46. goHome(){
  47. // wx.redirectTo({
  48. // url: '../index/index',
  49. // })
  50. let pages = getCurrentPages();
  51. let prevPage = pages[ pages.length - 2 ];
  52. if(this.data.wxqcode){
  53. prevPage.setData({ // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。
  54. wxqcode:this.data.wxqcode,
  55. formWxcode:true,
  56. })
  57. }
  58. console.log(prevPage,"prevPage")
  59. prevPage.setData({
  60. formAuth:true,
  61. })
  62. console.log(prevPage.data,"prevPage")
  63. wx.navigateBack({
  64. delta: 1 // 返回上一级页面。
  65. })
  66. },
  67. onClose(event){
  68. console.log(event);
  69. if(event.detail==="cancel"){
  70. this.setData({phonePop:false});
  71. wx.setStorage({
  72. data: true,
  73. key: 'clickPhone',
  74. })
  75. // wx.redirectTo({
  76. // url: '../index/index',
  77. // })
  78. this.goHome();
  79. }
  80. },
  81. // 创建用户
  82. async creatUser(){
  83. const data={};
  84. data.phone =app.globalData.phoneNumber;
  85. data.openId =wx.getStorageSync('openId') ;
  86. data.nickname=app.globalData.userInfo.nickName;
  87. // data.userName=app.globalData.userInfo.nickName;
  88. console.log(data);
  89. let res = await userSave(data);
  90. if(res.result==="success"){
  91. app.globalData.userId=res.id;
  92. wx.setStorageSync('userId', res.id);
  93. wx.setStorageSync('logined', true);
  94. }
  95. },
  96. async getUserLocation() {
  97. var that = this;
  98. await wx.getLocation({
  99. altitude: 'false',
  100. type: 'wgs84',
  101. complete: (res) => {
  102. that.setData({locationFalg:true})
  103. },
  104. success:async res=> {
  105. let data={}
  106. data.x=res.longitude;
  107. data.y=res.latitude;
  108. that.setData({
  109. latitude:res.latitude
  110. })
  111. that.setData({
  112. longitude:res.longitude
  113. })
  114. wx.setStorageSync('latitude', res.latitude)
  115. wx.setStorageSync('longitude', res.longitude)
  116. },
  117. fail() {
  118. // Toast.fail('未定位到您所在位置');
  119. // that.checkPower();
  120. // that.setData({havePower:{result:"fail",message:"未定位到您的位置"}})
  121. }
  122. })
  123. },
  124. /**
  125. * 生命周期函数--监听页面加载
  126. */
  127. onLoad: function (options) {
  128. this.getUserLocation();
  129. var wxqcode=wx.getStorageSync('wxqcode');
  130. if(wxqcode){
  131. this.setData({wxqcode:wxqcode})
  132. }
  133. },
  134. /**
  135. * 生命周期函数--监听页面初次渲染完成
  136. */
  137. onReady: function () {
  138. },
  139. /**
  140. * 生命周期函数--监听页面显示
  141. */
  142. onShow: function () {
  143. },
  144. /**
  145. * 生命周期函数--监听页面隐藏
  146. */
  147. onHide: function () {
  148. },
  149. /**
  150. * 生命周期函数--监听页面卸载
  151. */
  152. onUnload: function () {
  153. },
  154. })