index.jsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import { Component } from 'react'
  2. import Taro from '@tarojs/taro'
  3. import { connect } from 'react-redux'
  4. import {View, Button, Input} from '@tarojs/components'
  5. import {login} from '../../utils/servers/servers';
  6. import {setOpenId} from '../../utils/servers/servers';
  7. import { add, minus, asyncAdd } from '../../actions/counter'
  8. import './index.less'
  9. @connect(({ counter }) => ({
  10. counter
  11. }), (dispatch) => ({
  12. add () {
  13. dispatch(add())
  14. },
  15. dec () {
  16. dispatch(minus())
  17. },
  18. asyncAdd () {
  19. dispatch(asyncAdd())
  20. }
  21. }))
  22. class Login extends Component {
  23. constructor(props) {
  24. super(props);
  25. this.state={
  26. username:'',
  27. password:''
  28. }
  29. this.login = this.login.bind(this);
  30. }
  31. componentDidShow () {
  32. }
  33. componentDidMount() {
  34. }
  35. login(){
  36. // Taro.getSetting({
  37. // withSubscriptions: true,
  38. // success:(res)=>{
  39. // console.log(res.subscriptionsSetting,'res.subscriptionsSetting')
  40. // if(res.subscriptionsSetting.mainSwitch){
  41. // // res.subscriptionsSetting = {
  42. // // mainSwitch: true, // 订阅消息总开关
  43. // // itemSettings: { // 每一项开关
  44. // // SYS_MSG_TYPE_RANK: 'accept',
  45. // // 'pWia-KJPFwDM8ReDu_BOa2_42G7W2hXB16sUDbSxpcM': 'accept',
  46. // // 'j7WH3EwQnxGxwuV2HwmJhryxySPE8vOiV5cVOpp-42I': 'accept',
  47. // // 'vo28uzT2sLS-9ioroNyZbMSvu0mMvf6le2pDZVN891U': 'accept',
  48. // // }
  49. // // }
  50. // Taro.requestSubscribeMessage({ //获取下发权限
  51. // tmplIds: [
  52. // 'pWia-KJPFwDM8ReDu_BOa2_42G7W2hXB16sUDbSxpcM',
  53. // 'j7WH3EwQnxGxwuV2HwmJhryxySPE8vOiV5cVOpp-42I',
  54. // 'vo28uzT2sLS-9ioroNyZbMSvu0mMvf6le2pDZVN891U'
  55. // ], //此处写在后台获取的模板ID,可以写多个模板ID,看自己的需求
  56. // success: function (s) {
  57. // console.log(s)
  58. // },
  59. // fail: function (err) {
  60. // console.log(err)
  61. // }
  62. // })
  63. // }else if(res.subscriptionsSetting.mainSwitch.itemSettings){
  64. // console.log(res)
  65. // }else{
  66. // Taro.requestSubscribeMessage({
  67. // tmplIds: ['SLb0P5wDArFZU05iMrrnNEsXZ7H2wArqdY8__4M1NUUM'],
  68. // success : (res) => {
  69. //
  70. // }
  71. // })
  72. // }
  73. // }
  74. // })
  75. if(!this.state.username){
  76. Taro.showToast({
  77. title: '请输入用户名',
  78. icon: 'none',
  79. duration: 2000
  80. })
  81. return;
  82. }
  83. if(!this.state.password){
  84. Taro.showToast({
  85. title: '请输入密码',
  86. icon: 'none',
  87. duration: 2000
  88. })
  89. return;
  90. }
  91. this.setState({
  92. loading:true,
  93. })
  94. login({
  95. username:this.state.username,
  96. password:this.state.password
  97. }).then((msg)=>{
  98. this.setState({
  99. loading:false
  100. });
  101. if(!msg.data.openId){
  102. Taro.login({
  103. success: (res)=>{
  104. setOpenId({
  105. code:res.code,
  106. }).then(v=>{
  107. if(v.error.length === 0){
  108. Taro.switchTab({
  109. url: '/pages/applyDepart/index',
  110. })
  111. }else{
  112. Taro.showToast({title:v.error[0].message})
  113. }
  114. })
  115. },
  116. fail:()=>{
  117. Taro.showToast({title:'系统错误,请稍后重试'})
  118. }
  119. });
  120. }else{
  121. Taro.switchTab({
  122. url: '/pages/applyDepart/index',
  123. })
  124. }
  125. }).catch((err)=>{
  126. console.log(err)
  127. this.setState({
  128. loading:false
  129. })
  130. })
  131. }
  132. render () {
  133. return (
  134. <View>
  135. <View>
  136. <View>用户名</View>
  137. <Input
  138. type='text'
  139. placeholder='请输入用户名'
  140. value={this.state.username}
  141. onInput={(v)=>{
  142. this.setState({
  143. username: v.detail.value
  144. })
  145. }}
  146. />
  147. </View>
  148. <View>
  149. <View>密码</View>
  150. <Input
  151. type='password'
  152. password
  153. placeholder='请输入密码'
  154. value={this.state.password}
  155. onInput={(v)=>{
  156. this.setState({
  157. password: v.detail.value
  158. })
  159. }}
  160. />
  161. </View>
  162. <Button type='primary' loading={this.state.loading} onClick={this.login}>登录</Button>
  163. </View>
  164. )
  165. }
  166. }
  167. export default Login