login.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. util.ajax({
  176. func: "v2/login/account",
  177. data: data
  178. }, function (res) {
  179. if (res.code == 0) {
  180. res.data.role = util.userRole(Math.round(res.data.score));
  181. // 强制退出登录
  182. res.data.VERSION = 9
  183. wx.setStorageSync("WXuserInfo", res.data);
  184. app.globalData.userInfo = res.data
  185. if (that.data.type == "share") {
  186. wx.reLaunch({
  187. url: '/pages/home/index',
  188. });
  189. } else {
  190. // 返回逻辑
  191. that.back();
  192. // wx.navigateBack();
  193. }
  194. } else {
  195. util.showTips(res.reason);
  196. }
  197. that.setData({
  198. clickTimes: false
  199. })
  200. });
  201. },
  202. // 用户注册并登录
  203. registerLogin(e) {
  204. let that = this
  205. let data = {}
  206. if (e.detail.errMsg == "getPhoneNumber:ok") {
  207. data.vcode = e.detail.code;
  208. wx.login({
  209. success: res => {
  210. data.code = res.code;
  211. data.channelId = util.config.channelInfo.channel //渠道标识
  212. util.ajax({
  213. func: "applet/regUser",
  214. data: data,
  215. method: 'POST'
  216. }, function (data) {
  217. if (data.code == 0) {
  218. console.log(676767)
  219. data.data.role = util.userRole(Math.round(data.data.score));
  220. wx.setStorageSync("WXuserInfo", data.data);
  221. app.globalData.userInfo = data.data
  222. if (that.data.type == "share") {
  223. wx.reLaunch({
  224. url: '/pages/home/index',
  225. });
  226. } else {
  227. that.back();
  228. }
  229. } else
  230. util.showTips(data.reason);
  231. });
  232. }
  233. })
  234. }
  235. },
  236. // 用户登录
  237. getUserProfile() {
  238. let that = this;
  239. this.setData({
  240. clickTimes: true
  241. })
  242. wx.login({
  243. success: res => {
  244. that.setData({
  245. code: res.code
  246. })
  247. }
  248. });
  249. that.userProfile();
  250. },
  251. userProfile() {
  252. let that = this;
  253. wx.getUserProfile({
  254. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中
  255. success: (res) => {
  256. let it = {};
  257. if (!res.encryptedData && !res.iv) {
  258. it.version = "applet_v1.0.0";
  259. it.code = that.data.code;
  260. it.target = 'app';
  261. it.phone = that.data.phoneNum
  262. } else {
  263. it.version = "applet_v1.0.0";
  264. it.code = that.data.code;
  265. it.target = 'app';
  266. it.encryptedData = res.encryptedData;
  267. it.iv = res.iv;
  268. it.phone = that.data.phoneNum
  269. }
  270. that.data.it = it
  271. util.ajax({
  272. func: "applet_login",
  273. data: it
  274. }, function (data) {
  275. if (data.code == 0) {
  276. that.setData({
  277. unionId: data.data.unionId,
  278. socialUid: data.data.socialUid
  279. })
  280. // todo:
  281. // data.data.firstLogin = 1
  282. // if (data.data.firstLogin == 1) {
  283. // wx.setNavigationBarTitle({ title: '注册' })
  284. // that.setData({
  285. // flag: false
  286. // })
  287. // return
  288. // }
  289. data.data.role = util.userRole(Math.round(data.data.score));
  290. // 强制退出登录
  291. data.data.VERSION = 9
  292. wx.setStorageSync("WXuserInfo", data.data);
  293. app.globalData.userInfo = data.data
  294. if (that.data.type == "share") {
  295. wx.reLaunch({
  296. url: '/pages/home/index',
  297. });
  298. } else {
  299. that.back();
  300. }
  301. } else {
  302. util.showTips(data.reason);
  303. }
  304. that.setData({
  305. clickTimes: false
  306. })
  307. });
  308. },
  309. fail(rr) {
  310. console.log(67676767, rr)
  311. }
  312. });
  313. },
  314. // 用户注册绑定手机号
  315. getPhoneNumber(e) {
  316. let that = this
  317. let data = {};
  318. if (e.detail.errMsg == "getPhoneNumber:ok") {
  319. data.code = e.detail.code;
  320. util.ajax({
  321. func: "getUserPhone",
  322. data: data,
  323. }, function (data) {
  324. if (data.code == 0) {
  325. that.setData({
  326. phoneNum: data.data
  327. })
  328. // 已注册
  329. } else if (data.code == 1007) {
  330. that.setData({
  331. phoneNum: data.data,
  332. registeredShow: true
  333. })
  334. } else
  335. util.showTips(data.reason);
  336. });
  337. }
  338. },
  339. // 注册
  340. signIn() {
  341. let that = this
  342. if (this.data.username == "" && this.data.avatarUrl == "") {
  343. util.showTips("昵称头像不能为空");
  344. return
  345. } else if (this.data.username == "") {
  346. util.showTips("昵称不能为空");
  347. return
  348. } else if (this.data.avatarUrl == "") {
  349. util.showTips("请点击上传一张属于您自己的头像 ~");
  350. return
  351. } else if (this.data.password == "") {
  352. util.showTips("密码不能为空");
  353. return
  354. } else if (this.data.password.length < 6) {
  355. util.showTips("请输入至少6位密码");
  356. return
  357. } else if (this.data.passwordConfirmation != this.data.password) {
  358. util.showTips("两次密码不一致");
  359. return
  360. }
  361. let data = {};
  362. wx.login({
  363. success: res => {
  364. data.phone = that.data.phoneNum;
  365. data.code = res.code;
  366. data.nickname = that.data.username
  367. data.avatar = that.data.avatarUrl
  368. data.password = md.hexMD5(that.data.password)
  369. data.unionId = that.data.unionId
  370. data.socialUid = that.data.socialUid
  371. data.channelId = util.config.channelInfo.channel //渠道标识
  372. data.city = app.globalData.city.city
  373. // 非列表城市改为空
  374. let cityList = wx.getStorageSync("cityList");
  375. if (data.city && cityList) {
  376. data.city = cityList.indexOf(data.city) == -1 ? '' : data.city
  377. }
  378. util.ajax({
  379. func: "applet/regUser",
  380. data: data,
  381. method: 'POST'
  382. }, function (data) {
  383. if (data.code == 0) {
  384. data.data.role = util.userRole(Math.round(data.data.score));
  385. // 强制退出登录
  386. data.data.VERSION = 9
  387. app.globalData.userInfo = data.data;
  388. // that.data.phoneNum=data.phoneNum
  389. that.setData({
  390. phoneNum: data.data.phoneNum
  391. })
  392. wx.setStorageSync("WXuserInfo", data.data);
  393. if (that.data.needRecomCode == 1) {
  394. wx.navigateTo({
  395. url: '/pages/home/referre?type=' + that.data.type,
  396. });
  397. that.bindCode();
  398. return false
  399. }
  400. // wx.navigateBack()
  401. that.back();
  402. } else
  403. util.showTips(data.reason);
  404. });
  405. }
  406. })
  407. },
  408. onChooseAvatar(e) {
  409. const {
  410. avatarUrl
  411. } = e.detail
  412. let that = this
  413. console.log(that.data.unionId);
  414. // wx.showLoading({ title: '上传准备中' });
  415. // 直传头像
  416. util.uploadFile("oss/policy", avatarUrl, {
  417. unionId: that.data.unionId,
  418. type: "userAvatar"
  419. }, true).then((res) => {
  420. if (res.code == 0) {
  421. that.data.avatarUrl = res.url
  422. this.setData({
  423. avatarUrl: that.data.avatarUrl
  424. })
  425. }
  426. });
  427. },
  428. username(e) {
  429. // console.log(e.detail.value);
  430. this.data.username = e.detail.value
  431. this.setData({
  432. username: this.data.username
  433. })
  434. },
  435. pwSetting(e) {
  436. this.setData({
  437. password: e.detail.value
  438. })
  439. }, pwConfirmation(e) {
  440. this.setData({
  441. passwordConfirmation: e.detail.value
  442. })
  443. },
  444. // 绑定邀请码
  445. bindCode() {
  446. let that = this,
  447. code = wx.getStorageSync("WX-invite");
  448. if (this.data.shareInfo.roleType == 'normal') {
  449. util.showTips("该用户不是分享家/机构,无法绑定邀请码。");
  450. return false;
  451. }
  452. if (this.data.shareInfo.roleTypeDisable == 1) {
  453. util.showTips("该分享家/机构未启用,无法绑定邀请码。");
  454. return false;
  455. }
  456. util.ajax({
  457. func: "user/savecode",
  458. data: {
  459. "code": code,
  460. receiveType: 1
  461. }
  462. }, function (res) {
  463. if (res.code == 0) {
  464. app.globalData.userInfo['recomCode'] = code;
  465. wx.setStorageSync("WXuserInfo", app.globalData.userInfo);
  466. // console.log(code, that.data.shareInfo.roleType)
  467. if (that.data.shareInfo.roleType == 'sharer' || that.data.shareInfo.roleType == 'organ') {
  468. that.setData({
  469. show: true,
  470. vouchers: res.vouchers,
  471. invitePrice: res.invitePrice
  472. });
  473. // console.log(that.data.show)
  474. } else {
  475. that.back();
  476. }
  477. } else
  478. util.showTips(res.reason);
  479. });
  480. },
  481. })