app.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. App({
  2. data: {
  3. deviceInfo: {},
  4. },
  5. onLaunch: function () {
  6. var that = this;
  7. that.data.deviceInfo = wx.getSystemInfoSync();
  8. //调用API从本地缓存中获取数据
  9. var logs = wx.getStorageSync('logs') || []
  10. logs.unshift(Date.now());
  11. wx.setStorageSync('logs', logs);
  12. wx.clearStorage();
  13. // 查看是否授权【只有处于授权状态,才可以利用 wx.getUserInfo 获取用户信息】
  14. wx.getSetting({
  15. success: function (res) {
  16. console.log(res);
  17. if (res.authSetting['scope.userInfo']) {
  18. //对于授权过小程序的用户,获取用户信息
  19. that.getUserRole();
  20. }
  21. }
  22. })
  23. },
  24. //获取用户角色
  25. getUserRole: function (id) {
  26. var that = this;
  27. var host = that.globalData.servsers;
  28. wx.login({
  29. success: function (res) {
  30. var code = res.code;
  31. wx.getUserInfo({
  32. withCredentials: true,
  33. success: function (res) {
  34. that.globalData.userInfo = res.userInfo;
  35. wx.request({
  36. url: host + "api/distributeSell/getUserRole",
  37. data: {
  38. code: code,
  39. iv: res.iv,
  40. encryptedData: res.encryptedData,
  41. },
  42. method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  43. header: {
  44. 'content-type': 'application/x-www-form-urlencoded'
  45. },
  46. success: function (res) {
  47. //成功
  48. if (res.data.code == '200') {
  49. that.globalData.un_id = res.data.data.user_id;
  50. that.globalData.openid = res.data.data.openid;
  51. that.globalData.isNewUser = false;//不是新用户
  52. that.globalData.userRole = res.data.data.userRole;
  53. that.globalData.userInfo.avatarUrl = res.data.data.icon;
  54. that.globalData.user_id = res.data.data.user_id;
  55. var name = res.data.data.name;
  56. name = name.replace(/\?/g, '');
  57. name = unescape(escape(name).replace(/\%uD.{3}/g, ''));
  58. that.globalData.userInfo.nickName = name;
  59. if (that.employIdCallback) {
  60. that.employIdCallback(res.data.data.user_id);
  61. }
  62. if (that.employIdCallback2) {
  63. that.employIdCallback2(res.data.data.openid);
  64. }
  65. if (that.employIdCallbackUser) {
  66. that.employIdCallbackUser(false);
  67. }
  68. if (that.employIdCallbackRole) {
  69. that.employIdCallbackRole(res.data.data.userRole);
  70. }
  71. }else{
  72. wx.navigateTo({
  73. url: '/pages/authorize/authorize?link=midAutumn',
  74. })
  75. }
  76. if (res.data.data == '') {
  77. that.globalData.isNewUser = true;//是新用户
  78. }
  79. if (res == null || res.data == null) {
  80. console.error('网络请求失败');
  81. return;
  82. }
  83. }
  84. });
  85. },
  86. fail: function (e) {
  87. console.log("需重新授权");
  88. }
  89. })
  90. }
  91. });
  92. },
  93. //插入用户信息
  94. insertUser:function(e){
  95. var that = this;
  96. var host = that.globalData.servsers;
  97. wx.login({
  98. success: function (res) {
  99. var code = res.code;
  100. wx.getUserInfo({
  101. withCredentials: true,
  102. success: function (res) {
  103. that.globalData.userInfo = res.userInfo;
  104. }
  105. })
  106. }
  107. })
  108. },
  109. //设置全局变量
  110. globalData: {
  111. userInfo: [],
  112. un_id:null,
  113. open_id: null,
  114. openid : null,
  115. user_id:null,// 用户id
  116. userRole : '',
  117. isNewUser : '',
  118. fromUserId : '',//分享者的id
  119. fromUserRole : '',//分享者的role
  120. //servsers: "https://52yqf.cn/",
  121. // servsers: "https://www.daliangzao.net/",//正式库
  122. servsers: "https://dlz.info666.com/",//测试域名库
  123. //servsers: "https://tt.daliangzao.net/",//测试库
  124. appid: 'wxf9a0ddcb8c70b939',//appid
  125. secret: 'a009cb3e46c8ecef9c54a50522f11823',//secret
  126. }
  127. })