common.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import * as API_Passport from '../api/passport.js'
  2. import * as API_Shop from '../api/shop.js'
  3. const app = getApp()
  4. /** 自动登陆 */
  5. export async function toAutoLogin(){
  6. //尝试自动登陆之前判断有没有授权用户信息,没有授权用户信息直接跳回到欢迎页
  7. const setting=await wxgetSetting()
  8. if (!setting.authSetting['scope.userInfo']) {
  9. wx.removeStorageSync('welcome')
  10. wx.removeStorageSync('hasAuthorize')
  11. wx.redirectTo({
  12. url: '/pages/welcome/index',
  13. })
  14. return
  15. }
  16. console.log('autologintest',app.globalData.authorInfo)
  17. const authorInfo=app.globalData.authorInfo
  18. if(Object.keys(authorInfo).length>0){
  19. if(authorInfo.bool_o2o===1){
  20. wx.switchTab({
  21. url: '/pages/store/index',
  22. })
  23. }else{
  24. wx.redirectTo({
  25. url: '/pages/noshop/index',
  26. })
  27. }
  28. return
  29. }
  30. // 检测是否登录 如果已经登录 或者登录结果为账户未发现 则不再进行自动登录
  31. if (wx.getStorageSync('login_result') === 'account_not_found') {
  32. wx.redirectTo({
  33. url: '/pages/login/login',
  34. })
  35. return
  36. }
  37. let final = {}
  38. const code= await wxlogin()
  39. const uuid =wx.getStorageSync('uuid')
  40. const mp_type = 'miniprogramo2oseller'
  41. const result=await API_Passport.autoLogin({
  42. code,
  43. uuid,
  44. mini_program_type:mp_type
  45. })
  46. if (result.reson === 'unionid_not_found') { // unionid_not_found 如果已经进行过用户授权但是没有获取到unionID
  47. let {
  48. encryptedData,
  49. iv
  50. } = await wxgetUserInfo({
  51. withCredentials: true,
  52. lang: 'zh_CN'
  53. })
  54. final = await API_Passport.accessUnionID({
  55. code,
  56. uuid,
  57. encrypted_data: encryptedData,
  58. iv
  59. })
  60. }
  61. if(result.reson === 'account_not_found'){ //没有绑定账号 跳到登陆页面
  62. wx.setStorageSync('login_result', result.reson)
  63. wx.redirectTo({
  64. url: '/pages/login/login',
  65. })
  66. }
  67. const res=result.access_token ? result : final
  68. const {
  69. access_token_timeout,
  70. refresh_token_timeout,
  71. } = res
  72. let now = Math.round(new Date() / 1000)
  73. if (res.access_token && res.refresh_token && res.uid) {
  74. app.globalData.authorInfo={...res,'refresh_token_timeout':now+refresh_token_timeout-500,'access_token_timeout':now+access_token_timeout-500}
  75. //登陆信息写入本地存储,删除登陆结果标记
  76. wx.removeStorageSync('login_result')
  77. wx.setStorageSync('authorInfo', app.globalData.authorInfo)
  78. //登陆成功 跳转到店铺首页
  79. console.log('bool_o2o',app.globalData.authorInfo.bool_o2o)
  80. if(app.globalData.authorInfo.bool_o2o===0){
  81. wx.redirectTo({
  82. url: '/pages/noshop/index',
  83. })
  84. }else{
  85. wx.switchTab({
  86. url: '/pages/store/index',
  87. })
  88. }
  89. }
  90. }
  91. // 同步获取登陆code
  92. function wxlogin(){
  93. return new Promise((resolve,reject)=>{
  94. wx.login({
  95. complete: (res) => {resolve(res.code)}
  96. })
  97. })
  98. }
  99. // 同步获取用户信息
  100. function wxgetUserInfo(option){
  101. return new Promise((resolve,reject)=>{
  102. wx.getUserInfo({
  103. ...option,
  104. complete: (res) => {
  105. console.log(res)
  106. resolve(res)
  107. }
  108. })
  109. })
  110. }
  111. //同步获取用授权设置
  112. function wxgetSetting(){
  113. return new Promise((resolve,reject)=>{
  114. wx.getSetting({
  115. complete: (res) => {
  116. resolve(res)
  117. }
  118. })
  119. })
  120. }
  121. /** 获取当前店铺信息 */
  122. export function getCurrentShopData(){
  123. return new Promise((resolve, reject)=>{
  124. // const authorInfo=app.globalData.authorInfo
  125. // if(authorInfo===null||authorInfo===''){
  126. // wx.redirectTo({
  127. // url:'/pages/login/login'
  128. // })
  129. // return
  130. // }
  131. // if (authorInfo.bool_o2o===1){
  132. Promise.all([
  133. /**当前店铺信息 */
  134. API_Shop.getShopData(),
  135. /** 店铺订单统计信息 */
  136. API_Shop.getDashboardData()
  137. ])
  138. .then(res => {
  139. app.globalData.sellerShop = res[0]
  140. wx.setStorageSync('sellerShop', res[0])
  141. app.globalData.orderNum = res[1].wait_ship_order_num
  142. app.globalData.todayOrderNum = res[1].today_order_num
  143. app.globalData.todayOrderTotal=res[1].today_order_total
  144. resolve(res[0])
  145. }).catch(err=>{
  146. reject(err)
  147. })
  148. // }else{
  149. // wx.redirectTo({
  150. // url:'/pages/noshop/index'
  151. // })
  152. // }
  153. })
  154. }