user.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. await dispatch('login', {openid: state.openid, phone: ''})
  113. return 2
  114. }
  115. },
  116. async unCacheUserInfo({ state, commit, dispatch}, info){
  117. // 弹出询问获取手机号
  118. if(config['getPhone'] && state.sessionKey){
  119. commit('USER_GETPHONE', true);
  120. return 1
  121. }else {
  122. await dispatch('login', {openid: state.openid, phone: ''})
  123. return 2
  124. }
  125. },
  126. // 登陆
  127. async login({
  128. state,
  129. commit
  130. }, info) {
  131. const openid = info.openid;
  132. const phone = info.phone;
  133. const { nickName, gender, avatarUrl} = state.userInfo
  134. try{
  135. const res = await api("app.getOpenId", {
  136. "openid": openid,
  137. "phone": phone,
  138. "name": nickName
  139. })
  140. // 存储用户信息
  141. commit(USER_INFO, { ...state.userInfo, name: res.data.name});
  142. // 设置token
  143. commit(SET_TOKEN, openid);
  144. // 登陆状态设为已经登陆
  145. store.commit('SET_LOGINSTATE',true);
  146. // 存储用户信息
  147. // store.commit('SET_USERLIST',{mobile: form.mobile, token: token});
  148. // 获取客服电话
  149. // store.dispatch('getAdministratorTel')
  150. // 刷新地图
  151. // await store.dispatch('initMap')10.190.35.204
  152. return res.data
  153. }catch(e){
  154. console.log(e)
  155. //TODO handle the exception
  156. return e
  157. }
  158. },
  159. // 根据 token登录
  160. async loginRefresh({
  161. commit
  162. }, token ) {
  163. try{
  164. // const res = await api('h5.refreshToken', {token: token})
  165. // if(res.code == 1){
  166. // // 更新token
  167. // let token = res.data.token
  168. // commit(SET_TOKEN, token);
  169. // // 登陆状态设为已经登陆
  170. // store.commit('SET_LOGINSTATE',true);
  171. // return token
  172. // } else {
  173. // commit(OUT_LOGIN);
  174. // store.commit('SET_LOGINSTATE',false);
  175. // throw new Error(res.msg);
  176. // }
  177. }catch(e){
  178. //TODO handle the exception
  179. return e
  180. }
  181. },
  182. // 退出登陆
  183. loginOut({
  184. commit
  185. }) {
  186. commit(OUT_LOGIN);
  187. uni.$u.route({
  188. url: '/pages/view/public/login',
  189. type: 'redirectTo'
  190. })
  191. },
  192. }
  193. const mutations = {
  194. // 保存18岁成年状态
  195. [USER_ADULTHOOD](state, status) {
  196. state.adulthoodStatus = status
  197. state.adultSet = true
  198. // #ifdef MP-WEIXIN
  199. if(status){
  200. wx.hideTabBar()
  201. }else{
  202. wx.showTabBar()
  203. }
  204. // #endif
  205. setStorage('adultSet', true)
  206. },
  207. ['USER_GETPHONE'](state, status) {
  208. state.phoneStatus = status
  209. },
  210. // 保存用户信息
  211. [USER_INFO](state, userInfo) {
  212. state.userInfo = userInfo
  213. setStorage('userInfo', userInfo)
  214. },
  215. // 保存Token
  216. [SET_TOKEN](state, token) {
  217. state.token = token
  218. setStorage('token', token)
  219. },
  220. // 保存 sessionKey
  221. ["SET_SESSIONKEY"](state, sessionKey) {
  222. state.sessionKey = sessionKey
  223. setStorage('sessionKey', sessionKey)
  224. },
  225. // 保存openid
  226. [SET_OPENID](state, openid) {
  227. state.openid = openid
  228. setStorage('openid', openid)
  229. },
  230. // 保存手机号 和 token
  231. 'SET_USERLIST'(state, userObj) {
  232. const list = state.userList;
  233. // 之前没有存储过
  234. if(list.findIndex(obj => {return obj.mobile == userObj.mobile}) < 0){
  235. list.push(userObj)
  236. }
  237. state.userList = list;
  238. setStorage('userList', list)
  239. },
  240. // 退出登录
  241. [OUT_LOGIN](state, data) {
  242. removeStorage('token');
  243. removeStorage('userInfo');
  244. store.commit('SET_LOGINSTATE',false);
  245. store.commit('USER_INFO', {});
  246. store.commit('SET_TOKEN', '');
  247. },
  248. }
  249. const getters = {
  250. }
  251. export default {
  252. state,
  253. mutations,
  254. actions,
  255. getters
  256. }