store.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import Vue from 'vue' //引入 Vue
  2. import Vuex from 'vuex' //引入 Vuex
  3. import ajax from './common/js/ajax.js' //引入封装好的 request 方法
  4. import { //引入 api
  5. api_onLaunch,
  6. SaveUserInfo
  7. } from './api.js'
  8. Vue.use(Vuex) //套路
  9. const store = new Vuex.Store({
  10. state: { //状态
  11. N401: 0,
  12. code: '',
  13. gotLocation: false,
  14. id: uni.getStorageSync('id'),
  15. openid: uni.getStorageSync('openid'),
  16. networkType: '', //网络状态信息
  17. userWeixinInfo: {}, //用户微信信息,包括微信名,微信头像等
  18. userServerInfo: {}, //用户服务器信息,包括 token,用户名,等级等
  19. // showVerify: false, //是否显示手机短信验证弹窗
  20. showWarn: false, //是否显示警告用户弹窗
  21. showGetUserInfoButton: false, //是否显示获取用户信息button,
  22. showphoneBtn: false, //是否跳转用户手机号授权,
  23. courseList: [],
  24. // sex: uni.getStorageSync('sex')
  25. },
  26. mutations: {
  27. CHANGECOURSELIST(state, courseList) {
  28. state.courseList = courseList
  29. },
  30. CHANGENETWORKSTATUS(state, payload) { //修改网络类型
  31. state.networkType = payload
  32. },
  33. GETUSERWEIXININFO(state, payload) { //获取到用户微信信息之后的操作
  34. console.log('微信信息', payload)
  35. state.userWeixinInfo = payload
  36. },
  37. GETUSERSERVERINFO(state, payload) { //获取到用户服务器信息之后的操作
  38. if (!state.openid) {
  39. uni.setStorageSync('openid', payload.openid)
  40. state.openid = payload.openid
  41. }
  42. if (!state.id) {
  43. uni.setStorageSync('id', payload.id)
  44. state.id = payload.id
  45. }
  46. // payload.cha_nickname = 'TEST1211313123'
  47. // payload.signuped = true
  48. // payload.score =1300
  49. // payload.max_score=1800
  50. // payload.type = 2
  51. // payload.level_name = '批发商'
  52. // payload.money = 1
  53. // payload.status = 3
  54. state.userServerInfo = payload //把获取到的用户服务器信息,保存到 vuex 中
  55. },
  56. HIDEVERIFY(state) { // 关闭手机号码验证弹窗
  57. state.showVerify = false
  58. },
  59. SHOWVERIFY(state) { // 打开手机号码验证弹窗
  60. state.showVerify = true
  61. },
  62. HIDEWARN(state) { // 关闭警告用户弹窗
  63. state.showWarn = false
  64. },
  65. SHOWWARN(state) { // 打开警告用户弹窗
  66. state.showWarn = true
  67. },
  68. SHOWGETUSERINFOBUTTON(state) { //显示获取用户信息按钮
  69. state.showGetUserInfoButton = true
  70. },
  71. HIDEGETUSERINFOBUTTON(state) { //隐藏获取用户信息按钮
  72. state.showGetUserInfoButton = false
  73. },
  74. SHOWPHONE(state) {
  75. state.showphoneBtn = true
  76. },
  77. HIDEPHONE(state) {
  78. state.showphoneBtn = false
  79. },
  80. SIGNUP(state) { // 用户报名成功改变状态
  81. state.userServerInfo.signuped = true
  82. },
  83. GETLOCALTION(state) { // 获取位置信息
  84. state.gotLocation = true
  85. },
  86. READCHALLENGE(state) { // 已读挑战消息
  87. state.userServerInfo.cha_nickname = ''
  88. }
  89. },
  90. actions: {
  91. // 登录接口 进入首页初始化
  92. newLogin({
  93. commit,
  94. state,
  95. dispatch
  96. }) {
  97. return new Promise(async (resolve, reject) => {
  98. let CODE = '123'
  99. if (!state.openid) { //登录状态过期,重新获取 code,并将code 连同用户头像/昵称/openid一同发给后台
  100. const [, {
  101. code
  102. }] = await uni.login()
  103. CODE = code
  104. }
  105. uni.hideLoading() // 异步操作结束,停止 loading
  106. const latitude = uni.getStorageSync('latitude')
  107. const longitude = uni.getStorageSync('longitude')
  108. const nickName = uni.getStorageSync('nickName')
  109. const avatar = uni.getStorageSync('avatar')
  110. const sex = uni.getStorageSync('sex')
  111. this.$ajax.get(
  112. `${api_onLaunch}?id=${state.id}&sex=${sex}&latitude=${latitude}&longitude=${longitude}&code=${CODE}&avatar=${avatar}&openid=${state.openid ? state.openid : '123'}&nickname=${nickName}`
  113. ).then(([, {
  114. data: res
  115. }]) => {
  116. // res.code = 300
  117. if (res.code === 200) { //当前微信用户已经绑定手机号了
  118. // user_status为1,销售员不能进入系统
  119. if (res.data.user_status == 1) {
  120. uni.reLaunch({
  121. url: "../index/jurisdiction?phone=" + res.data.phone
  122. })
  123. return false
  124. }
  125. state.N401 = 0
  126. commit('GETUSERSERVERINFO', res.data)
  127. uni.hideLoading() //异步操作结束,停止 loading
  128. this.$ajax.get('/user/account_status').then(([, {
  129. data: res
  130. }]) => {
  131. if (res.code === 200) {
  132. if (res.data.status === 1) {
  133. uni.reLaunch({
  134. url: "../loginOutTip/loginOutTip"
  135. })
  136. return false
  137. }
  138. uni.getSetting({
  139. success(res) {
  140. if (!uni.getStorageSync(
  141. 'nickName') || !uni
  142. .getStorageSync('avatar')) {
  143. commit('SHOWGETUSERINFOBUTTON')
  144. } else {
  145. dispatch('getLocation')
  146. }
  147. },
  148. fail() {}
  149. })
  150. }
  151. resolve()
  152. }).catch(() => {
  153. resolve()
  154. })
  155. // } else if (res.code === 20013) { //当前微用户没有绑定手机号,弹出验证码框 // 401 表示 code 过期/失效 openid 获取不到等
  156. // uni.hideLoading() //异步操作结束,停止 loadin
  157. // commit('SHOWVERIFY')
  158. // uni.navigateTo({ url: '../boundPhone/boundPhone' })
  159. // resolve()
  160. // }else{ //其他边界情况,提示用户下拉刷新
  161. // uni.hideLoading() // 异步操作结束,停止 loading
  162. // uni.showModal({
  163. // content: res.msg,
  164. // showCancel: false
  165. // })
  166. // reject(500)
  167. // }
  168. } else if (res.code ===
  169. 300) { //当前微信用户没有绑定手机号,弹出验证码框 // 401 表示 code 过期/失效 openid 获取不到等
  170. uni.hideLoading() //异步操作结束,停止 loadin
  171. // uni.navigateTo({ url: '../boundPhone/boundPhone' })
  172. commit('SHOWPHONE')
  173. // console.log('store里', state.showphoneBtn)
  174. // uni.showModal({
  175. // title: '提示',
  176. // showCancel: false,
  177. // content: '请使用代理商系统的账号密码登录',
  178. // success: function (res) {
  179. // if (res.confirm) {
  180. // uni.navigateTo({ url: '../boundPhone/boundPhone' })
  181. // resolve()
  182. // }
  183. // }
  184. // });
  185. } else if (res.code === 401) {
  186. uni.hideLoading() // 异步操作结束,停止 loading
  187. reject(500)
  188. } else { //其他边界情况,提示用户下拉刷新
  189. uni.hideLoading() // 异步操作结束,停止 loading
  190. reject(500)
  191. }
  192. })
  193. })
  194. },
  195. // 位置信息授权
  196. getLocation({
  197. commit,
  198. state,
  199. dispatch
  200. }) {
  201. return new Promise(async (resolve, reject) => {
  202. const location = await uni.getLocation({
  203. type: 'gcj02'
  204. }) // 获取地理位置信息
  205. // 位置信息授权失败,重新授权
  206. const checkLocation = async () => {
  207. if (!state.gotLocation) {
  208. const [, {
  209. cancel,
  210. confirm
  211. }] = await uni.showModal({
  212. title: '提示',
  213. content: '获取您的位置信息失败,会导致您无法上传图片',
  214. // showCancel: false,
  215. confirmText: '重新获取'
  216. })
  217. if (confirm) {
  218. const settings = await new Promise(resolve => wx.openSetting({
  219. success(res) {
  220. resolve(res.authSetting)
  221. }
  222. }))
  223. if (settings['scope.userLocation']) {
  224. commit('GETLOCALTION')
  225. dispatch('newLogin')
  226. } else {
  227. checkLocation()
  228. }
  229. } else {
  230. commit('GETLOCALTION')
  231. // checkLocation()
  232. }
  233. }
  234. }
  235. if (location.length == 2) {
  236. uni.setStorageSync('latitude', location[1].latitude)
  237. uni.setStorageSync('longitude', location[1].longitude)
  238. commit('GETLOCALTION')
  239. } else {
  240. checkLocation()
  241. }
  242. })
  243. },
  244. // 用户信息
  245. getUserInfo({
  246. commit,
  247. state,
  248. dispatch
  249. }) {
  250. return new Promise(async (resolve, reject) => {
  251. const [getUserWeixinInfoErr, userWeixinInfo] = await uni.getUserProfile({
  252. desc: 'Wexin'
  253. }) // 获取用户信息
  254. if (getUserWeixinInfoErr) { //获取用户微信信息失败后,重新获取授权
  255. uni.hideLoading() //取消loading
  256. uni.showModal({
  257. title: '提示',
  258. showCancel: false,
  259. content: '不授权将无法获取到您的用户信息,请开启授权',
  260. success: function(res) {
  261. if (res.confirm) {
  262. commit('SHOWGETUSERINFOBUTTON')
  263. }
  264. }
  265. })
  266. } else { // 用户信息授权成功
  267. let that = this
  268. commit('GETUSERWEIXININFO', userWeixinInfo.userInfo) //将获取到的用户微信信息存在 store 里
  269. uni.setStorageSync('nickName', userWeixinInfo.userInfo.nickName)
  270. uni.setStorageSync('avatar', userWeixinInfo.userInfo.avatarUrl)
  271. uni.setStorageSync('sex', userWeixinInfo.userInfo.gender)
  272. that.$ajax.get(
  273. `${SaveUserInfo}?nickname=${ userWeixinInfo.userInfo.nickName}&avatar=${ userWeixinInfo.userInfo.avatarUrl}`
  274. ).then(([, {
  275. data: res
  276. }]) => {
  277. if (res.code == 200) {
  278. dispatch('newLogin')
  279. }
  280. })
  281. }
  282. })
  283. }
  284. }
  285. })
  286. Vuex.Store.prototype.$ajax = ajax(store)
  287. export default store