app.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. const
  2. Bmob = require('./utils/Bmob-1.6.1.min.js'),
  3. Storage = require('./utils/storage.js'),
  4. wxApi = require('./api/wxApi.js')
  5. ;
  6. console.log(Bmob);
  7. Bmob.initialize("1daf21c0def4ac59c9737a4ec004c9ee", "57fc22d76feb8d48d3d5d3c8c2a6b3ff");
  8. //app.js
  9. App({
  10. onLaunch: function (data) {
  11. console.log(data);
  12. let me = this;
  13. Bmob.User.auth().then(res => {
  14. let options = me.globalData.userInfo = {
  15. openid:res.authData.weapp.openid,
  16. session_key:res.authData.weapp.session_key,
  17. sessionToken:res.sessionToken,
  18. username:res.username
  19. };
  20. Object.assign(me.globalData.userInfo,options);
  21. let userInfo = Storage.get(options.openid);
  22. //如果本地不存在该用户 插入数据库和本地
  23. if(!userInfo){
  24. Storage.set(options.openid,options);
  25. }
  26. me.getUserInfoByStorage(options,me);
  27. }).catch(err => {
  28. console.error('Bmob.user!');
  29. console.log(err);
  30. });
  31. wxApi.getSystemInfo().then(res =>{
  32. me.globalData.systemInfo = res;
  33. console.log(me.globalData.systemInfo);
  34. })
  35. }
  36. ,onShow:function () {
  37. }
  38. ,onHide:function () {
  39. }
  40. ,onError:function () {
  41. }
  42. ,onPageNotFound:function () {
  43. }
  44. ,getUserInfoByStorage:getUserInfoByStorage
  45. ,getSystemInfo:getSystemInfo
  46. ,globalData:{
  47. userInfo:{},
  48. systemInfo:{}
  49. }
  50. })
  51. function getUserInfoByStorage(option,app) {
  52. let wxuser = Bmob.Query('wxuser');
  53. let options = option;
  54. wxuser.equalTo("openid","==",options.openid);
  55. wxuser.find().then(res =>{
  56. console.log(res);
  57. console.log('查找完毕');
  58. if(res && res.length != 0 && !options.encryptedData){
  59. app.globalData.userInfo = res[0];
  60. console.error('该用户已经打开过小程序');
  61. return;
  62. }
  63. if(res && res[0]&&res[0].avatarUrl){
  64. app.globalData.userInfo = res[0];
  65. console.error('数据库已经存在此数据,不再插入');
  66. return;
  67. }
  68. wxuser.set("openid",options.openid);
  69. wxuser.set("session_key",options.session_key);
  70. wxuser.set("sessionToken",options.sessionToken);
  71. wxuser.set("username",options.username);
  72. if(options.encryptedData){
  73. wxuser.set('id', res[0].objectId);
  74. wxuser.set("encryptedData",options.encryptedData);
  75. wxuser.set("errMsg",options.errMsg);
  76. wxuser.set("iv",options.iv);
  77. wxuser.set("rawData",options.rawData);
  78. wxuser.set("signature",options.signature);
  79. wxuser.set("avatarUrl",options.avatarUrl);
  80. wxuser.set("city",options.city);
  81. wxuser.set("country",options.country);
  82. wxuser.set("gender",options.gender);
  83. wxuser.set("language",options.language);
  84. wxuser.set("nickName",options.nickName);
  85. wxuser.set("province",options.province);
  86. }
  87. wxuser.save().then(res => {
  88. console.log(res)
  89. console.log('插入成功')
  90. }).catch(err => {
  91. console.log(err)
  92. })
  93. })
  94. }
  95. //获取屏幕信息
  96. function getSystemInfo(cb){
  97. wx.getSystemInfo({
  98. success: function(res) {
  99. cb(res.windowWidth, res.windowHeight)
  100. }
  101. })
  102. }
  103. //拓展对象
  104. Object.extend = function () {
  105. var args = arguments;
  106. if (args.length < 2) return;
  107. var firstObj = args[0];
  108. console.log('first',firstObj);
  109. for(var i = 1; i < args.length; i++){
  110. for(var x in args[i]){
  111. firstObj[x] = args[i][x];
  112. }
  113. }
  114. console.log('first',firstObj);
  115. return firstObj;
  116. }