authorize.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // pages/authorize/authorize.js
  2. var app = getApp();
  3. Page({
  4. data: {
  5. link :'' ,//授权成功后的跳转地址
  6. code : '',
  7. fromUserId : '',//分享者的id
  8. fromUserRole : '',//分享者的角色
  9. hiddenmodal: true, //提示弹窗 - 初始隐藏
  10. url:null,//重定向的url
  11. },
  12. onLoad: function (options) {
  13. let that = this,
  14. link = options.link,
  15. url = options.url;
  16. that.setData({
  17. link: link,
  18. url:url
  19. })
  20. },
  21. onShow:function(){
  22. var that = this;
  23. wx.login({
  24. success: function (res) {
  25. if (res.code) {
  26. that.setData({
  27. code: res.code
  28. })
  29. }
  30. }
  31. })
  32. },
  33. //确认授权
  34. onGotUserInfo:function(e){
  35. var that = this;
  36. var userInfo = e.detail.userInfo;
  37. var host = getApp().globalData.servsers;
  38. var un_id = e.detail.userInfo;
  39. var link = that.data.link;
  40. console.log(e.detail);
  41. if (userInfo != undefined){
  42. var pages = getCurrentPages();
  43. var currPage = pages[pages.length - 1]; //当前页面
  44. var prevPage = pages[pages.length - 2]; //上一个页面
  45. var prevPageUrl = prevPage.route;
  46. var name = e.detail.userInfo.nickName;
  47. name = name.replace(/\?/g, '');
  48. name = unescape(escape(name).replace(/\%uD.{3}/g, ''));
  49. var icon = e.detail.userInfo.avatarUrl;
  50. var iv = e.detail.iv;
  51. var encryptedData = e.detail.encryptedData;
  52. getApp().globalData.userInfo = userInfo;
  53. // wx.login({
  54. // success: function (res) {
  55. var code = that.data.code;
  56. console.log("code:" + code);
  57. wx.request({
  58. url: host + "userapi/insertadd",
  59. data: {
  60. user_id: code,
  61. name: name,
  62. icon: icon,
  63. iv: iv,
  64. encryptedData: encryptedData,
  65. },
  66. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  67. header: {
  68. 'Accept': 'application/json'
  69. },
  70. success: function (res) {
  71. console.log("auu:");
  72. console.log(res);
  73. if(res.data.code == 500){
  74. that.setData({
  75. hiddenmodal: false,
  76. modalCont: '授权超时,请重新授权!'
  77. })
  78. setTimeout(function () {
  79. that.setData({
  80. hiddenmodal: true
  81. })
  82. wx.login({
  83. success: function (res) {
  84. if (res.code) {
  85. that.setData({
  86. code: res.code
  87. })
  88. }
  89. }
  90. })
  91. }, 1000)
  92. return false;
  93. }else{
  94. //确认授权
  95. getApp().globalData.un_id = res.data.unionId;
  96. getApp().globalData.openid = res.data.openid;
  97. getApp().globalData.isNewUser = res.data.isNewUser;
  98. getApp().globalData.userRole = res.data.userRole;
  99. getApp().globalData.user_id = code;
  100. console.log("authorize-unionId:" + res.data.unionId);
  101. console.log("authorize-openid:" + res.data.openid);
  102. console.log("authorize-isNewUser:" + res.data.isNewUser);
  103. console.log("authorize-userRole:" + res.data.userRole);
  104. console.log("link:" + link);
  105. //授权成功后,跳转
  106. if (link == 'customized'){
  107. wx.redirectTo({
  108. url: "customized/pages/index/index",
  109. })
  110. }else {
  111. if (link != 'awaitState' && link != 'buy' && link != 'mine') {//来自收礼物页面的授权请求 来自产品详情页
  112. wx.redirectTo({
  113. url: "/pages/" + link + "/" + link,
  114. })
  115. } else if (link == 'buy') {
  116. prevPage.setData({
  117. authorize: 1
  118. })
  119. wx.navigateBack({
  120. delta: 1
  121. })
  122. } else if (link == 'mine') {
  123. wx.switchTab({
  124. url: "/pages/" + link + "/" + link,
  125. })
  126. } else if (link == 'midAutumn') {
  127. wx.navigateBack({
  128. delta: 1
  129. })
  130. } else {
  131. prevPage.setData({
  132. authorize: 1
  133. })
  134. wx.navigateBack({
  135. delta: 2
  136. })
  137. }
  138. }
  139. if (res == null || res.data == null) {
  140. console.error('网络请求失败');
  141. return;
  142. }
  143. }
  144. }
  145. })
  146. // }
  147. // })
  148. }else{
  149. //拒绝
  150. return false;
  151. }
  152. },
  153. //页面卸载[完全关闭]
  154. onUnload: function () {
  155. var that = this;
  156. that.setData({
  157. wrapShowFlag: '',
  158. loadingFinish: false,
  159. })
  160. wx.hideLoading();
  161. },
  162. })
  163. var Util = require('../../utils/util.js');