app.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. globalData: {
  86. userInfo: null,
  87. appCode:'HSAY_SHARE_COUPON',
  88. typeSwitchObj : {"C":'现金券',"D":"折扣券"}
  89. }
  90. })