login.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <template>
  2. <view class="login-wrap">
  3. <view class="account-login-wrap" :style="{ visibility: isCheckLogin ? 'visible' : 'hidden', zIndex: isCheckLogin ? 1 : -1 }">
  4. <view style="margin-bottom: 60rpx;">
  5. <uni-data-select
  6. v-model="areaId"
  7. :localdata="addressList"
  8. @change="change"
  9. placeholder="请选择校区"
  10. ></uni-data-select>
  11. </view>
  12. <u-input type="text" class="input" v-model="phoneNum" placeholder="请输入登录手机号"/>
  13. <u-input type="text" class="input" password v-model="password" placeholder="请输入密码"/>
  14. </view>
  15. <button v-if="isCheckLogin" class="wechat-login" @click="pdwLogin" >
  16. 登录
  17. </button>
  18. <button v-if="!isCheckLogin && !isChecked" @click="phoneLogin()" class="wechat-login" >
  19. 授权手机号登录
  20. </button>
  21. <button v-if="!isCheckLogin && isChecked" open-type="getPhoneNumber" @getphonenumber="authorization" class="wechat-login" >
  22. 授权手机号登录
  23. </button>
  24. <view class="argument">
  25. <checkbox @click="isChecked = !isChecked" class="blue" :class="{ checked: isChecked }" :checked="isChecked ? true : false" style="transform:scale(0.8)"></checkbox>
  26. <view class="title" @click="isChecked = !isChecked">请阅读并同意</view>
  27. <text class="text-color" @click="navToWebView('/pages/webView/web_view?src=http://127.0.0.1:8081/fuwu.html')">《用户服务协议》</text>
  28. <text class="text">和</text>
  29. <text class="text-color" @click="navToWebView('/pages/webView/web_view?src=http://127.0.0.1:8081/yinsi.html')">《用户隐私条款》</text>
  30. </view>
  31. <view class="other-login-wrap">
  32. <view v-if="!isCheckLogin" @click="changeClick" class="other-login" >
  33. 账号密码登录
  34. </view>
  35. <!-- <view v-else @click="changeClick" class="other-login" >
  36. 授权手机号登录
  37. </view> -->
  38. </view>
  39. </view>
  40. </template>
  41. <script>
  42. import {
  43. mapState
  44. } from 'vuex';
  45. import {
  46. mapMutations
  47. } from 'vuex';
  48. export default {
  49. data() {
  50. return {
  51. weixinLoginUrl: '/storeUserLoginApi/loginByWeixin',
  52. loginurl: '/storeUserLoginApi/loginbyps', //手机号密码登录
  53. addressUrl: (getApp().globalData.platApiUrl || getApp().globalData.apiUrl) + '/api/areaManage/getAreaListApp',
  54. isChecked: false,
  55. isCheckLogin: true,
  56. params: {},
  57. phoneNum: '',
  58. password: '',
  59. areaId: '',
  60. addressList: [],
  61. code: '',
  62. list: []
  63. }
  64. },
  65. computed:{
  66. isPhone() { //手机号码校验正则
  67. return /^1\d{10}$/.test(this.phone);
  68. },
  69. },
  70. onShow() {
  71. if (wx.canIUse("getUpdateManager")) {
  72. const updateManager = wx.getUpdateManager();
  73. updateManager.onCheckForUpdate(function(res) {
  74. // console.log(res.hasUpdate);
  75. // 请求完新版本信息的回调
  76. if (res.hasUpdate) {
  77. updateManager.onUpdateReady(function() {
  78. wx.showModal({
  79. title: "更新提示",
  80. content: "新版本已经准备好,是否重启应用?",
  81. success(res) {
  82. if (res.confirm) {
  83. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  84. updateManager.applyUpdate();
  85. }
  86. }
  87. });
  88. });
  89. updateManager.onUpdateFailed(function() {
  90. // 新的版本下载失败
  91. wx.showModal({
  92. title: "已经有新版本了哟~",
  93. content: "新版本已经上线啦~,请您删除当前小程序,重新搜索打开"
  94. });
  95. });
  96. }
  97. });
  98. } else {
  99. wx.showModal({
  100. title: "提示",
  101. content: "当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。"
  102. });
  103. }
  104. },
  105. methods: {
  106. ...mapMutations(['login']),
  107. phoneLogin() {
  108. if (!this.checkedFlag) {
  109. uni.showToast({
  110. title: '请先阅读并同意用户协议',
  111. icon: 'none'
  112. })
  113. return
  114. }
  115. },
  116. change(e) {
  117. this.address = this.list.filter((i) => {
  118. return i.areaId === e
  119. })
  120. this.areaId = this.address[0].areaId || ''
  121. getApp().globalData.storeInfo = this.address[0];
  122. this.code = this.address[0].code || ''
  123. },
  124. changeClick() {
  125. this.isCheckLogin = !this.isCheckLogin
  126. },
  127. // linkOthers (v) {
  128. // console.log(v == 0 ? '服务协议' : '隐私条款');
  129. // if (v == 0) {
  130. // uni.navigateTo({
  131. // url: '/pages/tools/userAngument'
  132. // })
  133. // } else {
  134. // uni.navigateTo({
  135. // url: '/pages/tools/privacyAngument'
  136. // })
  137. // }
  138. // },
  139. navToWebView(url) {
  140. uni.navigateTo({
  141. url: url
  142. })
  143. },
  144. pdwLogin () {
  145. if (!this.isChecked) {
  146. uni.showToast({
  147. title: '请先阅读并同意用户协议',
  148. icon: 'none'
  149. })
  150. return
  151. }
  152. if (this.areaId === '') {
  153. uni.showToast({
  154. title: '请选择校区',
  155. icon: 'none'
  156. })
  157. return
  158. }
  159. if (this.phoneNum === '') {
  160. uni.showToast({
  161. title: '请填写手机号',
  162. icon: 'none'
  163. })
  164. return
  165. }
  166. if (this.password === '') {
  167. uni.showToast({
  168. title: '请填写密码',
  169. icon: 'none'
  170. })
  171. return
  172. }
  173. uni.showLoading({
  174. title: "登录中"
  175. });
  176. let json = uni.getStorageSync('storeInfo')
  177. if (json) {
  178. uni.clearStorage()
  179. }
  180. var dateStr = (new Date()).getTime();
  181. var sign = this.getSign(this.phoneNum + this.password + dateStr);
  182. let that = this
  183. this.requst({
  184. url: that.loginurl,
  185. data: {
  186. phone: that.phoneNum,
  187. password: that.password,
  188. timestamp: dateStr,
  189. sign: sign,
  190. code: that.code,
  191. },
  192. success: res => {
  193. if (res.data.rtnBean.rtnCode == 0) {
  194. that.login(res.data.obj);
  195. that.loginx(that.code,res.data.obj.storeUserId);
  196. getApp().globalData.storeInfo = res.data.obj
  197. uni.switchTab({
  198. url: '/pages/index/index' //跳转到首页
  199. });
  200. // }, 1500)
  201. //}, 0);
  202. } else {
  203. uni.hideLoading();
  204. if (res.data.rtnBean.rtnMsg != '') {
  205. that.$api.msg(res.data.rtnBean.rtnMsg)
  206. setTimeout(() => {
  207. uni.hideToast();
  208. }, 1500)
  209. return;
  210. }
  211. }
  212. },
  213. fail: (e) => {
  214. uni.showToast({
  215. title: '失败,请稍后尝试!',
  216. icon: 'none',
  217. mask: 'true'
  218. });
  219. // this.close();
  220. return;
  221. },
  222. })
  223. },
  224. authorization(e) {
  225. console.log('e :>> ', e);
  226. if (e.detail.errMsg == "getPhoneNumber:ok") {
  227. var dateStr = (new Date()).getTime();
  228. var sign = this.getSign(e.detail.code + dateStr);
  229. this.requst({
  230. url: this.weixinLoginUrl, //服务器端地址
  231. data: {
  232. code: e.detail.code,
  233. timestamp: dateStr,
  234. sign: sign
  235. },
  236. success: (res) => {
  237. if (res.data.rtnBean.rtnCode == 0) {
  238. this.login(res.data.obj);
  239. // setTimeout(() => {
  240. // uni.showToast({
  241. // icon: 'none',
  242. // title: '登录成功',
  243. // });
  244. // setTimeout(() => {
  245. uni.switchTab({
  246. url: '/pages/index/index' //跳转到首页
  247. });
  248. console.log("########################");
  249. // }, 1500)
  250. // }, 0);
  251. } else {
  252. uni.hideLoading();
  253. if (res.data.rtnBean.rtnMsg != '') {
  254. this.$api.msg(res.data.rtnBean.rtnMsg)
  255. setTimeout(() => {
  256. uni.hideToast();
  257. }, 1500)
  258. return;
  259. }
  260. }
  261. },
  262. fail(res) {
  263. uni.showToast({
  264. title: '获取用户手机号失败,请稍后尝试!',
  265. icon: 'none',
  266. mask: 'true'
  267. });
  268. return;
  269. }
  270. });
  271. }
  272. },
  273. // 加载校区
  274. getAddress () {
  275. uni.showLoading({
  276. title: '加载中'
  277. })
  278. let that = this
  279. this.requst({
  280. url: this.addressUrl,
  281. data: {},
  282. success: res => {
  283. if (res.data.rtnBean.rtnCode == 0) {
  284. that.list = res.data.list
  285. if (that.list.length) {
  286. for (var i = 0; i < that.list.length; i++) {
  287. that.list[i]
  288. let address = {
  289. value: that.list[i].areaId,
  290. text: that.list[i].areaName
  291. }
  292. that.addressList.push(address)
  293. }
  294. }
  295. }
  296. uni.hideLoading()
  297. },
  298. fail: (e) => {
  299. console.log("接口调用失败:" + JSON.stringify(e));
  300. uni.hideLoading()
  301. }
  302. })
  303. },
  304. successLogin (res) {
  305. // 处理用户信息
  306. console.log('res', res);
  307. // this.$refs.uToast.show({
  308. // title: '登录成功',
  309. // type: 'success'
  310. // });
  311. // setTimeout(() => {
  312. // uni.redirectTo({
  313. // url: "/pages/index/index",
  314. // });
  315. // }, 1000)
  316. }
  317. },
  318. onReady () {
  319. const token = uni.getStorageSync('token')
  320. if (token) {
  321. uni.redirectTo({
  322. url: '/pages/index/index' //跳转到首页
  323. })
  324. }
  325. },
  326. onLoad() {
  327. this.getAddress();
  328. },
  329. onHide() {
  330. this.code = ''
  331. }
  332. }
  333. </script>
  334. <style lang="scss" scoped>
  335. .login-wrap {
  336. width: 100vw;
  337. height: 100vh;
  338. display: flex;
  339. flex-direction: column;
  340. align-items: center;
  341. justify-content: center;
  342. }
  343. .account-login-wrap {
  344. width: 80%;
  345. position: relative;
  346. z-index: 1;
  347. /deep/ .u-input {
  348. margin-bottom: 60rpx;
  349. }
  350. }
  351. .wechat-login {
  352. width: 80%;
  353. height: 80rpx;
  354. border-radius: 80rpx;
  355. display: flex;
  356. justify-content: center;
  357. align-items: center;
  358. background-color: #1A78F6;
  359. color: #fff;
  360. font-size: 32rpx;
  361. margin-bottom: 20rpx;
  362. }
  363. .argument {
  364. display: flex;
  365. justify-content: center;
  366. align-items: center;
  367. font-size: 24rpx;
  368. margin-bottom: 40rpx;
  369. .text-color {
  370. color: #1A78F6;
  371. }
  372. }
  373. .other-login-wrap {
  374. width: 100vw;
  375. height: 60rpx;
  376. position: fixed;
  377. left: 0;
  378. bottom: constant(safe-area-inset-bottom);
  379. bottom: env(safe-area-inset-bottom);
  380. @include flexCenter;
  381. .other-login {
  382. font-size: $font-base;
  383. color: #0F40F5;
  384. }
  385. }
  386. </style>