app.js 4.3 KB

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