index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // index.js
  2. let myPro = require("../../../utils/wxRequest.js");
  3. let util = require("../../../utils/util.js");
  4. Page({
  5. data: {
  6. },
  7. onLoad: function (options) {
  8. // Do some initialize when page load.
  9. let that = this;
  10. },
  11. onShow: function () {
  12. // Do something when page show.
  13. let that = this;
  14. // 拿code换openid
  15. wx.login({
  16. success: function(res) {
  17. console.log('获取wxcode',res)
  18. let params = {
  19. code: res.code
  20. };
  21. myPro.wxRequest("user/mini-auth","POST",params).then(res=>{
  22. let result = res.result;
  23. // 有token 是老用户
  24. if(result.token){
  25. console.log('token',result.token)
  26. getApp().globalData.token = result.token;
  27. // getApp().globalData.openid = result.user.openid;
  28. wx.setStorage({
  29. key: "token",
  30. data: result.token
  31. });
  32. getApp().globalData.is_member = result.user.is_member; // 会员 0否 1是
  33. getApp().globalData.userInfo = {
  34. nickname: result.user.nickname,
  35. thumb: result.user.thumb,
  36. phone: result.user.phone
  37. };
  38. if(result.user.store){
  39. getApp().globalData.role = 1; // 有店铺,即为门店
  40. };
  41. // 回跳到首页
  42. wx.reLaunch({
  43. url: "/pages/index/index"
  44. });
  45. // console.log('用户',getApp().globalData.userInfo)
  46. }else{
  47. // 新用户
  48. getApp().globalData.openid = result.openid;
  49. getApp().globalData.session_key = result.session_key;
  50. }
  51. }).catch(err=>{
  52. console.log('报错信息',err);
  53. wx.showToast({
  54. title: err,
  55. icon: "none"
  56. })
  57. })
  58. },
  59. fail: function(res){
  60. wx.showToast({
  61. title: "微信未授权成功,请在设置中允许使用用户信息",
  62. icon: "none"
  63. });
  64. }
  65. })
  66. },
  67. // 授权获取用户信息
  68. getUserProfile(event){
  69. if(wx.getUserProfile){
  70. wx.getUserProfile({
  71. desc: '获取您的昵称、头像、及性别', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  72. success: (res) => {
  73. // 存一下用户信息 昵称 头像 性别
  74. console.log('已授权',res);
  75. getApp().globalData.userInfo = {
  76. nickname: res.userInfo.nickName,
  77. thumb: res.userInfo.avatarUrl,
  78. sex: res.userInfo.gender
  79. };
  80. // console.log('用户信息',getApp().globalData.userInfo)
  81. // 跳到下一页
  82. wx.navigateTo({
  83. url: "/pages/login/wechat/index"
  84. });
  85. }
  86. })
  87. }else{
  88. // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示
  89. wx.showModal({
  90. title: '提示',
  91. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
  92. });
  93. }
  94. },
  95. onReady: function () {
  96. // Do something when page ready.
  97. },
  98. onHide: function () {
  99. // Do something when page hide.
  100. },
  101. onUnload: function () {
  102. // Do something when page close.
  103. },
  104. onPullDownRefresh: function () {
  105. // Do something when pull down.
  106. },
  107. onReachBottom: function () {
  108. // Do something when page reach bottom.
  109. },
  110. onPageScroll: function () {
  111. // Do something when page scroll
  112. },
  113. onResize: function () {
  114. // Do something when page resize
  115. },
  116. });