新代码如下,我异步异步改

    1. <script>
    2. import { updateToken } from "./utils/token.js"
    3. import {apiGetUserInfo, uniGetSetting, uniGetSystemInfo, uniGetUserInfo} from "./js/api_base";
    4. export default {
    5. onLaunch: function () {
    6. this.showFavoriteGuide()
    7. this.reviewVersionUpdate()
    8. uniGetSetting().then(res=>{
    9. // #ifdef MP-BAIDU
    10. const bdAppLogin = swan.isLoginSync().isLogin
    11. // #endif
    12. if(res['scope.userInfo'] && bdAppLogin){
    13. uniGetUserInfo().then(res=>{
    14. this.globalData.userInfo = res.userInfo
    15. this.globalData.encryptedData = res.encryptedData;
    16. this.globalData.iv = res.iv;
    17. }).catch(console.log)
    18. }
    19. })
    20. uniGetSystemInfo().then(res=>{
    21. this.globalData.screenWidth = res.screenWidth;
    22. this.globalData.screenHeight = res.screenHeight;
    23. this.globalData.statusBarHeight = res.statusBarHeight;
    24. })
    25. },
    26. onShow() {
    27. console.log("--appShow", this.globalData)
    28. if (this.globalData.tokenTime) {
    29. if (this.globalData.isPhone) { // 手机号登陆
    30. if (new Date().getTime() - this.globalData.tokenTime > 86400000) {// 手机号登陆的1天过期
    31. this.globalData.userInfo = null; // 过期就置空,方便在其他地方未登录判断
    32. this.globalData.tokenTime = null;
    33. this.globalData.token = null;
    34. }
    35. }else {
    36. // 正常登录后台默默刷新token
    37. if (new Date().getTime() - this.globalData.tokenTime > 7200000) {
    38. updateToken().then((res) => {
    39. if (res.data && res.data.code === 200) {
    40. this.globalData.tokenTime = new Date().getTime();
    41. this.globalData.token = res.data.data.token;
    42. }
    43. })
    44. }
    45. }
    46. }
    47. },
    48. globalData: {
    49. userInfo: null,
    50. firstOpenShowPreview: true,
    51. firstOpenShowNotice: true,
    52. },
    53. methods: {
    54. showFavoriteGuide(){
    55. // #ifdef MP-BAIDU
    56. swan.showFavoriteGuide({
    57. type: 'tip',
    58. content: '关注小程序,下次使用更便捷。'
    59. })
    60. // #endif
    61. },
    62. reviewVersionUpdate(){
    63. const updateManager = uni.getUpdateManager();
    64. updateManager.onCheckForUpdate(function (res) {
    65. // 请求完新版本信息的回调
    66. console.log(res.hasUpdate);
    67. });
    68. updateManager.onUpdateReady(function (res) {
    69. uni.showModal({
    70. title: '更新提示',
    71. content: '新版本已经准备好,是否重启应用?',
    72. success(res) {
    73. if (res.confirm) {
    74. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
    75. updateManager.applyUpdate();
    76. }
    77. }
    78. });
    79. });
    80. updateManager.onUpdateFailed(function (res) {
    81. // 新的版本下载失败
    82. });
    83. },
    84. customLogin(){
    85. return new Promise((resolve, reject) => {
    86. if (this.globalData.isPhone){
    87. resolve()
    88. }
    89. apiGetUserInfo().then(res=>{
    90. if (res.user_id === 0) {
    91. reject({err:401,msg:"用户未绑定,请用手机号登录!"});
    92. }
    93. this.globalData.tokenTime = new Date().getTime();
    94. this.globalData.token = res.token;
    95. this.updateGlobalUserinfo(res)
    96. }).catch(reject)
    97. })
    98. },
    99. phoneLogin(encryptedData, iv) {
    100. return new Promise((resolve, reject) => {
    101. // #ifdef MP-BAIDU
    102. swan.getLoginCode({
    103. success: function (res) {
    104. console.log("phoneLoginlogin", res);
    105. return resolve(res);
    106. },
    107. fail: function (res) {
    108. console.log("phoneLoginloginErr", res);
    109. reject("调用登录失败")
    110. }
    111. })
    112. // #endif
    113. }).then((res) => {
    114. let url = `/v2/login-code?code=${res.code}`;
    115. // #ifdef MP-BAIDU
    116. url = `/v2/login/baidu-app?code=${res.code}`
    117. // #endif
    118. return this.fetchPure('GET', url, undefined, false);
    119. }).then((res) => {
    120. if (res.data && res.data.code === 200) {
    121. this.globalData.tokenTime = new Date().getTime();
    122. this.globalData.token = res.data.data.token;
    123. return this.fetchPure('POST', '/v2/baidu-app/login-phone', {encryptedData, iv}, true).then((res) => {
    124. if (res.data && res.data.code === 200) {
    125. this.updateGlobalUserinfo(res.data.data)
    126. }
    127. });
    128. } else {
    129. return Promise.reject(res.data.msg || "获取login-code接口失败")
    130. }
    131. })
    132. },
    133. updateGlobalUserinfo(res) {
    134. this.globalData.id = res.user_id;
    135. this.globalData.vip_id = res.vip_id;
    136. this.globalData.vip_icon = res.vip_icon;
    137. this.globalData.userInfo = {
    138. avatarUrl: res.avatar_url,
    139. nickName: res.nickname
    140. }
    141. },
    142. fetchPure(method, path, data, isFormData) {
    143. const host = 'https://api.tusij.com'
    144. // const host = 'https://api.isheji5.com'
    145. let url = "";
    146. if(path.indexOf("?") > -1) {
    147. url = host + path + `&token=${this && this.globalData && this.globalData.token}`;
    148. }else {
    149. url = host + path + `?token=${this && this.globalData && this.globalData.token}`;
    150. }
    151. url += "&source=baidu_app";
    152. return new Promise((resolve, reject) => {
    153. uni.request({
    154. method: method.toLowerCase(),
    155. url: url,
    156. header: {
    157. 'content-type': isFormData ? "application/x-www-form-urlencoded" : 'application/json' // 默认值
    158. },
    159. data,
    160. success(res) {
    161. resolve(res)
    162. },
    163. fail(e) {
    164. reject(e);
    165. }
    166. })
    167. })
    168. },
    169. },
    170. }
    171. </script>