auth.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. import { T_REDIRECT_TYPE, T_NEW_BIND_TYPE, T_USER_TOKEN, T_STORAGE_KEY, clearAllCache } from '../../constant'
  2. import { A_TF_MAIN } from '../../config'
  3. import { CHNAGE_USER_INFO, CHNAGE_USER_TOKEN, CHNAGE_USER_IDENTITY } from './type'
  4. import store from '../index'
  5. import { getUrlCode } from '../../utils'
  6. import { getIsShopByUserApi, updatePhoneLoginRegisterApi, updateWXLoginApi, updateWXAppLoginApi, updateAlipayLoginApi, getUserInfoApi, updateUserInfoApi } from '../../api/anotherTFInterface'
  7. export default {
  8. namespaced: true,
  9. state() {
  10. return {
  11. userInfo: uni.getStorageSync(T_STORAGE_KEY) || {}, // 新团蜂的
  12. userToken: uni.getStorageSync(T_USER_TOKEN) || '', // 新团蜂的
  13. identityInfo: {
  14. type: [], // 9商家或8商家员工,1加盟商,2代理商
  15. shopInfo: {}
  16. }
  17. }
  18. },
  19. mutations: {
  20. [CHNAGE_USER_INFO](state, userInfo) {
  21. state.userInfo = userInfo
  22. uni.setStorageSync(T_STORAGE_KEY, userInfo)
  23. },
  24. [CHNAGE_USER_TOKEN](state, token) {
  25. state.userToken = token
  26. uni.setStorageSync(T_USER_TOKEN, token)
  27. },
  28. [CHNAGE_USER_IDENTITY](state, data) {
  29. if (data.type) state.identityInfo.type = data.type
  30. if (data.shopInfo) state.identityInfo.shopInfo = data.shopInfo
  31. }
  32. },
  33. actions: {
  34. // 手机验证码登录或手机密码登录或手机验证码密码注册
  35. phoneLoginRegisterAction({ state, commit, dispatch }, { isAfter, loginData } = { loginData: {} }) {
  36. return new Promise((resolve, reject) => {
  37. uni.showLoading({ mask: true })
  38. updatePhoneLoginRegisterApi({ ...loginData })
  39. .then(({ data }) => {
  40. console.log(data)
  41. if (data.phone) {
  42. try {
  43. uni.hideLoading()
  44. uni.showToast({ title: '登录成功', icon: 'none' })
  45. if (isAfter) dispatch('LoginAfterAction', { type: 'phone', data })
  46. resolve(data)
  47. } catch (err) {
  48. uni.hideLoading()
  49. uni.showToast({ title: JSON.stringify(err), icon: 'none' })
  50. reject(err)
  51. }
  52. } else {
  53. uni.hideLoading()
  54. uni.showToast({ title: '系统错误,未能注册手机号', icon: 'none' })
  55. reject()
  56. }
  57. })
  58. .catch((err) => {
  59. uni.hideLoading()
  60. reject(err)
  61. })
  62. })
  63. },
  64. // 微信登陆
  65. wxLoginAction({ state, commit, dispatch }, { isAfter } = {}) {
  66. uni.showLoading({ mask: true })
  67. return new Promise((resolve, reject) => {
  68. const loginData = {
  69. terminal: store.state.app.terminal,
  70. headImage: '',
  71. code: ''
  72. }
  73. if (loginData.terminal === 3) {
  74. const appid = 'wxb19ccb829623be12' // 团蜂wxb19ccb829623be12,新巨蜂wx603b04a561e4683e,别的wxdf390bb4f8a67641
  75. const local = store.state.app.isInMiniProgram ? `${A_TF_MAIN}/#/pages/login/login?miniProgram=1` : `${A_TF_MAIN}/#/pages/login/login`
  76. const code = getUrlCode().code
  77. if (!code) {
  78. // 如https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb19ccb829623be12&redirect_uri=http%3A%2F%2Flocalhost%3A8988%2FTFShop_Uni_H5%2F%23%2Fpages%2Flogin%2Flogin&response_type=code&scope=snsapi_userinfo#wechat_redirect
  79. window.location.href =
  80. 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
  81. appid +
  82. '&redirect_uri=' +
  83. encodeURIComponent(local) +
  84. '&response_type=code&scope=snsapi_userinfo#wechat_redirect'
  85. } else {
  86. updateWXLoginApi({ ...loginData, code })
  87. .then(({ data }) => {
  88. try {
  89. uni.hideLoading()
  90. if (isAfter) dispatch('LoginAfterAction', { type: 'wx', data })
  91. resolve(data)
  92. } catch (err) {
  93. uni.hideLoading()
  94. uni.showToast({ title: JSON.stringify(err), icon: 'none' })
  95. reject(err)
  96. }
  97. })
  98. .catch((err) => {
  99. uni.hideLoading()
  100. reject(err)
  101. })
  102. }
  103. } else if (loginData.terminal === 5) {
  104. uni.hideLoading()
  105. uni.showToast({ title: '暂不支持!', icon: 'none' })
  106. reject()
  107. } else if (loginData.terminal === 1) {
  108. uni.login({
  109. provider: 'weixin',
  110. success(loginRes) {
  111. uni.getUserInfo({ // 获取用户信息
  112. provider: 'weixin',
  113. success(infoRes) {
  114. updateWXAppLoginApi({ wechatOpenId: infoRes.userInfo.openId }).then(({ data }) => {
  115. uni.hideLoading()
  116. if (isAfter) dispatch('LoginAfterAction', { type: 'wx', data: { ...data, wechatOpenId: infoRes.userInfo.openId, headImage: infoRes.userInfo.avatarUrl } })
  117. resolve(data)
  118. })
  119. .catch((err) => {
  120. uni.hideLoading()
  121. reject(err)
  122. })
  123. },
  124. fail: (err) => {
  125. uni.hideLoading()
  126. uni.showToast({ title: '微信登录授权失败', icon: 'none' })
  127. reject(err)
  128. }
  129. })
  130. },
  131. fail: (err) => {
  132. uni.hideLoading()
  133. uni.showToast({ title: '微信登录授权失败', icon: 'none' })
  134. reject(err)
  135. }
  136. })
  137. } else if (loginData.terminal === 2) {
  138. uni.login({
  139. provider: 'weixin',
  140. success: (res) => {
  141. uni.hideLoading()
  142. loginData.code = res.code
  143. },
  144. fail: (err) => {
  145. uni.showToast({ title: '微信登录授权失败', icon: 'none' })
  146. }
  147. })
  148. uni.getUserProfile({
  149. desc: '正在获取', // 不写不弹提示框
  150. success: (res) => {
  151. updateWXLoginApi({ ...loginData, headImage: res.userInfo.avatarUrl })
  152. .then((data) => {
  153. uni.hideLoading()
  154. if (isAfter) dispatch('LoginAfterAction', { type: 'wx', data })
  155. resolve(data)
  156. })
  157. .catch((err) => {
  158. uni.hideLoading()
  159. reject(err)
  160. })
  161. },
  162. fail: (err) => {
  163. uni.hideLoading()
  164. uni.showToast({ title: '微信登录授权失败', icon: 'none' })
  165. reject(err)
  166. }
  167. })
  168. } else if (loginData.terminal === 4) {
  169. uni.hideLoading()
  170. uni.showToast({ title: '暂不支持!', icon: 'none' })
  171. reject()
  172. } else {
  173. uni.hideLoading()
  174. uni.showToast({ title: '未能获取系统信息', icon: 'none' })
  175. reject()
  176. }
  177. })
  178. },
  179. // 支付宝登陆
  180. aliPayLoginAction({ commit, dispatch }, { isAfter } = {}) {
  181. uni.showLoading({ mask: true })
  182. return new Promise((resolve, reject) => {
  183. uni.login({
  184. provider: 'alipay',
  185. scopes: 'auth_user',
  186. success(res) {
  187. updateAlipayLoginApi({
  188. code: res.authCode
  189. })
  190. .then(({ data }) => {
  191. uni.hideLoading()
  192. if (isAfter) dispatch('LoginAfterAction', { type: 'alipay', data })
  193. resolve(data)
  194. })
  195. .catch((err) => {
  196. uni.hideLoading()
  197. reject(err)
  198. })
  199. }
  200. })
  201. })
  202. },
  203. LoginAfterAction({ state, commit, dispatch }, { type, data }) {
  204. try {
  205. const tabbarList = ['pages/index/index', 'pages/business-district/business-district', 'pages/order/order', '/pages/user/user']
  206. const redirect = uni.getStorageSync(T_REDIRECT_TYPE)
  207. console.log(type)
  208. if (type === 'phone') {
  209. setTimeout(() => {
  210. commit(CHNAGE_USER_TOKEN, data.token)
  211. commit(CHNAGE_USER_INFO, data)
  212. if (data.roleId) commit(CHNAGE_USER_IDENTITY, { type: [ ...new Set([...state.identityInfo.type, data.roleId]) ] })
  213. dispatch('updateIdentityInfo')
  214. if (redirect) {
  215. uni.removeStorageSync(T_REDIRECT_TYPE)
  216. if (tabbarList.includes(this.redirect)) {
  217. uni.switchTab({
  218. url: this.redirect
  219. })
  220. } else {
  221. uni.redirectTo({
  222. url: this.redirect
  223. })
  224. }
  225. } else if (uni.getStorageSync(T_NEW_BIND_TYPE)) {
  226. uni.redirectTo({
  227. url: '/pages/jump/jump'
  228. })
  229. } else {
  230. uni.switchTab({
  231. url: '/'
  232. })
  233. }
  234. }, 2000)
  235. } else if (type === 'wx') {
  236. if (data.ifFirst == 0) {
  237. if (data.phone) {
  238. commit(CHNAGE_USER_TOKEN, data.token)
  239. commit(CHNAGE_USER_INFO, data)
  240. if (data.roleId) commit(CHNAGE_USER_IDENTITY, { type: [ ...new Set([...state.identityInfo.type, data.roleId]) ] })
  241. dispatch('updateIdentityInfo')
  242. if (this.redirect) {
  243. uni.removeStorageSync(T_REDIRECT_TYPE)
  244. window.location.replace(`${A_TF_MAIN}/#${this.redirect}`)
  245. } else if (uni.getStorageSync(T_NEW_BIND_TYPE)) {
  246. window.location.replace(`${A_TF_MAIN}/#/pages/jump/jump`)
  247. } else {
  248. window.location.replace(`${A_TF_MAIN}/#/pages/another-index/index`)
  249. }
  250. } else {
  251. uni.showToast({ title: '未能识别的错误', icon: 'none' })
  252. }
  253. } else {
  254. window.location.replace(`${A_TF_MAIN}/#/another-tf/another-serve/bindPhone/index?wechatOpenId=${data.wechatOpenId || ''}&headImage=${data.headImage || ''}&buyerUserId=${data.buyerUserId || ''}`) // data=${JSON.stringify(data)}&
  255. }
  256. } else if (type === 'alipay') {
  257. if (data.ifFirst == 0) {
  258. commit(CHNAGE_USER_TOKEN, data.token)
  259. commit(CHNAGE_USER_INFO, data)
  260. if (data.roleId) commit(CHNAGE_USER_IDENTITY, { type: [ ...new Set([...state.identityInfo.type, data.roleId]) ] })
  261. dispatch('updateIdentityInfo')
  262. if (redirect) {
  263. uni.removeStorageSync(T_REDIRECT_TYPE)
  264. if (tabbarList.includes(this.redirect)) {
  265. uni.switchTab({
  266. url: this.redirect
  267. })
  268. } else {
  269. uni.redirectTo({
  270. url: this.redirect
  271. })
  272. }
  273. } else if (uni.getStorageSync(T_NEW_BIND_TYPE)) {
  274. uni.redirectTo({
  275. url: '/pages/jump/jump'
  276. })
  277. } else {
  278. uni.switchTab({
  279. url: '/'
  280. })
  281. }
  282. } else { // 第一次登录,绑定手机号
  283. uni.redirectTo({
  284. url: `/another-tf/another-serve/bindPhone/index?wechatOpenId=${data.wechatOpenId || ''}&headImage=${data.headImage || ''}&buyerUserId=${data.buyerUserId || ''}`
  285. })
  286. }
  287. }
  288. } catch (err) {
  289. uni.showToast({ title: JSON.stringify(err), icon: 'none' })
  290. }
  291. },
  292. logoutAction({ commit }, isQuiet) {
  293. uni.removeStorageSync(T_USER_TOKEN)
  294. commit(CHNAGE_USER_INFO, {})
  295. commit(CHNAGE_USER_TOKEN, '')
  296. clearAllCache()
  297. if (isQuiet) {
  298. uni.showToast({ title: '退出成功', icon: 'none' })
  299. setTimeout(() => {
  300. uni.switchTab({
  301. url: '/'
  302. })
  303. }, 2000)
  304. }
  305. },
  306. updateUserInfoAction({ state, dispatch }, updateData) {
  307. uni.showLoading()
  308. updateUserInfoApi(updateData)
  309. .then((res) => {
  310. uni.hideLoading()
  311. uni.showToast({ title: '修改成功', icon: 'success' })
  312. dispatch('refrshUserInfoAction')
  313. })
  314. .catch((e) => {
  315. uni.hideLoading()
  316. })
  317. },
  318. // 刷新用户信息
  319. refrshUserInfoAction({ state, dispatch, commit }, cb) {
  320. return new Promise((resolve, reject) => {
  321. getUserInfoApi({})
  322. .then((res) => {
  323. const tempUserInfo = uni.getStorageSync(T_STORAGE_KEY)
  324. commit(CHNAGE_USER_INFO, Object.assign(res.data, {
  325. ifFirst: tempUserInfo.ifFirst,
  326. refreshToken: tempUserInfo.refreshToken,
  327. sessionKey: tempUserInfo.sessionKey,
  328. ssoUserInfo: tempUserInfo.ssoUserInfo,
  329. token: tempUserInfo.token
  330. }))
  331. if (res.data.roleId) commit(CHNAGE_USER_IDENTITY, { type: [ ...new Set([...state.identityInfo.type, res.data.roleId]) ] })
  332. dispatch('updateIdentityInfo')
  333. cb && typeof cb === 'function' && cb(res.data)
  334. resolve(res.data)
  335. })
  336. .catch((err) => {
  337. reject(err)
  338. })
  339. })
  340. },
  341. // 获取身份(是否商家)
  342. updateIdentityInfo({ state, dispatch, commit }) {
  343. return new Promise((resolve, reject) => {
  344. const userInfo = uni.getStorageSync(T_STORAGE_KEY)
  345. if (userInfo && userInfo.phone) {
  346. getIsShopByUserApi({ mobile: userInfo.phone })
  347. .then((res) => {
  348. if (res.data && res.data.shopId) {
  349. if (res.data.staff) {
  350. commit(CHNAGE_USER_IDENTITY, { type: [ ...new Set([...state.identityInfo.type, 8]) ], shopInfo: res.data || {} })
  351. } else {
  352. commit(CHNAGE_USER_IDENTITY, { type: [ ...new Set([...state.identityInfo.type, 9]) ], shopInfo: res.data || {} })
  353. }
  354. }
  355. resolve(res.data)
  356. })
  357. .catch((err) => {
  358. uni.hideToast()
  359. reject(err)
  360. })
  361. }
  362. })
  363. }
  364. }
  365. }