index.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import { login, loginQuery } from '../../services/api/account/index'
  2. import { appConfig } from '../../config/index'
  3. import { getDivisions, loginid, setAllEnums, setAllErrors, taAccounts } from '../../services/utils'
  4. import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
  5. import { getAllEnums, queryErrorInfos } from '../../services/api/common/index'
  6. import { hideLoading, showLoading } from '../../utils/message/index'
  7. import { encryptBody } from '../../utils/websocket/crypto'
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. /// 底部安全区域
  14. safeBottom: getApp().globalData.safeBottom,
  15. /// 姓名
  16. username: '110000000001',
  17. /// 密码
  18. pwd: '123456',
  19. /// 版本号
  20. version: appConfig.version,
  21. /// 是否loding
  22. isLoding: false,
  23. },
  24. /**
  25. * 合规性校验
  26. */
  27. check(): boolean {
  28. /// 请输入用户名
  29. if (this.data.username.length == 0) {
  30. Toast('请输入用户名!')
  31. return false
  32. }
  33. /// 请输入密码并且大于6位数!
  34. if (this.data.pwd.length < 6) {
  35. Toast('请输入密码并且大于6位数!')
  36. return false
  37. }
  38. return true
  39. },
  40. /**
  41. * 用户登录请求方法
  42. */
  43. onLogin() {
  44. /// 合规性校验
  45. if (!this.check()) return
  46. /// loding.....
  47. this.setData({ isLoding: true })
  48. /// loding......
  49. showLoading(() => {
  50. /// 进行账户登录
  51. login({
  52. data: {
  53. userName: this.data.username,
  54. password: encryptBody(this.data.pwd, true),
  55. clientType: 5
  56. },
  57. enableAuthorization: false,
  58. success: (res) => {
  59. hideLoading(() => {
  60. /// 登录失败
  61. if (res.code != 0) {
  62. /// isLoding
  63. this.setData({ isLoding: false })
  64. return
  65. }
  66. /// 存储token以及loginId、userId
  67. wx.setStorageSync('Authorization', res.data.token)
  68. wx.setStorageSync('LoginID', res.data.loginId)
  69. wx.setStorageSync('UserId', res.data.userId)
  70. /// 获取地区区域信息
  71. getDivisions()
  72. /// 获取资金账户信息
  73. taAccounts()
  74. /// 登录查询
  75. this.loginQuery()
  76. })
  77. },
  78. fail: (emsg) => {
  79. /// hideLoading
  80. hideLoading(()=> {
  81. this.setData({ isLoding: false })
  82. }, emsg, 'error')
  83. }
  84. })
  85. }, '登录中.....')
  86. },
  87. /// 登录查询
  88. loginQuery() {
  89. /// showLoading
  90. showLoading(()=>{
  91. /// 发送查询请求
  92. loginQuery({
  93. data: { loginID: loginid() },
  94. success: (res) => {
  95. /// 数据存储
  96. wx.setStorageSync('LoginQuery', res.data)
  97. /// 存储市场ID
  98. wx.setStorageSync('MarketID', res.data.markets[0].marketid)
  99. /// 获取所有的枚举值
  100. this.getAllEnums()
  101. /// 获取所有的错误码
  102. this.getAllErrors()
  103. /// 登录失败
  104. if (res.code != 200) {
  105. hideLoading(()=>{}, '数据查询失败,原因:'+res.msg, 'error')
  106. return
  107. }
  108. /// showToast
  109. hideLoading(()=>{
  110. /// 跳转到首页
  111. wx.reLaunch({ url: '/pages/home/index' })
  112. }, '登录成功', 'success')
  113. },
  114. fail: (emsg) => {
  115. /// showToast
  116. hideLoading(()=>{}, emsg, 'error')
  117. },
  118. complete: () => {
  119. /// isLoding
  120. this.setData({ isLoding: false })
  121. }
  122. })
  123. })
  124. },
  125. /// 获取所有的错误码
  126. getAllErrors() {
  127. /// 获取错误码
  128. queryErrorInfos({
  129. success: (res) => {
  130. setAllErrors(res.data)
  131. }
  132. })
  133. },
  134. /// 获取所有的枚举值
  135. getAllEnums() {
  136. /// 发送请求
  137. getAllEnums({
  138. success: (res) => {
  139. setAllEnums(res.data)
  140. }
  141. })
  142. },
  143. /**
  144. * 生命周期函数--监听页面加载
  145. */
  146. onLoad() {
  147. },
  148. /**
  149. * 生命周期函数--监听页面初次渲染完成
  150. */
  151. onReady() {
  152. },
  153. /**
  154. * 生命周期函数--监听页面显示
  155. */
  156. onShow() {
  157. },
  158. /**
  159. * 生命周期函数--监听页面隐藏
  160. */
  161. onHide() {
  162. },
  163. /**
  164. * 生命周期函数--监听页面卸载
  165. */
  166. onUnload() {
  167. },
  168. /**
  169. * 页面相关事件处理函数--监听用户下拉动作
  170. */
  171. onPullDownRefresh() {
  172. },
  173. /**
  174. * 页面上拉触底事件的处理函数
  175. */
  176. onReachBottom() {
  177. },
  178. /**
  179. * 用户点击右上角分享
  180. */
  181. onShareAppMessage() {
  182. }
  183. })