index.jsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. import { Component } from 'react'
  2. import Taro from '@tarojs/taro'
  3. import { connect } from 'react-redux'
  4. import { View, Input, Image } from '@tarojs/components'
  5. import { login, getWorkingHours, setOpenId } from '../../utils/servers/servers';
  6. import { add, minus, asyncAdd } from '../../actions/counter'
  7. import { AtButton, AtIcon, AtModal } from 'taro-ui'
  8. import NavBar from '../../components/common/NavBar';
  9. import BG from '../../image/bg.jpg';
  10. import 'taro-ui/dist/style/components/button.scss';
  11. import 'taro-ui/dist/style/components/loading.scss';
  12. import 'taro-ui/dist/style/components/icon.scss';
  13. import 'taro-ui/dist/style/components/modal.scss';
  14. import selectIcon from '../../image/select.png';
  15. import { getCurrentPageUrl } from '../../utils/servers/utils'
  16. import './index.less'
  17. @connect(({ counter }) => ({
  18. counter
  19. }), (dispatch) => ({
  20. add() {
  21. dispatch(add())
  22. },
  23. dec() {
  24. dispatch(minus())
  25. },
  26. asyncAdd() {
  27. dispatch(asyncAdd())
  28. }
  29. }))
  30. class Login extends Component {
  31. constructor(props) {
  32. super(props);
  33. this.state = {
  34. username: '',
  35. password: '',
  36. isOpened: false,
  37. openedType: 0,
  38. usernameList: Taro.getStorageSync('usernameList') || [],
  39. rememberPassword: true,
  40. }
  41. this.login = this.login.bind(this);
  42. this.authorization = this.authorization.bind(this);
  43. this.setUsernameList = this.setUsernameList.bind(this);
  44. this.getUsernameList = this.getUsernameList.bind(this);
  45. this.getWorkingHours = this.getWorkingHours.bind(this);
  46. }
  47. componentDidShow() {
  48. Taro.showShareMenu({
  49. withShareTicket: true
  50. })
  51. }
  52. componentDidMount() {
  53. }
  54. authorization() {
  55. this.setState({
  56. loading: true,
  57. })
  58. let _this = this;
  59. Taro.getSetting({
  60. withSubscriptions: true,
  61. success: (res) => {
  62. //console.log(res.subscriptionsSetting,'res.subscriptionsSetting')
  63. if (res.subscriptionsSetting.mainSwitch) {
  64. if (
  65. res.subscriptionsSetting['j7WH3EwQnxGxwuV2HwmJhryxySPE8vOiV5cVOpp-42I'] === "reject" ||
  66. res.subscriptionsSetting['pWia-KJPFwDM8ReDu_BOa9FJn31VFc81bp3yxfBmIRI'] === "reject" ||
  67. res.subscriptionsSetting['UGlNqF1VQsxYh6iVKrDuw_EkhrDNU6TXv4snGw4xze8'] === "reject") {
  68. this.setState({
  69. isOpened: true,
  70. loading: false,
  71. openedType: 1
  72. })
  73. return;
  74. }
  75. Taro.requestSubscribeMessage({ //获取下发权限
  76. tmplIds: [
  77. 'pWia-KJPFwDM8ReDu_BOa9FJn31VFc81bp3yxfBmIRI',
  78. 'j7WH3EwQnxGxwuV2HwmJhryxySPE8vOiV5cVOpp-42I',
  79. 'UGlNqF1VQsxYh6iVKrDuw_EkhrDNU6TXv4snGw4xze8'
  80. ], //此处写在后台获取的模板ID,可以写多个模板ID,看自己的需求
  81. success: function (s) {
  82. _this.setState({
  83. loading: false,
  84. })
  85. _this.login();
  86. },
  87. fail: function (err) {
  88. _this.setState({
  89. loading: false,
  90. })
  91. //console.log(err)
  92. }
  93. })
  94. } else {
  95. this.setState({
  96. isOpened: true,
  97. loading: false,
  98. })
  99. }
  100. },
  101. fail: () => {
  102. this.setState({
  103. loading: false,
  104. })
  105. }
  106. })
  107. }
  108. login() {
  109. if (!this.state.username) {
  110. Taro.showToast({
  111. title: '请输入用户名',
  112. icon: 'none',
  113. duration: 2000
  114. })
  115. return;
  116. }
  117. if (!this.state.password) {
  118. Taro.showToast({
  119. title: '请输入密码',
  120. icon: 'none',
  121. duration: 2000
  122. })
  123. return;
  124. }
  125. this.setState({
  126. loading: true,
  127. })
  128. login({
  129. username: this.state.username,
  130. password: this.state.password
  131. }).then((msg) => {
  132. this.setState({
  133. loading: false
  134. });
  135. if (!msg.data.openId) {
  136. Taro.login({
  137. success: (res) => {
  138. setOpenId({
  139. code: res.code,
  140. }).then(v => {
  141. if (v.error.length === 0) {
  142. this.setUsernameList(this.state.username, this.state.password);
  143. Taro.eventCenter.trigger('getStorageSync');
  144. this.getWorkingHours(msg.data.departmentId);
  145. } else {
  146. Taro.showToast({
  147. title: v.error[0].message,
  148. icon: 'none',
  149. })
  150. }
  151. })
  152. },
  153. fail: () => {
  154. Taro.showToast({ title: '系统错误,请稍后重试' })
  155. }
  156. });
  157. } else {
  158. this.setUsernameList(this.state.username, this.state.password);
  159. Taro.eventCenter.trigger('getStorageSync');
  160. this.getWorkingHours(msg.data.departmentId);
  161. }
  162. }).catch((err) => {
  163. //console.log(err)
  164. this.setState({
  165. loading: false
  166. })
  167. })
  168. }
  169. getWorkingHours(depId) {
  170. getWorkingHours({
  171. depId
  172. }).then((msg) => {
  173. if (msg.error.length === 0) {
  174. let obj = Taro.getStorageSync('userInfor');
  175. obj.workTimeInfor = msg.data;
  176. Taro.setStorageSync('userInfor', obj);
  177. Taro.switchTab({
  178. url: '/pages/punchClock/index',
  179. })
  180. } else {
  181. Taro.showToast({ title: msg.error[0].message, icon: 'none' })
  182. }
  183. }).catch((err) => {
  184. // console.log(err)
  185. })
  186. }
  187. setUsernameList(username, password) {
  188. if (!this.state.rememberPassword) {
  189. let usernameList = Taro.getStorageSync('usernameList') || [];
  190. let index = usernameList.findIndex(v => v.username === username);
  191. if (index !== -1) {
  192. usernameList.splice(index, 1)
  193. }
  194. Taro.setStorageSync('usernameList', usernameList);
  195. } else {
  196. let usernameList = Taro.getStorageSync('usernameList') || [];
  197. let index = usernameList.findIndex(v => v.username === username);
  198. if (index === -1) {
  199. usernameList.push({
  200. username,
  201. password
  202. })
  203. } else {
  204. usernameList[index] = {
  205. username,
  206. password
  207. }
  208. }
  209. Taro.setStorageSync('usernameList', usernameList);
  210. }
  211. }
  212. getUsernameList(value = this.state.username) {
  213. let usernameList = Taro.getStorageSync('usernameList') || [];
  214. let arr = [];
  215. for (let i of usernameList) {
  216. if (i.username.indexOf(value) !== -1) {
  217. arr.push(i)
  218. }
  219. }
  220. this.setState({
  221. usernameList: arr,
  222. isopenUsernameList: true
  223. })
  224. }
  225. render() {
  226. return (
  227. <View className='login' onClick={() => {
  228. this.setState({
  229. isopenUsernameList: false
  230. })
  231. }}>
  232. <NavBar
  233. extClass='NavBar'
  234. title='科德集团'
  235. background='#0000'
  236. color='#FFF'
  237. onHome={() => { }}
  238. />
  239. <View className='welcome'>
  240. <Image src={BG} className='bg' mode='heightFix' />
  241. <View className='welcomeTitle'>科德科技服务系统</View>
  242. <View className='welcomeTitle'>欢迎您!</View>
  243. </View>
  244. <privacy />
  245. <View className='loginContent'>
  246. <View className='loginItem'>
  247. <AtIcon value='user' size='20' color='#999999' />
  248. <View className='usernameContent'>
  249. <Input
  250. style={{ marginLeft: '15px', width: '100%' }}
  251. type='text'
  252. placeholder='请输入您的系统账户'
  253. value={this.state.username}
  254. onInput={(v) => {
  255. this.setState({
  256. username: v.detail.value
  257. })
  258. this.getUsernameList(v.detail.value);
  259. }}
  260. onFocus={() => {
  261. this.getUsernameList();
  262. }}
  263. onBlur={() => {
  264. // this.setState({
  265. // isopenUsernameList:false
  266. // })
  267. }}
  268. />
  269. </View>
  270. </View>
  271. <View className='usernameListContent'>
  272. {this.state.isopenUsernameList && this.state.usernameList.length > 0 ? <View className='usernameList'>
  273. {
  274. this.state.usernameList.map((v, k) => (
  275. <View key={k} className='usernameItem' onClick={() => {
  276. this.setState({
  277. username: v.username,
  278. password: v.password,
  279. isopenUsernameList: false
  280. })
  281. }}>
  282. <AtIcon value='user' size='20' color='#c6c4c4' />
  283. <View className='value'>
  284. <View className='name'>{v.username}</View>
  285. <View className='password'>*******</View>
  286. </View>
  287. </View>
  288. ))
  289. }
  290. </View> : null}
  291. </View>
  292. <View className='loginItem'>
  293. <AtIcon value='lock' size='20' color='#999999' />
  294. <Input
  295. style={{ marginLeft: '15px', width: '100%' }}
  296. type='password'
  297. placeholder='请输入密码'
  298. value={this.state.password}
  299. onInput={(v) => {
  300. this.setState({
  301. password: v.detail.value
  302. })
  303. }}
  304. />
  305. </View>
  306. <View className='rememberPassword'>
  307. <View className='selectContent' onClick={() => {
  308. this.setState({
  309. rememberPassword: !this.state.rememberPassword
  310. })
  311. }}>
  312. <Image src={selectIcon} className='selectIcon' style={{ visibility: this.state.rememberPassword ? "visible" : "hidden" }} />
  313. </View>
  314. 记住密码
  315. </View>
  316. <AtButton type='primary' circle loading={this.state.loading} onClick={this.authorization}>登录</AtButton>
  317. <View className='tips'>
  318. 暂无账号?请联系管理员进行添加
  319. </View>
  320. <View className='company'>
  321. 湖南科德信息咨询集团有限公司
  322. </View>
  323. </View>
  324. <AtModal
  325. isOpened={this.state.isOpened}
  326. title='提醒'
  327. cancelText='取消'
  328. confirmText='确认'
  329. onClose={() => {
  330. this.setState({
  331. isOpened: false,
  332. openedType: 0,
  333. })
  334. }}
  335. onCancel={() => {
  336. this.setState({
  337. isOpened: false,
  338. openedType: 0
  339. })
  340. }}
  341. onConfirm={() => {
  342. this.setState({
  343. isOpened: false,
  344. openedType: 0
  345. }, () => {
  346. Taro.openSetting({
  347. success: (value) => {
  348. }
  349. })
  350. })
  351. }}
  352. content={this.state.openedType === 1 ? '您存在拒绝接收的消息类型,请点击确认按钮,前往设置页->通知管理,点击不接受的消息通知类型,调整为允许' : '您关闭了接收通知权限,将不能收到订阅通知,请点击确认按钮,前往设置页->通知管理->接收通知,打开接收通知权限'}
  353. />
  354. </View>
  355. )
  356. }
  357. }
  358. export default Login