|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- // pages/login/login.js
- import {getCityTree} from '../../api/feeddback.js'
- import {getCityNameCode, getTenantData, getUserData, enterMini} from '../../api/login.js'
- import {getOpenidData, getUserMessage} from '../../utils/getUserInfo.js'
- import {getLocatonPermission} from '../../utils/getUserLocation.js'
- Page({
-
- /**
- * 页面的初始数据
- */
- data: {
- // 区域选项列表
- provinceOptions: [],
- cityOptions: [],
- districtOptions: [],
- provinceCurrent: -1, // 当前选择省
- cityCurrent: -1,
- districtCurrent: -1,
- params: {}, // 筛选条件
- obj: {},
- path: '/pages/FirstPage/index',
- fromType: 'tabbar',
- tenantId: '',
- tenantCurrent: -1, // 当前选择的租户数据index
- tenantOptions: [], // 租户列表
- formRules: {
- tenantId: {
- validator: function (value) {
- return value;
- },
- warning: false
- }
- }
- },
-
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- if(options) {
- this.setData({path: options.path, fromType: options.fromType})
- }
- },
-
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- // 隐藏返回图标
- wx.hideHomeButton();
- let positionParams = wx.getStorageSync('positionParams')
- let options = this.data.provinceOptions
- if(options.length === 0) {
- let provinceOptions = wx.getStorageSync('provinceOptions')
- if(provinceOptions.length) {
- this.setData({provinceOptions})
- if(Object.keys(positionParams).length) {
- let params = this.data.params
- params.provinceCode = positionParams.provinceCode
- params.cityCode = positionParams.cityCode
- params.districtCode = positionParams.districtCode
- this.setData({
- params,
- provinceCurrent: positionParams.provinceCurrent,
- cityCurrent: positionParams.cityCurrent,
- cityOptions: positionParams.cityOptions,
- districtCurrent: positionParams.districtCurrent,
- districtOptions: positionParams.districtOptions
- })
- // 获取租户信息
- this.getTenantByParams(params)
- } else {
- this.getUserLocation()
- }
- } else {
- this.getCityTreeList().then(res=> {
- if(Object.keys(positionParams).length) {
- let params = this.data.params
- params.provinceCode = positionParams.provinceCode
- params.cityCode = positionParams.cityCode
- params.districtCode = positionParams.districtCode
- this.setData({
- params,
- provinceCurrent: positionParams.provinceCurrent,
- cityCurrent: positionParams.cityCurrent,
- cityOptions: positionParams.cityOptions,
- districtCurrent: positionParams.districtCurrent,
- districtOptions: positionParams.districtOptions
- })
- wx.setStorage({
- key: 'positionParams',
- data: positionParams
- })
- } else {
- this.getUserLocation()
- }
- })
- }
- }
- const tenantObj = wx.getStorageSync('tenant')
- if(tenantObj) {
- const {tenantCurrent, tenantId} = tenantObj
- this.setData({tenantCurrent, tenantId})
- }
- const openid = wx.getStorageSync('openid')
- if(openid) {
- this.setData({openid})
- } else {
- getOpenidData().then(res=> {
- this.setData({openid: res.openid})
- })
- }
- },
- validate(name) {
- let formRules = this.data.formRules;
- let validator = formRules[name].validator
- let result = validator ? !validator(this.data[name]) : false;
- formRules[name].warning = result
- this.setData({formRules})
- return result
- },
- validateForm() {
- return new Promise((resolve, reject) => {
- try {
- let formRules = this.data.formRules;
- let result = false;
- for (let key in formRules) {
- let temp = this.validate(key)
- if (temp) {
- result = temp
- }
- }
- resolve(!result)
- } catch (e) {
- reject(e)
- }
- })
- },
- /**
- * 根据省市区查询已有租户
- */
- getTenantByParams(params) {
- getTenantData(params).then(res=> {
- if(res.code === 0) {
- this.setData({
- tenantOptions: res.data
- })
- }
- })
- },
-
- /**
- * 选择租户
- */
- selectTenant(e) {
- const tenantCurrent = e.detail.value
- let tenantId = this.data.tenantOptions[tenantCurrent].id
- this.setData({
- tenantCurrent,
- tenantId
- })
- this.validate('tenantId')
- },
- /**
- * 根据name查找list
- */
- getListByName(obj) {
- let provinceOptions = this.data.provinceOptions
- let provinceCurrent, cityCurrent, districtCurrent, cityOptions, districtOptions
- let params = this.data.params
- params.provinceCode = obj.provinceCode
- params.cityCode = obj.cityCode
- params.districtCode = obj.districtCode
- if(provinceOptions.length) {
- provinceOptions.forEach((item,index)=> {
- if(item.citycode === obj.provinceCode) {
- provinceCurrent = index
- cityOptions = item?.itemList || []
- if(cityOptions.length) {
- cityOptions.forEach((i, j)=> {
- if(i.citycode === obj.cityCode) {
- cityCurrent = j
- districtOptions = i?.itemList || []
- if (districtOptions.length) {
- districtOptions.forEach((m, n) => {
- if(m.citycode === obj.districtCode) {
- districtCurrent = n
- }
- })
- }
- }
- })
- }
- }
- })
- }
- this.setData({provinceCurrent, cityCurrent, districtCurrent, cityOptions, districtOptions, params})
- },
-
- /* 获取省市区 */
- getCityTreeList() {
- return new Promise((resolve)=> {
- getCityTree().then(res=> {
- wx.setStorage({
- key: 'provinceOptions',
- data: res.data
- })
- this.setData({
- provinceOptions: res.data
- })
- resolve(res)
- })
- })
- },
- /* 筛选 */
- updateAreaChange(e) {
- // 清空租户信息
- this.setData({
- tenantId: '',
- tenantCurrent: -1,
- tenantOptions: [],
- })
- const type = e.target.dataset.type
- const current = e.detail.current
- let params = this.data.params
- let code = this.data[type+'Options'][current].citycode
- params[type+'Code'] = code
- let itemList = this.data[type+'Options'][current].itemList || []
- if(type === 'province') {
- this.setData({
- provinceCurrent: current,
- cityOptions: itemList,
- cityCurrent: -1,
- districtCurrent: -1
- })
- params.cityCode = ""
- params.districtCode = ""
- } else if(type === 'city') {
- this.setData({
- districtOptions: itemList,
- cityCurrent: current,
- districtCurrent: -1
- })
- params.districtCode = ""
- } else if(type === 'district') {
- this.setData({districtCurrent: current})
- }
- this.setData({
- params: params
- })
- this.getTenantByParams(params)
- },
- /* 根据current获取当前选中的值 */
- getValueByCurrent(current,type) {
- let code = this.data[type+'Options'][current].value
- return code
- },
- // 获取用户位置信息
- getUserLocation(e) {
- let cityObj = wx.getStorageSync('cityObj')
- if(!Object.keys(cityObj).length) {
- getLocatonPermission().then(res=> {
- wx.setStorage({
- key: 'location',
- data: res
- })
- let latlng = {}
- latlng.lat = res.latitude
- latlng.lng = res.longitude
- getCityNameCode(latlng).then(res=> {
- let cityObj = res.data
- const params = {
- provinceCode: cityObj.provinceCode,
- cityCode: cityObj.cityCode,
- districtCode: cityObj.districtCode
- }
- this.setData({cityObj})
- this.getListByName(cityObj)
- this.getTenantByParams(params)
- })
- })
- }
- },
- /**
- * 进入首页
- */
- goHome() {
- this.validateForm().then(res => {
- if(res) {
- enterMini({openid: this.data.openid, tenantId: this.data.tenantId}).then(item=> {
- if(item.code === 0) {
- const tenantObj = {
- tenantCurrent: this.data.tenantCurrent,
- tenantId: this.data.tenantId
- }
- wx.setStorage({
- key: 'tenant',
- data: tenantObj
- })
- // 进入首页
- const path = this.data.path
- const fromType = this.data.fromType
- if(fromType === 'page') {
- wx.redirectTo({
- url: path,
- })
- } else {
- wx.switchTab({
- url: path,
- })
- }
- }
- })
- }
- })
- }
- })
|