login.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <template>
  2. <view class="login-page">
  3. <view style="width: 100%; height: 80rpx;"></view>
  4. <view class="padding">
  5. <checkbox-group @change="handelChange">
  6. <label class="text-center">
  7. <checkbox value="1" style="transform:scale(0.7)" :checked="isChecked"/>
  8. 我已阅读并同意
  9. </label>
  10. <text class="text-blue" @click="goPage('/pages/login/agreement')">《晋江人力用户服务协议》</text>
  11. </checkbox-group>
  12. </view>
  13. <!-- #ifdef MP-WEIXIN -->
  14. <view class="padding flex flex-direction" v-if="formName=='wxLoginForm'">
  15. <button class="cu-btn bg-green margin-tb-sm lg" @click="wxLogin">授权登录</button>
  16. <button class="cu-btn margin-tb-sm lg" @tap="toPage">返回</button>
  17. </view>
  18. <view class="padding flex flex-direction" v-if="formName=='wxCheckMobileForm'">
  19. <button class="cu-btn bg-green margin-tb-sm lg" open-type="getPhoneNumber" @getphonenumber="wxMobile">手机号快捷登录</button>
  20. <button class="cu-btn margin-tb-sm lg" @tap="checkMobile" data-formname="editMobileModal">其他手机号绑定</button>
  21. <button class="cu-btn margin-tb-sm lg" @tap="toPage">取消注册登录操作</button>
  22. </view>
  23. <view class="padding flex flex-direction" v-if="formName=='editMobileModal'">
  24. <form>
  25. <view class="cu-form-group">
  26. <view class="title">手机号码</view>
  27. <input type="number" placeholder="请输入手机号..." @input="wxInput" data-val="editmobile" :value="weixinInfo.editmobile"></input>
  28. </view>
  29. <view class="cu-form-group margin-bottom solids-bottom">
  30. <view class="title">验证码</view>
  31. <input type="number" placeholder="请输入验证码..." @input="wxInput" data-val="editcode" :value="weixinInfo.editcode"></input>
  32. <button class='cu-btn bg-green shadow' @click="getSmsCode(weixinInfo.editmobile)">{{smsinfo.text}}</button>
  33. </view>
  34. </form>
  35. <button class="cu-btn bg-green margin-tb-sm lg" open-type="getUserInfo" @getuserinfo="editMobile">手机号快捷登录</button>
  36. <button class="cu-btn margin-tb-sm lg" @tap="checkMobile" data-formname="wxCheckMobileForm">返回</button>
  37. </view>
  38. <!-- #endif -->
  39. <!-- #ifdef APP-PLUS -->
  40. <view class="padding flex flex-direction">
  41. <form>
  42. <view class="cu-form-group">
  43. <view class="title">手机号码</view>
  44. <input type="number" placeholder="请输入手机号..." @input="appInput" data-val="editmobile" :value="mobileInfo.editmobile"></input>
  45. </view>
  46. <view class="cu-form-group margin-bottom solids-bottom">
  47. <view class="title">验证码</view>
  48. <input type="number" placeholder="请输入验证码..." @input="appInput" data-val="editcode" :value="mobileInfo.editcode"></input>
  49. <button class='cu-btn bg-green shadow' @click="getSmsMCode(mobileInfo.editmobile)">{{smsinfo.text}}</button>
  50. </view>
  51. </form>
  52. <button class="cu-btn bg-green margin-tb-sm lg" @tap="regMobile">立即登录</button>
  53. </view>
  54. <!-- #endif -->
  55. </view>
  56. </template>
  57. <script>
  58. var _this;
  59. export default {
  60. data() {
  61. return {
  62. isChecked: false,
  63. isRotate: false,
  64. backpage: "/pages/index/index",
  65. formName: "editMobileModal",
  66. weixinInfo: {
  67. parentid: 0,
  68. openid: "",
  69. nickname: "",
  70. avatar: "",
  71. editmobile: "",
  72. editcode: ""
  73. },
  74. mobileInfo: {
  75. parentid: 0,
  76. editmobile: "",
  77. editcode: ""
  78. },
  79. timerId: null,
  80. smsinfo: {
  81. text: '获取验证码',
  82. status: false,
  83. codeTime: 60,
  84. smscodepass: ""
  85. }
  86. };
  87. },
  88. onLoad: function(option) {
  89. _this = this;
  90. var backpage = option.backpage || "/pages/index/index";
  91. _this.backpage = decodeURIComponent(backpage);
  92. _this.isLogin();
  93. _this.weixinInfo.parentid = uni.getStorageSync('parentid') || 0;
  94. _this.mobileInfo.parentid = uni.getStorageSync('parentid') || 0;
  95. _this.formName = "wxLoginForm";
  96. },
  97. onHide: function() {
  98. clearInterval(_this.timerId);
  99. },
  100. onUnload: function() {
  101. clearInterval(_this.timerId);
  102. },
  103. methods: {
  104. // 判断是否已登录
  105. isLogin: function() {
  106. try {
  107. const userinfo = uni.getStorageSync('userinfo') || null;
  108. if (userinfo) {
  109. _this.checkuserinfoall(userinfo);
  110. _this.toPage();
  111. }
  112. } catch (e) {
  113. console.log("e: " + JSON.stringify(e));
  114. }
  115. },
  116. handelChange: function (e){
  117. _this.isChecked = (e.detail.value.length > 0);
  118. },
  119. showAgreement: function () {
  120. },
  121. checkuserinfoall: function(userinfo){
  122. _this.$req.ajax({
  123. path: "my/getuser",
  124. title: "正在获取用户信息",
  125. data: {
  126. userid: userinfo.id,
  127. }
  128. }).then((data) => {
  129. //判断用户资料是否完善
  130. // if(data.user.education=="" || data.user.address=="" || data.user.workexperience=="" || data.user.eduexperience==""){
  131. // uni.navigateTo({
  132. // url: "/pages/my/myinfo"
  133. // });
  134. // return false;
  135. // }
  136. //判断用户是否已实名认证
  137. _this.toPage();
  138. if(data.user.authstatus==1){
  139. uni.navigateTo({
  140. url: "/pages/my/myauth"
  141. });
  142. return false;
  143. }
  144. _this.isRotate = false;
  145. }).catch((err) => {
  146. uni.showModal({
  147. title: '信息提示',
  148. content: err,
  149. showCancel: false
  150. });
  151. _this.isRotate = false;
  152. });
  153. },
  154. // 页面跳转
  155. toPage: function() {
  156. uni.reLaunch({
  157. url: _this.backpage,
  158. fail: function() {
  159. uni.switchTab({
  160. url: _this.backpage
  161. });
  162. }
  163. });
  164. },
  165. goPage: function(pageurl) {
  166. uni.navigateTo({
  167. url: pageurl,
  168. fail: function() {
  169. uni.switchTab({
  170. url: pageurl
  171. });
  172. }
  173. });
  174. },
  175. // S 微信端登录注册
  176. // 自填手机号
  177. editMobile: function() {
  178. if (_this.isRotate) {
  179. return false;
  180. }
  181. _this.isRotate = true;
  182. _this.$req.ajax({
  183. path: "login/seteditmobile",
  184. title: "正在授权登录",
  185. data: {
  186. parentid: _this.weixinInfo.parentid,
  187. openid: _this.weixinInfo.openid,
  188. nickname: _this.weixinInfo.nickname,
  189. avatar: _this.weixinInfo.avatar,
  190. editmobile: _this.weixinInfo.editmobile,
  191. editcode: _this.weixinInfo.editcode,
  192. smscodepass: _this.smsinfo.smscodepass
  193. }
  194. }).then((data) => {
  195. uni.setStorageSync('userinfo', data.userinfo);
  196. _this.toPage();
  197. _this.isRotate = false;
  198. }).catch((err) => {
  199. uni.showModal({
  200. title: '信息提示',
  201. content: err,
  202. showCancel: false
  203. });
  204. _this.isRotate = false;
  205. });
  206. },
  207. // 微信手机号
  208. wxMobile: function(e) {
  209. if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
  210. uni.showModal({
  211. title: '警告',
  212. content: '您点击了拒绝授权,将无法正常注册登录,点击确定重新获取授权。',
  213. showCancel: false
  214. });
  215. } else {
  216. if (_this.isRotate) {
  217. return false;
  218. }
  219. _this.isRotate = true;
  220. _this.$req.ajax({
  221. path: "login/setwxmobile",
  222. title: "正在授权登录",
  223. data: {
  224. parentid: _this.weixinInfo.parentid,
  225. openid: _this.weixinInfo.openid,
  226. session_key: _this.weixinInfo.session_key,
  227. nickname: _this.weixinInfo.nickname,
  228. avatar: _this.weixinInfo.avatar,
  229. iv: e.detail.iv,
  230. encryptedData: encodeURIComponent(e.detail.encryptedData)
  231. }
  232. }).then((data) => {
  233. uni.setStorageSync('userinfo', data.userinfo);
  234. _this.checkuserinfoall(data.userinfo);
  235. _this.toPage();
  236. _this.isRotate = false;
  237. }).catch((err) => {
  238. uni.showModal({
  239. title: '信息提示',
  240. content: err,
  241. showCancel: false
  242. });
  243. _this.isRotate = false;
  244. });
  245. }
  246. },
  247. // 获取OpenId
  248. wxLogin: function() {
  249. if (!_this.isChecked) {
  250. uni.showModal({
  251. title: '警告',
  252. content: '请先查看并勾选协助。',
  253. showCancel: false
  254. });
  255. return false;
  256. }
  257. uni.login({
  258. provider: 'weixin',
  259. success: function(loginRes) {
  260. if (_this.isRotate) {
  261. return false;
  262. }
  263. _this.isRotate = true;
  264. _this.$req.ajax({
  265. path: "login/getwxopenid",
  266. title: "正在授权登录",
  267. data: {
  268. code: loginRes.code
  269. }
  270. }).then((data) => {
  271. if (data.userinfo == null) {
  272. uni.showModal({
  273. title: '温馨提示',
  274. content: '亲,授权微信登录后才能正常使用小程序功能',
  275. success(res) {
  276. //如果用户点击了确定按钮
  277. if (res.confirm) {
  278. uni.getUserProfile({
  279. desc: '获取你的昵称、头像、地区及性别',
  280. success: infoRes => {
  281. _this.weixinInfo.openid = data.openid;
  282. _this.weixinInfo.session_key = data.session_key;
  283. _this.weixinInfo.nickname = infoRes.userInfo.nickName;
  284. _this.weixinInfo.avatar = infoRes.userInfo.avatarUrl;
  285. _this.formName = "wxCheckMobileForm";
  286. },
  287. fail: res => {
  288. uni.showModal({
  289. title: '警告',
  290. content: '您点击了拒绝授权,将无法正常注册登录,点击确定重新获取授权。',
  291. showCancel: false,
  292. success: function(res) {
  293. if (res.confirm) {
  294. uni.openSetting();
  295. }
  296. }
  297. });
  298. }
  299. });
  300. } else if (res.cancel) {
  301. //如果用户点击了取消按钮
  302. uni.showToast({
  303. title: '警告',
  304. content: '您点击了拒绝授权,将无法正常注册登录,点击确定重新获取授权。',
  305. icon: 'error',
  306. duration: 2000
  307. });
  308. return;
  309. }
  310. }
  311. });
  312. // uni.getUserProfile({
  313. // desc:"获取个人信息",
  314. // provider: 'weixin',
  315. // withCredentials: true,
  316. // success: function(infoRes) {
  317. // console.log(infoRes);
  318. // return false;
  319. // _this.weixinInfo.openid = data.openid;
  320. // _this.weixinInfo.session_key = data.session_key;
  321. // _this.weixinInfo.nickname = infoRes.userInfo.nickName;
  322. // _this.weixinInfo.avatar = infoRes.userInfo.avatarUrl;
  323. // _this.formName = "wxCheckMobileForm";
  324. // },
  325. // fail: function(res) {
  326. // console.log(res);
  327. // uni.showModal({
  328. // title: '警告',
  329. // content: '您点击了拒绝授权,将无法正常注册登录,点击确定重新获取授权。',
  330. // showCancel: false,
  331. // success: function(res) {
  332. // if (res.confirm) {
  333. // uni.openSetting();
  334. // }
  335. // }
  336. // });
  337. // }
  338. // });
  339. } else {
  340. uni.setStorageSync('userinfo', data.userinfo);
  341. _this.checkuserinfoall(data.userinfo);
  342. _this.toPage();
  343. }
  344. _this.isRotate = false;
  345. }).catch((err) => {
  346. uni.showModal({
  347. title: '信息提示',
  348. content: err,
  349. showCancel: false
  350. });
  351. _this.isRotate = false;
  352. });
  353. }
  354. });
  355. },
  356. checkMobile: function(e) {
  357. _this.formName = e.currentTarget.dataset.formname;
  358. },
  359. // 获取短信验证码
  360. getSmsCode: function(mobile) {
  361. if (_this.smsinfo.status) {
  362. return false;
  363. }
  364. _this.smsinfo.status = true;
  365. _this.$req.ajax({
  366. path: "login/smsregister",
  367. data: {
  368. identifier: mobile
  369. }
  370. }).then((data) => {
  371. _this.smsinfo.smscodepass = data.smscodepass;
  372. _this.timerId = setInterval(() => {
  373. var codeTime = _this.smsinfo.codeTime;
  374. codeTime--;
  375. _this.smsinfo.codeTime = codeTime;
  376. _this.smsinfo.text = codeTime + "s 后重新发送";
  377. if (codeTime < 1) {
  378. clearInterval(_this.timerId);
  379. _this.smsinfo.text = "重新获取";
  380. _this.smsinfo.codeTime = 60;
  381. _this.smsinfo.status = false;
  382. }
  383. }, 1000);
  384. }).catch((err) => {
  385. uni.showModal({
  386. title: '信息提示',
  387. content: err,
  388. showCancel: false
  389. });
  390. _this.smsinfo.status = false;
  391. });
  392. },
  393. wxInput: function(e) {
  394. var dataval = e.currentTarget.dataset.val;
  395. _this.weixinInfo[dataval] = e.detail.value;
  396. },
  397. // E 微信端登录注册
  398. // S APP端登录注册
  399. appInput: function(e) {
  400. var dataval = e.currentTarget.dataset.val;
  401. _this.mobileInfo[dataval] = e.detail.value;
  402. },
  403. // 获取短信验证码
  404. getSmsMCode: function(mobile) {
  405. if (_this.smsinfo.status) {
  406. return false;
  407. }
  408. _this.smsinfo.status = true;
  409. _this.$req.ajax({
  410. path: "login/smsmobilelogin",
  411. data: {
  412. identifier: mobile
  413. }
  414. }).then((data) => {
  415. _this.smsinfo.smscodepass = data.smscodepass;
  416. _this.timerId = setInterval(() => {
  417. var codeTime = _this.smsinfo.codeTime;
  418. codeTime--;
  419. _this.smsinfo.codeTime = codeTime;
  420. _this.smsinfo.text = codeTime + "s 后重新发送";
  421. if (codeTime < 1) {
  422. clearInterval(_this.timerId);
  423. _this.smsinfo.text = "重新获取";
  424. _this.smsinfo.codeTime = 60;
  425. _this.smsinfo.status = false;
  426. }
  427. }, 1000);
  428. }).catch((err) => {
  429. uni.showModal({
  430. title: '信息提示',
  431. content: err,
  432. showCancel: false
  433. });
  434. _this.smsinfo.status = false;
  435. });
  436. },
  437. regMobile: function() {
  438. if (_this.isRotate) {
  439. return false;
  440. }
  441. _this.isRotate = true;
  442. _this.$req.ajax({
  443. path: "login/mobilelogin",
  444. title: "正在登录",
  445. data: {
  446. parentid: _this.mobileInfo.parentid,
  447. editmobile: _this.mobileInfo.editmobile,
  448. editcode: _this.mobileInfo.editcode,
  449. smscodepass: _this.smsinfo.smscodepass
  450. }
  451. }).then((data) => {
  452. uni.setStorageSync('userinfo', data.userinfo);
  453. _this.toPage();
  454. _this.isRotate = false;
  455. }).catch((err) => {
  456. uni.showModal({
  457. title: '信息提示',
  458. content: err,
  459. showCancel: false
  460. });
  461. _this.isRotate = false;
  462. });
  463. }
  464. // E APP端登录注册
  465. },
  466. onShareAppMessage: function(res) {
  467. return {
  468. title: "登录注册",
  469. path: "/pages/login/login"
  470. }
  471. }
  472. }
  473. </script>
  474. <style>
  475. .login-page {
  476. width: 100%;
  477. height: 100vh;
  478. background-color: #FFFFFF;
  479. }
  480. .cu-form-group .title {
  481. min-width: calc(4em + 15px);
  482. }
  483. </style>