login.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // pages/login/login.js
  2. import * as API_Passport from '../../api/passport.js'
  3. import Toast from '../../miniprogram_npm/vant-weapp/toast/toast.js'
  4. import * as RegExp from '../../utils/RegExp.js'
  5. import * as API_Shop from '../../api/shop.js'
  6. import * as Reg from '../../utils/RegExp.js'
  7. import * as Common from '../../utils/common.js'
  8. import {
  9. $wuxToptips,
  10. $wuxToast,
  11. $wuxLoading,
  12. $wuxDialog
  13. } from '../../wux/index.js'
  14. const app = getApp()
  15. Page({
  16. /**
  17. * 页面的初始数据
  18. */
  19. data: {
  20. username: '',
  21. password: '',
  22. mobile:'',
  23. smscode:'',
  24. username_label: '用户名',
  25. password_label: '密码',
  26. mobile_label: '手机号',
  27. smscode_label: '短信验证码',
  28. send_btn_disable:false,
  29. send_btn_init:true,
  30. send_btn_reset:false,
  31. active:0,
  32. buttonClicked:false
  33. },
  34. /**
  35. * 生命周期函数--监听页面加载
  36. */
  37. onLoad: function(options) {
  38. //如果没有自动登录记录,首先进行自动登录尝试,如果有登录信息,通过toAutologin实现跳转逻辑
  39. if(wx.getStorageSync('login_result')==='' || wx.getStorageSync('authorInfo')!==''){
  40. // Common.toAutoLogin()
  41. }
  42. },
  43. /**
  44. * 生命周期函数--监听页面初次渲染完成
  45. */
  46. onReady: function() {
  47. },
  48. /**
  49. * 生命周期函数--监听页面显示
  50. */
  51. onShow: function() {
  52. this.setData({
  53. send_btn_disable:false,
  54. sending:false,
  55. btn_tip:'发送验证码'
  56. })
  57. },
  58. /**
  59. * 生命周期函数--监听页面隐藏
  60. */
  61. onHide: function() {
  62. },
  63. /**
  64. * 生命周期函数--监听页面卸载
  65. */
  66. onUnload: function() {
  67. },
  68. /**
  69. * 页面相关事件处理函数--监听用户下拉动作
  70. */
  71. onPullDownRefresh: function() {
  72. },
  73. /**
  74. * 页面上拉触底事件的处理函数
  75. */
  76. onReachBottom: function() {
  77. },
  78. /**
  79. * 用户点击右上角分享
  80. */
  81. onShareAppMessage: function() {
  82. },
  83. onChange(event) {
  84. const that = this
  85. let dataKey = event.currentTarget.dataset.name
  86. // event.detail 为当前输入的值
  87. this.setData({
  88. [dataKey]: event.detail
  89. })
  90. },
  91. onSmscodeChange(e){
  92. const that=this
  93. const value = e.detail
  94. try {
  95. if (!value) {
  96. that.setData({
  97. ['smscode_error']: true,
  98. ['smscode_error_message']: '短信验证码必填'
  99. })
  100. throw new Error()
  101. }else{
  102. that.setData({
  103. ['smscode_error']: false,
  104. ['smscode_error_message']: '',
  105. ['smscode']:value
  106. })
  107. }
  108. } catch (e) {
  109. }
  110. },
  111. login() {
  112. let that = this
  113. wx.login({
  114. success: function(res) {
  115. let param = {
  116. username: null,
  117. password: null
  118. }
  119. let ps = {
  120. ...param,
  121. ...that.data
  122. }
  123. Object.keys(param).forEach(
  124. n => {
  125. if (!ps[n]) {
  126. that.setData({
  127. [n + '_error']: true,
  128. [n + '_error_message']: that.data[n + '_label'] + '必填'
  129. })
  130. }
  131. }
  132. )
  133. if(that.data['username_error']||that.data['password_error']){
  134. return false
  135. }
  136. that.setData({
  137. buttonClicked:true
  138. })
  139. let params = {
  140. username: that.data.username,
  141. password: that.data.password,
  142. uuid: wx.getStorageSync('uuid'),
  143. code: res.code,
  144. mini_program_type: app.globalData.miniProgramType
  145. }
  146. API_Passport.login(params).then(response=>{
  147. const now=Date.parse(new Date())/1000
  148. //恢复按钮可点击状态
  149. that.setData({
  150. buttonClicked:false
  151. })
  152. if (response.access_token) {
  153. //保存登录信息
  154. const {refresh_token_timeout,access_token_timeout}=response
  155. app.globalData.authorInfo={...response,'refresh_token_timeout':now+refresh_token_timeout-1000,'access_token_timeout':now+access_token_timeout-1000}
  156. //登录信息写入本地存储
  157. wx.setStorageSync('authorInfo', app.globalData.authorInfo)
  158. if(app.globalData.authorInfo.bool_o2o===0){
  159. wx.redirectTo({
  160. url: '/pages/noshop/index',
  161. })
  162. }else{
  163. wx.switchTab({
  164. url: '/pages/store/index',
  165. })
  166. }
  167. // wx.switchTab({
  168. // url: '/pages/store/index',
  169. // })
  170. }
  171. if(response.result==='existed'){
  172. wx.showModal({
  173. title: '提示',
  174. content: '当前微信已经绑定了一个用户,请点击确定按钮登录已绑定账号,如需更换账号,登录账户后退出登录完成解绑',
  175. showCancel:false,
  176. success (res) {
  177. if (res.confirm) {
  178. wx.removeStorageSync('login_result')
  179. Common.toAutoLogin()
  180. } else if (res.cancel) {
  181. console.log('用户点击取消')
  182. }
  183. }
  184. })
  185. }
  186. }).catch(err=>{
  187. $wuxToptips().error({
  188. icon: 'cancel',
  189. hidden: false,
  190. text: err.error?err.error:err.message,
  191. duration: 2000,
  192. success:()=>{
  193. that.setData({
  194. buttonClicked:false
  195. })
  196. }
  197. })
  198. return
  199. })
  200. }
  201. })
  202. },
  203. /** 输入框失去焦点 */
  204. onBlur(e) {
  205. const that = this
  206. let dataKey = e.currentTarget.dataset.name
  207. const {
  208. rules,
  209. label
  210. } = e.currentTarget.dataset
  211. const {
  212. value
  213. } = e.detail
  214. if (rules) {
  215. that.setData({
  216. [dataKey + '_error']: false,
  217. [dataKey + '_error_message']: ''
  218. })
  219. try {
  220. rules.split(',').forEach(n => {
  221. if (n === 'required' && !value) {
  222. that.setData({
  223. [dataKey + '_error']: true,
  224. [dataKey + '_error_message']: label + '必填'
  225. })
  226. throw new Error()
  227. }
  228. if (n !== 'required' && !Reg[n].test(value)) {
  229. that.setData({
  230. [dataKey + '_error']: true,
  231. [dataKey + '_error_message']: label + '格式不正确'
  232. })
  233. }
  234. })
  235. } catch (e) {
  236. }
  237. }
  238. },
  239. /** 发送短信验证码 **/
  240. sendsmscode(e){
  241. let that=this
  242. let param = {
  243. mobile: null
  244. }
  245. let ps = {
  246. ...param,
  247. ...that.data
  248. }
  249. Object.keys(param).forEach(
  250. n => {
  251. if (!ps[n]) {
  252. that.setData({
  253. [n + '_error']: true,
  254. [n + '_error_message']: that.data[n + '_label'] + '必填'
  255. })
  256. }
  257. }
  258. )
  259. let flag = true
  260. if (that.data['mobile_error']) {
  261. return false
  262. }
  263. const countDown = this.selectComponent('.control-count-down')
  264. countDown.start()
  265. API_Passport.sendLoginSms(that.data.mobile).then(res=>{
  266. that.setData({
  267. send_btn_disable:true,
  268. sending:true
  269. })
  270. }).catch(err=>{
  271. $wuxToptips().error({
  272. icon: 'cancel',
  273. hidden: false,
  274. text: err.message,
  275. duration: 2000
  276. })
  277. return
  278. })
  279. },
  280. /** 倒计时结束 */
  281. finished(e){
  282. this.setData({
  283. send_btn_disable:false,
  284. sending:false,
  285. btn_tip:'重新获取'
  286. })
  287. const countDown = this.selectComponent('.control-count-down')
  288. countDown.reset()
  289. },
  290. /** 短信验证码登录提交*/
  291. loginsmscode(e){
  292. let that = this
  293. wx.login({
  294. success: function(res) {
  295. if (!that.data['mobile']) {
  296. that.setData({
  297. ['mobile_error']: true,
  298. ['mobile_error_message']: '手机号必填'
  299. })
  300. return false
  301. }
  302. if(!Reg['mobile'].test(that.data['mobile'])){
  303. that.setData({
  304. ['mobile_error']: true,
  305. ['mobile_error_message']: '手机格式错误!'
  306. })
  307. return false
  308. }
  309. if(!that.data['smscode']){
  310. that.setData({
  311. ['smscode_error']: true,
  312. ['smscode_error_message']: '短信验证码必须填写!'
  313. })
  314. return false
  315. }
  316. that.setData({
  317. buttonClicked:true
  318. })
  319. let params = {
  320. mobile: that.data.mobile,
  321. smscode: that.data.smscode,
  322. uuid: app.globalData.uuid,
  323. code: res.code,
  324. mini_program_type: app.globalData.miniProgramType
  325. }
  326. API_Passport.smslogin(params).then(response=>{
  327. const now=Date.parse(new Date())/1000
  328. that.setData({
  329. buttonClicked:false
  330. })
  331. if (response.access_token) {
  332. //保存登录信息
  333. const {refresh_token_timeout,access_token_timeout}=response
  334. app.globalData.authorInfo={...response,'refresh_token_timeout':now+refresh_token_timeout-1000,'access_token_timeout':now+access_token_timeout-1000}
  335. console.log(app.globalData.authorInfo)
  336. //登录信息写入本地存储
  337. wx.setStorageSync('authorInfo', app.globalData.authorInfo)
  338. if(app.globalData.authorInfo.bool_o2o===0){
  339. wx.redirectTo({
  340. url: '/pages/noshop/index',
  341. })
  342. }else{
  343. wx.switchTab({
  344. url: '/pages/store/index',
  345. })
  346. }
  347. }
  348. }).catch(err=>{
  349. $wuxToptips().error({
  350. icon: 'cancel',
  351. hidden: false,
  352. text: err.message,
  353. duration: 2000,
  354. success:()=>{
  355. that.setData({
  356. buttonClicked:false
  357. })
  358. }
  359. })
  360. return
  361. })
  362. }
  363. })
  364. },
  365. tabChange(event) {
  366. // event.detail 的值为当前选中项的索引
  367. this.setData({ active: event.detail });
  368. },
  369. wechatLogin() {
  370. Common.toAutoLogin()
  371. }
  372. })