index.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // pages/login/index.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. /** 激活的标签页 */
  23. tabActive: 0,
  24. /**用户信息 */
  25. userInfo: {},
  26. regmobile_label: '手机号',
  27. regpassword_label: '密码',
  28. regpassword2_label: '确认密码',
  29. username_label: '用户名',
  30. password_label: '密码',
  31. /** app已存储用户信息 */
  32. hasUserInfo: false,
  33. /** 获取用户信息接口可用? */
  34. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  35. buttonClicked:false
  36. },
  37. /**
  38. * 生命周期函数--监听页面加载
  39. */
  40. onLoad: function(options) {
  41. //注册之前删除自动登录记录,注册成功之后需要进行自动登录,因为注册时绑定了微信unionId,可以自动登录成功.如果不删除自动登录记录,自动登录不会进行
  42. wx.removeStorageSync('login_result')
  43. wx.getSetting({
  44. complete: (res) => {
  45. console.log('getsetting',res)
  46. if (!res.authSetting['scope.userInfo']) {
  47. wx.removeStorageSync('welcome')
  48. wx.removeStorageSync('hasAuthorize')
  49. wx.redirectTo({
  50. url: '/pages/welcome/index',
  51. })
  52. } else {
  53. wx.getUserInfo({
  54. complete: (res) => {
  55. console.log(res)
  56. this.setData({
  57. 'userInfo': res.userInfo
  58. })
  59. },
  60. })
  61. }
  62. },
  63. })
  64. },
  65. /**
  66. * 生命周期函数--监听页面初次渲染完成
  67. */
  68. onReady: function() {
  69. },
  70. /**
  71. * 生命周期函数--监听页面显示
  72. */
  73. onShow: function() {
  74. },
  75. /**
  76. * 生命周期函数--监听页面隐藏
  77. */
  78. onHide: function() {
  79. },
  80. /**
  81. * 生命周期函数--监听页面卸载
  82. */
  83. onUnload: function() {
  84. },
  85. /**
  86. * 页面相关事件处理函数--监听用户下拉动作
  87. */
  88. onPullDownRefresh: function() {
  89. },
  90. /**
  91. * 页面上拉触底事件的处理函数
  92. */
  93. onReachBottom: function() {
  94. },
  95. /**
  96. * 用户点击右上角分享
  97. */
  98. onShareAppMessage: function() {
  99. },
  100. onChange(event) {
  101. // event.detail 为当前输入的值
  102. let dataKey = event.currentTarget.dataset.name
  103. this.setData({
  104. [dataKey]: event.detail
  105. })
  106. },
  107. login() {
  108. let that = this
  109. wx.login({
  110. success: function(res) {
  111. let param = {
  112. username: null,
  113. password: null
  114. }
  115. let ps = {
  116. ...param,
  117. ...that.data
  118. }
  119. Object.keys(param).forEach(
  120. n => {
  121. if (!ps[n]) {
  122. that.setData({
  123. [n + '_error']: true,
  124. [n + '_error_message']: that.data[n + '_label'] + '必填'
  125. })
  126. }
  127. }
  128. )
  129. let flag = true
  130. Object.keys(that.data).forEach(n => {
  131. if (n.indexOf('_error') > -1 && that.data[n]) {
  132. flag = false
  133. }
  134. })
  135. if (!flag) {
  136. return false
  137. }
  138. let params = {
  139. username: that.data.username,
  140. password: that.data.password,
  141. uuid: app.globalData.uuid,
  142. code: res.code,
  143. mini_program_type: app.globalData.miniProgramType
  144. }
  145. API_Passport.bindUser(params).then(
  146. response => {
  147. if (response.result === 'bind_success') {
  148. Common.autoLogin().then(res => {
  149. $wuxToptips().success({
  150. hidden: false,
  151. text: '绑定成功!',
  152. duration: 2000,
  153. success: () => {
  154. wx.switchTab({
  155. url: '/pages/store/index',
  156. })
  157. }
  158. })
  159. })
  160. }
  161. }
  162. ).catch(error => {
  163. $wuxToptips().error({
  164. icon: 'cancel',
  165. hidden: false,
  166. text: error.message,
  167. duration: 2000
  168. })
  169. })
  170. }
  171. })
  172. },
  173. /** 输入框失去焦点 */
  174. onBlur(e) {
  175. const that = this
  176. let dataKey = e.currentTarget.dataset.name
  177. const {
  178. rules,
  179. label
  180. } = e.currentTarget.dataset
  181. const {
  182. value
  183. } = e.detail
  184. if (rules) {
  185. that.setData({
  186. [dataKey + '_error']: false,
  187. [dataKey + '_error_message']: ''
  188. })
  189. try {
  190. rules.split(',').forEach(n => {
  191. if (n === 'required' && !value) {
  192. that.setData({
  193. [dataKey + '_error']: true,
  194. [dataKey + '_error_message']: label + '必填'
  195. })
  196. throw new Error()
  197. }
  198. if (n !== 'required' && !Reg[n].test(value)) {
  199. that.setData({
  200. [dataKey + '_error']: true,
  201. [dataKey + '_error_message']: label + '格式不正确'
  202. })
  203. }
  204. })
  205. } catch (e) {
  206. }
  207. }
  208. },
  209. /** tab标签切换 */
  210. tabChange(event) {
  211. },
  212. /** 获取用户信息 */
  213. getUserInfo(e) {
  214. const {
  215. errMsg
  216. } = e.detail
  217. if (errMsg === 'getUserInfo:ok') {
  218. app.globalData.userInfo = e.detail.userInfo
  219. wx.setStorageSync('userInfo', e.detail.userInfo)
  220. this.setData({
  221. userInfo: e.detail.userInfo,
  222. hasUserInfo: true
  223. })
  224. }
  225. },
  226. /** 注册新用户 */
  227. register(e) {
  228. const that=this
  229. let param = {
  230. regmobile: null,
  231. regpassword: null,
  232. regpassword2: null
  233. }
  234. let ps = {
  235. ...param,
  236. ...this.data
  237. }
  238. Object.keys(param).forEach(
  239. n => {
  240. if (!ps[n]) {
  241. this.setData({
  242. [n + '_error']: true,
  243. [n + '_error_message']: this.data[n + '_label'] + '必填'
  244. })
  245. }
  246. }
  247. )
  248. let flag = true
  249. Object.keys(this.data).forEach(n => {
  250. if (n.indexOf('_error') > -1 && this.data[n]) {
  251. flag = false
  252. }
  253. })
  254. if (this.data.regpassword2 !== this.data.regpassword) {
  255. flag = false
  256. this.setData({
  257. regpassword2_error: true,
  258. regpassword2_error_message: '两次密码不一致'
  259. })
  260. }
  261. if (!flag) {
  262. return false
  263. }
  264. that.setData({
  265. buttonClicked:true
  266. })
  267. const para = {
  268. nick_name: this.data.userInfo.nickName,
  269. face: this.data.userInfo.avatarUrl,
  270. sex: this.data.userInfo.gender,
  271. mobile: this.data.regmobile,
  272. password: this.data.regpassword
  273. }
  274. API_Passport.registerBind(app.globalData.uuid, para).then(response => {
  275. if (response.result === 'bind_success') {
  276. let now = Math.round(new Date() / 1000)
  277. if (response.access_token) {
  278. //保存登录信息
  279. const {refresh_token_timeout,access_token_timeout}=response
  280. app.globalData.authorInfo={...response,'refresh_token_timeout':now+refresh_token_timeout-1000,'access_token_timeout':now+access_token_timeout-1000}
  281. //登录信息写入本地存储
  282. wx.setStorageSync('authorInfo', app.globalData.authorInfo)
  283. }
  284. $wuxToptips().success({
  285. hidden: false,
  286. text: '注册成功!',
  287. duration: 2000,
  288. success: () => {
  289. that.setData({
  290. buttonClicked:false
  291. })
  292. //注册成功自动登录,在该函数中统一实现跳转逻辑
  293. Common.toAutoLogin()
  294. }
  295. })
  296. }
  297. }).catch(error=>{
  298. $wuxToptips().error({
  299. hidden: false,
  300. text: error.message,
  301. duration: 2000,
  302. success:()=>{
  303. that.setData({
  304. buttonClicked:false
  305. })
  306. }
  307. })
  308. })
  309. }
  310. })