app.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. async doDecodePhoneNew(code,call='') {
  53. //验证登录是否失效
  54. var params = {
  55. code:code,
  56. appCode:this.globalData.appCode
  57. };
  58. try {
  59. let res = Activity.getAuthMobileNew(params);
  60. res.then(userInfo=>{
  61. console.log(userInfo)
  62. let mobile = userInfo.phoneNumber;
  63. // @todo从缓存里面取openId
  64. let openId = Activity.getOpenId();
  65. //保存用户信息
  66. Activity.saveUser(openId,mobile);
  67. Activity.setMobileCache(mobile);
  68. call();
  69. })
  70. }catch(err) {
  71. console.log(err);
  72. }
  73. },
  74. /**
  75. * 接口请求手机号
  76. * @param {*} encryptedData
  77. * @param {*} iv
  78. * @param call
  79. */
  80. async doDecodePhone(encryptedData,iv,call='') {
  81. let _self = this;
  82. //验证登录是否失效
  83. _self.checkInvalid({encryptedData:encryptedData,iv:iv,call:call},function(param){
  84. var params = {
  85. encryptedData: param.encryptedData,
  86. iv: param.iv,
  87. sessionKey:Activity.getSessionKey(),
  88. };
  89. try {
  90. let res = Activity.getAuthMobile(params);
  91. res.then(userInfo=>{
  92. let mobile = userInfo.phoneNumber;
  93. // @todo从缓存里面取openId
  94. let openId = Activity.getOpenId();
  95. //保存用户信息
  96. Activity.saveUser(openId,mobile);
  97. Activity.setMobileCache(mobile);
  98. if(param.call && typeof(param.call)=='function') {
  99. param.call();
  100. }
  101. })
  102. }catch(err) {
  103. console.log(err);
  104. }
  105. });
  106. },
  107. showToast(title,icon='success',time=2000,call='') {
  108. wx.showToast({
  109. title: title,
  110. icon: icon,
  111. duration: time,
  112. success:function(){
  113. if(typeof(call)=='function') {
  114. call();
  115. }
  116. }
  117. })
  118. },
  119. globalData: {
  120. userInfo: null,
  121. appCode:'HSAY_SHARE_COUPON',
  122. typeSwitchObj : {"C":'现金券',"D":"折扣券"}
  123. },
  124. showLoading() {
  125. if (this._loadingLock) {
  126. return
  127. }
  128. this._loadingLock = true
  129. const page = this.getComponent("#Page")
  130. if (page) {
  131. page.setValue("showLoading", true)
  132. }
  133. },
  134. hideLoading() {
  135. const page = this.getComponent("#Page")
  136. if (page) {
  137. // setTimeout(_ => {
  138. // page.setValue("showLoading", false)
  139. // }, 1000)
  140. page.setValue("showLoading", false)
  141. }
  142. this._loadingLock = false
  143. },
  144. getComponent(id) {
  145. const page = getCurrentPages()[getCurrentPages().length - 1]
  146. return page.selectComponent(id)
  147. }
  148. })