app.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.phone = res.data.data.user_phone
  54. that.globalData.userInfo.avatarUrl = res.data.data.icon;
  55. that.globalData.user_id = res.data.data.user_id;
  56. var name = res.data.data.name;
  57. name = name.replace(/\?/g, '');
  58. name = unescape(escape(name).replace(/\%uD.{3}/g, ''));
  59. that.globalData.userInfo.nickName = name;
  60. if (that.employIdCallback) {
  61. that.employIdCallback(res.data.data.user_id);
  62. }
  63. if (that.employIdCallback2) {
  64. that.employIdCallback2(res.data.data.openid);
  65. }
  66. if (that.employIdCallbackUser) {
  67. that.employIdCallbackUser(false);
  68. }
  69. if (that.employIdCallbackRole) {
  70. that.employIdCallbackRole(res.data.data.userRole);
  71. }
  72. }else{
  73. wx.navigateTo({
  74. url: '/pages/authorize/authorize?link=midAutumn',
  75. })
  76. }
  77. if (res.data.data == '') {
  78. that.globalData.isNewUser = true;//是新用户
  79. }
  80. if (res == null || res.data == null) {
  81. console.error('网络请求失败');
  82. return;
  83. }
  84. }
  85. });
  86. },
  87. fail: function (e) {
  88. console.log("需重新授权");
  89. }
  90. })
  91. }
  92. });
  93. },
  94. //插入用户信息
  95. insertUser:function(e){
  96. var that = this;
  97. var host = that.globalData.servsers;
  98. wx.login({
  99. success: function (res) {
  100. var code = res.code;
  101. wx.getUserInfo({
  102. withCredentials: true,
  103. success: function (res) {
  104. that.globalData.userInfo = res.userInfo;
  105. }
  106. })
  107. }
  108. })
  109. },
  110. //设置全局变量
  111. globalData: {
  112. userInfo: [],
  113. un_id:null,
  114. open_id: null,
  115. openid : null,
  116. user_id:null,// 用户id
  117. userRole : '',
  118. isNewUser : '',
  119. fromUserId : '',//分享者的id
  120. fromUserRole : '',//分享者的role
  121. //servsers: "https://52yqf.cn/",
  122. servsers: "https://www.daliangzao.net/",//正式库
  123. // servsers: "https://dlz.info666.com/",//测试域名库
  124. //servsers: "https://tt.daliangzao.net/",//测试库
  125. appid: 'wxf9a0ddcb8c70b939',//appid
  126. secret: 'a009cb3e46c8ecef9c54a50522f11823',//secret
  127. }
  128. })