login.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. // pages/login/login.js
  2. var app = getApp();
  3. const util = require('../../utils/util.js');
  4. const WebIM = require("../../utils/WebIM")["default"];
  5. const CusBase64 = require('../../utils/base64.js');
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. imgPath: app.globalData.imgPath,
  12. phone: '',
  13. vCode: '',
  14. sendText: '发送验证码',
  15. timer: null,
  16. disabled: false,
  17. grant_type: "password",
  18. longitude: null,
  19. latitude: null,
  20. },
  21. /**
  22. * 生命周期函数--监听页面加载
  23. */
  24. onLoad: function (options) {
  25. //获取页面栈
  26. let pages = getCurrentPages();
  27. if (pages.length > 1) {
  28. wx.reLaunch({
  29. url: '../login/login',
  30. });
  31. }
  32. if (options && options.scene) {
  33. util.get({
  34. url: '/api/wechat/getQrCodeParams?key=' + options.scene,
  35. success: (res) => {
  36. console.info(res);
  37. if (res.data.code != 200) {
  38. wx.hideLoading();
  39. util.toast(res.data.msg);
  40. } else {
  41. // 扫码加群
  42. if ('GROUP' == res.data.data.type) {
  43. wx.hideLoading();
  44. wx.setStorageSync('groupId', res.data.data.groupId);
  45. if (!wx.getStorageSync('Authorization')) {
  46. wx.showModal({
  47. content: '您需要先登录,才可加入群聊',
  48. showCancel: false
  49. });
  50. }
  51. }
  52. // 扫码连接WIFI
  53. else if ('WIFI' == res.data.data.type) {
  54. let wifi = res.data.data.wifi;
  55. let password = res.data.data.password;
  56. this.connectWifi(wifi, password);
  57. }
  58. }
  59. }
  60. });
  61. }
  62. },
  63. /**
  64. * 手机号赋值
  65. */
  66. setPhone: function (e) {
  67. this.setData({
  68. phone: e.detail.value
  69. });
  70. },
  71. /**
  72. * 验证码赋值
  73. */
  74. setVcode: function (e) {
  75. this.setData({
  76. vCode: e.detail.value
  77. });
  78. },
  79. /**
  80. * 获取验证码
  81. */
  82. getVerifyCode: function () {
  83. let that = this;
  84. let disabled = that.data.disabled;
  85. if (disabled) {
  86. return;
  87. }
  88. let phone = that.data.phone;
  89. if (!phone) {
  90. util.toast('请输入手机号', 'error');
  91. return false;
  92. }
  93. let reg = /1[3-9]\d{9}/;
  94. if (!reg.test(phone)) {
  95. util.toast('手机号格式有误', 'error');
  96. return false;
  97. }
  98. if (!that.data.longitude || !that.data.latitude) {
  99. that.getUserLocation(() => {
  100. that.sendMessage();
  101. });
  102. return false;
  103. }
  104. that.sendMessage();
  105. },
  106. /**
  107. * 发送短信验证码
  108. */
  109. sendMessage() {
  110. let that = this;
  111. that.setData({
  112. sendText: '60s后再次发送',
  113. disabled: true
  114. });
  115. // 再次发送短信时间倒计时
  116. that.timeTask();
  117. util.post({
  118. url: '/api/sms/sendSms/' + that.data.phone + '/' + that.data.longitude + '/' + that.data.latitude,
  119. success: (res) => {
  120. wx.hideLoading();
  121. //console.info(res);
  122. if (res.data.code != 200) {
  123. util.toast(res.data.msg);
  124. clearInterval(that.data.timer);
  125. that.setData({
  126. sendText: '发送验证码',
  127. disabled: false
  128. });
  129. } else {
  130. util.toast(res.data.msg);
  131. }
  132. },
  133. });
  134. },
  135. /**
  136. * 60s倒计时定时器
  137. */
  138. timeTask: function () {
  139. let that = this;
  140. let i = 60;
  141. let timer = setInterval(function () {
  142. i--;
  143. if (i < 1) {
  144. that.setData({
  145. sendText: '发送验证码',
  146. disabled: false
  147. });
  148. clearInterval(timer);
  149. } else {
  150. that.setData({
  151. sendText: i + 's后再次发送'
  152. });
  153. }
  154. }, 1000);
  155. that.setData({
  156. timer: timer
  157. });
  158. },
  159. /**
  160. * 登录系统
  161. */
  162. loginSys: function (e) {
  163. let that = this;
  164. let values = e.detail.value;
  165. if (!values.phone) {
  166. util.toast('请输入手机号', 'error');
  167. return false;
  168. }
  169. let reg = /1[3-9]\d{9}/;
  170. if (!reg.test(values.phone)) {
  171. util.toast('手机号格式有误', 'error');
  172. return false;
  173. }
  174. if (!values.code) {
  175. util.toast('请输入验证码', 'error');
  176. return false;
  177. }
  178. values.type = 'CAPTCHA';
  179. if (!that.data.longitude || !that.data.latitude) {
  180. that.getUserLocation(() => {
  181. values.longitude = that.data.longitude;
  182. values.latitude = that.data.latitude;
  183. that.login(values);
  184. });
  185. return false;
  186. }
  187. values.longitude = that.data.longitude;
  188. values.latitude = that.data.latitude;
  189. that.login(values);
  190. },
  191. /**
  192. * 登录
  193. * @param {*} datas
  194. */
  195. login: function (datas) {
  196. let that = this;
  197. util.post({
  198. url: '/login/mobile',
  199. header: {
  200. 'Content-Type': 'application/x-www-form-urlencoded'
  201. },
  202. data: datas,
  203. success: (res) => {
  204. wx.hideLoading();
  205. if (res.data.code != 200) {
  206. util.toast(res.data.msg);
  207. } else {
  208. wx.setStorageSync('Authorization', res.data.data.Authorization);
  209. that.getUserInfo();
  210. }
  211. }
  212. });
  213. },
  214. /**
  215. * 登录验证通过后获取用户信息和角色信息
  216. */
  217. getUserInfo: function () {
  218. let that = this;
  219. util.get({
  220. url: '/sys/userInfoAndRole',
  221. success: (res) => {
  222. wx.hideLoading();
  223. if (res.data.code != 200) {
  224. util.toast(res.data.msg);
  225. } else {
  226. let datas = res.data.data;
  227. wx.setStorageSync('userInfo', datas);
  228. wx.setStorageSync('roleInfo', datas.roleList[0]);
  229. wx.setStorageSync('myUsername', datas.phone);
  230. app.conn.open({
  231. // apiUrl: WebIM.config.apiURL,
  232. user: datas.phone,
  233. pwd: CusBase64.CusBASE64.encoder(datas.phone),
  234. grant_type: that.data.grant_type,
  235. appKey: WebIM.config.appkey
  236. });
  237. }
  238. }
  239. });
  240. },
  241. /**
  242. * 微信获取手机号快捷登录
  243. */
  244. getPhoneNumber: function (res) {
  245. let that = this;
  246. if (res.detail.errMsg == 'getPhoneNumber:ok') {
  247. if (!res.detail.code) {
  248. util.toast('请升级微信版本');
  249. return;
  250. }
  251. } else {
  252. wx.showModal({
  253. content: '您拒绝了授权,将无法进入小程序,请授权之后再进入!!!',
  254. showCancel: false,
  255. confirmText: '返回授权'
  256. });
  257. return;
  258. }
  259. if (!that.data.longitude || !that.data.latitude) {
  260. that.getUserLocation(() => {
  261. that.login({
  262. code: res.detail.code,
  263. type: 'QUICK',
  264. longitude: that.data.longitude,
  265. latitude: that.data.latitude,
  266. });
  267. });
  268. return false;
  269. }
  270. that.login({
  271. code: res.detail.code,
  272. type: 'QUICK',
  273. longitude: that.data.longitude,
  274. latitude: that.data.latitude,
  275. });
  276. },
  277. /**
  278. * 清理本地数据缓存
  279. */
  280. cleanWxStorage: function () {
  281. wx.clearStorage({
  282. success: (res) => {
  283. console.info('清理本地数据缓存', res);
  284. },
  285. });
  286. },
  287. /**
  288. * 获取用户当前定位
  289. */
  290. getUserLocation: function (callBack) {
  291. let that = this;
  292. wx.getSetting({
  293. success: (res) => {
  294. console.info('getSetting', res);
  295. if (res.authSetting['scope.userLocation'] == undefined) {
  296. wx.authorize({
  297. scope: 'scope.userLocation',
  298. success: (res) => {
  299. console.info(res);
  300. that.getLocation(callBack);
  301. },
  302. fail: (err) => {
  303. console.info(err);
  304. }
  305. });
  306. } else if (res.authSetting['scope.userLocation'] == true) {
  307. that.getLocation(callBack);
  308. } else {
  309. wx.showModal({
  310. content: '请开启相关权限,以便更好地体验小程序',
  311. success: (res) => {
  312. if (res.confirm) {
  313. wx.openSetting({
  314. success: (res) => {
  315. console.info('openSetting', res.authSetting)
  316. if (res.authSetting['scope.userLocation']) {
  317. util.toast('授权成功');
  318. that.getLocation(callBack);
  319. } else {
  320. util.toast('用户未授权');
  321. }
  322. }
  323. });
  324. }
  325. }
  326. });
  327. }
  328. }
  329. });
  330. },
  331. /**
  332. * 获取定位坐标
  333. */
  334. getLocation: function (callBack) {
  335. let that = this;
  336. wx.getLocation({
  337. type: 'gcj02',
  338. success: function (res) {
  339. console.info('getLocation', res);
  340. that.setData({
  341. latitude: res.latitude,
  342. longitude: res.longitude
  343. });
  344. callBack();
  345. }
  346. });
  347. },
  348. /**
  349. * 连接WIFI
  350. */
  351. connectWifi: function (wifi, password) {
  352. // 获取设备系统类型
  353. wx.getSystemInfo({
  354. success: (res) => {
  355. console.info(res);
  356. if (!res.wifiEnabled) {
  357. wx.hideLoading();
  358. util.toast('请打开WIFI功能');
  359. return;
  360. }
  361. // 1、初始化Wi-Fi模块
  362. wx.startWifi({
  363. success: (res) => {
  364. console.info('startWifi', res);
  365. if (res.errMsg == 'startWifi:ok') {
  366. // 4、连接Wi-Fi
  367. wx.connectWifi({
  368. SSID: wifi,
  369. password: password,
  370. success: (res) => {
  371. wx.hideLoading();
  372. console.info('connectWifi', res);
  373. if (res.errMsg == 'connectWifi:ok') {
  374. util.toast('WIFI连接成功', 'success');
  375. } else {
  376. util.toast(res.errMsg);
  377. }
  378. },
  379. fail: (e) => {
  380. wx.hideLoading();
  381. console.info(e);
  382. util.toast('WIFI连接失败');
  383. }
  384. });
  385. } else {
  386. wx.hideLoading();
  387. util.toast(res.errMsg);
  388. }
  389. },
  390. fail: (e) => {
  391. wx.hideLoading();
  392. console.info(e);
  393. util.toast('初始化WiFi模块失败');
  394. }
  395. });
  396. },
  397. fail: (e) => {
  398. wx.hideLoading();
  399. util.toast('获取设备系统类型失败');
  400. }
  401. });
  402. }
  403. })