vue-mixin.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import Vue from 'vue'
  2. import Storage from '@/utils/storage'
  3. import { API } from '@/utils/request'
  4. import * as API_Connect from '@/api/connect'
  5. import * as Foundation from '@/ui-utils/Foundation'
  6. import 'wx-promise-pro'
  7. import store from '@/store'
  8. const mp_type = 'miniprogram_uu_sleep'
  9. const mixin = {
  10. data() {
  11. return {
  12. // 项目名称
  13. projectName: '守护之影',
  14. // 图片上传API
  15. MixinUploadApi: `${API.base}/uploaders`,
  16. // 是否已经授权 默认false
  17. hasAuth: false,
  18. userMiniProgramAppId: 'wx013064b7d1caf28f'
  19. }
  20. },
  21. onLoad(query) {
  22. const code = Storage.getItem("codeMsg")
  23. if (!code) {
  24. if (query && (query.type || query.q)){
  25. console.log('我是通过分享或者扫码进来的哦......vue-mixin')
  26. let codeMsg = query
  27. if (query.q) {
  28. const q = decodeURIComponent(query.q)
  29. console.log('q===', q)
  30. codeMsg = Foundation.getQueryObject(q)
  31. }
  32. console.log('codeMsg===', codeMsg)
  33. Storage.setItem("codeMsg", codeMsg)
  34. }
  35. }
  36. },
  37. /** 解决在真机上下拉不回弹 */
  38. onPullDownRefresh() {
  39. wx.stopPullDownRefresh()
  40. },
  41. onShareAppMessage(){
  42. },
  43. methods: {
  44. async checkHasLogin() {
  45. const {
  46. code
  47. } = await wx.pro.login({
  48. timeout: 8000
  49. })
  50. const uuid = Storage.getItem('uuid')
  51. let result = await API_Connect.checkHasLogin({
  52. code,
  53. uuid,
  54. mp_type
  55. })
  56. // console.log('是否登录返回=', result)
  57. let _this = this
  58. if (result.autologin === 'yes') {
  59. _this.hasAuth = true
  60. Storage.setItem('haslogin', { uid: result.uid, loginType: 'auto' })
  61. const {
  62. access_token,
  63. refresh_token,
  64. access_token_timeout,
  65. refresh_token_timeout,
  66. uid,
  67. no_union_id
  68. } = result
  69. // 如果登录成功 存储token(access_token refresh_token) uid 获取用户数据
  70. if (access_token && refresh_token && uid) {
  71. const now=Math.round(new Date()/1000)
  72. store.dispatch('setAccessTokenAction', access_token)
  73. store.dispatch('setRefreshTokenAction', refresh_token)
  74. Storage.setItem('uid', uid)
  75. Storage.setItem('access_token_timeout',now+access_token_timeout-60)
  76. Storage.setItem('refresh_token_timeout',now+refresh_token_timeout-60)
  77. Storage.setItem('haslogin', { uid: result.uid, loginType: 'auto' })
  78. // this.autoBindPart()
  79. store.dispatch('getUserDataAction').then(()=>{
  80. let page = Storage.getItem('goPage')
  81. console.log('mixin-goPage===', page);
  82. uni.switchTab({
  83. url: '/pages/tabBar/home'
  84. })
  85. })
  86. } else if (no_union_id) {
  87. Storage.setItem('haslogin', { loginType: 'hand_movement' })
  88. uni.reLaunch({
  89. url:'/pages/login/auth'
  90. })
  91. } else {
  92. uni.reLaunch({
  93. url:'/pages/login/index'
  94. })
  95. }
  96. } else {
  97. uni.showModal({
  98. title: '提示',
  99. content: "您还未登录,是否前往登录呢?",
  100. confirmText: "确定",
  101. showCancel: true,
  102. success: function(res) {
  103. if (res.confirm) {
  104. uni.reLaunch({
  105. url:'/pages/login/auth'
  106. })
  107. }
  108. }
  109. })
  110. }
  111. },
  112. // 处理result
  113. disposeResult(result) {
  114. const {
  115. access_token,
  116. refresh_token,
  117. access_token_timeout,
  118. refresh_token_timeout,
  119. uid,
  120. no_union_id
  121. } = result
  122. // 如果登录成功 存储token(access_token refresh_token) uid 获取用户数据
  123. if (access_token && refresh_token && uid) {
  124. const now=Math.round(new Date()/1000)
  125. store.dispatch('setAccessTokenAction', access_token)
  126. store.dispatch('setRefreshTokenAction', refresh_token)
  127. Storage.setItem('uid', uid)
  128. Storage.setItem('access_token_timeout',now+access_token_timeout-60)
  129. Storage.setItem('refresh_token_timeout',now+refresh_token_timeout-60)
  130. Storage.setItem('haslogin', { uid: result.uid, loginType: 'auto' })
  131. // this.autoBindPart()
  132. store.dispatch('getUserDataAction').then(()=>{
  133. let page = Storage.getItem('goPage')
  134. console.log('mixin-goPage===', page);
  135. uni.switchTab({
  136. url: '/pages/tabBar/home'
  137. })
  138. })
  139. } else if (no_union_id) {
  140. Storage.setItem('haslogin', { loginType: 'hand_movement' })
  141. uni.reLaunch({
  142. url:'/pages/login/auth'
  143. })
  144. } else {
  145. uni.reLaunch({
  146. url:'/pages/login/index'
  147. })
  148. }
  149. },
  150. async toAutoLogin() {
  151. this.hasAuth = true
  152. // 检测是否登录 如果已经登录 或者登录结果为账户未发现 则不再进行自动登录
  153. // if (Storage.getItem('refresh_token') || Storage.getItem('login_result') === 'account_not_found') return
  154. let final = {}
  155. const {
  156. code
  157. } = await wx.pro.login({
  158. timeout: 8000
  159. })
  160. const uuid = Storage.getItem('uuid')
  161. try {
  162. let result = await API_Connect.loginByAuto({
  163. code,
  164. uuid,
  165. mp_type
  166. })
  167. if (result.reson === 'unionid_not_found') { // 如果已经进行过用户授权但是没有获取到unionID
  168. let {
  169. encryptedData,
  170. iv
  171. } = await wx.pro.getUserInfo({
  172. withCredentials: true,
  173. lang: 'zh_CN'
  174. })
  175. final = await API_Connect.accessUnionID({
  176. code,
  177. uuid,
  178. encrypted_data: encryptedData,
  179. iv
  180. })
  181. }
  182. if(result.reson === 'account_not_found' || final.reson === 'account_not_found'){ //没有绑定账号 跳到登陆页面
  183. uni.reLaunch({
  184. url:'/pages/login/index'
  185. })
  186. }
  187. Storage.setItem('login_result', result.reson)
  188. Storage.setItem('haslogin', { loginType: 'hand_movement' })
  189. this.disposeResult(result)
  190. } catch (e) {
  191. Storage.setItem('login_result', false)
  192. if (e.response.data.code === 'E133') {
  193. uni.redirectTo({
  194. url: '/pages/tabBar/home'
  195. })
  196. this.hasAuth = false
  197. Storage.removeItem('uuid')
  198. }
  199. }
  200. },
  201. goBuy() {
  202. wx.navigateToMiniProgram({
  203. appId: this.userMiniProgramAppId,
  204. path: '/pages/b2b2c/index?type=SHOPPING&page=shopping&no=1789',
  205. //develop开发版;trial体验版;release正式版
  206. // envVersion: 'release',
  207. envVersion: 'trial',
  208. success(res) {
  209. // 打开成功
  210. console.log("跳转小程序成功!", res);
  211. },
  212. fail(res) {
  213. console.log('fail===', res)
  214. }
  215. })
  216. },
  217. }
  218. }
  219. Vue.prototype.$toAutoLogin = mixin.methods.toAutoLogin
  220. Vue.prototype.$checkHasLogin = mixin.methods.checkHasLogin
  221. export default mixin