store.js 11 KB

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