app.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // app.js
  2. import Activity from './api/activity';
  3. App({
  4. onLaunch() {
  5. // 展示本地存储能力
  6. // const logs = wx.getStorageSync('logs') || []
  7. // logs.unshift(Date.now())
  8. // wx.setStorageSync('logs', logs)
  9. // 登录
  10. var openId = wx.getStorageSync('loginInfo').openId
  11. if(!openId){
  12. this.login();
  13. }
  14. },
  15. checkInvalid(param={},callBack='') {
  16. let _self = this;
  17. wx.checkSession({
  18. success () {
  19. //session_key 未过期,并且在本生命周期一直有效
  20. if(typeof(callBack)==='function'){
  21. callBack(param);
  22. }
  23. },
  24. fail () {
  25. _self.login(function(){
  26. // session_key 已经失效,需要重新执行登录流程
  27. if(typeof(callBack)==='function'){
  28. callBack(param);
  29. }
  30. });
  31. }
  32. })
  33. },
  34. // 登录
  35. login() {
  36. wx.login({
  37. success: res => {
  38. let code = res.code;
  39. let result = Activity.getSessionKeyFromApi(code);
  40. result.then(res=>{
  41. wx.setStorageSync('loginInfo',{
  42. openId:res.openId,
  43. sessionKey:res.sessionKey
  44. });
  45. });
  46. // if(typeof(call)=='function') {
  47. // call();
  48. // }
  49. }
  50. })
  51. },
  52. /**
  53. * 接口请求手机号
  54. * @param {*} encryptedData
  55. * @param {*} iv
  56. * @param call
  57. */
  58. async doDecodePhone(encryptedData,iv,call='') {
  59. let _self = this;
  60. //验证登录是否失效
  61. _self.checkInvalid({encryptedData:encryptedData,iv:iv,call:call},function(param){
  62. var params = {
  63. encryptedData: param.encryptedData,
  64. iv: param.iv,
  65. sessionKey:Activity.getSessionKey(),
  66. };
  67. try {
  68. let res = Activity.getAuthMobile(params);
  69. res.then(userInfo=>{
  70. let mobile = userInfo.phoneNumber;
  71. // @todo从缓存里面取openId
  72. let openId = Activity.getOpenId();
  73. //保存用户信息
  74. Activity.saveUser(openId,mobile);
  75. Activity.setMobileCache(mobile);
  76. if(param.call && typeof(param.call)=='function') {
  77. param.call();
  78. }
  79. })
  80. }catch(err) {
  81. console.log(err);
  82. }
  83. });
  84. },
  85. showToast(title,icon='success',time=2000,call='') {
  86. wx.showToast({
  87. title: title,
  88. icon: icon,
  89. duration: time,
  90. success:function(){
  91. if(typeof(call)=='function') {
  92. call();
  93. }
  94. }
  95. })
  96. },
  97. globalData: {
  98. userInfo: null,
  99. appCode:'HSAY_SHARE_COUPON',
  100. typeSwitchObj : {"C":'现金券',"D":"折扣券"}
  101. }
  102. })