user.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // 用户数据模块
  2. import api from '@/common/request/index'
  3. import store from '@/store'
  4. import { setStorage, getStorage, removeStorage } from '@/util/index.js'
  5. import mpWx from '@/common/mpWX.js'
  6. import config from '@/config.js'
  7. import {
  8. USER_INFO,
  9. SET_TOKEN,
  10. OUT_LOGIN,
  11. SET_LOGINSTATE,
  12. USER_ADULTHOOD,
  13. SET_OPENID
  14. } from '../types.js'
  15. const state = {
  16. adultSet: getStorage('adultSet') || false,
  17. adulthoodStatus: false, // 18岁询问状态
  18. userInfo: getStorage('userInfo') ? getStorage('userInfo') : {}, // 用户信息
  19. openid: getStorage('openid') ? getStorage('openid') : '', // token
  20. sessionKey: '', // token
  21. token: getStorage('token') ? getStorage('token') : '', // token
  22. userList: getStorage('userList') ? getStorage('userList') : [],
  23. phoneStatus: false, // 请求获取手机号
  24. phone: '', // 请求获取手机号
  25. }
  26. const actions = {
  27. // 获取用户openId
  28. async getOpenId({
  29. state,
  30. commit
  31. }, {code}) {
  32. try{
  33. const obj = await api("app.getOpenId", {code: code})
  34. const { openid, sessionKey } = obj.data;
  35. console.log(openid)
  36. // 存储用户信息
  37. // if(userinfo){
  38. // commit(USER_INFO, userinfo);
  39. // }
  40. // 判断是否设计过年龄
  41. // if(!is18){
  42. // commit(USER_ADULTHOOD, true)
  43. // }
  44. // 储存openId
  45. commit(SET_OPENID, openid)
  46. // 设置token
  47. commit(SET_TOKEN, openid);
  48. // 储存sessionKey
  49. state.sessionKey = sessionKey
  50. if(obj.code == 200){
  51. // 登陆状态设为已经登陆
  52. store.commit('SET_LOGINSTATE',true);
  53. } else{
  54. }
  55. }catch(e){
  56. //TODO handle the exception
  57. }
  58. },
  59. // 关闭
  60. async closePhone({ state, commit }, phoneCode) {
  61. // 关闭手机号请求窗口
  62. state.phoneStatus = false
  63. },
  64. // 获取用户手机号
  65. async disPhone({ state, commit }, phoneCode) {
  66. // 获取code
  67. // const obj = await api("app.getPhone", {code: phoneCode})
  68. // return obj
  69. },
  70. // 判断是否成年
  71. async disAdulthood({
  72. state,
  73. commit
  74. }, info) {
  75. const adultType = info.detail;
  76. const pos = info.pos;
  77. const rawData = { city: pos.city, country: pos.country, province: pos.province, district: pos.district}
  78. if(adultType){
  79. try{
  80. const obj = await api("app.adultUpdate", {is18: true, ...rawData})
  81. console.log("success:","设置成年,请求成功")
  82. commit(USER_ADULTHOOD, false)
  83. }catch(e){
  84. console.log("error: ","设置成年,请求失败")
  85. //TODO handle the exception
  86. // uni.showModal({
  87. // title: '提示',
  88. // content: "设置成年,请求失败"
  89. // });
  90. }
  91. }else{
  92. console.log("未满18岁")
  93. // const obj = await api("app.adultUpdate", {is18: false, ...rawData})
  94. wx.exitMiniProgram({
  95. success(e) {
  96. console.log("退出")
  97. },
  98. fail(e) {
  99. console.log("退出失败")
  100. },
  101. })
  102. }
  103. },
  104. async cacheUserInfo({ state, commit, dispatch}, info){
  105. const userInfo = info.detail.userInfo;
  106. // 存储用户信息
  107. commit(USER_INFO, userInfo);
  108. // 弹出询问获取手机号
  109. if(config['getPhone'] && state.sessionKey){
  110. commit('USER_GETPHONE', true);
  111. }else {
  112. const res = await dispatch('login', {openid: state.openid})
  113. if(res.code == 500){
  114. commit('USER_GETPHONE', true);
  115. return 1
  116. }
  117. return 2
  118. }
  119. },
  120. async unCacheUserInfo({ state, commit, dispatch}, info){
  121. // 弹出询问获取手机号
  122. if(config['getPhone'] && state.sessionKey){
  123. commit('USER_GETPHONE', true);
  124. return 1
  125. }else {
  126. await dispatch('login', {openid: state.openid, phone: ''})
  127. return 2
  128. }
  129. },
  130. // 登陆
  131. async login({
  132. state,
  133. commit
  134. }, info) {
  135. const openid = info.openid;
  136. const phone = info.phone;
  137. const { nickName, gender, avatarUrl} = state.userInfo
  138. try{
  139. const res = await api("app.getOpenId", {
  140. "openid": openid,
  141. "phone": phone,
  142. "name": nickName
  143. })
  144. // 存储用户信息
  145. commit(USER_INFO, { ...state.userInfo, name: res.data.name});
  146. // 设置token
  147. commit(SET_TOKEN, openid);
  148. // 登陆状态设为已经登陆
  149. store.commit('SET_LOGINSTATE',true);
  150. // 存储用户信息
  151. // store.commit('SET_USERLIST',{mobile: form.mobile, token: token});
  152. // 获取客服电话
  153. // store.dispatch('getAdministratorTel')
  154. // 刷新地图
  155. // await store.dispatch('initMap')10.190.35.204
  156. return res
  157. }catch(e){
  158. console.log(e)
  159. //TODO handle the exception
  160. return e
  161. }
  162. },
  163. // 根据 token登录
  164. async loginRefresh({
  165. commit
  166. }, token ) {
  167. try{
  168. // const res = await api('h5.refreshToken', {token: token})
  169. // if(res.code == 1){
  170. // // 更新token
  171. // let token = res.data.token
  172. // commit(SET_TOKEN, token);
  173. // // 登陆状态设为已经登陆
  174. // store.commit('SET_LOGINSTATE',true);
  175. // return token
  176. // } else {
  177. // commit(OUT_LOGIN);
  178. // store.commit('SET_LOGINSTATE',false);
  179. // throw new Error(res.msg);
  180. // }
  181. }catch(e){
  182. //TODO handle the exception
  183. return e
  184. }
  185. },
  186. // 退出登陆
  187. loginOut({
  188. commit
  189. }) {
  190. commit(OUT_LOGIN);
  191. uni.$u.route({
  192. url: '/pages/view/public/login',
  193. type: 'redirectTo'
  194. })
  195. },
  196. }
  197. const mutations = {
  198. // 保存18岁成年状态
  199. [USER_ADULTHOOD](state, status) {
  200. state.adulthoodStatus = status
  201. state.adultSet = true
  202. // #ifdef MP-WEIXIN
  203. if(status){
  204. wx.hideTabBar()
  205. }else{
  206. wx.showTabBar()
  207. }
  208. // #endif
  209. setStorage('adultSet', true)
  210. },
  211. ['USER_GETPHONE'](state, status) {
  212. state.phoneStatus = status
  213. },
  214. // 保存用户信息
  215. [USER_INFO](state, userInfo) {
  216. state.userInfo = userInfo
  217. setStorage('userInfo', userInfo)
  218. },
  219. // 保存Token
  220. [SET_TOKEN](state, token) {
  221. state.token = token
  222. setStorage('token', token)
  223. },
  224. // 保存 sessionKey
  225. ["SET_SESSIONKEY"](state, sessionKey) {
  226. state.sessionKey = sessionKey
  227. setStorage('sessionKey', sessionKey)
  228. },
  229. // 保存openid
  230. [SET_OPENID](state, openid) {
  231. state.openid = openid
  232. setStorage('openid', openid)
  233. },
  234. // 保存手机号 和 token
  235. 'SET_USERLIST'(state, userObj) {
  236. const list = state.userList;
  237. // 之前没有存储过
  238. if(list.findIndex(obj => {return obj.mobile == userObj.mobile}) < 0){
  239. list.push(userObj)
  240. }
  241. state.userList = list;
  242. setStorage('userList', list)
  243. },
  244. // 退出登录
  245. [OUT_LOGIN](state, data) {
  246. removeStorage('token');
  247. removeStorage('userInfo');
  248. store.commit('SET_LOGINSTATE',false);
  249. store.commit('USER_INFO', {});
  250. store.commit('SET_TOKEN', '');
  251. },
  252. }
  253. const getters = {
  254. }
  255. export default {
  256. state,
  257. mutations,
  258. actions,
  259. getters
  260. }