index.ts 4.7 KB

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