login.js 13 KB

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