app.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // app.js
  2. import Activity from './api/activity';
  3. App({
  4. onLaunch() {
  5. // 展示本地存储能力
  6. var that = this;
  7. wx.checkSession({
  8. success: (res) => {
  9. var openId = wx.getStorageSync('loginInfo').openId;
  10. //过渡代码 以前存到前端的现在不存在了
  11. var sessionKey = wx.getStorageSync('loginInfo').sessionKey;
  12. if(!openId || sessionKey){
  13. that.login();
  14. }
  15. },
  16. fail () {
  17. // session_key 已经失效,需要重新执行登录流程
  18. that.login() //重新登录
  19. }
  20. })
  21. },
  22. checkInvalid(param={},callBack='') {
  23. let _self = this;
  24. wx.checkSession({
  25. success () {
  26. //session_key 未过期,并且在本生命周期一直有效
  27. if(typeof(callBack)==='function'){
  28. callBack(param);
  29. }
  30. },
  31. fail () {
  32. _self.login(function(){
  33. // session_key 已经失效,需要重新执行登录流程
  34. if(typeof(callBack)==='function'){
  35. callBack(param);
  36. }
  37. });
  38. }
  39. })
  40. },
  41. // 登录
  42. login() {
  43. wx.login({
  44. success: res => {
  45. let code = res.code;
  46. let result = Activity.getSessionKeyFromApi(code);
  47. result.then(res=>{
  48. wx.setStorageSync('loginInfo',{
  49. openId:res.openId,
  50. });
  51. });
  52. }
  53. })
  54. },
  55. async doDecodePhoneNew(code,call='') {
  56. //验证登录是否失效
  57. var params = {
  58. code:code,
  59. appCode:this.globalData.appCode
  60. };
  61. try {
  62. let res = Activity.getAuthMobileNew(params);
  63. res.then(userInfo=>{
  64. console.log(userInfo)
  65. let mobile = userInfo.phoneNumber;
  66. // @todo从缓存里面取openId
  67. let openId = Activity.getOpenId();
  68. //保存用户信息
  69. Activity.saveUser(openId,mobile);
  70. Activity.setMobileCache(mobile);
  71. call();
  72. })
  73. }catch(err) {
  74. console.log(err);
  75. }
  76. },
  77. /**
  78. * 接口请求手机号
  79. * @param {*} encryptedData
  80. * @param {*} iv
  81. * @param call
  82. */
  83. async doDecodePhone(encryptedData,iv,call='') {
  84. let _self = this;
  85. //验证登录是否失效
  86. _self.checkInvalid({encryptedData:encryptedData,iv:iv,call:call},function(param){
  87. var params = {
  88. encryptedData: param.encryptedData,
  89. iv: param.iv,
  90. // sessionKey:Activity.getSessionKey(),
  91. openId:wx.getStorageSync('loginInfo').openId,
  92. };
  93. try {
  94. let res = Activity.getAuthMobile(params);
  95. res.then(userInfo=>{
  96. let mobile = userInfo.phoneNumber;
  97. // @todo从缓存里面取openId
  98. let openId = Activity.getOpenId();
  99. //保存用户信息
  100. Activity.saveUser(openId,mobile);
  101. Activity.setMobileCache(mobile);
  102. if(param.call && typeof(param.call)=='function') {
  103. param.call();
  104. }
  105. })
  106. }catch(err) {
  107. console.log(err);
  108. }
  109. });
  110. },
  111. showToast(title,icon='success',time=2000,call='') {
  112. wx.showToast({
  113. title: title,
  114. icon: icon,
  115. duration: time,
  116. success:function(){
  117. if(typeof(call)=='function') {
  118. call();
  119. }
  120. }
  121. })
  122. },
  123. globalData: {
  124. userInfo: null,
  125. appCode:'HSAY_SHARE_COUPON',
  126. typeSwitchObj : {"C":'现金券',"D":"折扣券"}
  127. },
  128. showLoading() {
  129. if (this._loadingLock) {
  130. return
  131. }
  132. this._loadingLock = true
  133. const page = this.getComponent("#Page")
  134. if (page) {
  135. page.setValue("showLoading", true)
  136. }
  137. },
  138. hideLoading() {
  139. const page = this.getComponent("#Page")
  140. if (page) {
  141. // setTimeout(_ => {
  142. // page.setValue("showLoading", false)
  143. // }, 1000)
  144. page.setValue("showLoading", false)
  145. }
  146. this._loadingLock = false
  147. },
  148. getComponent(id) {
  149. const page = getCurrentPages()[getCurrentPages().length - 1]
  150. return page.selectComponent(id)
  151. }
  152. })