store.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. const launchOptions = wx.getLaunchOptionsSync()
  112. //从争霸赛跳转过来传参HeaderID免重新登录
  113. var userId = '';
  114. if (launchOptions.referrerInfo.extraData) {
  115. const extraData = launchOptions.referrerInfo.extraData // 获取传递的参数
  116. userId = extraData.HeaderID
  117. console.log(extraData.HeaderID, '接收到的参数')
  118. }
  119. // var userId = 18925
  120. this.$ajax.get(
  121. `${api_onLaunch}?id=${state.id}&sex=${sex}&latitude=${latitude}&longitude=${longitude}&code=${CODE}&avatar=${avatar}&openid=${state.openid ? state.openid : '123'}&nickname=${nickName}&HeaderID=${userId}`
  122. ).then(([, {
  123. data: res
  124. }]) => {
  125. // res.code = 300
  126. if (res.code === 200) { //当前微信用户已经绑定手机号了
  127. // user_status为1,销售员不能进入系统
  128. if (res.data.user_status == 1) {
  129. uni.reLaunch({
  130. url: "../index/jurisdiction?phone=" + res.data.phone
  131. })
  132. return false
  133. }
  134. state.N401 = 0
  135. uni.setStorageSync('userServerInfo', res.data)
  136. commit('GETUSERSERVERINFO', res.data)
  137. // if (res.data.name != '微信用户') {
  138. // uni.setStorageSync('nickName', res.data.name)
  139. // uni.setStorageSync('avatar', res.data.avatar)
  140. // }
  141. uni.hideLoading() //异步操作结束,停止 loading
  142. this.$ajax.get('/user/account_status').then(([, {
  143. data: res
  144. }]) => {
  145. if (res.code === 200) {
  146. if (res.data.status === 1) {
  147. uni.reLaunch({
  148. url: "../loginOutTip/loginOutTip"
  149. })
  150. return false
  151. }
  152. uni.getSetting({
  153. success(res) {
  154. // if (!uni.getStorageSync(
  155. // 'nickName') || !uni
  156. // .getStorageSync('avatar')) {
  157. // commit('SHOWGETUSERINFOBUTTON')
  158. // } else {
  159. dispatch('getLocation')
  160. // }
  161. },
  162. fail() {}
  163. })
  164. }
  165. resolve()
  166. }).catch(() => {
  167. resolve()
  168. })
  169. // } else if (res.code === 20013) { //当前微用户没有绑定手机号,弹出验证码框 // 401 表示 code 过期/失效 openid 获取不到等
  170. // uni.hideLoading() //异步操作结束,停止 loadin
  171. // commit('SHOWVERIFY')
  172. // uni.navigateTo({ url: '../boundPhone/boundPhone' })
  173. // resolve()
  174. // }else{ //其他边界情况,提示用户下拉刷新
  175. // uni.hideLoading() // 异步操作结束,停止 loading
  176. // uni.showModal({
  177. // content: res.msg,
  178. // showCancel: false
  179. // })
  180. // reject(500)
  181. // }
  182. } else if (res.code ===
  183. 300) { //当前微信用户没有绑定手机号,弹出验证码框 // 401 表示 code 过期/失效 openid 获取不到等
  184. uni.hideLoading() //异步操作结束,停止 loadin
  185. // uni.navigateTo({ url: '../boundPhone/boundPhone' })
  186. commit('SHOWPHONE')
  187. // console.log('store里', state.showphoneBtn)
  188. // uni.showModal({
  189. // title: '提示',
  190. // showCancel: false,
  191. // content: '请使用代理商系统的账号密码登录',
  192. // success: function (res) {
  193. // if (res.confirm) {
  194. // uni.navigateTo({ url: '../boundPhone/boundPhone' })
  195. // resolve()
  196. // }
  197. // }
  198. // });
  199. } else if (res.code === 401) {
  200. uni.hideLoading() // 异步操作结束,停止 loading
  201. reject(500)
  202. } else { //其他边界情况,提示用户下拉刷新
  203. uni.hideLoading() // 异步操作结束,停止 loading
  204. reject(500)
  205. }
  206. })
  207. })
  208. },
  209. // 位置信息授权
  210. getLocation({
  211. commit,
  212. state,
  213. dispatch
  214. }) {
  215. return new Promise(async (resolve, reject) => {
  216. const location = await uni.getLocation({
  217. type: 'gcj02'
  218. }) // 获取地理位置信息
  219. // 位置信息授权失败,重新授权
  220. const checkLocation = async () => {
  221. if (!state.gotLocation) {
  222. const [, {
  223. cancel,
  224. confirm
  225. }] = await uni.showModal({
  226. title: '提示',
  227. content: '获取您的位置信息失败,会导致您无法上传图片',
  228. // showCancel: false,
  229. confirmText: '重新获取'
  230. })
  231. if (confirm) {
  232. const settings = await new Promise(resolve => wx.openSetting({
  233. success(res) {
  234. resolve(res.authSetting)
  235. }
  236. }))
  237. if (settings['scope.userLocation']) {
  238. commit('GETLOCALTION')
  239. dispatch('newLogin')
  240. } else {
  241. checkLocation()
  242. }
  243. } else {
  244. commit('GETLOCALTION')
  245. // checkLocation()
  246. }
  247. }
  248. }
  249. if (location.length == 2) {
  250. uni.setStorageSync('latitude', location[1].latitude)
  251. uni.setStorageSync('longitude', location[1].longitude)
  252. commit('GETLOCALTION')
  253. } else {
  254. checkLocation()
  255. }
  256. })
  257. },
  258. // 用户信息
  259. // getUserInfo({
  260. // commit,
  261. // state,
  262. // dispatch
  263. // }) {
  264. // return new Promise(async (resolve, reject) => {
  265. // uni.getUserProfile({
  266. // desc: "获取你的昵称、头像、地区及性别",
  267. // success: (res) => {
  268. // console.log("所有",res)
  269. // },
  270. // fail:(err)=> {
  271. // uni.getUserProfile({})
  272. // }
  273. // })
  274. // return
  275. // const [getUserWeixinInfoErr, userWeixinInfo] = await uni.getUserProfile({
  276. // desc: 'Wexin'
  277. // }) // 获取用户信息
  278. // if (getUserWeixinInfoErr) { //获取用户微信信息失败后,重新获取授权
  279. // uni.hideLoading() //取消loading
  280. // uni.showModal({
  281. // title: '提示',
  282. // showCancel: false,
  283. // content: '不授权将无法获取到您的用户信息,请开启授权',
  284. // success: function(res) {
  285. // if (res.confirm) {
  286. // commit('SHOWGETUSERINFOBUTTON')
  287. // }
  288. // }
  289. // })
  290. // } else { // 用户信息授权成功
  291. // let that = this
  292. // commit('GETUSERWEIXININFO', userWeixinInfo.userInfo) //将获取到的用户微信信息存在 store 里
  293. // uni.setStorageSync('nickName', userWeixinInfo.userInfo.nickName)
  294. // uni.setStorageSync('avatar', userWeixinInfo.userInfo.avatarUrl)
  295. // uni.setStorageSync('sex', userWeixinInfo.userInfo.gender)
  296. // that.$ajax.get(
  297. // `${SaveUserInfo}?nickname=${ userWeixinInfo.userInfo.nickName}&avatar=${ userWeixinInfo.userInfo.avatarUrl}`
  298. // ).then(([, {
  299. // data: res
  300. // }]) => {
  301. // if (res.code == 200) {
  302. // dispatch('newLogin')
  303. // }
  304. // })
  305. // }
  306. // })
  307. // }
  308. //用户信息
  309. // getUserInfo({
  310. // commit,
  311. // state,
  312. // dispatch
  313. // }) {
  314. // return new Promise(async (resolve, reject) => {
  315. // console.log(uni.getStorageSync('nickName'), uni.getStorageSync('avatar'))
  316. // if (!uni.getStorageSync('nickName') || !uni.getStorageSync('avatar')) {
  317. // uni.showModal({
  318. // content: '为了更好的体验该程序,我们将获取您的昵称和头像',
  319. // success: res => {
  320. // if (res.confirm) {
  321. // uni.navigateTo({
  322. // url: '../information/information'
  323. // })
  324. // }
  325. // }
  326. // })
  327. // }
  328. // })
  329. // }
  330. }
  331. })
  332. Vuex.Store.prototype.$ajax = ajax(store)
  333. export default store