login.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. const util = require('../../utils/util.js');
  2. const md = require('../../utils/md5.js');
  3. const app = getApp();
  4. const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
  5. Page({
  6. data: {
  7. clickTimes: false,
  8. type: '',
  9. firstLogin: false,
  10. needRecomCode: 0,
  11. code: '',
  12. show: false,
  13. invitePrice: 0,
  14. flag: true,
  15. shareInfo: {},
  16. vouchers: [{
  17. price: 0,
  18. lowestPrice: 0,
  19. description: '0'
  20. }],
  21. avatarUrl: '',
  22. // 注册
  23. username: "",
  24. phoneNum: "",
  25. password: '',
  26. // passwordConfirmation: '',
  27. it: {},
  28. unionId: "",
  29. socialUid: '',
  30. authorization: true,//登录模式 为true是微信登录
  31. formData: {
  32. loginName: '',
  33. password: '',
  34. },
  35. quitLogin: false,//退出登录
  36. registeredShow: false//手机号已注册弹窗
  37. },
  38. // 页面加载 获取小程序码链接参数
  39. onLoad(options) {
  40. this.registerStatus()
  41. this.setData({
  42. quitLogin: options.quitLogin || false
  43. });
  44. let that = this;
  45. if (!util.isEmpty(options.scene)) {
  46. util.ajax({
  47. func: "params",
  48. data: {
  49. "code": options.scene.replace("urlcode%3D", "").replace("urlcode=", "")
  50. },
  51. load: false
  52. }, function (res) {
  53. if (res.code == 0) {
  54. wx.setStorageSync("WX-invite", res.data.code);
  55. that.setData({
  56. type: 'share'
  57. });
  58. if (!util.isObjEmpty(app.globalData.userInfo)) {
  59. wx.reLaunch({
  60. url: '/pages/home/index',
  61. });
  62. }
  63. that.isRecomCode();
  64. }
  65. });
  66. } else {
  67. this.setData({
  68. type: options.type || ''
  69. });
  70. that.isRecomCode();
  71. }
  72. },
  73. onShow() {
  74. wx.hideHomeButton()
  75. },
  76. // 获取用户注册情况
  77. registerStatus() {
  78. let that = this
  79. wx.login({
  80. success: res => {
  81. util.ajax({
  82. func: "v2/login/check_user_exist",
  83. data: {
  84. "code": res.code
  85. },
  86. method: 'POST'
  87. }, function (datas) {
  88. if (datas.code == 0) {
  89. that.setData({
  90. firstLogin: datas.data
  91. })
  92. } else {
  93. }
  94. });
  95. }
  96. });
  97. },
  98. // 获取邀请人信息
  99. isRecomCode() {
  100. let that = this,
  101. code = wx.getStorageSync("WX-invite");
  102. if (util.isEmpty(code)) return false;
  103. util.ajax({
  104. func: "inviter/info",
  105. data: {
  106. "code": code
  107. }
  108. }, function (res) {
  109. if (res.code == 0) {
  110. that.setData({
  111. needRecomCode: ((res.data.roleType == 'sharer' || res.data.roleType == 'organ') && res.data.roleTypeDisable == 0 ? 1 : 0),
  112. shareInfo: res.data
  113. });
  114. }
  115. });
  116. },
  117. back() {
  118. this.setData({
  119. show: false
  120. })
  121. // 退出登录
  122. if (this.data.quitLogin) {
  123. wx.reLaunch({
  124. url: '/pages/home/index'
  125. })
  126. } else {
  127. setTimeout(() => {
  128. wx.navigateBack();
  129. }, 200);
  130. }
  131. },
  132. // 切换登录
  133. toggleLogin() {
  134. this.setData({
  135. authorization: !this.data.authorization
  136. })
  137. },
  138. phoneNumInput(e) {
  139. this.setData({
  140. 'formData.loginName': e.detail.value
  141. })
  142. if (this.data.formData.loginName.length == 11) {
  143. this.setData({
  144. passwordFocus: true
  145. })
  146. }
  147. },
  148. passwordInput(e) {
  149. this.setData({
  150. 'formData.password': e.detail.value
  151. })
  152. },
  153. navigatorURl() {
  154. wx.navigateTo({
  155. url: '/pages/account/setting/password?noLogin=true',
  156. });
  157. },
  158. // 手机号密码登录
  159. accountLogin() {
  160. let that = this,
  161. data = that.data.formData
  162. if (util.isPhone(data.loginName)) {
  163. util.showTips("请输入正确的手机号。");
  164. return false;
  165. }
  166. if (util.isEmpty(data.password)) {
  167. util.showTips("请输入密码。");
  168. return false;
  169. }
  170. if (data.password < 6) {
  171. util.showTips("请输入至少6位密码。");
  172. return false;
  173. }
  174. data.password = md.hexMD5(data.password);
  175. wx.login({
  176. success: loginRes => {
  177. // 用于绑定微信
  178. data.code = loginRes.code
  179. // 生成时间戳做唯一值 用来获取openid
  180. let timestamp = new Date().getTime();
  181. data.payUserCode = Math.floor(timestamp / 1000)
  182. wx.setStorageSync("payUserCode", data.payUserCode);
  183. util.ajax({
  184. func: "v2/login/account",
  185. data: data
  186. }, function (res) {
  187. if (res.code == 0) {
  188. res.data.role = util.userRole(Math.round(res.data.score));
  189. // 强制退出登录
  190. res.data.VERSION = 9
  191. wx.setStorageSync("WXuserInfo", res.data);
  192. app.globalData.userInfo = res.data
  193. that.getOpenId()
  194. if (that.data.type == "share") {
  195. wx.reLaunch({
  196. url: '/pages/home/index',
  197. });
  198. } else {
  199. // 返回逻辑
  200. that.back();
  201. }
  202. } else {
  203. util.showTips(res.reason);
  204. }
  205. that.setData({
  206. clickTimes: false
  207. })
  208. });
  209. },
  210. fail(err) {
  211. util.showTips(err.errno + "微信授权失败,请刷新页面后重试");
  212. }
  213. });
  214. },
  215. getOpenId() {
  216. util.ajax({
  217. func: "v2/login/getPayUserOpenId",
  218. data: { payUserCode: wx.getStorageSync("payUserCode") }
  219. }, function (res) {
  220. if (res.code == 0) {
  221. // 将新获取的作为下单openid
  222. app.globalData.userInfo.socialUid = res.data
  223. } else {
  224. util.showTips(res.reason);
  225. }
  226. })
  227. },
  228. // 用户注册并登录
  229. registerLogin(e) {
  230. let that = this
  231. let data = {}
  232. if (e.detail.errMsg == "getPhoneNumber:ok") {
  233. data.vcode = e.detail.code;
  234. wx.login({
  235. success: res => {
  236. data.code = res.code;
  237. data.channelId = util.config.channelInfo.channel //渠道标识
  238. util.ajax({
  239. func: "applet/regUser",
  240. data: data,
  241. method: 'POST'
  242. }, function (data) {
  243. if (data.code == 0) {
  244. console.log(676767)
  245. data.data.role = util.userRole(Math.round(data.data.score));
  246. wx.setStorageSync("WXuserInfo", data.data);
  247. app.globalData.userInfo = data.data
  248. if (that.data.type == "share") {
  249. wx.reLaunch({
  250. url: '/pages/home/index',
  251. });
  252. } else {
  253. that.back();
  254. }
  255. } else
  256. util.showTips(data.reason);
  257. });
  258. }
  259. })
  260. }
  261. },
  262. // 用户登录
  263. getUserProfile() {
  264. let that = this;
  265. this.setData({
  266. clickTimes: true
  267. })
  268. wx.login({
  269. success: res => {
  270. that.setData({
  271. code: res.code
  272. })
  273. }
  274. });
  275. that.userProfile();
  276. },
  277. userProfile() {
  278. let that = this;
  279. wx.getUserProfile({
  280. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中
  281. success: (res) => {
  282. let it = {};
  283. if (!res.encryptedData && !res.iv) {
  284. it.version = "applet_v1.0.0";
  285. it.code = that.data.code;
  286. it.target = 'app';
  287. it.phone = that.data.phoneNum
  288. } else {
  289. it.version = "applet_v1.0.0";
  290. it.code = that.data.code;
  291. it.target = 'app';
  292. it.encryptedData = res.encryptedData;
  293. it.iv = res.iv;
  294. it.phone = that.data.phoneNum
  295. }
  296. that.data.it = it
  297. util.ajax({
  298. func: "applet_login",
  299. data: it
  300. }, function (data) {
  301. if (data.code == 0) {
  302. that.setData({
  303. unionId: data.data.unionId,
  304. socialUid: data.data.socialUid
  305. })
  306. // todo:
  307. // data.data.firstLogin = 1
  308. // if (data.data.firstLogin == 1) {
  309. // wx.setNavigationBarTitle({ title: '注册' })
  310. // that.setData({
  311. // flag: false
  312. // })
  313. // return
  314. // }
  315. data.data.role = util.userRole(Math.round(data.data.score));
  316. // 强制退出登录
  317. data.data.VERSION = 9
  318. wx.setStorageSync("WXuserInfo", data.data);
  319. app.globalData.userInfo = data.data
  320. if (that.data.type == "share") {
  321. wx.reLaunch({
  322. url: '/pages/home/index',
  323. });
  324. } else {
  325. that.back();
  326. }
  327. } else {
  328. util.showTips(data.reason);
  329. }
  330. that.setData({
  331. clickTimes: false
  332. })
  333. });
  334. },
  335. fail(rr) {
  336. console.log(67676767, rr)
  337. }
  338. });
  339. },
  340. // 用户注册绑定手机号
  341. getPhoneNumber(e) {
  342. let that = this
  343. let data = {};
  344. if (e.detail.errMsg == "getPhoneNumber:ok") {
  345. data.code = e.detail.code;
  346. util.ajax({
  347. func: "getUserPhone",
  348. data: data,
  349. }, function (data) {
  350. if (data.code == 0) {
  351. that.setData({
  352. phoneNum: data.data
  353. })
  354. // 已注册
  355. } else if (data.code == 1007) {
  356. that.setData({
  357. phoneNum: data.data,
  358. registeredShow: true
  359. })
  360. } else
  361. util.showTips(data.reason);
  362. });
  363. }
  364. },
  365. // 注册
  366. signIn() {
  367. let that = this
  368. if (this.data.username == "" && this.data.avatarUrl == "") {
  369. util.showTips("昵称头像不能为空");
  370. return
  371. } else if (this.data.username == "") {
  372. util.showTips("昵称不能为空");
  373. return
  374. } else if (this.data.avatarUrl == "") {
  375. util.showTips("请点击上传一张属于您自己的头像 ~");
  376. return
  377. } else if (this.data.password == "") {
  378. util.showTips("密码不能为空");
  379. return
  380. } else if (this.data.password.length < 6) {
  381. util.showTips("请输入至少6位密码");
  382. return
  383. }
  384. // else if (this.data.passwordConfirmation != this.data.password) {
  385. // util.showTips("两次密码不一致");
  386. // return
  387. // }
  388. let data = {};
  389. wx.login({
  390. success: res => {
  391. data.phone = that.data.phoneNum;
  392. data.code = res.code;
  393. data.nickname = that.data.username
  394. data.avatar = that.data.avatarUrl
  395. data.password = md.hexMD5(that.data.password)
  396. data.unionId = that.data.unionId
  397. data.socialUid = that.data.socialUid
  398. data.channelId = util.config.channelInfo.channel //渠道标识
  399. data.city = app.globalData.city.city
  400. // 非列表城市改为空
  401. let cityList = wx.getStorageSync("cityList");
  402. if (data.city && cityList) {
  403. data.city = cityList.indexOf(data.city) == -1 ? '' : data.city
  404. }
  405. util.ajax({
  406. func: "applet/regUser",
  407. data: data,
  408. method: 'POST'
  409. }, function (data) {
  410. if (data.code == 0) {
  411. data.data.role = util.userRole(Math.round(data.data.score));
  412. // 强制退出登录
  413. data.data.VERSION = 9
  414. app.globalData.userInfo = data.data;
  415. // that.data.phoneNum=data.phoneNum
  416. that.setData({
  417. phoneNum: data.data.phoneNum
  418. })
  419. wx.setStorageSync("WXuserInfo", data.data);
  420. if (that.data.needRecomCode == 1) {
  421. wx.navigateTo({
  422. url: '/pages/home/referre?type=' + that.data.type,
  423. });
  424. that.bindCode();
  425. return false
  426. }
  427. // wx.navigateBack()
  428. that.back();
  429. } else
  430. util.showTips(data.reason);
  431. });
  432. }
  433. })
  434. },
  435. onChooseAvatar(e) {
  436. const {
  437. avatarUrl
  438. } = e.detail
  439. let that = this
  440. console.log(that.data.unionId);
  441. // wx.showLoading({ title: '上传准备中' });
  442. // 直传头像
  443. util.uploadFile("oss/policy", avatarUrl, {
  444. unionId: that.data.unionId,
  445. type: "userAvatar"
  446. }, true).then((res) => {
  447. if (res.code == 0) {
  448. that.data.avatarUrl = res.url
  449. this.setData({
  450. avatarUrl: that.data.avatarUrl
  451. })
  452. }
  453. });
  454. },
  455. username(e) {
  456. // console.log(e.detail.value);
  457. this.data.username = e.detail.value
  458. this.setData({
  459. username: this.data.username
  460. })
  461. },
  462. pwSetting(e) {
  463. this.setData({
  464. password: e.detail.value
  465. })
  466. },
  467. // pwConfirmation(e) {
  468. // this.setData({
  469. // passwordConfirmation: e.detail.value
  470. // })
  471. // },
  472. // 绑定邀请码
  473. bindCode() {
  474. let that = this,
  475. code = wx.getStorageSync("WX-invite");
  476. if (this.data.shareInfo.roleType == 'normal') {
  477. util.showTips("该用户不是分享家/机构,无法绑定邀请码。");
  478. return false;
  479. }
  480. if (this.data.shareInfo.roleTypeDisable == 1) {
  481. util.showTips("该分享家/机构未启用,无法绑定邀请码。");
  482. return false;
  483. }
  484. util.ajax({
  485. func: "user/savecode",
  486. data: {
  487. "code": code,
  488. receiveType: 1
  489. }
  490. }, function (res) {
  491. if (res.code == 0) {
  492. app.globalData.userInfo['recomCode'] = code;
  493. wx.setStorageSync("WXuserInfo", app.globalData.userInfo);
  494. // console.log(code, that.data.shareInfo.roleType)
  495. if (that.data.shareInfo.roleType == 'sharer' || that.data.shareInfo.roleType == 'organ') {
  496. that.setData({
  497. show: true,
  498. vouchers: res.vouchers,
  499. invitePrice: res.invitePrice
  500. });
  501. // console.log(that.data.show)
  502. } else {
  503. that.back();
  504. }
  505. } else
  506. util.showTips(res.reason);
  507. });
  508. },
  509. })